/*$j(document).ready(function() {
	InitImageFramer();
});*/
$j(window).load(function() {
	InitImageFramer();
});
function InitImageFramer() {
	var cb = $j("#contentBlock");
 	if (!cb.length) return;
	var imgs = $j("#contentBlock img");
	imgs.each(function(idx) {
		if ($j(this).parent("div").hasClass("yellowBox")) return; 
		var img = this;
		$j.ajax({
			url: $j(this).attr("src") + "/info",
			dataType: "json",
			error: function(xho, textStatus, errorThrown) {
			},
			success: function(data, textStatus) {
                if (data["video"]) {
					var id = "v_" + Date.parse(new Date()) + "_" + Math.floor(1 + Math.random()*999999);
					var klass = "";
					if ($j(img).hasClass("image-left")) {
						klass = "left";
					} else if ($j(img).hasClass("image-right")) {
						klass = "right";
					}
					$j(img).attr("id", id);

					// Added by GFB 20091006@1033
					var dataWidth = parseInt(data['width']);
					var tagWidth = $j(img).attr('width');
					try {
						tagWidth = parseInt(tagWidth);
					} catch(e) {
						tagWidth = 0;
					}
					//alert("tagWidth = " + tagWidth + "\ndataWidth = " + dataWidth);
					data['width'] = (tagWidth && tagWidth != dataWidth) ? tagWidth : dataWidth; 
					var dataHeight = parseInt(data['height']);
					var tagHeight = $j(img).attr('height');
					try {
						tagHeight = parseInt(tagHeight);
					} catch(e) {
						tagHeight = 0;
					}
					//alert("tagHeight = " + tagHeight + "\ndataHeight = " + dataHeight);
					data['height'] = (tagHeight && tagHeight != dataHeight) ? tagHeight : dataHeight; 
					// End Add

					var width = data['width'] + 16;
                    var height = data['height'] + 64;
					var params = {
	        			'quality': 'high',
    	    			'align': 'middle',
        				'play': 'true',
        				'loop': 'true',
        				'scale': 'noscale',
	        			'wmode': 'window',
    	    			'devicefont': 'false',
        				'bgcolor': '#ffffff',
	        			'allowFullScreen': 'false',
    	    			'allowScriptAccess':'sameDomain',
        				'salign': 'lt'
					}
					swfobject.embedSWF("scale_video.swf", id, width, height, "9.0.0", false, {xmlPath: $j(img).attr("src") + "/scale_video.xml" }, params, {'class': klass});

				} else if (!data["credits"] && !data["caption"] && !data["hires_image"] && !data["gallery"]) {
					return;
				} else {
					var yb = $j('<div class="yellowBox articleImage"></div>');
					if ($j(img).hasClass("image-left")) {
						yb.addClass("left");
					} else if ($j(img).hasClass("image-right")) {
						yb.addClass("right");
					}
					$j(img).removeClass();
					$j(img).removeAttr("align");

					yb.width($j(img).width() + "px");
					$j(img).wrap(yb);
					yb = $j(img).parent("div");
					yb.append("<br />");
					if (data["credits"]) yb.append('<div class="credit">Credit: ' + data["credits"] + '</div>');
					if (data["caption"]) yb.append('<div class="caption">' + data["caption"] + '</div>');
					if (data["hires_image"]) {
						var path = $j(img).attr("src") + "/hires_image";
                        if (data["legacy"]) {
							path = $j(img).attr("src").split("/");
							if (path[path.length-1].contains("image_")) path.pop();
							path = path.join("/") + "_high";
						}
						var a = $j('<a href="' + path + '" class="zoom">High Resolution Version.</a>');
						a.click(function() {
							window.open($j(this).attr('href'), '', '');
							return false;
						});
						yb.append(a);
					}
					if (data["gallery"]) {
						var path = data["gallery"]; 
						var a = $j('<a href="' + path + '/photo_gallery_view" class="zoom" style="float:right;clear:right">Photo Gallery' + (data['galleryTitle'] ? ': ' + data['galleryTitle'] : '') + '</a>');
						a.click(function() {
							window.open($j(this).attr('href'), '', 'height=510,width=525');
							return false;
						});
						yb.append(a);
					}
				}
			}
		});
	});
}
/*
function ImageFramer() {
	return;
	var cb = document.getElementById('contentBlock');
	if (!cb) return;
	var imgs = cb.getElementsByTagName("img");
	var xo = GetXHO();
	for (var x=imgs.length-1;x>=0;x--) {
		var img = imgs[x];
		var p = img.parentNode;
		if (p.tagName == "DIV" && p.className.search(/yellowBox/i) > -1) continue;
		xo.open("GET", img.src + "/credits", false);
		xo.send(null);
		var c = xo.responseText;
		xo.open("GET", img.src + "/caption", false);
		xo.send(null);
		var d = xo.responseText;
		if (c == 0 && d == 0) continue;

		var yb = document.createElement("div");
		yb.className = "yellowBox articleImage";
		if (img.className == "image-right") yb.className += " right";
		img.className = "";
		img.align = "";
		yb.style.width = img.width + "px";
		p.replaceChild(yb, img);
		yb.appendChild(img);
		var br = document.createElement("br");
		yb.appendChild(br);
		if (c != 0) {
			var cre = document.createElement("div");
			cre.className = "credit";
			cre.innerHTML = c;
			yb.appendChild(cre);
		}
		if (d != 0) {
			var cap = document.createElement("div");
			cap.className = "caption";
			cap.innerHTML = d;
			yb.appendChild(cap);
		}
	}
	delete xo;
}
//registerPloneFunction(function() { window.setTimeout("ImageFramer()", 500); });
*/
