//sets up js function includion for pages
//*********************************
// a "vars" attribute will be passed to the calling function as paramaters

//array to store the function names and the action event
//FunctionList["NAME-Of-FUNCTION"] = "ACTION";
var FunctionList = new Array();
FunctionList["URLOnChange"] = "change";
FunctionList["avPlayer"] = "click";
FunctionList["videoPopup"] = "click";
FunctionList["openNewWin"] = "click";

$(document).ready(function() {
  //attaches the functions to html objects
  if (CallWrap != undefined)
    for (var tmpFcnName in FunctionList)
      try
      {
        eval(tmpFcnName);
        eval("$('.js-"+tmpFcnName+"')."+FunctionList[tmpFcnName]+"(function(){return CallWrap('"+tmpFcnName+"',this)})");
      }
      catch (err)
      {
        if (console) console.error("JS Attach Function: "+err.message);
      }
      
  //binds class names to rails to define the size of each rail (Rail1-Rail6
  $(".layoutA #mainContentColExtra,.layoutAa #mainContentColExtra,.layoutB #mainContentCol3,.layoutC #mainContentColExtra,.layoutD #mainContentCol1,.layoutDa #mainContentCol1,.layoutE #mainContentCol2,.layoutF #mainContentCol1,.layoutG #mainContentCol1").addClass("Rail1");
  $(".layoutA #mainContentCol1,.layoutA #mainContentCol4,.layoutAa #mainContentCol1,.layoutAa #mainContentCol4,.layoutC #mainContentCol1,.layoutD #mainContentCol2,.layoutE #mainContentCol1,.layoutDa #mainContentCol2").addClass("Rail2");
  $(".layoutA #mainContentCol2,.layoutA #mainContentCol3,.layoutAa #mainContentCol2,.layoutAa #mainContentCol3,.layoutB #mainContentCol1,.layoutB #mainContentCol2,.layoutC #mainContentCol2,.layoutC #mainContentCol3,.layoutD #mainContentCol3,.layoutD #mainContentCol4,.layoutDa #mainContentCol3,.layoutDa #mainContentCol4").addClass("Rail3");
  $(".layoutG #mainContentColExtra").addClass("Rail4");
  $(".layoutB #mainContentColExtra,.layoutD #mainContentColExtra,.layoutE #mainContentColExtra,.layoutH #mainContentColExtra").addClass("Rail5");
  $(".layoutI #mainContentColExtra").addClass("Rail6");

  //fixes BoxContainer spacing issues
  $(".xboxcontent .moduleWrap:last").css("margin-bottom", "0px")
});
//function wrapper to map the vars to paramaters, and appends the Item
function CallWrap(FunctionName, Item)
{
  var fcnCall = FunctionName+"(";
  if ($(Item).attr('vars') != undefined && $(Item).attr('vars') != "")
    fcnCall += $(Item).attr('vars')+",";
  fcnCall += "Item)";
  
  try
  {
    return eval(fcnCall);
  }
  catch (err)
  {
    if (console) console.error("JS CallWrap: "+err.message);
    return false;
  }
}

// changes the URL when a list value is changed
// NewWin - set to true to open link in a new window (optional)
// Item - the item containing the date,
function URLOnChange(NewWindow, Item)
{
  if (typeof(Item) == "undefined")
    Item = NewWindow;

  if (typeof(NewWindow) != "boolean")
    NewWindow = false;
  
  if (Item.value != undefined && Item.value != "" && Item.value.search(/(^\/)|(^http)/) == 0)
    if (NewWindow)
    {
      var WinName = "URLWindow";
      if (Item.id != "")
        WinName = Item.id;

      var newWin = window.open(Item.value,WinName);
      newWin.focus();
    }
    else
      location.href = Item.value;
}


// genericmedia player
// the following are the available paramaters
// mediaURL - url of media(REQUIRED)
// mediaCaption - optional text to show below the clip,limited to 50 characters -must use \" to show a " and \' to show a '
// audPic - the Image to use (default: http://images.beliefnet.com/imgs/v4/broadband/bbSwirl.swf')
// wid - video width - default to '320'
// hei - video width - default to '320'
// av - 'aud' for audio  or 'vid' - default is 'aud'
// mediaType - 'rm' or 'flash' - default to 'rm'
// logoURL - default is none
// sqAd - square ad tag code - default to '00/13/21/79'
// bnrAd - bottom banner ad tag code - default to '00/13/21/78'
function avPlayer(mediaURL, mediaCaption, audPic, wid, hei, av, mediaType, logoURL, sqAd, bnrAd) {
  if (!mediaCaption)
    mediaCaption = '';
  if (!audPic)
    audPic = '/imgs/v4/broadband/bbSwirl.swf';
  if (!wid)
    wid = '320';
  if (!hei)
    hei = '240';
  if (!av)
    av = 'aud';
  if (!mediaType)
    mediaType = 'rm';
  if (!logoURL)
    logoURL = 'none';
  if (!sqAd)
    sqAd = '00/13/21/79';
  if (!bnrAd)
    bnrAd = '00/13/21/78';
  if (mediaURL && mediaURL != '')
  {
    var tmpLoc = '/Tools/bnetPlayer.aspx?mediaType=' + mediaType + '&mediaURL=' + escape(mediaURL); tmpLoc = tmpLoc + '&mediaCaption=' + escape(mediaCaption) + '&av=' + av + '&audPic=' + escape(audPic);
    tmpLoc = tmpLoc + '&wid=' + wid + '&hei=' + hei + '&logoURL=' + escape(logoURL) + '&sqAd=' + sqAd + '&bnrAd=' + bnrAd; avWin = window.open(tmpLoc, 'avPlayerWin', 'height=570,width=770,resizable=yes');
  }
}

//code to embed a video in a page, still requires the brightcove javascript include in the page
// arguments
// vid - the brightcove video ids ()Required
// pid - the brightcove playerID defaults to '1460867924'
function embedVideo(vid,pid)
{
  var config = new Array();
  config["videoId"] = vid;
  config["videoRef"] = null;
  config["lineupId"] = null;
  config["playerTag"] = null;
  config["autoStart"] = false;
  config["preloadBackColor"] = "#FFFFFF";
  config["width"] = 486;
  config["height"] = 412;

  config["wmode"] = "transparent";
  config["transparentBackground"] = true; 
 
  if (typeof(pid) == "undefined")
    config["playerId"] = 1460867924;
  else
    config["playerId"] = pid;
  
  //if the brightcove coad is loaded
  if (typeof(createExperience) != "undefined")
    createExperience(config, 8);
  else //write a place holder
    document.write ("<!-- brightcove functions not found -->");
}

//a wrapper for openNewWin() that is spicific to brightcove video
// arguments
// videoID - the brightcove Video ids
// loadPage - bool saying weather a page will be loaded under the popup
function videoPopup(videoID,loadPage)
{
  AdStr = "";
  if(document.body)
  {
    adCdBase=document.body.innerHTML;
    adStrt=adCdBase.search("src=\"http://ad.doubleclick.net");
    if(adStrt>-1)
    {
      adCdBase=adCdBase.substr(adStrt+30);
      adCdBase=adCdBase.substr(0,adCdBase.search("pos"));
      if(adCdBase.charAt(0)=="/")
        AdStr="&AdCode="+adCdBase;
    }
  }
 
  openNewWin('/Tools/popupplayer.aspx?videoId='+videoID+AdStr,'popupplayer',845,455);
  
  if (loadPage)
    return true;
  else
    return false;
}

//opens a web page in a popup window
function openNewWin(page,win_name,width,height,scrollbars)
{
  var remote;
  
  //spicificly to support video popups under brightcove
  if (page.toLowerCase().indexOf("/av/popupplayer.aspx") > -1)
  {
    page = page.replace("/av/popupplayer.aspx","/Tools/popupplayer.aspx")
    page = page.replace("/video/AstrologyPopupPlayer.aspx","/Tools/AstrologyPopupPlayer.aspx")
    width=845;
    height=455;
  }
  
  if(win_name=="login_popup")
  {
	startIndex=page.indexOf("?");
	parent.document.location.href="/login.asp\?"+page.substring(startIndex+1,page.length);
  }
  else
  {
    var xMax=screen.width;xMax=xMax-(width+15);
	if(scrollbars)
    {
      remote=window.open(page,win_name,"width="+width+",height="+height+",scrollbars,dependent=yes,left="+xMax+",screenX="+xMax+",top=0,screenY=0");
    }
    else 
    {
      remote=window.open(page,win_name,"width="+width+",height="+height+",scrollbars=auto,resize=no,dependent=yes,left=" +xMax+ ",screenX=" +xMax+ ",top=0,screenY=0");
    }
	remote.focus();
  }
}


function getIstValue() 
{
	var overlayCookie = $.cookie("ModalOverlay");
	var result = "";
	
	if (overlayCookie !=  undefined  && overlayCookie != '' && overlayCookie.indexOf('viewed') >=0)
	   result = "ist";
   
   return result;
}
  

