//var SiteName = "koki.dayport.com";
var SiteName = "video.fox23.com";


// LaunchSyndicateArticle is the function to launch the video viewer. It takes 2 arguments:
// The first argument, ArtId, is a comma separated list of article id's
//	This argument is required
// The second argument, template, is the name of the video viewer template to use
//	This argument is optional. If not passed in, the default video viewer template will be used
// Both arguments should be passed into the function as strings

// For example, LaunchSyndicateArticle('3142','newviewer.tpl')
// would launch article #3142 in the newviewer.tpl template

// Another example, LaunchSyndicateArticle('3142,5612,1123');
// would launch a playlist of all 3 articles in the default video viewer template 

function LaunchSyndicateArticle(ArtId,template,CustId,ContId)
{
  var SiteRegExp = new RegExp('@' + SiteName.replace(/\//g,"\\/"),"gi");
  var ArtString=ArtId.toString().replace(SiteRegExp,"");
  var TemplateFile="";
  var Customer_ID="";
  var Contract_ID="";
  var ParentFile="koki23player.htm";
  var kWidth = 0;
  var kHeight = 0;
  var winName = "KOKIViewer";
  var page;

  if ((template != '') && (typeof template != "undefined"))
  {
     TemplateFile = '&tf='+template;
     switch (template)
     {
        case "weatherviewer.tpl":
          ParentFile = "";
          kWidth = 730;
          kHeight = 440;
          winName = "KOKIWeatherViewer";
          break;
     }
  }
  
  if ((CustId != '') && (typeof CustId != "undefined"))
  {
    if (CustId.charAt(0) == "-")
      Customer_ID = '&ExCust='+CustId.slice(1);
    else      
      Customer_ID = '&Customer_ID='+CustId;
  }  

  if ((ContId != '') && (typeof ContId != "undefined") && (ContId == "0"))
    Contract_ID = '&NoAds=true';
  else if ((ContId != '') && (typeof ContId != "undefined")) 
    Contract_ID = '&Contract_ID='+ContId;

  if (ParentFile == "")
    page = "http://video.fox23.com/viewer/viewerpage.php?Art_ID=" + ArtString + TemplateFile + Customer_ID + Contract_ID;
  else
    page = "http://cdn.dayport.com/kokiimg/htm/"+ParentFile+"?Art_ID=" + ArtString + TemplateFile + Customer_ID + Contract_ID;

  if (kWidth == 0)
    kWidth = 730;  
  
  if (kHeight == 0) 
    kHeight = 440;

  var xPosition = screen.width/2-(kWidth/2);
  var yPosition = screen.height/2-(kHeight/2);
  var Params = "height=" + kHeight + ",width=" + kWidth + ",left=" + xPosition + ",top=" + yPosition + ",screenX=" + xPosition + ",screenY=" + yPosition;
  //window.scrollTo(0,0);
  var kWin=window.open(page,winName,Params); 
  kWin.opener = self;
  kWin.focus();
  //kWin.moveTo(xPosition, yPosition);  
  return true;
}


// BuildSyndicatePlaylist is the function to parse HTML forms and build the comma separated
// list of articles that can be passed into LaunchSyndicateArticle. It takes 1 argument:
// The fist argument, template, is the name of the video viewer template to use
//      This argument will be passed directly into LaunchSyndicateArticle
//	This argument is optional. If not passed in, the default video viewer template will be used

// BuildSyndicatePlaylist will parse the HTML on a page for all forms whose name attribute
// begins with the string 'ArticleList' (ie <form name="ArticleList_TopStories">).
// Within a matching FORM, the function will look for an array of checkboxes named V1.
// The value of a given checkbox should be the ID of the particular article.
// BuildSyndicatePlaylist will build a comma separated list of the values of all these checkboxes
// and pass that comma separated list into LaunchSyndicateArticle.
// If no matching forms or checkboxes are found, or if no boxes were checked,
// The function returns a message telling the user "You must select a story to add to your playlist!"

// Example HTML Form
// <form name="ArticleList_TopStories">
// <input type="button" onclick="BuildSyndicatePlaylist();" value="Launch Your Playlist"><br>
// <a href="/" onClick="LaunchSyndicateArticle('23'); return false;">Watch Story #23</a> 
// <input type="checkbox" name="V1" value="23">Add To Playlist<br>
// <a href="/" onClick="LaunchSyndicateArticle('29'); return false;">Watch Story #29</a> 
// <input type="checkbox" name="V1" value="29">Add To Playlist<br>
// <a href="/" onClick="LaunchSyndicateArticle('35'); return false;">Watch Story #35</a> 
// <input type="checkbox" name="V1" value="35">Add To Playlist<br>
// </form>

function BuildSyndicatePlaylist(template)
{
    var ArtString="";
    var ArtArray = new Array();
    var TemplateFile="";

    for (var j=0; j<document.forms.length; j++)
    {
        if (document.forms[j].name.substr(0,11) == 'ArticleList')
        {
            FORM = eval('document.'+document.forms[j].name);
            if (FORM.V1)
            {
                if ((!FORM.V1.length) && (FORM.V1.checked == true))
                { 
                    // When only one checkbox exists in form and it is checked
                    v = parseInt(FORM.V1.value);
                    if((!ArtArray[v]) || (ArtArray[v] != 1))
                    {
                        ArtArray[v] = 1;
                        if (ArtString=="")
                            ArtString=ArtString + FORM.V1.value;
                        else
                            ArtString=ArtString + "," + FORM.V1.value;
                    }
                }
                else
                {
                    for (i=0;i<FORM.V1.length;i++)
                    {
                        if (FORM.V1[i].checked==true)
                        {
                            v = parseInt(FORM.V1[i].value);
                            if((!ArtArray[v]) || (ArtArray[v] != 1))
                            {
                                ArtArray[v] = 1;
                                if (ArtString=="")
                                    ArtString=ArtString + FORM.V1[i].value;
                                else
	                            ArtString=ArtString + ","  + FORM.V1[i].value;
                            }
                        }
                    }
                }
            }
        }
    }

    if (ArtString=="")
    	alert("You must select a story to add to your playlist!");
    else
    	LaunchSyndicateArticle(ArtString,template);
}


// getURLVar is a utility function for parsing the URL to return the value of a
// particular URL variable. It takes 1 argument:
// The first argument, v, is the name of the URL variable whose value to return

// For example, suppose the URL is this: http://www.dayport.com/?page=home&cat=33
// getURLVar('page') would return 'home' and getURLVar('cat') would return '33'

function getURLVar(v)
{
    // strip off the URL parameters
    var url_params = window.location.search.substr(1);

    var retn = '';
    if (url_params != '')
    {
        var url_param_array = url_params.split('&');
        var url_param_count = url_param_array.length;
        var temp;

        for(var count = 0; count < url_param_count; count++)
        {
            temp = url_param_array[count].split('=');
            if (temp[0] == v)
                retn = temp[1];
        }
    }
    return retn;
}


// moreLink is a function used by JavaScript syndication using DayPort-AL templates

function moreLink(catid)
{
    // strip off the URL parameters
    var url_params = window.location.search.substr(1);

    var next = "";
    var cat = "";
    if (url_params != '')
    {
        //alert(url_params);
        var url_param_array = url_params.split('&');
        var url_param_count = url_param_array.length;
        var temp;

        for(var count = 0; count < url_param_count; count++)
        {
            //alert(url_param_array[count]);
            temp = url_param_array[count].split('=');
            //alert(temp[0]); alert(temp[1]);
            if (temp[0] == 'next')
                next = temp[1];
            else if (temp[0] == 'prev')
                prev = temp[1];
            else if (temp[0] == 'cat')
                cat = temp[1];
        }
    }
    if (catid == cat)
        return next;
    else
        return 0;
}
