// JavaScript Document


// 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 = $('#photoBox').attr("rel").substr(4);
		$(this).wrap("<a href=\"/pages/media.aspx?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", "/pages/media.aspx?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", "/pages/media.aspx?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);
	}

