﻿            
function photocreditcontainer()
{
this.PhotoCredits = new Array();

   this.AddCredit = function(credit)
   {
       this.PhotoCredits[this.PhotoCredits.length]=credit;
   }

   this.RenderCredits = function()
   {
       if (this.PhotoCredits.length == 0)
       {
       //    document.write('Photo Credits Go Here.');
       } else {
           document.write('Photo Credits: ');
           for(var i=0; i<this.PhotoCredits.length; i++)
           {
            if ( this.PhotoCredits[i] != '' )
            {
                if (i!=0) document.write('; ');
                document.write(this.PhotoCredits[i]);
            }
           }
       }
   }
}

function getObjectIDFromURL(str)
{
    try
    {
        var m = str.match(/\/(\d+)$/);
        if(m){ return parseInt(m[1],10); }
    }
    catch(e)
    { 
    }
    
    return 0;
}

var PhotoCredits = new photocreditcontainer();

// listings variables for dart ads
var strServiceProvider = "";
var strSearchKey = "";
var strTvObjectName = "";
var strTvObjectType = "";
var iTvObjectID = 0;
var iGridAds = 0;
var bHDTVOnlyFilter = false;
var optionGenreFilter   = 0;
var strRealURL = "";

// grab search keyword
var qs = document.location.search;
var mt = qs.match(/keyword=([^\&]+)/i);
if(mt)
{
    strSearchKey = mt[1];
}

// set doc domain to tvguide.com so IFrames can communicate
try
{
    setDocDomain();
}
catch(err)
{
}  

function setDocDomain()
{
    var domParts = document.domain.split('.');
    switch(domParts.length)
    {
        case 0:
            break;
        case 1:
            break;
        default:
            document.domain = domParts[domParts.length-2] + '.' + domParts[domParts.length-1];
            break;
    }
}

function commentCount(iContentID)
{
    var icnt = parseInt(Math.random()*100,10);
    document.write("<div class='cmt-cnt'>" + icnt + " comments...</div>");
}

function fmtNum(i)
{
    if(parseInt(i,10)<10)
    {
        return "0" + i;
    }
    return i;
}

function cmtdt(iContentID, Yr, Mo, Dy, Hr, Mn)
{    
    // articles older than 12 hours show the timestamp
    // newer articles show time respective to current
    // all times used are from tvg servers
    var maxHrs = 12;
    
    var stmp = "";
    var cdt = new Date(Yr, Mo, Dy, Hr, Mn, 0, 0); 
    
    // fake random comments 
    var icnt = parseInt(Math.random()*100,10);

    // age of article in milliseconds          
    var msecs = tvgServerTimeUTC.getTime()-cdt.getTime();
    
    // cutoff age in milliseconds
    var cmsecs = maxHrs * 60 * 60 * 1000;
     
    // if less than cutoff  
    if(msecs < cmsecs)
    {
        // show time elapsed
        var ihrs = parseInt( (msecs/(60*60*1000)), 10);
        var imns = parseInt( (msecs-(ihrs*60*60*1000)) / (60*1000), 10);
        
        switch(ihrs)
        {
            case 0: stmp = imns + " minutes ago";
                break;
            case 1: stmp = ihrs + "hour " + imns + " minutes ago";
                break;
            default: stmp = ihrs + "hours " + imns + " minutes ago";
                break;                        
        }
    }
    else
    {
        // show absolute posting time
        stmp = cdt.getFullYear() + "." + fmtNum(cdt.getMonth()+1) + "." + fmtNum(cdt.getDay());                
        stmp += " " + fmtNum(cdt.getHours()) + ":" + fmtNum(cdt.getMinutes()) + " GMT";
    }                   
    document.write("<div class='cmt-cnt'>" + stmp + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + icnt + " comments...</div>");    
}


function setCookie(strName, strValue, iDays)
{
	var strCookie = strName + "=" + strValue + "; ";
	if(iDays!=0)
	{
	    var date = new Date();
	    date.setTime(date.getTime()+(iDays*24*60*60*1000));
		strCookie += "expires=" + date.toGMTString() + "; ";
	}
    var dom = document.domain;
    var tvg = ".tvguide.com";    
    if(dom.indexOf(tvg)==dom.length-tvg.length)
    {
        strCookie += "domain=" + tvg + "; ";
    }
	strCookie += "path=/";
	document.cookie = strCookie;
}

function readCookie(strName)
{
	var name = strName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
	}
	return null;
}

function deleteCookie(strName)
{
	setCookie(strName, "", -1);
}

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp = null;
	
    if (window.XMLHttpRequest) 
    {
        objXmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
		//IE5 and IE6
        objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(objXmlHttp)
	{
		objXmlHttp.onreadystatechange = handler;
	}

	return objXmlHttp;
}

//callback func param is optional
function ajaxRequest(url, elementid, callbackfunc) {
    var objXmlHttp = null;
     
    if (window.XMLHttpRequest) {
        objXmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {//IE5 and IE6
        try {
            objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) { 
        }
    }
    
    if (objXmlHttp != null) {
        objXmlHttp.onreadystatechange = function updateElem() {
            if (objXmlHttp.readyState == 4 || objXmlHttp.readyState == "complete") {
                var elem = document.getElementById(elementid);
                if (elem)
                    elem.innerHTML = objXmlHttp.responseText;
            }
            if (typeof callbackfunc != "undefined") {
                callbackfunc();
            }
        };
        objXmlHttp.open("GET", url, true);
        objXmlHttp.send(null);
    }
}

var strHostName = document.location.hostname.toLowerCase();
var strTvgHost = strHostName;
var strTvgServer = "";

switch(strHostName)
{
    case "tvguide.com" :
    case "video.tvguide.com":
    case "video-qa.tvguide.com":
    case "rad1onlsps1.tvguide.com" :
    case "movies-dev.tvguide.com":
    case "sports-dev.tvgol.local":
        strTvgHost = "www.tvguide.com";
        break;
    case "movies-qa.tvguide.com":
    case "sports-qa.tvguide.com":
        strTvgHost = "qa.tvguide.com";
        break;
}

if(strHostName!=strTvgHost)
{
    strTvgServer = "http://" + strTvgHost;
}

function submitOVGSearch()
{
  var OVGSearchBox = document.getElementById("txtOVGSearch");
  var searchString;
  
  if(OVGSearchBox)
  {
      searchString = OVGSearchBox.value;
      if(searchString)
      {
        doOVGSearch(searchString);
      }
      else
        alert("Please enter some search text");
  }
}

function doOVGSearch(searchString)
{
  var submitURL = "http://video.tvguide.com/results.aspx?primary=SEARCH&searchvalue=";
  if(searchString)
  {
    searchString = EncodeCrossBrowser(searchString);
    submitURL += searchString;
    window.location=submitURL;
  }
}

function EncodeCrossBrowser(stringToEncode) {
    // Use JavaScript built-in function IE 5.5+ and Netscape 6+ and Mozilla
    if (typeof encodeURIComponent == "function")
        return encodeURIComponent(stringToEncode);
    else
        return escape(stringToEncode);
}

function urchinTracker(strURL){}

function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function CheckIfCookiesEnabled() {
    setCookie('itester', 'yes', 1);
    if(readCookie('itester'))
    {
        deleteCookie('itester');
        return true;
    }
    else
        return false;
}
//prestitial
if (typeof prestitial_active !== 'undefined')
{
    if (prestitial_active) {
        var current_datetime = new Date();
        var dt_prestitial_start = new Date(prestitial_start);
        var dt_prestitial_end = new Date(prestitial_end);
        if (CheckIfCookiesEnabled() && current_datetime > dt_prestitial_start && current_datetime < dt_prestitial_end) {
            if (readCookie(prestitial_name) != 't') {
                setCookie(prestitial_name, 't', 30)
                if (readCookie(prestitial_name) == 't') //triple check that cookie was set
                {
                    document.location.href = prestitial_url + "?o_url=" + EncodeCrossBrowser(document.location.href);
                }
            }
        }
    }
}

/* dvr tombstone */
$(document).ready(function() {
    if ($("#breakingnews-alert-w").length > 0) {
        var bnanum = "0";
        if ($("#breakingnews-alert-w em").length > 0) {
            bnanum = $("#breakingnews-alert-w em").attr("title");
        }
        if (readCookie('show_message') != bnanum) {
            $('#breakingnews-alert-w').show("fast");
        }
        //close button at right corner clicked 
        $('#breakingnews-alert-close').click(function() {
            $('#breakingnews-alert-w').hide("slow");
            setCookie('show_message', bnanum, 1);
        });
    }
});
