/*Common constants*/
var GLOBAL_SITE_ID = 13118; /*warning this can be a problem when CDN is introduced*/
var trackingSiteId = 13118;

var reRemoveWWW = /^www\./
var strHostname = location.hostname.replace(reRemoveWWW, "");
  
var never = new Date();
never.setTime(never.getTime() + (365*24*60*60*1000));

var isSSLRequest = ('https:' == document.location.protocol);

var navVodList = 0;

/*Print element*/
function printElement(element) {
  if ((element != "") && (element != null)) {
    w = window.open();
    w.document.write('<style>@media print{#close{display: none;}} .scheduleTable table {border-collapse:collapse;} #close{display:block;padding-bottom: 50px;} </style>');
    w.document.write('<style>@import url("/style/0,,'+GLOBAL_SITE_ID+'~38086,00.css"); </style>');
    w.document.write('<style>body {background: #fff;} </style>');
    w.document.write('<a href="#" id="close" onclick="window.close()"> Close this window</a>');
    w.document.write($(element).html());
    w.print();
    //w.close();
  }
}

function seekTo(value) {
var obj = swfobject.getObjectById("flashElement");
if(obj) {
obj.seekTo(value);
}
}
function hideIcons() {
$('.icon').hide();
$('.show').show();
$('.hide').hide();
}
function showIcons() {
$('.icon').show();
$('.hide').show();
$('.show').hide();
}
                   

/*Cookie manipulation*/
function GetCookie (name, decoded) {  
  var arg = name + "=";  
  var alen = arg.length;  
  var clen = document.cookie.length;  
  var i = 0;
  if (typeof decoded == undefined) {
    var decoded = true;
  }

  while (i < clen) {    
    var j = i + alen;    
    if (document.cookie.substring(i, j) == arg) return getCookieVal (j, decoded);
    i = document.cookie.indexOf(" ", i) + 1;    
    if (i == 0) break;   
  }  
  return null;
}


function getCookieVal (offset,decoded) {
  var cookieValue = document.cookie;
  var endstr = cookieValue.indexOf (";", offset);
  if (endstr == -1) {
    endstr = cookieValue.length;
  }
  
  if (cookieValue.length > 0 && cookieValue.charAt(offset) == '"' && cookieValue.charAt(endstr-1) == '"') {
    offset++;
    endstr--;
  }
  
  if (decoded) return unescape(cookieValue.substring(offset, endstr));
  else  return cookieValue.substring(offset, endstr);
}


function SetCookie (name, value) {  
  var argv = SetCookie.arguments;  
  var argc = SetCookie.arguments.length;  
  var expires = (argc > 2) ? argv[2] : null;  
  var path = (argc > 3) ? argv[3] : null;  
  var domain = (argc > 4) ? argv[4] : null;  
  var secure = (argc > 5) ? argv[5] : false;  

  document.cookie = name + "=" + value +          
    ((expires == null) ? ""         : ("; expires=" + expires.toGMTString())) + 
    ((path == null)    ? "; path=/" : ("; path=" + path)) +  
    ((domain == null)  ? ""         : ("; domain=" + domain)) +    
    ((secure == true)  ? "; secure" : "");
}


function DeleteCookie (name) {  
  
   var exp = new Date();  
   exp.setTime (exp.getTime() - 1);  
   var cval = GetCookie (name);  
   document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); 
   //  var cval = null ;
   //  var cval = '';
   //  SetCookie(name, '');
  
}

function GetChip (cookiename, name) {  
  var cookie=GetCookie(cookiename);
  
  if (cookie != null) {
    var arg=name+"=";  
    var alen=arg.length;  
    var i = cookie.indexOf(arg); 
    if (i>=0) {
      var j = cookie.indexOf("&", i);
      if (j>=0) {
        return cookie.substring(i+alen,j);
      } else {
        return cookie.slice(i+alen);
      }
    } else {
      return null;
    }
  } else {
    return null;
  }      
}

function SetChip(cookiename, name, value){
   var argv = SetChip.arguments;  
   var argc = SetChip.arguments.length;  
   var expires = (argc > 2) ? argv[3] : never; 
   var cookie=GetCookie(cookiename);
   var arg=name+"=";
   if (cookie==null) cookie = "";
   var i = cookie.indexOf(arg);
   var trail =""; 
   if (i>=0) {               
     var j = cookie.indexOf("&", i); 
     if (j>=0) trail=cookie.slice(j);  
     SetCookie(cookiename, cookie.substring(0,i)+arg+value+trail, expires, '/', strHostname);    
   } else {                             
     SetCookie(cookiename, cookie+'&'+arg+value, expires, '/', strHostname);    
   } 
}

function DeleteChip(cookiename, name){
  var cookie=GetCookie(cookiename);
  if (cookie != null) {
    var arg=name+"=";
    var i = cookie.indexOf(arg);
    var trail ="";
    if (i>=0) {              
      var j = cookie.indexOf("&", i);  
      if (j>=0) {
        trail=cookie.slice(j+1);     
        SetCookie(cookiename, cookie.substring(0,i)+trail, never);
        return 1;
      } else {
        SetCookie(cookiename, cookie.substring(0,i-1), never);
        return 1;
      }
    } else {
      return null;
    }        
  } else {
    return null;
  }
}

function SaveUserSetting(property, value) {
  SetCookie(property,value); 
}

function LoadUserSetting(property) {
  return GetCookie(property);
}

/*Domain manipulation*/
function getSecureCookieDomain() {
  var secureCookieDomain;
  
  if(window.GLOBAL_SECURE_COOKIE_DOMAIN) {
    secureCookieDomain = window.GLOBAL_SECURE_COOKIE_DOMAIN;
  } else if(isSSLRequest && document.domain) {
    secureCookieDomain = document.domain;
  } else {
    secureCookieDomain = '';
  }
  
  return secureCookieDomain;
}


function getNonSecureSchemeAndHost() {
  var nonSecureSchemeAndHost;
  
  if(window.GLOBAL_NON_SECURE_SCHEME_AND_HOST) {
    nonSecureSchemeAndHost = window.GLOBAL_NON_SECURE_SCHEME_AND_HOST;
  } else if(!isSSLRequest && document.domain) {
    nonSecureSchemeAndHost = 'http://' + document.domain;
  } else {
    
    nonSecureSchemeAndHost = '';
  }
  
  return nonSecureSchemeAndHost;
}


function getNonSecureCookieDomain() {
  var nonSecureCookieDomain;
  
  if(window.GLOBAL_NON_SECURE_COOKIE_DOMAIN) {
    nonSecureCookieDomain = window.GLOBAL_NON_SECURE_COOKIE_DOMAIN;
  } else if(!isSSLRequest && document.domain) {
    nonSecureCookieDomain = document.domain.substring(domainForLogOut.indexOf('.'));
  } else {
    nonSecureCookieDomain = '';
  }
  
  return nonSecureCookieDomain;
}


function getSecureSchemeAndHost() {
  var secureSchemeAndHost;
  
  if(window.GLOBAL_SECURE_SCHEME_AND_HOST) {
    secureSchemeAndHost = window.GLOBAL_SECURE_SCHEME_AND_HOST;
  } else if(isSSLRequest && document.domain) {
    secureSchemeAndHost = 'https://' + document.domain;
  } else {
    
    secureSchemeAndHost = '';
  }
  
  return secureSchemeAndHost;
}

/*Url manipulation*/
function urlDecode(theString) {
  if (typeof theString != "undefined" && theString != "") {
    var returnString = new String();
    returnString = theString;
    return unescape(returnString.replace(/[+]/g,"%20"));
  }
  return "";
}

function urlEncode(theString) {
  var returnString = new String();
  returnString = escape(theString);
  return returnString.replace(/[+]/g,"%2b");
}

var qs = new Array();
var querystring = document.location.search;
var varArray = querystring.substring(querystring.indexOf("?")+1 ,querystring.length).split("&");
for (var ii = 0; ii < varArray.length; ii++) {
  var varValueArray = varArray[ii].split("=");
  if (typeof qs[varValueArray[0]] != "undefined") {
    if (typeof qs[varValueArray[0]] == "object") {
      qs[varValueArray[0]].push(urlDecode(varValueArray[1]));
    } else {
      var valueArray = new Array(qs[varValueArray[0]],urlDecode(varValueArray[1]));
      qs[varValueArray[0]] = valueArray;
    }
  } else {
    qs[varValueArray[0]] = urlDecode(varValueArray[1]);
  }
} 

var rateCounter= new Array();
 
function alreadyRated() {
  alert("You have already liked this.");             
  return;
}

function ratedThankYou(detailInstanceId) {
  alert("You like this video. Thanks for your feedback!");
  rateCounter[detailInstanceId] = true;
  return;
}

function rate(rating, detailTypeId, detailInstanceId) {
  if (!rateCounter[detailInstanceId]) {
    validateRatingAndSubmit(rating, 'like', '', '', '', detailTypeId, detailInstanceId);           
    ratedThankYou(detailInstanceId);
  } else {
    alreadyRated();
  }
  return;
}

function onRatingRetrieved(avgRating) {
 // alert(avgRating);
}

function onRatingSubmitted(rating, avgRating) {
$.ajax({
      type: "GET",
      url: '/page/ugc/xmlHttpRequest/getAssetRating.xml?detailId=' + detailInstanceId  + '&detailTypeId=' + GLOBAL_DETAIL_TYPE_ID,
      cache: false,
      success: function(msg){
        var avgRating = msg.getElementsByTagName("rating").item(0).getAttribute("numberOfTopRatings");       
        if(window.onRatingRetrieved) {
          onRatingRetrieved(avgRating);
        }
      }
    });
return;
}

/* Code from toolkit login form */

/*
var navVodList = 0;
    
    


$(document).ready(function(){  
  $(".compositetabs").cTabs();
  $(".tabs").tabs();
  $(".timestamp").ago();
  // $("#login-form").attr("target","iFrame");
  // $("#loginFailurePage").attr("value","http://www.premierrugby.premiumtv.co.uk/loginFailure.html");  
  
  // Bind LightBox clicks on video links
  $("a.subscriberContent").each(function(){
    $(this).bind("click",function(){        
    if (loggedInStatus==1){
      userHasNoAccess();
      return false;
      } else if (loggedInStatus==0){
      userNotLoggedIn();
      return false;      
    }
  });
  });
    
  // Lightboxes 
  $('#loginModal').jqm({trigger: '.loginTrigger',overlay: 85});    
  $('#forgottenEmailModal').jqm({trigger: '.emailReminderTrigger',overlay: 85});
  
  var clipId;
  $('div.nextAndPrev .previous').hide();
  $('div.nextAndPrev .next').hide();  
  if ($('#'+clipId).prev().attr('id') > '0' || $('.pagination .prev').is('a'))
   $('div.nextAndPrev .previous').show();
  if ( $('#'+clipId).next().attr('id') > '0' || $('.pagination .next').is('a'))
   $('div.nextAndPrev .next').show();
});        



function initVodList (step) {
    updateObjectProperty('luceneSearchString','param_pageNumber', Number(luceneSearchString.param_pageNumber)+step );
    updateFilter();
    loadVideos(getLuceneSearchString());
    return;
}
  
function navigateVodList(clipId, step) {
  var elmt;                    
  if (step == 1) {    
  if ( clipId == $("#videoList div:last").prev().attr("id") && $('.pagination .next').is('a') ) {
    initVodList(1);                      
    navVodList = 1;                      
  } else if ( $('#'+clipId).next().attr('id') > '0' ) {
    playVod($('#'+clipId).next().attr('id'));
  }
  }
  else { //step == -1
  if ( clipId == $("#videoList div:first").attr("id") && $('.pagination .prev').is('a') )             {                      
    initVodList(-1);                      
    navVodList = -1;
  } else if ( $('#'+clipId).prev().attr('id') > '0' ) {
    playVod($('#'+clipId).prev().attr('id'));
  }
  }                    
}*/                


