var glo_stream_file_name;
var glo_stream_title;
var glo_stream_desc;
var glo_stream_id;
var glo_stream_context_id;
var glo_current_channel;
var glo_current_channel_id;
var glo_stream_url;
var glo_stream_category;
var glo_vod_mode    = 'vod';
var glo_live_mode   = 'live';
var glo_stream_netrating_url;

var streamList = new Array();
var streamIdList = new Array();
var streamDesc = new Array();
var streamFilenameList = new Array();
var streamContextIdList = new Array();
var streamUrlList = new Array();
var streamcategory = new Array();

//Thickbox global variables declaration
var emailThisHref = "/servlet/popupPage?view=emailThis";
var userFeedbackBoxHref = "/servlet/popupPage?view=userFeedbackBox";
var thickboxIframeEmail = "&TB_iframe=true&height=500&width=470&modal=true";
var thickboxIframeFeedBack = "&TB_iframe=true&height=475&width=500&modal=true";
var vodAdsController = "";
var vodAdsUrl = "/servlet/adsVideo/?stream=:contentBeanId&channel=:channelContentId";
var vodAdsUrl2 = "/servlet/adsVideo/?stream=";

var live_channel  ="live_channel";
var other_channel ="other_channel";
var tablive         = "tab_live";
var tabvod          = "tab_vod";
var tabmostrecent   = "tab_mostrecent";
var tabVodMostRecent= "tab_vod_mostrecent"
var rsSI="http://www.razor.tv";

////////////////////////////////////////////////////////////////////////////////
// initial configuration
var currTab         = tablive;
var totalTransPanel = 2;
var currentPlayingIndex = 0;
////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//to check is the videoBox fully loaded before update the global variable
var isVideoBoxFullyLoaded=false;
///////////////////////////////////////////////////////////////////////////

var videoBoxStreamTitleList = new Array();
var videoBoxStreamIdList = new Array();
var videoBoxStreamDesc = new Array();
var videoBoxStreamFilenameList = new Array();
var videoBoxStreamContextIdList = new Array();
var videoBoxStreamUrlList = new Array();
var videoBoxStreamCatId = new Array();
var videoBoxStreamCategoryList = new Array();
var videoBoxStreamFlashNetRatingUrl = new Array();
var glo_streamType = "";

// Function to update the stream info
function updateStreamInfo(itemCount) {
    //alert(videoBoxStreamFilenameList[itemCount]);
    glo_stream_file_name  = videoBoxStreamFilenameList[itemCount];
    glo_stream_title      = videoBoxStreamTitleList[itemCount];
    glo_stream_desc       = videoBoxStreamDesc[itemCount];
    glo_stream_id         = videoBoxStreamIdList[itemCount];
    glo_stream_context_id = videoBoxStreamContextIdList[itemCount];
    glo_stream_url        = videoBoxStreamUrlList[itemCount];
    glo_stream_category   = videoBoxStreamCategoryList[itemCount];
    //vodAdsController = vodAdsUrl + videoBoxStreamIdList[itemCount];
    glo_stream_netrating_url = videoBoxStreamFlashNetRatingUrl[itemCount];
//    alert('glo_stream_file_name : ' + glo_stream_file_name + ', glo_stream_title : ' + glo_stream_title);
    updateCommentWifAnalytics('main');
    updateEmbedPanelContent();
    updateRatingPanelContent();
}

//Function to update the stream from param
function loadComment() {
    $jq(document).ready(loadCommentInFirstLoad);
}

// change tab color
function changeTabsLayout(divname) {
   var divNames = new Array(tablive, tabvod, tabmostrecent);
   var spanLink;
   var spanNoLink;
   var div;
   var i;

   for(i=0; i<divNames.length; i++) {
    div = document.getElementById(divNames[i]);
    spanLink = document.getElementById(divNames[i]+"_link");
    spanNoLink = document.getElementById(divNames[i]+"_nolink");

       if(divname == divNames[i]) {
           currTab = divname;
           div.style.background = "#222222";
           spanLink.style.display = "none";
           spanNoLink.style.display = "block";
       } else {
             div.style.background = "#4C4C4C";
             spanLink.style.display = "block";
           spanNoLink.style.display = "none";
       }
   }
}

// Javascript trim function
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function trimToFixedSize(stringToTrim, size){
    return stringToTrim.substring(0,size - 1) + "...";
}

function getRequestWithScript(url, div) {
    $jq.ajax({
           type: "GET",
           url: url,
           success: function(data){
                document.getElementById(div).innerHTML = data;
                evalJScript(data);
           }
         });
}

function postResponseWithScript(url, div) {
    $jq.ajax({
           type: "POST",
           url: url,
           success: function(data){
                document.getElementById(div).innerHTML = data;
                evalJScript(data);
           }
         });
}

function evalJScript(data){
  var scripts = getJavaScript(data);
    return evalJavaScript(scripts);
}

function getJavaScript(data){
    var script, scripts;
    scripts = [];
    var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
    while ((script = regexp.exec(data))) {
    scripts.push(script[1]);
    }
    return scripts;
}

function getAlertJavaScript(scripts){
  var alerts = [];
    for (i=0;i<scripts.length;i++) {
    if (scripts[i].indexOf('alert') != -1) {
        alerts.push(scripts[i]);
      }
    }
    return alerts;
}

function evalJavaScript(scripts){
  scripts = scripts.join('\n');
    //alert(scripts);
    if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
  return true;
}

function getRequest(url, div) {
    // alert('div : ' + div);
    $jq.get(url,
        function(data){
            document.getElementById(div).innerHTML = data;
            //$('#' + div).innerHTML = data;
    });
}

function postRequest(url, div) {
    $jq.post(url,
        function(data){
        document.getElementById(div).innerHTML = data;
    });
}

function redirectTo(url) {
  window.location=url;
}

function getStreamNetratingURL(sStreamNetRatingUrl, sGlo_stream_category, sNetrating_stream_category) {
    if (sGlo_stream_category == "") {
        sStreamNetRatingUrl = sStreamNetRatingUrl.replace(sNetrating_stream_category);
    } else {
        if (sGlo_stream_category == "WHAT WILL YOU DEFEND") {
            sStreamNetRatingUrl = sStreamNetRatingUrl.replace(sNetrating_stream_category, "-CLIENT");
        } else if (sGlo_stream_category == "MOF DEBATE CHALLENGE") {
            sStreamNetRatingUrl = sStreamNetRatingUrl.replace(sNetrating_stream_category, "-CLIENT2");
        } else if (sGlo_stream_category == "VIDEO GREETINGS") {
            sStreamNetRatingUrl = sStreamNetRatingUrl.replace(sNetrating_stream_category, "-GENERAL");
        } else {
            sStreamNetRatingUrl = sStreamNetRatingUrl.replace(sNetrating_stream_category, "-" + sGlo_stream_category);
        }
    }
    return sStreamNetRatingUrl;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function changeTitle(newTitle)
{
    document.title="RAZORTV - "+ unescapeXML(newTitle);
}

function changeOriginalTitle()
{
    document.title="RAZORTV";
}


//Unescape XML in javascript
function unescapeXML(stringToUnescaped){
    stringToUnescaped = stringToUnescaped.replace(/&lt;/g,"<");
    stringToUnescaped = stringToUnescaped.replace(/&gt;/g,">");
    stringToUnescaped = stringToUnescaped.replace(/&amp;/g,"&");
    stringToUnescaped = stringToUnescaped.replace(/&#039;/g,"'");
    stringToUnescaped = stringToUnescaped.replace(/&#034;/g,'"');
    return stringToUnescaped;
}
