// *********************************
// script written by Radek HULAN
// http://hulan.cz/
// (C) 2008
// *********************************

// requirements - mootols core

// top menu
function switchMenu(typ) {
	var ele = $('#switchmenu');
	ele.removeClass().addClass(typ);
	ele.css('backgroundImage',"url(/styles/images/menu-top-" + typ + ".jpg)");
	if (typ == 'zbozi') {
		$('#menuznacka').hide(300);
		$('#menuzbozi').show(300);
	} else {
		$('#menuzbozi').hide(300);
		$('#menuznacka').show(300);
	}
	return false;
}

function switchLists(typ,id) {
	var ele;
	var total = $('#menu'+typ.substr(0,typ.length-2)+' > li').size();
	for (var i = 1; i<=total; i++) {
		ele = $('#'+typ+i);
		if (i == id) ele.removeClass().addClass('active'); else ele.removeClass().addClass('folded');
	}
	return false;
}

// akcni zbozi
var currentZbozi = 1;
var casovac;
function akcniZbozi(smer) {
	var zbozi = $('#akcnizbozi > div').size();
	if (smer < 0 && currentZbozi > 1) {
		$('#akcniproduct'+(currentZbozi + 2)).hide(); 
		currentZbozi -= 1;
		$('#akcniproduct'+(currentZbozi)).addClass('product').show(300); 
	} else if (smer > 0 && currentZbozi + 2 < zbozi) {
		$('#akcniproduct'+(currentZbozi)).hide(); 
		currentZbozi += 1;
		$('#akcniproduct'+(currentZbozi + 2)).addClass('product').show(300); 
	}
	var timeout = true;
	if (currentZbozi + 3 > zbozi || currentZbozi == 1) timeout = false;
	if (timeout) casovac = setTimeout("akcniZbozi("+smer+")",1500);
}

function stopZbozi() {
	if (casovac) clearTimeout(casovac);
}

function setAkcniMove() {
	$('#akcnizbozi_previous').bind('mouseenter click',function(){ akcniZbozi(-1); return false; }).mouseleave(function() { stopZbozi(); });
	$('#akcnizbozi_next').bind('mouseenter click',function(){ akcniZbozi(1); return false; }).mouseleave(function() { stopZbozi(); });
}

// filtry
function switchFiltr() {
	if ($('#filtrshow').is(':visible'))
		$('#filtrmore').html("<img src='/styles/images/filtr-more.png' alt='Filtr' />zobrazit všechna kritéria");
	else
		$('#filtrmore').html("<img src='/styles/images/filtr-less.png' alt='Filtr' />skrýt filtr");
	$('#filtrshow').toggle(300);
}

// eshop
function calcTotalPrice() {
	var price = $('#totalprice span').text();
	price = price.replace(" ","");
	price = price.replace(",",".");
	price = parseFloat(price);
	var uhradaid = parseInt( $('input[name=uhrada]:checked').attr('rel') );
	var uhrada = parseFloat($('#uhrada'+uhradaid).attr('title'));
	var doruceniid = parseInt( $('input[name=doruceni]:checked').attr('rel') ); 
	var doruceni = parseFloat($('#doruceni'+doruceniid).attr('title'));
	$('p.doruceniswitch').hide();
	var isok = false; 
	for (var i=0; i < relaceDU[uhradaid-1].length; i++) {
		$('#doruceni'+relaceDU[uhradaid-1][i]).show();
		if (doruceniid == parseInt(relaceDU[uhradaid-1][i]))
			isok = true;
	}
	if (!isok) {
		$('#doruceni'+relaceDU[uhradaid-1][0]+' input').attr('checked',true);
		var doruceni = parseFloat($('#doruceni'+relaceDU[uhradaid-1][0]).attr('title'));
	}
	var celkem = price + uhrada + doruceni;
	$('#totalpricedelivery span').text(celkem);
}

function confirmAction() {
	return confirm("Skutečně chcete smazat tuto položku?");
}
function confirmDoIt() {
	return confirm("Skutečně chcete provést tuto akci?");
}

function bookmarksite(title,url) {
	if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ 
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all)
		void(0);
}

function hashAJAXeshop() {
   var oldhash = window.location.hash;
   $(window).hashchange( function(){
     var hash = window.location.hash;
     if (hash == oldhash) return;
     oldhash = hash;
     $('#loader').toggleClass('hide');
     if (hash) hash = hash.substr(1);
     $.ajax({
		url: "/eshop_ajax.php",
       	type: "GET",
       	dataType: "html", 
       	data: hash,
       	success: function(result) { 
       		$('#eshopresult').html(result); 
       		var o = $('#eshopresult').offset();
       		$('html, body').animate({scrollTop:o.top}, 400);
       	},
       	error: function(jqXHR, textStatus, errorThrown) {
       		$('#loader').toggleClass('hide');
       	}
     });
   })
   if (oldhash) {
     oldhash = '';
     $(window).hashchange();
   }
}

// colorbox
function processLightbox() {
	$("a.smoothbox").colorbox( {
  		href: function() { return $(this).attr('href'); },
  		iframe: true,
  		width: '700px',
  		height: '600px',
  		fixed: true
	});
	$("a.fotobox").colorbox( {
		slideshow:true, 
  		slideshowAuto:false, 
  		href: function() { return $(this).attr('href'); },
  		maxWidth:'95%', 
  		maxHeight:'95%', 
  		scalePhotos:true
	});
}

$(document).ready(function(){ 
	processLightbox(); 
  	$("#squery").autocomplete(
  		'/select-produkt.php', 
  		{multiple: false,selectFirst: false,matchSubset: 0 }
  	).result( function(e,d) { 
  		if (d) $('#searchform').submit(); 
  	});
});

