/*
Highly modified version of thickbox for Rogers
Publicis, August 2009
*/

/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
		  
var tb_pathToImage = "/js/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){   
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
	//var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(a,g, this);
	this.blur();
	
	// tracking
	if (pageTracker) {
		pageTracker._trackPageview(this.href);
	}
	
	document.location = "#header";
	return false;
	});
};

function tb_show(url, imageGroup, tt) {//function called when the user clicks on a thickbox link

	$("#subsection, #landing").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		
	var baseURL;
	if (url.indexOf("?")!==-1){ //ff there is a query string involved
		baseURL = url.substr(0, url.indexOf("?"));
	} else{ 
		baseURL = url;
	}
	   
			
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = tb_parseQuery( queryString );
			
			
	if(document.getElementById("TB_overlay") === null){
		$("#subsection, #landing").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
		$("#TB_overlay").click(tb_remove);
	}
			
				
	$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity

		
					
		if ($(tt).hasClass("video")) {
		
			$("#TB_window").append('<h2 id="vidname">'+tt.title+'</h2><div style="height: 352px;"><div style="height: 352px; width: 528px; background: #FFF" id="videobox">For a more engaging experience, view this site using <a href="http://get.adobe.com/flashplayer" target="_blank">Adobe Flash Player</a>.</div></div><div class="vidnextprev"><a href="#prev" class="previousvideo">Previous</a> | <a href="#next" class="nextvideo">Next</a></div>');			
			$("#TB_window").append('<div style="position: absolute; left: 535px; margin-top: -45px; width: 200px;"><a href="/how-to-videos/" class="btn"><span><strong>&raquo;</strong> FIND MORE VIDEOS</span></a></div>');			
			$("#TB_window").append("<div id='TB_closeAjaxWindow'><a href='#' class='TB_closeWindowButton' title='Close'><img src='/img/global/close.png' alt='x' /></a></div>");


			RCVID.init(url);
		
			TB_WIDTH = 528; //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = 352; //defaults to 360 if no paramaters were added to URL
			
			tb_position();
			
			
		} else {
		
			$("#TB_window").hide();

			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = (params['height']*1) + 40 || 360; //defaults to 360 if no paramaters were added to URL
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			
			
			$("#TB_window").append("<div id='TB_contentholder' style='background: #FFF;'><div id='TB_closeAjaxWindow'><a href='#' class='TB_closeWindowButton'><img src='/img/global/close.png' alt='x' /></a></div><div id='TB_ajaxContent'></div></div>");


			$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){ //to do a post change this load method
			
				$("#TB_load").remove();

				$("#TB_ajaxContent a.print").click(function() {window.print(); return false;});
				
				var h = parseInt($("#TB_window").outerHeight(), 10);
				if (h < 200) {
					$("#TB_ajaxContent").css("height", "200px");
				}


				$("#TB_contentholder").corners();				
				$(".TB_closeWindowButton").click(tb_remove);
				
				$("#TB_closeAjaxWindow").hide();
				
				tb_position();

			});	
				
		}
		
		

		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				}	
			};
		}


					
		$(".TB_closeWindowButton").click(tb_remove);
		return false;
		
};


//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {

 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_closeAjaxWindow").hide();
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();

	document.onkeydown = "";
	document.onkeyup = "";
      
	return false;
};

function tb_position() {
	
	$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	$("#TB_window").css({marginTop: '-' + parseInt(( $("#TB_window").outerHeight() / 2),10) + 'px'});
		
	$("#TB_window").fadeIn(350, function() {
		$("#TB_closeAjaxWindow").show();
		$("#TB_closeAjaxWindow").pngFix( { blankgif:'/js/blank.gif' } );
	});

};


function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
};

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
};

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
};


