var SiteName = "kcbs.dayport.com";

function LaunchArticle(ArtId,template)
{
  var SiteRegExp = new RegExp('@' + SiteName.replace(/\//g,"\\/"),"gi");
  var ArtString=ArtId.toString().replace(SiteRegExp,"");
  var TemplateFile="";
  var ParentFile="";
  var kWidth = 0;
  var kHeight = 0;
  var winName = "KCBSViewer";
  var page;

  if ((template != '') && (typeof template != "undefined"))
  {
     TemplateFile = '&tf='+template;
     switch (template)
     {
        case "weatherviewer.tpl":
          ParentFile = "";
          kWidth = 470;
          kHeight = 497;
          winName = "KCBSWeatherViewer";
          break;
     }
  }
  
  if (ParentFile == "")
    page = "http://kcbs.dayport.com/viewer/viewerpage.php?Art_ID=" + ArtString + TemplateFile;
  else
    page = "http://kcbsimg.dayport.com/htm/"+ParentFile+"?Art_ID=" + ArtString + TemplateFile;

  if (kWidth == 0)
    kWidth = 620;  
  
  if (kHeight == 0) 
    kHeight = 356;

  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;
}

function BuildPlaylist(template)
{
    var ArtString="";
    var ArtArray = new Array();
    var TemplateFile="";

    //alert(document.forms.length);
    for (var j=0; j<document.forms.length; j++)
    {
        //alert(document.forms[j].name);
        //alert(document.forms[j].name.substr(0,11));
        if (document.forms[j].name.substr(0,11) == 'ArticleList')
        {
            //alert('found a match '+document.forms[j].name)
            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
                {
                    //alert(FORM.V1.length);
                    for (var 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
    	LaunchArticle(ArtString,template);
}

var old,skn,iex=(document.all);

var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
var x,y,image_left,image_top,msg;
document.onmousemove = mouseMoveHandler;

function dekInit()
{
  var ns4=document.layers;
  if (ns4)
    skn=document.dek;
  else if (ns6)
    skn=document.getElementById("dek").style;
  else if (ie4)
    skn=document.all.dek.style;
  if(ns4)
    document.captureEvents(Event.MOUSEMOVE);
  else
  {
    skn.visibility="visible";
    skn.display="none";
  }
}

function mouseMoveHandler (evt) {
    x=(ns4||ns6)?evt.pageX:event.x+document.body.scrollLeft;
    y=(ns4||ns6)?evt.pageY:event.y+document.body.scrollTop;
}

function killPopup(){
    if(ns4){
        skn.visibility="hidden";
    }
    else if (ns6||ie4)
        skn.display="none";
}

function popup(msg,id){
    // Pre-load the popup image
    popupimg = new Image();
    popupimg.src=msg;

    msg = "<IMG SRC='" + msg + "'>";
    image_object = 'preview_' + id;

    var content="<TABLE CELLPADDING=0 CELLSPACING=0><TD ALIGN=center>"+msg+"</TD></TABLE>";

    // Display popup according to where the original image lives
    if (ns4) {
        element = eval('document.' + image_object);

        // First handle the vertical placement
        pageheight = top.document.height;

        image_top = element.y;

        if (pageheight > (image_top + element.height + popupimg.height + 2))
            image_top += element.height + 2;  // Added the + 2 to make images line up better
        else
            image_top = image_top - popupimg.height - 2; // Place the popup image above the other image


        // Now handle horizontal placement
        pagewidth = top.document.width

        image_left = element.x; // - 2;  // Add the - 2 to make the images line up better

        if (pagewidth < (image_left + popupimg.width))
            image_left = image_left - (popupimg.width - element.width) // Pin to bottom-right corner
        else
            image_left = image_left - 2; // Pin to bottom-left corner, add the - 2 to make the images line up better
    }
    else {
        // First handle the vertical placement
        pageheight = top.document.body.scrollHeight;

        image_top = document.getElementById(image_object).offsetTop;
        tempEl1 = document.getElementById(image_object).offsetParent;
        while (tempEl1 != null) {
  	        image_top += tempEl1.offsetTop;
  	        tempEl1 = tempEl1.offsetParent;
        }

        // Check if there's room to put it underneath
        if (pageheight > (image_top + document.getElementById(image_object).height + popupimg.height + 4))
            image_top += document.getElementById(image_object).height + 4;
        else
            image_top = image_top - popupimg.height - 2; // place the popup image above the other image


        // Now handle horizontal placement
        pagewidth = top.document.body.scrollWidth;

        image_left = document.getElementById(image_object).offsetLeft;
        tempEl2 = document.getElementById(image_object).offsetParent;
        while (tempEl2 != null) {
  	        image_left += tempEl2.offsetLeft;
  	        tempEl2 = tempEl2.offsetParent;
        }

        if (pagewidth < (image_left + popupimg.width))
            image_left = image_left - (popupimg.width - document.getElementById(image_object).width); // Pin to bottom-right corner
        else
            image_left += 0; // Pin to bottom-left corner
    }
    skn.top = image_top;
    skn.left= image_left;

    if(ns4){
        skn.document.write(content);
        skn.document.close();
        skn.visibility="visible";
    }
    if(ns6){
        document.getElementById("dek").innerHTML=content;
        skn.display='';
    }
    if(ie4){
        document.all("dek").innerHTML=content;
        skn.display='';
    }
}

function checkLaunch()
{
    // strip off the URL parameters
    var url_params = window.location.search.substr(1);
    var custID = "";
    var contID = "";
    var template = "";
    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] == 'tf')
                template = temp[1];
            else if (temp[0] == 'Customer_ID')
              custID = temp[1];
            else if (temp[0] == 'Contract_ID')
              contID = temp[1];
        }
    }

    //alert(window.location.href);
    var url_regex = window.location.href.replace(/http:\/\//g,"");
    //alert(url_regex);
    var url_array = url_regex.split("/");

    if (url_array[1])
    {
        if (url_array[2])
        {
            if((url_array[1]=='launcher') && (url_array[2]!=''))
            {
                //alert('calling LaunchArticle with '+url_array[2]);
                LaunchSyndicateArticle(url_array[2],template,custID,contID);
                window.location='http://www.cbs2.com/';
            }
        }
    }
}

function BuildVideoLink(type,hasvideo,article,articlename)
{
    // If this article does not have video, stop right now
    if (hasvideo == 'false')
        return true;

    // Setup the values to be used in the strings
    var LinkText = 'View Video of the ' + articlename + ' story';
    var VideoImage = 'http://kcbsimg.dayport.com/img/video1.gif';
    var VideoText = 'Watch video';

    switch(type)
    {
        case "image":
            var LinkValue = '<img src="' +VideoImage+ '" border="0" alt="'+LinkText+'">';
        break;

        case "text":
            var LinkValue = VideoText;
        break;

        case "combo":
            var LinkValue = VideoText + '</a>&nbsp;<a href="/" onclick="LaunchArticle('+article+'); return false;" title="'+LinkText+'" class="DayPortVideoLink"><img src="' +VideoImage+ '" border="0" alt="'+LinkText+'">';
        break;

        default:
            return false;
        break;
    }
    document.write('&nbsp;<a href="/" onclick="LaunchArticle('+article+'); return false;" title="'+LinkText+'" class="DayPortVideoLink">');
    document.write(LinkValue);
    document.write('</a>');         
    return true;
}

function FormattedDate(created_def)
{
      created_arr2 = created_def.split(" ");
      created_arr = created_arr2[0].split("-");
      montharg = created_arr[0] - 1;
      created = new Date(created_arr[2], montharg, created_arr[1]);
      switch(created.getDay())
      {
        case 0:
          day = "Sunday";
        break;
        case 1:
          day = "Monday";
        break;
        case 2:
          day = "Tuesday";
        break;
        case 3:
          day = "Wednesday";
        break;
        case 4:
          day = "Thursday";
        break;
        case 5:
          day = "Friday";
        break;
        case 6:
          day = "Saturday";
        break;
      }
      document.write("<font class=\"PublishDate\">" + day + ", " + created_arr[0] + "/" + created_arr[1] + "/" + created_arr[2] + "</font>");
	return true;
}

function rtnElem(elemName, addStyle, isParent, parentID)
{
  //alert("in rtnElem()");
  var elemObj;
  var styleStr = '';

  if (addStyle)
    styleStr = '.style';

  if (ie4)
    elemObj = eval('document.all.' + elemName + styleStr);
  else if (ns6)
    elemObj = eval('document.getElementById("' + elemName + '")' + styleStr);
  else if (ns4)
  {
    if (isParent)
      elemObj = eval('document.' + elemName);
    else
      elemObj = eval('document.' + parentID + '.document.' + elemName);
  }

  return elemObj;
}

function changeDivPosition(divID,newleft,newtop)
{  
  var tempElemObj = rtnElem(divID, true, true);
  if (tempElemObj)
  {
    //alert("Yep");
    var newContent = "";
    tempElemObj.left = newleft;
    tempElemObj.top = newtop;

    return true;
  }
  else
  {
    //alert("Nope");
    return false;
  }
}
