// CountrySelection
var cstimeout = 500;
var csrangetimeout = 500;
var csrangetimeout2 = 200;
var csclosetimer = null;
var csrangechangetimer = null;
var csrangeclosetimer = null;
var csopened = null;
var csrangeopened = null;
var csrangeopenedli = null;
var cscountryhighlighted = null;

function InitCS() {
   var cs_div = $('countrySelect');
   var cs_lis = getByTagName(cs_div, "LI");
   for (var i = 0; i < cs_lis.length; i++) {
      if (cs_lis[i].className == "CurrentCountry") {
         var currentCountryelem = $('CurrentCountry');
         currentCountryelem.onmouseover = function() {
            OpenCS();
         }
         currentCountryelem.onmouseout = function() {
            CSCloseTimer();
         }
      } else if (cs_lis[i].className == "countryRange") {
         cs_lis[i].onmouseover = function() {
            var sub_list = getByTagName(this, "UL");
            CancelCSCloseTimer();
            CancelCSRangeChangeTimer();
            CSRangeChangeTimer(sub_list[0], this);
            //OpenCSRange(sub_list[0], this);
         }
         cs_lis[i].onmouseout = function() {
            CSCloseTimer();
         }
      } else if (cs_lis[i].className == "country") {
         cs_lis[i].onmouseover = function() {
            CSHighlightCountry(this);
         }
      }
   }
   var cs_div = $('countrySelect2');
   var cs_lis = getByTagName(cs_div, "LI");
   for (var i = 0; i < cs_lis.length; i++) {
      if (cs_lis[i].className == "CurrentCountry") {
         var currentCountryelem = $('CurrentCountry');
         currentCountryelem.onmouseover = function() {
            OpenCS();
         }
         currentCountryelem.onmouseout = function() {
            CSCloseTimer();
         }
      } else if (cs_lis[i].className == "countryRange") {
         cs_lis[i].onmouseover = function() {
            var sub_list = getByTagName(this, "UL");
            CancelCSCloseTimer();
            CancelCSRangeChangeTimer();
             CSRangeChangeTimer(sub_list[0], this);
            //OpenCSRange(sub_list[0], this);
         }
         cs_lis[i].onmouseout = function() {
            CSCloseTimer();
         }
      } else if (cs_lis[i].className == "country") {
         cs_lis[i].onmouseover = function() {
            CSHighlightCountry(this);
         }
      }
   }
    OpenCS();
}
function OpenCS() {
   CancelCSCloseTimer();
   CSClose();
   csopened = $('countryList');
   csopened.setStyle({display: 'inline', color: '#ffffff'});
}
function CSClose() {
   if (csopened) {
      CSUnhighlightCountry();
      CSRangeClose();
      csopened.hide();
      csopened.setStyle({color: '#000000'});
      csopened = null;
   }
}
function CSCloseTimer() {
   csclosetimer = window.setTimeout(CSClose, cstimeout);

}
function CancelCSCloseTimer() {
   if (csclosetimer) {
      window.clearTimeout(csclosetimer);
      csclosetimer = null;
   }
}
function OpenCSRange(toopenrange, currentli) {
   CancelCSRangeCloseTimer();
   CSRangeClose();
   csrangeopened = toopenrange;
   csrangeopened.style.display = 'inline';
   csrangeopenedli = currentli;
   csrangeopenedli.style.backgroundColor = '#167ebc';
   csrangeopenedli.style.color = '#ffffff';
}
function CSRangeClose() {
   if (csrangeopened) {
      csrangeopened.style.display = 'none';
      csrangeopened = null;
      csrangeopenedli.style.backgroundColor = '#ffffff';
      csrangeopenedli.style.color = '#000000';
      csrangeopenedli = null;
   }
}
function CSRangeCloseTimer() {
   csrangeclosetimer = window.setTimeout(CSRangeClose, csrangetimeout);

}
function CSRangeChangeTimer(first, second) {
   csrangechangetimer = window.setTimeout(function(){OpenCSRange(first, second)}, csrangetimeout2);
}
function CancelCSRangeChangeTimer() {
   if (csrangechangetimer) window.clearTimeout(csrangechangetimer);

}
function CancelCSRangeCloseTimer() {
   if (csrangeclosetimer) {
      window.clearTimeout(csrangeclosetimer);
      csrangeclosetimer = null;
   }
}
function CSHighlightCountry(currentli) {
   CSUnhighlightCountry();
   cscountryhighlighted = currentli;
   cscountryhighlighted.style.backgroundColor = '#167ebc';
   var cs_a = getByTagName(cscountryhighlighted, "a");
   cs_a[0].style.color = '#ffffff';
}
function CSUnhighlightCountry() {
   if (cscountryhighlighted) {
      cscountryhighlighted.style.backgroundColor = '#ffffff';
      var cs_a = getByTagName(cscountryhighlighted, "a");
   	cs_a[0].style.color = '#000000';
      cscountryhighlighted = null;
   }
}
function SetCountry(country) {
   if (country.length >= 1) {
      if (confirm($F('CountryChangeConfirm'))) {
      	new Ajax.Request("ajax/set_country.php", {
              method: 'get',
              parameters: {shopcountry: country},
              onComplete: function() {
                  document.location.reload();
              }
         });  
      }
   }
}