// JavaScript Document
/* Lightbox Start */
(function($) {
	$.fn.cwiLB = function(op){
		//CanWestInteractive Lightbox
		// Version 1.0
		// Design by Andrew Davies. Code by Arron Gibson
		/* DEFAULTS FOR OPTIONS (op Object)*/
		if(op.target == null){
			op.target = this.attr('href');
		}
		if(op.speed == null){
			op.speed = "slow";
		}
		if(op.filter == null){
			op.filter = "#mediaGallery";
		}
		if(op.opacity == null){
			op.opacity = 0.6;
		}
		if(op.height == null){
			op.height = 700;
		}
		/*  CLICK FUNCTIONALITY */
		this.click(function(){
			if($("#cwpPlayerDiv")[0]){
				cwpTogglePause(true);
			}
			if(!$("#cwi_lb")[0]){
				$('<div id="cwi_lb" style="display:none;"><div id="cwi_lb_content" style="display:none;"><iframe id="cwi_lb_iframe" src="' + op.target + '" height="' + op.height + '" frameborder="0" ></iframe></div><div id="cwi_lb_bg"">&nbsp;</div></div>').appendTo("body");
			}
			$("#cwi_lb_bg").css("opacity", "0");
			$("#cwi_lb").show()
			$("#cwi_lb_bg").fadeTo(op.speed, op.opacity, function (){  
				var mainCol = $("#mainColumn").offset();
				$("#cwi_lb_content").css("left", $(document).width()/2-480).css("margin-top", mainCol.top).fadeIn(op.speed, function(){ 
					$("#cwi_lb_bg").click(function(){  
						$("#cwi_lb_content").fadeOut(op.speed, function(){ 
							$("#cwi_lb_bg").fadeTo(op.speed, 0.01, function(){ 
								$("#cwi_lb").remove(); 
								$("#mainColumn #theSpacer").remove();
							}); 
						}); 
					});
					/*
					$.ajax({
						type: "GET",
						url: op.target,
						dataFilter: function(data){
							return $(data).filter(op.filter);
						},
						success: function(html){
							$("#cwi_lb_content").html(html);
							mediaAjaxComplete(op);
						}
					});
					*/
				});
			});
			$.scrollTo("#mainColumn", "slow");
			return false;
		});
		$(document).keyup(function(event){
			if (event.keyCode == 27) {
				$("#cwi_lb_bg").click();
			}
		});
		return this;
	}
})(jQuery);




/* Lightbox End */

// Start Doc Ready
$(function(){
/*
	Setup Lightbox
*/
	// main photo launches lighbox as well
	
	$("#storyPage #photoBox img").each(function(){
		var storySP6ID = $("#storyPage").attr("rel").substr(4);
		var theSP6ID = $(this).attr("src").match(/\d+\.bin/).toString().replace(/\.bin/, "").toString();
		$(this).wrap("<a href=\"media.html?id=" + storySP6ID + "&mediaID=" + theSP6ID + "\" class=\"generatedLink\"></a>");
		$(this).parent().cwiLB({speed:"fast", height: $("#mainContent").height()});
	});
	$("#storyPage .videoLink a, #storyPage .photoLink a").each(function(){
		var theRel = $(this).attr("rel");
		var theSP6ID = theRel.substr(4);
		var storySP6ID = $("#storyPage").attr("rel").substr(4);
		$(this).attr("href", "media.html?id=" + storySP6ID + "&mediaID=" + theSP6ID);
		$(this).cwiLB({speed:"fast", height: $("#mainContent").height()});
	});
	//var allOfThem = "";
	$("#mediaPage .videoLink a, #mediaPage .photoLink a").each(function(){
		//allOfThem = allOfThem + " " + $(this).attr("rel");
		var theRel = $(this).attr("rel");
		var theSP6ID = theRel.substr(4);
		var storySP6ID = $("#mediaPage").attr("rel").substr(4);
		$(this).attr("href", "media.html?id=" + storySP6ID + "&mediaID=" + theSP6ID);
	});
	//alert("\/");
	//alert(allOfThem);
/*
	Image Sizes on story page
*/
	$("#storyPage #photoBox.StoryDefaultSize img").each(function(){
		if($(this).attr("width") < 379){
			//image is not large enough for the space force it larger
			$(this).attr("width", 380);
		}
	});
	$("#storyPage #photoBox.StorySmallSize img").each(function(){
		if($(this).attr("width") < 219){
			//image is not large enough for the space force it larger
			$(this).attr("width", 220);
		}
	});
	$("#storyPage #photoBox.StoryLargeSize img").each(function(){
		if($(this).attr("width") < 619){
			//image is not large enough for the space force it larger
			$(this).attr("width", 620);
		}
	});
	$("#storyPage .relatedShort li img").each(function(){
		//$(this).parent().append("<div class=\"clearLeft bottomMargin\" />");
	});
	
// Fix for IE really messing up the float calculations
		// clearing related smalls in ie when there are large pics or video
		//firstly is it IE?
		if($.browser.msie && $.browser.version < 8){
			//alert($.browser.version);
			if($("#photoBox.StoryLargeSize")[0]){
				var photoBoxPicHeight = $("#photoBox img").height();
				var photoBoxTextHeight = $("#photoBox .photoText").height();
				$(".relatedShort:not(.contestRelatedLinks)").css("padding-top" , (photoBoxPicHeight+photoBoxTextHeight));
			
			}
			if($("#videoBox")[0]){
				$(".relatedShort, .relatedRowLength2").css("padding-top" , $("#videoBox").height());
				//alert("video");
			
			}
		
		
		}
	// Lightbox comments	
	$("#moreComments a").cwiLB({speed:"fast", height: $("#mainContent").height()});
	$("#btnPostCommentSubmit").click(function(){
		//alert("Please wait while your comment is posted.");
	});
	
	// Canada.com vid tab stuff.
	if(getURLParamVal("tab").toLowerCase() == "vid"){
		//auto play the first vid in the related links
		$(".relatedLinks li.videoLink:first a").click();
	}
	if(getURLParamVal("tab").toLowerCase() == "phot"){
		//auto play the first vid in the related links
		$(".relatedLinks li.photoLink:first a").click();
	}
		
}); // End Doc Ready
	function getURLParamVal(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
		  if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
			var aParam = aQueryString[iParam].split("=");
			strReturn = aParam[1];
			break;
		  }
		}
	  }
	  return unescape(strReturn);
	}
