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()
{
  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 LaunchPopup(H,W,URL)
{
  xPosition = screen.width/2-(W/2);
  yPosition = screen.height/2-(H/2);
  Params = "height=" + H + ",width=" + W + ",left=" + xPosition + ",top=" + yPosition + ",screenX=" + xPosition + ",screenY=" + yPosition;
  kWin=window.open(URL,'KEYCPopup',Params);
  kWin.focus();
  return true;
}

function LaunchArticles(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 (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);
}

function LaunchArticles_old()
{
  var ArtString="";
  if ( (!document.ArticleList) || (!document.ArticleList.V1) )
  {
    alert("You must select a story to add to your Playlist!");
    return false;
  }
  else
  {
    if (!document.ArticleList.V1.length)
    {
      if (document.ArticleList.V1.checked == true)
        ArtString=ArtString + document.ArticleList.V1.value;
    }
    else
    {
      for (i=0;i<document.ArticleList.V1.length;i++)
      {
        if (document.ArticleList.V1[i].checked==true)
        {
	        if (ArtString=="")
          {
	  	      ArtString=ArtString + document.ArticleList.V1[i].value;
	        }
          else
          {
	  	      ArtString=ArtString + ","  + document.ArticleList.V1[i].value;
	        }
	      }
      }
    }

    if (ArtString=="")
    {
      alert("You must select a story to add to your Playlist!");
    }
    else
    {
	    //var ArtString=ArtId;
	    page = "/viewer/viewerpage.php?Art_ID=" + ArtString;
      kHeight = 340;
      kWidth = 635;
	    xPosition = screen.width/2-(kWidth/2);
	    yPosition = screen.height/2-(kHeight/2);
	    Params = "height=" + kHeight + ",width=" + kWidth + ",left=" + xPosition + ",top=" + yPosition + ",screenX=" + xPosition + ",screenY=" + yPosition;
      kWin=window.open(page,'KEYCViewer',Params);
      kWin.focus();
    }
    return true;
  }
}

function LaunchAd(CustId)
{
    var CustString=CustId;
    page = "/viewer/viewerpage.php?Customer_ID=" + CustString;
    kHeight = 340;
    kWidth = 635;
    xPosition = screen.width/2-(kWidth/2);
    yPosition = screen.height/2-(kHeight/2);
    Params = "height=" + kHeight + ",width=" + kWidth + ",left=" + xPosition + ",top=" + yPosition + ",screenX=" + xPosition + ",screenY=" + yPosition;
    kWin=window.open(page,'KEYCViewer',Params);
    kWin.focus();
    return true;
}

var SiteName = "keyc.dayport.com";

function LaunchArticle(ArtId, NoAds, 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 kWidth = 0;
  var kHeight = 0;
  var winName = "KEYCViewer";
  var page;
  
  if ((template != '') && (typeof template != "undefined"))
  {
     TemplateFile = '&tf='+template;
     switch (template)
     {
        case "weatherviewer.tpl":
          ParentFile = "";
          kWidth = 470;
          kHeight = 497;
          winName = "KEYCWeatherViewer";
          break;

        case "studio12viewer.tpl":
          ParentFile = "";
          kWidth = 400;
          kHeight = 440;
          winName = "Studio12Viewer";
          Contract_ID += "&Contract_DefID=8&PreloadContract_DefID=7";
          break;
     }
  }

  if ((CustId != '') && (typeof CustId != "undefined"))
    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;

  page = "/viewer/viewerpage.php?Art_ID=" + ArtString + TemplateFile + Customer_ID + Contract_ID;

  if ((NoAds != '') && (typeof NoAds != "undefined"))
  {
    page = page + "&NoAds=true";
  }

  if (kWidth == 0)
    kWidth = 635;  
  
  if (kHeight == 0) 
    kHeight = 340;

  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;
  var kWin=window.open(page,winName,Params);
  kWin.focus();
  return true;
}

function checkLaunch()
{
    // strip off the URL parameters
    var url_params = window.location.search.substr(1);
    var template = "";
    var custID = "";
    var contID = "";
    if (url_params != '')
    {
      //alert(url_params);
      var url_param_array = url_params.split('&');
      var url_param_count = url_param_array.length;
      var temp;
    
      for(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);
    url_regex = url_regex.split("?");
    var url_array = url_regex[0].split("/");

    if (url_array[1] == "launcher")
    {
        if (url_array[2])
        {
            if (url_array[2] != '')
            {
                //alert('calling LaunchArticle('+url_array[2]+', \'\', \''+template+'\', '+custID+', '+contID+')');
                LaunchArticle(url_array[2], '', template, custID, contID);
                window.location = '/';
            }            
        }
        else if (custID != '')
        {
            //alert('calling LaunchArticle(\'\', \'\', \''+template+'\', '+custID+', '+contID+')');
            LaunchArticle('', '', template, custID, contID);
            window.location = '/studio12/';        
        }
    }
}

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 ' + unescape(articlename) + ' story';
    var VideoImage = 'http://cdn.dayport.com/keycimg/img/video1.gif';
    var VideoText = 'Watch the Video';

    switch(type)
    {
        case "image":
            var LinkValue = '<img src="' +VideoImage+ '" border="0" alt="'+LinkText+'">';
        break;

        case "text":
            var LinkValue = VideoText;
        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;
}

// 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;
}