$(document).ready(function(){
    
    // Global Nav
    $('html').addClass('js');
    
    // Country select box styling	       
    $('.countrySelectBoxData').sSelect().change(
       function(){
           exitWebSite=false;  // for iPerception code
       }
    );           

 });

//iPerceptions Read Cookie
function RC(NameOfCookie) {
  if (document.cookie.length > 0) {
      begin = document.cookie.indexOf(NameOfCookie + "=");
      if (begin != -1) {
          begin += NameOfCookie.length + 1; end = document.cookie.indexOf(";", begin);
          if (end == -1) end = document.cookie.length;
          return unescape(document.cookie.substring(begin, end));
      }
  }
  return null;
}

//iPerceptions Create Cookie
function CC(name, value, days) {
  if (days) { var expDate = new Date(); expDate.setTime(expDate.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + expDate.toUTCString(); }
  else var expires = ""; document.cookie = name + "=" + value + expires + "; path=/";
}

//iPerceptions Get Version of IE
function getIEVersionNumber() {
  var ua = navigator.userAgent;
  var MSIEOffset = ua.indexOf("MSIE ");
  if (MSIEOffset == -1) {
      return 0;
  } else {
      return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
  }
}

//iPerceptions Exit Survey Popup
function popSurvey() {
  //screen height fix
  var version = getIEVersionNumber();
  var height = "600"; if (version > 7) { height = "620"; }

  //Lookup if cookie was set by Webvalidator
  webVal = RC("IPERCEPTIONS_563");
  if ((webVal === null) || (webVal === undefined)) { webVal = true; } else { webVal = false; }

  //If not Dolby link clicked && Browser=IE and WebValidator not Served ->POP
  if (exitWebSite && MSIEOffset && webVal) {
      //Create cookie for WebValidator to see if exit was served
      CC("dolbyExit", true, 1);
      url = "http://ai-collect01.iperceptions.com/study734/start.aspx?sessionID=1&lang=1&tagLang=1&re=1&tagre=1&drive=C&compCode=279&SDate=1/26/2009&surveyID=734&UGRandomTags=0&RQRandomQs=0&RQStart=0&RQEnd=10&UGQualifyingQNumber=1&indicator=4.2&multiplier=1.4&compName=&referrer=";
      var windowhandle = window.open(url, "Dolby", "toolbar=no,personalbar=no,location=no,directories=no,status=no,menubar=nos,scrollbars=no,resizable=no,width=610,height=" + height + ",top=200,left=250");
      windowhandle.focus();
  }
}

function processEnterKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if (evt.keyCode == 13) {
        if (node.name == "searchInputBox") {
            submitSearch(document.forms[0].searchInputBox.value);
        } else if (node.name == "q") {
            submitSearch(document.forms[0].q.value);
        }
        else if (node.name == "gmaps") {
            searchCine();
        }
        return false;
    }
    return true;
}

//function submitSearch(searchValue) {
//    window.location = "/us/en/sitesearch/index.aspx?q=" + encodeURI(searchValue);
//    return false;
//}

function submitSearch(searchValue) {
    var pUrl = parent.document.URL;

    if (pUrl.indexOf("us/en") != -1)
        window.location = "/us/en/sitesearch/index.aspx?q=" + encodeURI(searchValue);
    else if (pUrl.indexOf("gb/en") != -1)
        window.location = "/gb/en/sitesearch/index.aspx?q=" + encodeURI(searchValue);
    else if (pUrl.indexOf("jp/ja") != -1)
        window.location = "/jp/ja/sitesearch/index.aspx?q=" + encodeURI(searchValue);
    else if (pUrl.indexOf("cn/zh") != -1)
        window.location = "/cn/zh/sitesearch/index.aspx?q=" + encodeURI(searchValue);
    return false;
}

function clearInputBox(theBox, str) {
    if (theBox.value == str)
        theBox.value = "";
}
function populateInputBox(theBox, str) {
    if (theBox.value.replace(/^\s+|\s+$/g, "") == "")
        theBox.value = str;
}
function rollImage(theImage, newImage) {
    theImage.src = newImage.src;
}
