/* 
 * (c) Copyright 2010 Mogalupa.
 * http://www.mogalupa.com
 */

function showOption(tickerToShow) {
 if(document.getElementById(tickerToShow).className != 'option-ticker option-ticker-selected') {
  document.getElementById(tickerToShow).className = 'option-ticker option-ticker-show';
 }
}

function hideOption(tickerToHide) {
 if(document.getElementById(tickerToHide).className != 'option-ticker option-ticker-selected') {
  document.getElementById(tickerToHide).className = 'option-ticker option-ticker-hide';
 }
}

function changeOption(elementSelectId, selectIndex, allIds) {
 document.getElementById(elementSelectId).selectedIndex = selectIndex;
 var currentOption = document.getElementById(elementSelectId);
 var currentOptionFullText = currentOption.options[selectIndex].text;
 var currentOptionStrings = currentOptionFullText.split('_');
 var currentOptionText = currentOptionStrings[1];
 document.getElementById('current-option').innerHTML = currentOptionText;
 document.getElementById('current-option').className = 'show';
 document.getElementById('current-option-alert').className = 'hide';

 optionToActivate = 'option-' + selectIndex;
 tickerToShow = 'option-ticker-' + selectIndex;
 for(i=1; i<allIds+1; i++) {
  currentOption = 'option-ticker-' + i;
  document.getElementById(currentOption).className = 'option-ticker option-ticker-hide';
 }
 document.getElementById(tickerToShow).className = 'option-ticker option-ticker-selected';
}

function checkOptions(alert) {
 currentOptionText = document.getElementById('current-option').className;
 if(currentOptionText == 'hide') {
  document.getElementById('current-option-alert').innerHTML = alert;
  document.getElementById('current-option-alert').className = 'show';
 }
}

function polaOnTop(polaId) {
 var pola = '#' + polaId;
 jQuery(document).ready(function($) {
  $(pola).topZIndex( { increment: 1 } );
 });
}

/**************** AJAX ***********************/

function ajaxConnection() {

 var xmlhttp;
 if (window.XMLHttpRequest)
 {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  return xmlhttp;
 }
 else if (window.ActiveXObject)
 {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  return xmlhttp;
 }
 else
 {
  alert("Your browser does not support XMLHTTP!");
 }
}

function updateCitiesList(countryCode) {
 if(countryCode != 'reset') {

  var connection = ajaxConnection();
  connection.onreadystatechange=function()
  {
   if(connection.readyState==1) document.getElementById('cities-list').innerHTML='<img src="../bt_ajax-loader.gif"/>';
   if(connection.readyState==4)
   {
    var response = connection.responseText;
    document.getElementById('cities-list').innerHTML=response;
   }
  }

  var url="../mogashops/index/city";
  url=url+"?country="+countryCode;
  url=url+"&sid="+Math.random();
  connection.open("GET",url,true);
  connection.send(null);
 }
}


function updateShopsList(el) {
 document.getElementById('shops-area').className='hide';
 selectIndex = document.getElementById(el).selectedIndex;
 selectedText = document.getElementById(el).options[selectIndex].text;
 if(selectIndex != 0) {
  var connection = ajaxConnection();
  connection.onreadystatechange=function()
  {
   if(connection.readyState==1) document.getElementById('shops-area-loading').innerHTML='<img src="../bt_ajax-loader.gif"/>';
   if(connection.readyState==4)
   {
    var response = connection.responseText;
    document.getElementById('shops-list').innerHTML=response;
    jQuery(document).ready(function($) {
     $('#shops').jScrollPane({
      scrollbarWidth:18
     });
     $('#jScrollPaneDrag').css({
      'height':'18px'
     });
    });
    document.getElementById('shops-area-loading').innerHTML='';
    document.getElementById('shops-area').className='show';
   }
  }

  var url="../mogashops/index/shops";
  url=url+"?city="+selectedText;
  url=url+"&sid="+Math.random();
  connection.open("GET",url,true);
  connection.send(null);
 }
}