// SlideShow v1.0
// Author: Son Pham
// Date: 20th April, 2009

// Minimum requirement: jQuery 1.3
var slideshow = {
	slideImgWidth: 0,
	slideImgHeight: 0,
	imgMaxWidth: 500,
	init: function(selector) {
		jQuery(selector).each(function() {
			jQuery(this).click(function() {
				slideshow.create(this.href);
				return false;
			});
		});
	},
	create : function(url) {
		// Remove previous lightbox
		jQuery("#lightBox_wrapper").remove();
		jQuery("#lightBox_container").remove();

		var wrapper = jQuery(document.createElement("div")).attr("id","lightBox_wrapper");
		jQuery(wrapper).css("width",jQuery(window).width());
		jQuery(wrapper).css("height",jQuery(document).height());
		jQuery(wrapper).appendTo("body");
		jQuery(wrapper).css("display","block");

		/* need to write direct HTML -> this solves IE frameborder issue */
		jQuery("<iframe id='slideshow_content' scrolling='no' frameborder='0' src='"+url+"' style='visibility:hidden;'></iframe>").appendTo("body");
		
		jQuery("#slideshow_content").load(function(){
			jQuery(this).css("visibility","hidden");

			slideshow.render(this.contentWindow);

			jQuery(this).css("visibility","visible");
			/* jQuery(jQuery(this.contentWindow)[0].document.body).find("#wrapper").css("visibility","visible"); */
			jQuery(jQuery(this.contentWindow)[0].document.body).find(".dart_ad").css("visibility","visible");					

		});
		
		// Close actions
		jQuery(wrapper).click(function() {
			slideshow.remove();
		});			
	},
	render : function(obj) {
		if (obj) {
			var objBody = jQuery(obj)[0].document.body;
			var objImg = jQuery(objBody).find("#imgMain");
			
			// Get & Set content dimensions
			var imgWidth = (slideshow.slideImgWidth === 0) ? jQuery(objImg).width() : slideshow.slideImgWidth;
			var imgHeight = (slideshow.slideImgHeight === 0) ? jQuery(objImg).height() : slideshow.slideImgHeight;

			// Need to resize image?
			var needToResize = false;
			if (imgWidth > slideshow.imgMaxWidth) {
				needToResize = true;
			}
			if (needToResize) {
				var newImgHeight = slideshow.resizeImage(imgWidth, imgHeight);
				imgWidth = slideshow.imgMaxWidth;
				jQuery(objImg).attr("width",imgWidth).attr("height",newImgHeight);
			}
			
			var adWidth = jQuery(objBody).find("#slideshow_ad").width();
			// set iframe content total width (without padding)
			var width = imgWidth + adWidth;

			// get slidePadding
			var imgPaddingL, imgPaddingR;
			var objAsset = jQuery(objBody).find("#slideshow_asset");
			imgPaddingL = parseInt(jQuery(objAsset).css("padding-left"),10);
			imgPaddingR = parseInt(jQuery(objAsset).css("padding-right"),10);

			if (isNaN(imgPaddingL)) {
				imgPaddingL = 0;
			} 
			if (isNaN(imgPaddingR)) {
				imgPaddingR = 0;
			}
			// set iframe content total
			width += imgPaddingL+imgPaddingR;

			jQuery(objBody).css("width",width);
			var objContentWrapper = jQuery(objBody).find("#wrapper").css("width",width);


			// Get & Set iframe dimensions
			var content, cWidth, cHeight;
			if (jQuery.browser.msie) {
				content = document.getElementById("slideshow_content").contentWindow;
				cHeight = content.wrapper.clientHeight + 10;
			} else {
				cHeight = jQuery(objContentWrapper).height() + 70;				
			}
			cWidth = width + ((imgPaddingL+imgPaddingR) / 2);
			
			// Set iframe dimension for positioning
			var objIframe = jQuery("#slideshow_content");
			jQuery(objIframe).attr("width",cWidth).attr("height",cHeight).css("visibility","hidden");
			
			// Center content
			var left = parseInt((jQuery(window).width() - jQuery(objIframe).width()) / 2, 10);
			if (left < 0) { left = 10; }			

			var top  = parseInt((jQuery(window).height() - jQuery(objIframe).height()) / 2, 10) + jQuery(window).scrollTop();
			if (top < 0) { top = 30; }			
			
			jQuery(objIframe).css("left", left + "px").css("top", top + "px");

			// Close button
			jQuery(objBody).find(".close").click(function() {
				slideshow.remove();
			});
		}
	},
	resizeImage: function(imgW, imgH) {
		var height = slideshow.imgMaxWidth * (imgH / imgW);
		return height;	
	},
	remove : function() {
		jQuery("#lightBox_wrapper").remove();
		jQuery("#slideshow_content").remove();
	}
};
