// jQuery Plugins and Functions

/**
 * Animate Backgrounds
 * @author Alexander Farkas
 * v. 1.1
 */

(function($){
	
	if(!document.defaultView || !document.defaultView.getComputedStyle){
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
})(jQuery);

(function($) {
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

// AD SYNCHING ------------------------------------------------------------------------
var adTile = 1;
var ordinal = Math.floor( (Math.random() * 100000000));


function loadAds ()
{
    var banner = document.getElementById("thetopContainer");
    var bbox = document.getElementById("bigboxContainer");
    
//    if (banner != null && banner != 'undefined')
//    {
//        var adUrl = getAdUrl ("thetop", ""); 
//        multiTagSyncRoadBlock ("thetop");
//    }   
    if (bbox != null && bbox != 'undefined')
    {
        var adUrl = getAdUrl ("bigbox", "");
        multiTagSyncRoadBlock ("bigbox");
    }   
}


function getAdSize(type) 
{ 
    var size = "";
    if( type.toLowerCase() == 'banner' || type.toLowerCase() == 'thetop'  ) 
        size = "468x60";

    if( type.toLowerCase() == 'bbox' || type.toLowerCase() == 'bigbox' ) 
        size = "250x250";

    if( type.toLowerCase() == 'sky' ) 
        size = "120x240";
        
    return size;        
} 

function getAdUrl(location, kws) 
{
    var zone = getZone();
    var keywords = "kw=globaltv;" + 
                getKeywords(zone) + 
                ";" + kws;
    var adSize = getAdSize (location);  
    var adUrl = "";  

    adUrl = "http://ad.ca.doubleclick.net/N3081/adi/global_prg.com" + zone + ";" + keywords + ";sz=" + adSize + ";tile=1;dc_seed=213106815";
    
    return adUrl;
}

function getZone ()
{
    var url = "";
    var startPos = 0;

    //remove the domain...
    url = document.location.toString().replace("http://", "");
    startPos = url.indexOf("/");
    url = url.substring(startPos, url.length);
    
    //remove question marks
    startPos = url.indexOf("?");
    if (startPos>0)
        url = url.substring(0, startPos);

    //remove #
    startPos = url.indexOf("#");
    if (startPos>0)
        url = url.substring(0, startPos);
            
    //remove html, htm...
    url = url.replace(".html", "").replace(".htm", "");
    
    return url + ";";    
}  

function getKeywords (zone)
{
    if (zone == null || zone == 'undefined')
        return "";
    var parts= zone.split('/');
    if (parts == null || parts == 'undefined' || parts.length ==0)
        return "";
    var i=0;
    var kws = "";
    
    for (i=0; i<parts.length; i++)
        if (parts[i] != null && parts[i] != "" && parts[i] != "/")
            kws+= "kw=" + parts[i] + ";";
    return kws;
}  


/**************************************
	Instantiations Begin
************************************/
$(function(){ //document ready
/* 
	SEARCH BOX BEHAVIOR START 
*/	
	
	// START MAIN SEARCH
		var mainOriginal = $("#mainSearch").attr("value");
		$("#mainSearch").focus(function(){
			$(this).addClass("focused");
			if($(this).val() == mainOriginal){
				$(this).val("").css("color", "#000000");
			}
		});
		$("#mainSearch").blur(function(){
			$(this).removeClass("focused");
			if($(this).val() == ""){
				$(this).val(mainOriginal).css("color", "#666666");
			}
		});
		$("#mainSearchImg").click(function(){
			if (($("#mainSearch").val() == mainOriginal) || ($("#mainSearch").val() == "")){
				$("#mainSearch").focus();
			} else { 
				// Do not remove this from here and place in inline onclick.
				return submitSearch('mainSearch');
			}
		});
	// END MAIN SEARCH
	// START VIDEO SEARCH
		var videoOriginal = $("#videoSearch").attr("value");
		$("#videoSearch").focus(function(){
			$(this).addClass("focused");
			if($(this).val() == videoOriginal){
				$(this).val("").css("color", "#000000");
			}
		});
		$("#videoSearch").blur(function(){
			$(this).removeClass("focused");
			if($(this).val() == ""){
				$(this).val(videoOriginal).css("color", "#666666");
			}
		});
		
		$("#videoSearchImg").click(function(){
			if (($("#videoSearch").val() == videoOriginal) || ($("#videoSearch").val() == "")){
				$("#videoSearch").focus();
			}
			else { submitSearch('videoSearch'); }
		});
	// END VIDEO SEARCH
	// START SEARCH RESULTS TOP
		var search2Original = $("#searchBox2").attr("value");
		$("#searchBox2").focus(function(){
			$(this).addClass("focused");
			if($(this).val() == search2Original){
				$(this).val("").css("color", "#000000");
			}
		});
		$("#searchBox2").blur(function(){
			$(this).removeClass("focused");
			if($(this).val() == ""){
				$(this).val(search2Original).css("color", "#666666");
			}
		});
		
		$("#searchButton2").click(function(){
			if (($("#searchBox2").val() == search2Original) || ($("#searchBox2").val() == "")){
				$("#searchBox2").focus();
			}
			else { submitSearch('searchBox2'); }
		});
	// END SEARCH RESULTS TOP
	// START SEARCH RESULTS BOTTOM
		var search3Original = $("#searchBox3").attr("value");
		$("#searchBox3").focus(function(){
			$(this).addClass("focused");
			if($(this).val() == search3Original){
				$(this).val("").css("color", "#000000");
			}
		});
		$("#searchBox3").blur(function(){
			$(this).removeClass("focused");
			if($(this).val() == ""){
				$(this).val(search3Original).css("color", "#666666");
			}
		});
		
		$("#searchButton3").click(function(){
			if (($("#searchBox3").val() == search3Original) || ($("#searchBox3").val() == "")){
				$("#searchBox3").focus();
			}
			else { submitSearch('searchBox3'); }
		});
	// START SEARCH RESULTS BOTTOM
	
	// Enter Press
	$(document).keypress(function(event){
		if (event.keyCode == 13 && $("#mainSearch.focused")[0] && $("#mainSearch").val() != "" && $("#mainSearch").val() != mainOriginal) {
			submitSearch('mainSearch');
		} else if (event.keyCode == 13 && $("#videoSearch.focused")[0] && $("#videoSearch").val() != "" && $("#videoSearch").val() != videoOriginal) {
			submitSearch('videoSearch');
		} else if (event.keyCode == 13 && $("#searchBox2.focused")[0] && $("#searchBox2").val() != "" && $("#searchBox2").val() != search2Original) {
			submitSearch('searchBox2');
		} else if (event.keyCode == 13 && $("#searchBox3.focused")[0] && $("#searchBox3").val() != "" && $("#searchBox3").val() != search3Original) {
			submitSearch('searchBox3');
		} else {
			
			//alert("event.keyCode: "+event.keyCode);
		}
	});
/* 
	SEARCH BOX BEHAVIOR END
*/
/* 
	HOMEPAGE NEWS VIDEO SLIDER WIDGET BEGIN 
*/
	//$(".newsVideo ul li:first").addClass("noLeftMargin")
	$(".newsVideo ul").jcarousel({
        // Configuration goes here
    });

/* 
	HOMEPAGE NEWS VIDEO SLIDER WIDGET END 
*/
/* 
	HOMEPAGE LOCAL WIDGET START 
*/
	var localStationCookie = $.cookies.get('localStation');
	if(!localStationCookie){
		//if no cookie.  show BC
		$("#widgetLocalNewsBC").show();
		$.cookies.set('localStation', 'BC', {hoursToLive:672});
		var localStation = "BC"
	} else {
		var localStationID = "#widgetLocalNews" + localStationCookie;
		$(localStationID).show();
	}
	
	$("#widgetLocalNewsSelect select").change(function(){ var target = $(this).val(); $(".localNewsContent").hide();$(target).show(); $.cookies.set('localStation', target.substr(16), {hoursToLive:24*7*4} );});
	
	$("#newsSelect select").change(function()
		{
			var target = $(this).val();
			if ((target != "http://news.globaltv.com") && (target != "http://www.globalnational.com"))
			{
				target = target.replace(/http\:\/\/www\.global(tv)?/gi, "");
				target = target.replace(/\.com/gi, "");
				target = target.substr(0, 1).toUpperCase() + target.substr(1);
				$.cookies.set('localStation', target, {hoursToLive:672} );
			}
		}
	);
/* 
	HOMEPAGE LOCAL WIDGET END 
*/
/*
	Breaking News
*/
	$("#breakingNews").hover(
		function () {
			$(this).animate(
				{backgroundPosition:"(56px 0px)"}, 
				{duration:500, easing:"linear"}
			).animate(
				{backgroundPosition:"(0px 0px)"}, 
				{duration:0}
			);
			$(this).everyTime(500, 'scrollBack', function(){
				$(this).animate(
					{backgroundPosition:"(56px 0px)"}, 
					{duration:500, easing:"linear"}
				).animate(
					{backgroundPosition:"(0px 0px)"}, 
					{duration:0}
				);
			/*
			
			var bgImageSrc = $(this).css("background-image").replace(/url\(/, "");
			var bgImageSrc = bgImageSrc.replace(/\)/, "");
			var bgImage = new Image();
			bgImage.src = bgImageSrc;
			var bgPos = $(this).css("background-position").split(" ");
			var newLeftBgPos = bgPos[0].replace(/px/, "");
			newLeftBgPos++;
			if(newLeftBgPos >= bgImage.width){
				newLeftBgPos = 0
			}
			var newBgPos = newLeftBgPos + "px " + bgPos[1];
			$(this).css("background-position", newBgPos);
			*/
			});
		}, 
		function () {
			$(this).stopTime('scrollBack');
		}
    );


});
