// ***********************************************************************************
// Copyright (c) 2011 http://MyWebdesign.cz/
// ***********************************************************************************

// eshop
function calcTotalPrice() {
	var price = $('#totalprice').attr('data-value');
	price = price.replace(" ","");
	price = price.replace(",",".");
	price = parseFloat(price);
	var doruceniid = parseInt( $('input[name=doruceni]:checked').attr('data-rel') ); 
	var total = parseInt( $('input[name=doruceni]:checked').attr('data-total') ); 
	if (doruceniid != total) 
		$('#popup').html('').removeClass('showit').addClass('hideit');
	else {
		var h = $('input[name=skladid]:checked').attr('data-html'); 
		$('#popup').html(h).removeClass('hideit').addClass('showit');
	}
	var doruceni = parseFloat($('#doruceni'+doruceniid).attr('data-value'));
	var uhradaid = parseInt( $('input[name=uhrada]:checked').attr('data-rel') );
	var uhrada = parseFloat($('#uhrada'+uhradaid).attr('data-value'));
	var isok = false; 
	$('input[name=uhrada]').attr('disabled','disabled');
	for (var i=0; i < relaceDU[doruceniid-1].length; i++) {
		$('#uhrada'+relaceDU[doruceniid-1][i]).removeAttr('disabled');
		if (uhradaid == parseInt(relaceDU[doruceniid-1][i]))
			isok = true;
	}
	if (!isok) {
		$('#uhrada'+relaceDU[doruceniid-1][0]).attr('checked',true);
		var uhrada = parseFloat($('#uhrada'+relaceDU[doruceniid-1][0]).attr('data-value'));
	}
	var celkem = price + uhrada + doruceni;
	$('#totalprice').text(celkem);
}

function setSelectedUhrada(p){
	$('.mainform label').removeClass('selected notactive');
	$('input[name=doruceni]:checked').parent().addClass('selected');
	$('input[name=uhrada]:checked').parent().addClass('selected');
	$('.mainform input[disabled=disabled]').parent().addClass('notactive');
	if ($('#doruceni'+p).is(':checked'))
		$('.prodejny label').removeClass('notactive');
	else
		$('.prodejny label').addClass('notactive');
}

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 confirmAction() {
	return confirm("Skutečně chcete smazat tuto položku?");
}
function confirmDeleteDodaci() {
	return confirm("Skutečně chcete smazat tuto dodací adresu?");
}
function confirmDoIt() {
	return confirm("Skutečně chcete provést tuto akci?");
}

var isCompleteAjax;
function hashAJAXeshop() {
   var oldhash = window.location.hash;
   $(window).hashchange( function(){
     var hash = window.location.hash;
     if (hash == oldhash) return;
     oldhash = hash;
     $('.loader').removeClass('hidden');
     if (hash) hash = hash.substr(1);
     if (!hash) hash = isEmptyHash;
     $.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').addClass('hidden');
       	}
     });
     if (isCompleteAjax) {
     	$.ajax({
			url: "/eshop/ajax-filtr.php",
	       	type: "GET",
    	   	dataType: "html", 
       		data: hash,
       		success: function(result) {  $('#filter').html(result); },
     	});
     }
   })
   if (oldhash) {
     oldhash = '';
     $(window).hashchange();
   }
}
function setEshopAjax() {
	$('.sort a').click(function() {
		var d = $(this).attr('href'); var i = d.indexOf('?'); if (i > 0) d = d.substr(i+1); else d = '';
    	isCompleteAjax = true;
    	window.location.hash = d;
    	return false;
	});
	$('.pager a').click(function() {
		var d = $(this).attr('href'); var i = d.indexOf('?'); if (i > 0) d = d.substr(i+1); else d = '';
    	isCompleteAjax = false;
    	window.location.hash = d;
    	return false;
	});
}
function setFiltrAjax() {
	$('#filter a').click(function() {
		var d = $(this).attr('href'); var i = d.indexOf('?'); if (i > 0) d = d.substr(i+1); else d = '';
    	isCompleteAjax = true;
    	window.location.hash = d;
    	return false;
	});
}

function movableMenu() {
	$('<ul class="menu" id="switchcats"><li><a id="clickothercats" href="#othercats">Další kategorie ›</a></li></ul>').insertBefore('#filter');
	$('#clickothercats').click( function() {
		$('#switchcats').hide().html( $('ul#othercats').html() ).show(300);
		$('ul#othercats').remove();
		return false;
	});
}

// colorbox
function processLightbox() {
	$("a.colorbox").colorbox( {
		slideshow:true, 
  		slideshowAuto:false, 
  		href: function() { return $(this).attr('href'); },
  		rel: 'a',
  		maxWidth:'95%', 
  		maxHeight:'95%', 
  		scalePhotos:true
	});
}

$(document).ready(function(){ 
	// naseptavac
 	$("#squery").autocomplete({
   		source: '/select-search.php', 
   		delay: 100,
   		minLength: 2,
   		html: 'html',
		select: function( event, ui ) {
			$('#queryid').val(ui.item.value);
			$('#searchform').submit();
		}
 	}).focus(function() {
 		var t = $(this).val();
 		if (t == 'Hledej') $(this).val('');
 	});

 	// zoom produktovych fotek
	function onCycleBefore(curr, next, opts) {
		var index = opts.currSlide;
		$(next).trigger('mouseenter');
	}
 	$('.produkt p.image.cycler').live('mouseenter',function() {
 		var el = $(this).find('span:first');
 		if (el.attr('data-cycle') == 'on') {
 			el.cycle('resume');
 			return;
 		}
 		el.attr('data-cycle','on');
 		var r = el.attr('data-item');
 		el.cycle({ 
    		fx: 'scrollHorz', 
    		prev: $('#prev'+r),
    		next: $('#next'+r),
    		before: onCycleBefore,
     		timeout: 0,
     		nowrap: false,
       		speed: 150
    	});
 	});
 	$('.produkt p.image img').live('mouseenter',function() {
 		var el = $(this).parents('.produkt');
 		if (el.attr('data-big') != 'true') return;
 		var p = el.attr('data-vel');
 		if (typeof p != 'undefined' && p != '') p = '<p>Velikosti: '+p+'</p>'; else p = '';
 		var s = $(this).attr('data-url');
 		s = s.toString();
 		if (s == 'undefined' || s == '') return;
 		var b = $('#bigimage');
 		if (!b.size()) {
 			$('#items').prepend('<div id="bigimage"></div>');
 			b = $('#bigimage');
 		}
		b.html('<img src="'+s+'" />'+p);
		var h = $(window).scrollTop();
		b.css('top',(h+90)+'px').show();
 	});
 	$('.produkt').live('mouseleave',function() {
		$('#bigimage').hide();
 		var el = $(this).find('p.image.cycler span:first');
 		if (el.size()) {
 			if (el.attr('data-cycle') == 'on') el.cycle('pause');
	 		$(this).find('a.arrow-left, a.arrow-right').hide();
		}
 	}).live('mouseenter',function() {
 		$(this).find('a.arrow-left, a.arrow-right').show();
 	});
 	
 	processLightbox();
});
