/************************** ads.js *******************************/
function ajaxPlacementRefresh(){                       
    if (ajaxRefreshPlacementsArray != null){
        asyncTaskInProgress = true;
        $jQuery.each(ajaxRefreshPlacementsArray, function() {                                  
            var placement = $jQuery("#" + this).attr("placement");
            var currentControlId = this;
            $jQuery.ajax({
                type: "POST",
                url: "/Content/GetContentReference.ashx?stationId=" + stationId + "&sectionId=" + sectionId + "&regionId=" + regionId + "&placement=" + placement,
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",             
                success: function(ad) {
                     if (ad.Id > 1){
                         var randomnumber = Math.floor(Math.random() * 1000001);
                         var width = $jQuery("#" + currentControlId).width();
                         var height = $jQuery("#" + currentControlId).height();
                         var iframeId = "fr_" + randomnumber;
                         var iframeUrl = "/Content/ServeContent.aspx?id=" + ad.Id +"&ticks=" + randomnumber + "&iframe=1";
                         var iframe = $jQuery(document.createElement("iframe"));                                                                 
                         iframe.load( function (){
                                            $jQuery("#" + this.parentNode.id + " :not(#" + iframeId +  ")").remove();
                                            $jQuery("#" + iframeId).attr("height",height);
                                            $jQuery("#" + iframeId).attr("width",width);
                                            asyncTaskInProgress = false;
                                    });
                         iframe.attr("id",iframeId);
                         iframe.attr("frameBorder","0");
                         iframe.attr("height","0");
                         iframe.attr("width","0");
                         iframe.attr("marginWidth","0");
                         iframe.attr("marginHeight","0");
                         iframe.attr("scrolling","no");
                         iframe.attr("src",iframeUrl);
                         $jQuery("#" + currentControlId).append(iframe);  
                     }                                                                                                                                
                 }              
           });      
        })
    }
}

/************************** settings.js *******************************/

$jQuery(document).ready(function(){
    var regionId;
    var allCookies = document.cookie.split( ';' );
    var hostName = window.location.hostname;
    var cookieName = 'RegionCookiesite' + stationId;
    var queryStringParamName = 'regioncookie';
    for ( i = 0; i < allCookies.length; i++ )
	{
	    var currentCookie = allCookies[i].split( '=' );
	    
	}
	
	regionId = getQueryVariable(queryStringParamName, currentQueryString);
	if (regionId != null )
	    SetCookie(cookieName, regionId, 15000)	
});	

function getQueryVariable(variable, queryString) {
  var query = queryString.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0].toLowerCase() == variable) {
      return pair[1];
    }
  }
  return null;
} 

function SetCookie(cookieName,cookieValue,nDays) { 
  var today = new Date(); 
  var expire = new Date(); 
  if (nDays==null || nDays==0) nDays=-1; 
  var expireGap = 3600000*24*nDays;
  expire.setTime(today.getTime() + expireGap); 
  
  document.cookie = cookieName+"="+escape(cookieValue) 
                  + ";expires="+expire.toGMTString()+"; path=/"; 
 } 

/************************** Clock.js *******************************/
 function Clock(CE, timeZone) {
     var currentDate = new Date();
     var currentDateMil = currentDate.getTime();
     var UTCOffsetMil = currentDate.getTimezoneOffset() * 60 * 1000;
     var currentDateUTCMil = new Date(currentDateMil + UTCOffsetMil).getTime();
     var currentDateESTMil = currentDateUTCMil + 1000 * 60 * 60 * timeZone;
     this.Init(CE, new Date(currentDateESTMil));
 }

 Clock.prototype.Init = function(CE, startTime) {
     this.TTO = 0;
     this.CE = CE;
     this.Flag = true;
     this.tD = startTime;
     this.Upd();
 }
Clock.prototype.Upd=function()
{
	if(this.TTO){clearTimeout(this.TTO);this.TTO=0;}
	this.tD.setSeconds(this.tD.getSeconds()+1)
	this.CE.innerHTML=this.FormatDate(this.tD);	
	var self=this;
	this.TTO=setTimeout(function(){self.Upd();},3000);
	//this.Flag=!this.Flag;
}

Clock.prototype.FormatDate=function(dt)
{
	var hour=dt.getHours();
	var minutes=dt.getMinutes();
	var pm=true;
	if(hour<12)
	{
	    pm=false;
	    if(hour==0) 
	    {
		        hour=12;
	    }        	
	}
	else if(hour>12)
		hour=hour-12;
	return hour + ((this.Flag) ? ":":" ") + this.Pad(minutes) + " " + (pm ? "pm":"am");
}

Clock.prototype.Pad=function(number)
{
	return (number<=9) ? "0"+number:""+number;		
}
