/*$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("image-frame")) 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="image-frame"></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");

					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; 

					yb.width(data['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 = data["hires_image"];//$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=500');
							return false;
						});
						yb.append(a);
					}
				}
			}
		});
	});
}
