//	Publisher: Transcontinental Media Digital
//	Developer: Son Pham
//	Version: v0.9

//	Instructions:   1) include jquery.js before this .js file
//					2) Create and Define css for the following selectors: [#lightBox_wrapper, #lightBox_container, #lightBox_action, #lightBox_data]
//					3) Create a css class .hide {display:none}
//					4) To use your close button, simply set the tag with class="lightBox_close" otherwise the function will create its own close X

//  Usage: 		lightBox("selector_to_bind_event","tag_id");
//	Example:    lightBox("a","myDivId");											

// lightBox close label
if ($label_close == null) {
	var $label_close = "Close";
}

function lightBoxImage(selector) {
	jQuery(selector).each(function() {
		jQuery(this).click(function() {
			// 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");

			var container = jQuery(document.createElement("div")).attr("id","lightBox_container").addClass("loading");
			jQuery(container).appendTo("body");

			// Close actions
			jQuery(wrapper).click(function() {
				jQuery(this).remove();
				jQuery("#lightBox_container").remove();
			});			

			// Get local reference to object this toi be used in img.load
			var objThis = jQuery(this);

			var img = new Image();
			jQuery(img).load(function () {
				jQuery(this).hide();

				jQuery("#"+jQuery(container).attr("id")).removeClass("loading","slow").append(this);

				// Resize Container
				jQuery("#"+jQuery(container).attr("id")).css("width", jQuery(this).width() + "px"); 
				jQuery("#"+jQuery(container).attr("id")).css("height", "auto");

				jQuery(this).fadeIn("slow");

				// Create Content
				var content_data = jQuery(document.createElement("div")).attr("id","lightBox_data");
				jQuery(content_data).appendTo("#"+jQuery(container).attr("id"));
	
				// BEGIN EDIT CUSTOM DATA
				var arrLegendContent = jQuery(objThis).parent().parent().children().filter(".legend").children().filter("dl.subject","dl.cost","dl.url","p");
				jQuery(arrLegendContent).each(function() {
					jQuery(this).clone().appendTo("#"+jQuery(content_data).attr("id"));  
				});
				// END EDIT CUSTOM DATA

				// Create Close action
				var content_action = jQuery(document.createElement("div")).attr("id","lightBox_action");
				jQuery(content_action).html("<span class='lightBox_close'>"+$label_close+" <span>X</span></span>");
				jQuery(content_action).appendTo("#"+jQuery(container).attr("id"));

				// Close actions
				jQuery(content_action).children().filter(".lightBox_close").click(function() {
					jQuery("#lightBox_wrapper").remove();
					jQuery("#"+jQuery(container).attr("id")).remove();
				});
				
				// FireFox Issue here need to do centering stuff here
				var left = parseInt((jQuery(window).width() - jQuery(this).width()) / 2, 10) + "px";
				var top  = parseInt((jQuery(window).height() - jQuery("#"+jQuery(container).attr("id")).height()) / 2, 10) + jQuery(window).scrollTop() + "px";
				jQuery("#"+jQuery(container).attr("id")).css("left", left);
				jQuery("#"+jQuery(container).attr("id")).css("top", top);
				
			}).error(function() {
				alert("error while loading image!");
			}).attr('src', jQuery(this).attr("href"));

			// Center content
			var left = parseInt((jQuery(window).width() - jQuery(img).width()) / 2, 10) + "px";
			var top  = parseInt((jQuery(window).height() - jQuery(img).height()) / 2, 10) + jQuery(window).scrollTop() + "px";
			jQuery("#"+jQuery(container).attr("id")).css("left", left);
			jQuery("#"+jQuery(container).attr("id")).css("top", top); 

			return false;
		});
	});
}

function setToolTips() { 
	// settings
	// tooltip position
	var xOffset = 25;
	var yOffset = 20;  
	
	// tooltip width
	var maxWidth = 300;
	

	// remove previous tooltip
	jQuery("#toolTip").remove();

	jQuery(".toolTip").each(function() {
		jQuery(this).hover(function(e){                                               
		// Get Text
		this.tempTitle = this.title;
		var txt;
		txt = jQuery(this).parent().children().filter(".caption").html();

		if (txt == '' || txt == null) {
			txt = this.title;
		}
		this.title = "";
		
		// Create ToolTip if text exists
		if (txt != '' && txt != null) {
			var toolTip = jQuery(document.createElement("div")).attr("id","toolTip");
			var tip_wrapper = jQuery(document.createElement("div")).attr("id","tip_wrapper");	
			var tip_content = jQuery(document.createElement("div")).attr("id","tip_content");
			jQuery(tip_content).append(txt);
			jQuery(tip_wrapper).append(jQuery(tip_content).html());
			jQuery(toolTip).append(jQuery(tip_wrapper).html());
			jQuery(toolTip).appendTo("body");
			
			if (jQuery(toolTip).width() > maxWidth) {
				jQuery(toolTip).css("width",maxWidth + "px");
			}
		
			// Position toolTip
			jQuery("#toolTip")
				 .css("top",(e.pageY - xOffset) + "px")
				 .css("left",(e.pageX + yOffset) + "px")
				 .fadeIn("fast");      
			}
		},
		function(){
			 this.title = this.tempTitle;
			 jQuery("#toolTip").fadeOut("fast");
			 // remove previous tooltip
			 jQuery("#toolTip").remove();
		});
		
		
		jQuery(this).mousemove(function(e){
			jQuery("#toolTip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
		});
	});
}