
$(document).ready(function () {
	
	updateSideList();
	$('#search').one('focus',searchField)
	$('#sitesearch').submit(sitesearch);
	$('.searchsubmit').click(function (e) { e.preventDefault(); $('#search').focus(); $('#sitesearch').submit(); });
	
	if ($('#comparebox').size() > 0)
	{
	 var iniOffset = $('#comparebox').offset().top;
   $('#comparebox').hide();
	 $(window).scroll(function () {
	   var fromtop = ($(window).scrollTop() - iniOffset > -40) ? $(window).scrollTop() - iniOffset +40 : 0;
	   $('#comparebox').css({marginTop: fromtop});
	 });
	  
	}
	
	$('#chooselang li.selected').click(footerLanguage);
	$('#toplang li.selected').click(headerLanguage);


	$('#iimenu dt').each(function (i,e) {
		$(e).attr({ord:i});
	}).mouseenter(function (e) {  
 
  // $(this).next().hasClass('iimenuexpand') && 
		if (!$(this).hasClass('expanded'))
		{
			$(this).addClass('expanded');
		  
			var orig = $(this);
		
    if (jQuery.browser.msie && (parseInt(jQuery.browser.version)==7)) {
        var o = orig.children().clone().appendTo('#iimenu');}
    else
       {	var o = orig.next().clone().appendTo('#iimenu');}
		
			clearTimeout(timeouts[orig.attr('ord')]);
			o.show().css({zIndex:2,position:'absolute',top: $('#iimenu').outerHeight(), left: $(this).position().left}).children().hide().css({opacity:0});
			o.children().slideDown(100).animate({opacity:1},100);
			orig.one('mouseleave',function () {
				timeouts[orig.attr('ord')] = setTimeout(function () {
					orig.removeClass('expanded'); 
					o.children().animate({opacity:0},100).slideUp(100,function () { o.remove(); });
				},400);  
			});
			o.mouseenter(function () {
				clearTimeout(timeouts[orig.attr('ord')]); 
			})
			.mouseleave(function () {  
				timeouts[orig.attr('ord')] = setTimeout(function () {
					orig.removeClass('expanded');
					o.children().animate({opacity:0},100).slideUp(100,function () { o.remove(); });
				},400);
			})
			;
		}
	});
	$('#iitotop a').click(function () {
		$(document).scrollTop(0);
	});
});

var timeouts = [];

function footerLanguage (e) {
	e.preventDefault();
	if ($('#chooselang .active').size() > 0) return;
	var o = $('#chooselang #lang');
	var d = o.clone().appendTo('#chooselang');
	d.css({position: 'absolute',bottom:0,left:0,height:'auto',background:'#000',opacity: 0.9}).find('li').show();
	d.mouseleave(function () { d.find('li:not(.selected)').stop().animate({opacity:0},200).slideUp(200,function () { d.remove();}); });
	d.find('li:not(.selected)').css({opacity:0}).hide().slideDown(300).animate({opacity: 1},200).addClass('active');
}

function searchField (e) {
	e.preventDefault();
	$('#search').val('');
}

function headerLanguage(e) {
	e.preventDefault();
	$('#toplang .selected').removeClass('selected').addClass('active');
	$('#toplang li').stop().clearQueue().slideDown(50);
	$('#toplang').one('mouseleave',function () { $('#toplang li:not(.active)').delay(500).slideUp(50, function () { $('#toplang li.active').addClass('selected'); });  });
}

$.fn.colorize = function (addclass) {
		$(this).html(function(i,text){
		    return text.replace(/\S+\s|^\S+$/, function(match)
		    {
		        return '<span class="'+addclass+'">' + match + '</span>'; 
		    });
		});
	return this;
}

function locationHref() {
	return location.href.split('#')[0];
}

$.fn.center = function () {
	var w = $(this).outerWidth();
	var ww = $(window).width();
	
	var l = Math.max(0,(ww-w)/2);
	var t = $(window).scrollTop();
	
	$(this).css({
		top: t,
		left: l,
		margin: 0
	});
	return this;
}

function basketPop (id) {
	$('#popwdw_compare td[pid='+id+']').remove();
	if ($('#popwdw_compare table td').size() == 0)
		$('#popwdw_compare').remove();
	var c = $.cookie('iicompare').split(',');
	var o = [];
	for (i in c)
		if ('#'+id != c[i])
			o[o.length] = c[i];
	compareBasket = o;
	$.cookie('iicompare',compareBasket.join(','),{path: '/'});
	$('div[pid='+id+'] .remove').click();
}

function basketFlush()
{
  $('a.remove').click();
  updateBasket([]);
}

function updateBasket (newBasket)
{
  compareBasket = newBasket;
  $.cookie('iicompare',compareBasket.join(','),{path: '/'});
  if (compareBasket.length > 4)
    truncateBasket();
  updateSideList();
}

function updateSideList ()
{
  if ($('#comparebox').size() == 0)
    return;
  if (compareBasket.length > 0)
    $('#comparebox').fadeIn(200);
  else
    $('#comparebox').fadeOut(200);
  if ($('#compareitems').size() > 0)
  {
    var url = location.href.split('#')[0];
    $.post(
      url,
      { sidelist : 1 },
      function (html) { 
      $('#compareitems').animate({opacity:0},50,function () {
        $(this).html(html).animate({opacity:1},100); 
      });
    });
  }
}

function truncateBasket() {
  var v = compareBasket[0];
  compareBasket = $.grep(compareBasket,function (n, i) { return n != v; });
  $.cookie('iicompare',compareBasket.join(','),{path: '/'});
  $('div[pid='+v.substr(1)+']').removeClass('active').find('a.compare').toggle().first().removeClass('active');
  $('div[pid='+v.substr(1)+'] div:last:not(.tilemenu)').remove();
}

$.fn.compareOff = function () {
	
	$(this).parent().removeClass('active').find('a.compare').toggle().first().removeClass('active');
	$(this).remove();
}
$.fn.compareOn = function () {
	var v = $(this).attr('href');
	var o = $(this).addClass('active').parent().parent().addClass('active').css({ position: 'relative', top: 0, left: 0});
	$('<div><a class="remove" href="'+v+'">x</a></div>').css({position:'absolute', top: 0, right: '0.4em'}).appendTo(o).click(function (e) {
		e.preventDefault();
		$(this).compareOff();
		var basketTemp = $.grep(compareBasket,function (n, i) { return n != v; });
		updateBasket(basketTemp);
	});
	$(this).hide().next().show();
}



$.fn.expandables = function () {
  var selector = $(this).selector;
  var exp = ($.cookie('iiprodexp')) ? $.cookie('iiprodexp').split(',') : [];

  $(this).each(function (i,e) {
    var o = $(e);
    var c = o.find('a').attr('href');
    var ex = false;
    
    for (i in exp)
      if (exp[i] == c)
          ex = true;
    
    if ((i == 0 && exp.length == 0) || ex)
    {
      o.find('a').addClass('active');
      var h = o.nextAll().outerHeight();
      o.nextAll().removeClass('hidden').show().wrapAll('<div />').parent();
    }
    else
    {
      o.find('a').removeClass('active');
      var h = o.nextAll().outerHeight();
      o.nextAll().removeClass('hidden').show().wrapAll('<div />').parent().hide();
    }
    o.find('a').click(function (e) {
      e.preventDefault();
      if (o.find('a').hasClass('active'))
        o.collapseList();
      else
        o.expandList();
      verifyExpandAll(selector);
    });
  });
  $('.expall').expandAll(selector);
  $('.colall').collapseAll(selector);
  verifyExpandAll(selector);
}

$.fn.expandList = function () {
  var o = $(this);
  if (o.next().is(':animated'))
    return o;
  if (o.find('a').hasClass('active'))
    return o;

  o.find('a').addClass('active');
  o.nextAll().stop().clearQueue().css({opacity: 0}).slideDown(50).animate({opacity:1},100);
  return o;
}

$.fn.collapseList = function () {
  var o = $(this);
  if (o.next().is(':animated'))
    return o;
  if (!o.find('a').hasClass('active'))
    return o;

  o.find('a').removeClass('active');
  o.nextAll().stop().clearQueue().animate({opacity:0},100).slideUp(50);
  return o;
}

function verifyExpandAll (selector) {
  var expanded = true;
  $(selector).find('a').each(function (i,e) {
    if (!$(e).hasClass('active'))
      expanded = false;
  });
  if (expanded)
  { 
    $('.expall').hide();
    $('.colall').show();
  }
  else
  {
    $('.expall').show();
    $('.colall').hide();
  }
  storeExpanded(selector);
}

function storeExpanded(selector) {
  var stored = [];
  $(selector+' a').each(function (i,e) {
    if ($(e).hasClass('active'))
      stored[stored.length] = $(e).attr('href');
  });
  $.cookie('iiprodexp',stored.join(','),'/');
}

function restoreExpanded(selector) {
  var exp = $.cookie('iiprodexp').split(',');
  if (exp.length == 0)
    return;
  for (i in exp)
    $(selector+' a[href='+exp[i]+']').css({ color : '#FF0000'}).expand();
}

$.fn.expandAll = function (selector) {
  $(this).click(function (e) {
    e.preventDefault();
    var o = $(selector);
    o.each(function (i,e) {
      $(e).expandList();
    });
    verifyExpandAll(selector);
  });
}

$.fn.collapseAll = function (selector) {
  $(this).click(function (e) {
    e.preventDefault();
    var o = $(selector);
    o.each(function (i,e) {
      $(e).collapseList();
    });
    verifyExpandAll(selector);
  });
}

function sitesearch (e) {
	e.preventDefault();
	var url = $(this).attr('action');
	var s = $.trim($('#search').val());
	if (s == '')
	 return;
	var post = { search : s };
	$.ajax({
	  data: post,
	  type: 'post',
	  dataType: 'html',
	  url: url,
	  beforeSend: function () {
	    $('#searchresults').fadeOut(150,function () { $(this).empty(); });
	    $('#searchloading').fadeIn(200);
	  },
	  success: function (data) {
      $('#searchloading').fadeOut(150, function () {
        $('#searchresults').hide().html(data).slideDown(400,function() { $('#searchresults').children().animate({opacity: 1}); });
        $('#searchclose').click(function (e) {
          e.preventDefault();
          $('#searchresults').fadeOut(200);
        });
        $('#searchresults').children().css({opacity: 0});
      });
	  },
	  error: function () {
	    $('#searchloading').fadeOut(500);
	  }
	});
}

