function switch_mode(mode) {
  var switcher = document.getElementById('listmode_switcher');
  if ( switcher ) {
    var lis = switcher.getElementsByTagName('li');
    if ( lis.length>0 ) {
      var classN = '';
      for( i=0; i<lis.length; i++ ){
        classN = lis[i].className;
        classN = classN.replace(/_on$/,'');
        if (mode==classN){
          lis[i].className = classN+'_on';
        }else{
          lis[i].className = classN;
        }
      }
    }
  }
  var id_listing = document.getElementById('contentListing');
  $('.featuredCell').height('');
  if (id_listing) id_listing.className=mode;
  setCookie('displayMode', mode, 0, '/', location.hostname);
  normalizeFeat(mode);
  return false;
}
function normalizeFeat( mode ){
  if ( mode=='grid' || mode=='gallery' ){
    var heightM=0;
    $('.'+mode+' .featuredCell').each( function(){
      var cH = $(this).height();
      if ( cH>heightM) heightM = cH;
    } );
    if (heightM>0) {
     $('.'+mode+' .featuredCell').height(heightM+2);
    }
  }
}
$(document).ready(function(){
  var m = '';
  $("#listmode_switcher li[class$=_on]").each( function(){
    m = this.className;
    m=m.replace(/_on$/,'');
  } );
  normalizeFeat(m);
});

function setCookie (name, value, expires, path, domain, secure) {
  if ( typeof domain != 'undefined' ) {
    var cl = domain;
    domain = cl.replace(/www\./i, '');
  }
  document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return '';//null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));;
}

var currentFloat = false;
var mouseX = mouseY = 0;
var OP = (navigator.userAgent.indexOf('Opera') != -1);
var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP);
var GK = (navigator.userAgent.indexOf('Gecko') != -1);
var SA = (navigator.userAgent.indexOf('Safari') != -1);
var DOM = document.getElementById;

function floatMove(){
 if (!currentFloat) return;
 var winX = getWinX() - (((GK && !SA) || OP || SA) ? 17 : 0);
 var winY = getWinY() - (((GK && !SA) || OP || SA) ? 17 : 0);
 var x = mouseX;
 var y = mouseY;

 if(x + currentFloat.clientWidth + 25 > winX + getScrX())
   x -= currentFloat.clientWidth + 20;
 else x += 20;

 if(y + currentFloat.clientHeight + 25 > winY + getScrY())
   y -= currentFloat.clientHeight;
 else y += 20;

 currentFloat.style.top = y + 'px';
 currentFloat.style.left = x + 'px'; 
}

function floatInfo(id){
  if (id==undefined){
    if (currentFloat) currentFloat.style.display = 'none';
    currentFloat = false;
  }else{
    var _p = document.getElementById(id);
    if ( _p ) {
      currentFloat = _p;
      currentFloat.style.position = 'absolute';
      currentFloat.style.display = '';
      floatMove();
    }
  } 
}

function getWinX() {
  var size = 0;
  if(window.innerWidth)
    size = window.innerWidth;
  else if(document.documentElement && document.documentElement.clientWidth)
    size = document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    size = document.body.clientWidth;
  else size = screen.width;
  return size;
}

function getWinY() {
  var size = 0;
  if(window.innerHeight)
    size = window.innerHeight;
  else if(document.documentElement && document.documentElement.clientHeight)
    size = document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    size = document.body.clientHeight;
  else size = screen.height;
  return size;
}

function getScrX() {
  var offset = 0;
  if(window.pageXOffset)
    offset = window.pageXOffset;
  else if(document.documentElement && document.documentElement.scrollLeft)
    offset = document.documentElement.scrollLeft;
  else if(document.body && document.body.scrollLeft)
    offset = document.body.scrollLeft;
  return offset;
}

function getScrY() {
  var offset = 0;
  if(window.pageYOffset)
    offset = window.pageYOffset;
  else if(document.documentElement && document.documentElement.scrollTop)
    offset = document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop)
    offset = document.body.scrollTop;
  return offset;
}

function getMouseXY(e) {
  if(e && e.pageX != null) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  else if(event && event.clientX != null) {
    mouseX = event.clientX + getScrX();
    mouseY = event.clientY + getScrY();
  }
  if(mouseX < 0) mouseX = 0;
  if(mouseY < 0) mouseY = 0;
  floatMove();
}
document.onmousemove = getMouseXY;

var max_compare_list = 4;
function addCompare(id, name, ext) {
  var checkbox = document.getElementById(id);
  if( typeof ext != 'boolean' ) ext = false;
  if ( !checkbox ) return;
  var nID = id.replace(/[^0-9]*/,'');
  if ( ext ) checkbox.checked=!checkbox.checked;
  if(checkbox.checked) {
    if ( typeof prod_list != 'undefined' ) prod_list[ nID ] = name;
    var products = getCookie('cmp_products');
    //products
    products = products.replace(/^_/,'');
    var products_list = products.length>0?products.split('_'):[];
    if ( products_list.length>=max_compare_list ) {
      alert("You have selected "+max_compare_list+" products already");
      checkbox.checked = false;
    }else{
      products_list.push(nID);
      products = products_list.join('_');
      setCookie('cmp_products', products, 0, '/', location.hostname);
      syncCompare();
    }
  } else {
    delCompare(id)
  }
  return false;
}

function delCompare(id) {
  var products = getCookie('cmp_products');
  var products_list = products.split('_');
  var nID = id.replace(/[^0-9]*/,'');
  var tmp_list = new Array();
  for(i = 0; i < products_list.length; i++) {
    if(products_list[i] != nID) tmp_list.push(products_list[i]);
  }
  var visual = document.getElementById('cmp_'+nID);
  if ( visual && typeof visual.checked != 'undefined' ) visual.checked=false;
  products = ( tmp_list.length>0 )?tmp_list.join('_'):'';
  setCookie('cmp_products', products, 0, '/', location.hostname);
  syncCompare();
  return false;
}

function syncCompare(){
  var products = getCookie('cmp_products');
  var products_list = products.split('_');
  var html_data = document.getElementById('compare_products');
  var html_data_btn = document.getElementById('compare_btn_holder');
  if(html_data) {
    html_data.innerHTML = '';
    var hide_btn = true;
    if ( typeof prod_list == 'undefined' ) return; 
    for(i = 0; i < products_list.length; i++) {
      if ( typeof prod_list[products_list[i]] == 'undefined' ) continue;
      html_data.innerHTML+= '<b>' + prod_list[products_list[i]] + '</b> <a href="" onclick="return delCompare(\'cmp_' + products_list[i] + '\');" style="color:red;">remove</a><br>';
      hide_btn=false;
    }
    if ( html_data_btn ) {
      html_data_btn.style.display = ( hide_btn )?'none':''; 
    }
  }
}
