// *********************************
// script written by Radek HULAN
// http://hulan.cz/
// (C) 2011
// *********************************

// 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) {
		$('input[name=doruceni]:visible').first().attr('checked',true);
		var doruceni = parseFloat($('input[name=doruceni]:visible').first().parents('p').attr('title'));
	}
	var celkem = price + uhrada + doruceni;
	$('#totalpricedelivery span').text(celkem);
}

// menu functions
var oMenuTimeout, noClickProcess = false;
function hideMenu(){ oMenuTimeout=window.setTimeout("hideAllMenu()",400); }
function showMenu(item){
	if (oMenuTimeout) window.clearTimeout(oMenuTimeout);
	for (var i = 1; i<=5; i++) {
		if (i == item) $('#menu'+i).show(150); else $('#menu'+i).hide(150);
	}
}
function hideAllMenu(){ showMenu(0); }

$(document).ready(function(){
    $('#menu > ul > li').mouseenter( function(){
    	noClickProcess = true;
    	var i = parseInt($(this).attr('rel')); 
   		showMenu(i); 
    }).click( function() {
    	if (noClickProcess) return;
    	var i = parseInt($(this).attr('rel'));
    	if (i > 0) {
    		var s = $('#menu'+i+':hidden').size();
    		if (s > 0) showMenu(i); else hideAllMenu();
    	} else hideAllMenu();
    	return false;
    });
	$('#menu').mouseleave( function(){ hideMenu(); });
});

// colorbox
function processLightbox() {
	$("a[rel='lightbox']").colorbox( {
		slideshow:true, 
  		slideshowAuto:false, 
  		href: function() { return $(this).attr('href'); },
  		maxWidth:'95%', 
  		maxHeight:'95%', 
  		scalePhotos:true
	});
}
$(document).ready(function(){ processLightbox(); });

var filtrcenarange, filtrvyskarange;

function hashAJAXkola() {
   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: "/wizard-ajax.php",
       	type: "GET",
       	dataType: "html", 
       	data: hash,
       	success: function(result) { 
			$('#koloresult').html(result);      
			var o = $('#koloresult').offset();
			$('html, body').animate({scrollTop:o.top}, 400);
			setMinMax();
       	},
       	error: function(jqXHR, textStatus, errorThrown) {
       		$('#loader').toggleClass('hide');
       	}
     });
   })
   if (oldhash) {
     oldhash = '';
     $(window).hashchange();
   }
}

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: "/wizard-eshop.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();
   }
}

function setMinMax() {
	var api = $(filtrcenarange).data("rangeinput");
    api.setMin($('#mincena').text());
    api.setMax($('#maxcena').text());
    $('#cenakola').html('Cena kola od <strong>'+$('#mincena').text()+' Kč</strong> do <strong>'+$('#maxcena').text()+' Kč</strong>');
    var api = $(filtrvyskarange).data("rangeinput");
    api.setMin($('#minvyska').text());
	api.setMax($('#maxvyska').text());
}

function filtrAJAXkola() {
	$('#filtrbutton').click( function(){
		$('#filtrcontent').toggle(400);
		$('#filtrbutton').toggleClass('active');
	});
	filtrcenarange = $("#cenaid").rangeinput();
	filtrvyskarange = $("#vyskaid").rangeinput();
  	$(".slider").mousedown(function() {
    	var el = $(this).parent().find(".sliderswitcher").first();
    	if (el.text() == 'zapnout') {
      		el.addClass('active').text('vypnout');
      		$(this).parent().find("input").attr('disabled','');
    	}
  	});
  	$(".sliderswitcher").click( function(){
    	var r = $(this).attr('rel');
    	var t = $(this).text();
    	if (t == 'zapnout') {
      		$(this).addClass('active').text('vypnout');
      		$('input[name='+r+']').attr('disabled','');
    	} else {
      		$(this).removeClass('active').text('zapnout');
      	$('input[name='+r+']').attr('disabled','disabled');
    	}
  	});
}

function confirmDoIt() {
	return confirm("Skutečně chcete provést tuto akci?");
}

