/* Takes in the id number of the slide to load */
/* and makes it the active slide */
var slide_duration = 10000; //in ms

var current = 's1' //Start off with a default current value of 1
var firstTimePlaying = 0 //use this to set pause as the start button

function goToSlide(id) {
  resetSlides();
  slidelink = document.getElementById(id);
  slidelink.className = 'active';
  slidenum = parseInt(id.substr(1),10);
  // setShadows(slidenum)
  changeImage(slidenum);
  changeText(slidenum);
  current = id;
  }

function viewSlide(id) {
  goToSlide(id)
  pause()
  }

function pause() {
  //set the control class to play
  control = document.getElementById('playpausebutton')
  control.className = 'play'
  }

function prev() {
  //find out what current is
  //if current is 1, then next slide is 8
  //else +1 to current
  if (current == 's1') { goToSlide('s8') }
  // skipping Competition slide (s4) for the time being
  else if (current == 's5') { goToSlide('s3'); }
  else {
    slidenum = parseInt(current.substr(1),10)
    goToSlide('s'+(slidenum-1))
    }
  }

function next() {
  if (current == 's8') { goToSlide('s1'); }
  // skipping Competition slide (s4) for the time being
  else if (current == 's3') { goToSlide('s5'); }
  else {
    slidenum = parseInt(current.substr(1),10)
    goToSlide('s'+(slidenum+1))
    }
  }

function toggleRotate() {
  //check if button is set to play or pause
  //if set to pause, start slideshow
  //if set to play, stop slideshow
  //toggle button class
  control = document.getElementById('playpausebutton')
  if (firstTimePlaying == 0) {
    rotate();
    firstTimePlaying++;
    }
  else {
    control.className = (control.className == 'play' ? 'pause' : 'play' );
    rotate();
    }
  }

function rotate() {
  if (document.getElementById('playpausebutton').className == 'pause') {
    next();
    setTimeout("rotate()",slide_duration);
    }
  }

function changeText(id) {
  heading = document.getElementById('title')
  heading.innerHTML = headings[id-1]

  descMain = document.getElementById('desc')
  descMain.innerHTML = descriptions[id-1]
  }

function resetSlides() {
  var images = 8;
  for (i = 1; i <= images; i++) {
    slidelink = document.getElementById('s'+i);
    if (slidelink) {
      slidelink.className = '';
      }
    }
  }

function changeImage(id) {
  image = document.getElementById('slide_image');
  image.src = '/ui/thk_home/slide_'+id+'.jpg';
  }


/***************************************************************************************/
/* Using cookies, we will set a temporary marker with the current region of
   think.com, and using a persistent cookie with the location of the visitor.

   -- The first visit to Think.com will set region to en_us.
   -- Once a user selects a country from the drop down list,
      the country selected's region will be stored in the temporary cookie
    and when the page reloads, the temporary cookie will be used to
    write links to TOS and Privacy Policies
  --Once a user checks the "Remember my Location" checkbox, a new persistent
    cookie will be set with the user's region.  Now, whenever a user visits think.com,
    it will redirect to the user's regional website
  --If a user enters the URL of a think.com location manually, such as /es/,
    then the country will be set to the most predominant country that uses that language.

*/

window.onload = initCountryCookie;

function setCookie(name,value,persistent) {
  //Sets a cookie taking in a name and value.
  //If persistent is set to TRUE, then the cookie
  //will have a much longer expiration date

  //check if a cookie with same name exists
//  if (getCookie(name))
//    deleteCookie(name)

  //set path to '/'
  var path = '/';
  //set domain to ''
  var domain = '';
  //set secure to ''
  var secure = '';
  //set expire for persistent
  var days_to_expire = 30;

  var today = new Date();
  today.setTime(today.getTime());

  var persistent_expires_date = new Date( today.getTime() + (days_to_expire * 1000 * 60 * 60 * 24) );

  var cookie_string = name + "=" + escape(value) +
  ( (persistent) ? ";expires=" + persistent_expires_date.toGMTString() : ";expires=" + persistent_expires_date.toGMTString() ) +
  ( (path) ? ";path=" + path : "" ) +
  ( (domain) ? ";domain=" + domain : "" ) +
  ( (secure) ? ";secure" : "" );

  document.cookie = cookie_string;
}

function getCookie(cookie_name) {
  //Checks if a cookie with a given name exists
  //if so, returns cookie value
  if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(cookie_name + "=")
    if (c_start!=-1) {
      c_start=c_start + cookie_name.length+1
    c_end=document.cookie.indexOf(";",c_start)

    if (c_end==-1) c_end=document.cookie.length

    return unescape(document.cookie.substring(c_start,c_end))
    }
    }
  return ""
}

function deleteCookie(cookie_name) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function languageRedirect(region) {
  //Takes in a region, such as en_us,
  //and sets a new temprary cookie with the region
  //and redirects to selected language

  //this is called from the selectbox
  var directory;
  deleteCookie("tcTempCookie");
  setCookie("tcTempCookie",region);
  if (getCookie("tcTempCookie").substring(0,2) == 'zh'){
    directory = 'zh_cn';
  }
  else if (getCookie("tcTempCookie").substring(0,2) == 'pt'){
    directory = 'pt_br';
  }
  else{
    directory = getCookie("tcTempCookie").substring(0,2);
  }
  location.href = '/'+directory+'/';
}

function indexRedirect() {
  //check if tcPermRegion cookie exists.
  //if so, rediect to its region
  //if not, redirect to english by default
  if(getCookie("tcTempCookie")) {
    var permRegion = getCookie("tcTempCookie");
    languageRedirect(permRegion);
    }
  else {
    languageRedirect("en_us");
  }
}

/************************************
function setPermCookie() {
  //set a persistent cookie using region from
  //tcTempRegion if user checks the remember location checkbox
  var CountryRememberCheckBox = document.getElementById("CountryRememberCheckBox")
  if (CountryRememberCheckBox.checked) {
    var tempRegion = getCookie("tcTempCookie");
    setCookie("tcPermCookie",tempRegion,true);
    }
}
***************************************/

function getRegionFromURL() {
  var countryPath = location.href.substr(location.href.indexOf('/',7) + 1, 2);
  var region = "en_us";
  //Set Default countries for language directories
  switch(countryPath) {
    case "en":
      region = "en_us";
    break
    case "de":
      region = "de_de";
    break
    case "fr":
      region = "fr_fr";
    break
    case "es":
      region = "es_es";
    break
    case "zh_cn":
      region = "zh_cn";
    break
    case "it":
      region = "it_it";
    break
    case "hi":
      region = "hi_in";
    break
    case "nl":
      region = "nl_nl";
    break
    case "pt_br":
      region = "pt_br";
    break
    case "th":
      region = "th_th";
    break
    case "tr":
      region = "tr_tr";
    break
    }
  return region;
}

function initCountryCookie() {
  //run everytime page loads
  //checks if a temporary cookie exists
  //   yes: does it match the current language?
  //        no: set new cookie from url
  //   no:  set new cookie from url
  if (!getCookie('tcTempCookie')) {
  //   if (getCookie('tcTempCookie').substr(0,2) != location.href.substr(location.href.indexOf('/',7) + 1, 2)) {
  //     setCookie('tcTempCookie',getRegionFromURL()); }
  // }
  // else {
    setCookie('tcTempCookie',getRegionFromURL()); }
}

// returns a complete URL taking into account <base> tag (IE6 and IE7 ignore the <base> tag
// when you set location.href in JavaScript)
function getCompleteURL(p_url) {
  var l_base = '';

  if (String(p_url).substr(0, 1) == '/') {
    if (document.getElementsByTagName) {
      var l_base_tags = document.getElementsByTagName('base');
      if (l_base_tags.length > 0) {
        l_base = String(l_base_tags[0].href).replace(/\/$/, '');
      }
    }
  }

  return l_base + p_url;
} // getCompleteURL()

function sendToCountryPage(url) {
  //  First check if a cookie exists.
  //  If it does: make the links on the external pages point to that lang
  //  If it does not: make links point to USA.
  var country_code;
  if(getCookie("tcTempCookie")) {
    var permRegion = getCookie("tcTempCookie");
    if (permRegion == 'pt_br') {
      location.href = getCompleteURL('/'+permRegion+url);
      }
    else if (permRegion == 'zh_cn') {
      location.href = getCompleteURL('/'+permRegion+url);
      }
    else if (permRegion == 'pt_pt') {
      location.href = getCompleteURL('/pt_br'+url);
      }
    else {
      location.href = getCompleteURL('/'+permRegion.substring(0,2)+url);
      }
    }
  else {
    // If no cookie for country exists,
    // send to engl version.
    location.href = getCompleteURL('/'+'en'+url);
    }
}

/*****************************
function checkRememberBox() {
    if (getCookie('tcPermCookie')) {
        if ( (getCookie('tcPermCookie')) == (getCookie('tcTempCookie')) ) {
            document.getElementById("CountryRememberCheckBox").checked = true;
        }
    }
}
*****************************/

function printCountrylist(countryName,countryDir) {
  currentRegion = getCookie('tcTempCookie')
  for (i=0; i < countryDir.length; i++) {
    if (countryDir[i] == currentRegion) {
      document.write('<option value="' + countryDir[i] + '" selected>' + countryName[i] + '</option>');
      }
  else {
    document.write('<option value="' + countryDir[i] + '">' + countryName[i] + '</option>');
    }
  }
}

function enrollRedirect() {
  var cookie_value = getCookie('tcTempCookie');
  if (cookie_value == '') {
    cookie_value = 'en';
  }
  location.href = getCompleteURL('/pls/reghtml/registration.beginapplication?p=tc&l=' + cookie_value);
}

// Get link to documentts

function getCountryLink(link_type, old_country, document_type){

  country = getCookie('tcTempCookie').substr(3,2);

  if (link_type == 'support_loginhelp') {
    location.href = getCompleteURL('/pls/html/think.support?p=loginhelp&c='+country);
  }
  else if (link_type == 'support_feedback') {
    location.href = getCompleteURL('/pls/html/think.support?p=feedback&c='+country);
  }
  else if (link_type == 'parentsdirectory') {
    location.href = getCompleteURL('/pls/html/think.parentsdirectory?c='+old_country);
  }
  else if (link_type == 'document') {
    //  Send them to the proper URL for competition page vs projects page
    if (location.href.substr(location.href.indexOf('/',7)+1,11) == 'competition' || location.href.substr(location.href.indexOf('/',7)+1,7) == 'library') {
      location.href = getCompleteURL('/pls/html/think.document?t='+document_type+'_comp'+'&c=us');
    }
    else {
      location.href = getCompleteURL('/pls/html/think.document?t='+document_type+'&c='+country);
    }
  }
  else if (link_type == 'registration_preview') {
    newWin('/pls/html/registration.previewagreement?c='+country+'','550','450','fullpopup','yes');
  }
  else if (link_type == 'try_register') {
    location.href = getCompleteURL('/pls/html/try.register?c='+country);
  }
  else if (link_type == 'help') {
    location.href = getCompleteURL('/pls/html/think.help?id=');
  }
  else if (link_type == 'faq_library') {
    location.href = getCompleteURL('/pls/html/think.help?id=199737');
  }
}

// Do Login
function doLogin() {
  document.login.action = 'https://' + location.hostname + '/pls/html/think.web_login';
  document.login.submit();
}//end doLogin function

function enrollRedirect() {
  var cookie_value = getCookie('tcTempCookie');
  if (cookie_value == '') {
    cookie_value = 'en';
  }
  var url = '/pls/reghtml/registration.beginapplication?p=tc&l=' + cookie_value;
  location.href = url;
}

function newRWin(url_path, win_width, win_height, win_name, win_scroll)
{
  if (win_scroll == ''){
    win_scroll = 'no';
  }
  newWindow =
    window.open(url_path,win_name,config="resizable=yes,scrollbars=no,screenX=50,screenY=50,left=50,top=50,width="+
    win_width+",height="+win_height);
    windowWidth = parseInt(win_width);
    windowHeight = parseInt(win_height);
  newWindow.resizeTo(windowWidth,windowHeight+50)
  newWindow.focus();
}


//------------------------------------------------
//-- S U B M I T E N T E R
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
   {
   doLogin();
   return false;
   }
else
   return true;
}

//------------------------------------------------
//-- P U T F O C U S
function putFocus() {
  document.login.p_username.focus();
}
