$(document).ready(function() {
	cart_add_init();
	effects_init();
	fancybox_init();
	JT_init();
	filters_init();
	main_menu_init();
});

function cart_add_submit(linkId, $form) {
	var expl = linkId.split(":");
	var itemId = expl[1];
	var itemUnit = expl[2];
	var itemPackage = expl[3];
	var can_submit = true;
	var msg_add = '';
	var itemAmount = parseFloat(document.getElementById('amount_'+itemId).value.replace(',', '.'));
	if (isNaN(itemAmount)) {
		itemAmount = 0;
	}
	
	if($("#JT").length) {
		$('#JT').remove();
	}
	else {
		if (itemAmount == 0) {
			JT_show2(linkId, '<b>Zadejte prosím požadované množství.</b>');
			can_submit = false;
		}
		else if (itemPackage != '') {
			itemPackage = parseFloat(itemPackage.replace(',', '.'));
			
			mdl = Math.round((itemAmount % itemPackage) * 100) / 100;
			if (mdl == itemPackage) {
				mdl = 0;
			}
			if (mdl != 0 || itemAmount == 0) {
				if (itemAmount < itemPackage) {
					msg_add = 'Vyberte nejméně <b>' + itemPackage.toString().replace('.', ',') + itemUnit + '</b>';
				}
				else {
					choose1 = (Math.round((itemAmount - mdl) * 100) / 100).toString().replace('.', ',');
					choose2 = (Math.round((itemAmount - mdl + itemPackage) * 100) / 100).toString().replace('.', ',')
					msg_add = 'Vyberte buď <b>' + choose1 + itemUnit + '</b> nebo ' + '<b>' + choose2 + itemUnit + '</b>';
				}
				JT_show2(linkId, 'Produkt prodáváme pouze po násobcích <b>' + itemPackage.toString().replace('.', ',') + itemUnit + '</b>.<br />' + msg_add);
				can_submit = false;
			}
		}
		
		if (can_submit == true) {
			$form.submit();
		}
	}
}
	
function cart_add_init() {
	$('table.list td.cart_cell a').click(function(){
		cart_add_submit($(this).attr("id"), $(this).parents("form"));
		return false;
	});
	
    $('table.list td.cart_cell input:text').bind("keypress", function(e) {
		keycode = e.which;
		if(keycode == 13) { // enter
			cart_add_submit($(this).parents("form").find("a").attr("id"), $(this).parents("form"));
			return false;
		}
	});	
}
	
function filters_init() {
	$('#price_display_sbox, #sort_by_sbox').change(function(){
		$(this).parents("form").submit();
	});
}
	
function main_menu_init() {
	var $css = {
		'border-bottom-color' : '#ffffff',
		'border-bottom-style' : 'solid',
		'border-bottom-width' : '1px'
	}
	$("ul.sf-menu li ul li").each(function() {
		if ($(this).is(':last-child')) {
			$(this).children().not('ul').css($css);
		}
	});

	$("ul.sf-menu").supersubs({
	    minWidth:	100,
	    maxWidth:	5000,
	    extraWidth:	0
    })
    .superfish({
		dropShadows:	false,
    	speed:			100,
    	delay:			600
    });
}
	
function fancybox_init() {
	$("a.fancybox").fancybox({
		'overlayColor'		:	'#000',
		'overlayOpacity'	:	0.2
	});
}

function effects_init() {
	$("a#login_show, a#login_hide").bind("click", function() {
		var box = $("div#login_box");
		
		if (box.is(":hidden")) {
			box.slideDown("fast");
		}
		else {
			box.slideUp("fast");
		}
	});	
	
    $("table.auto_row_color tr:odd td")
    .css("background-color", "#d1d1d1")
	
	$('div.list_box div.detail a img, div.list_box_r div.detail a img, div.section_hot_box div.detail a img')
	.mouseover(function(){
		$(this).attr('src', uni_path_url + '/img/link-detail-hover.gif');
	})
	.mouseout(function(){
		$(this).attr('src', uni_path_url + '/img/link-detail.gif');
	});

	$('input.btn_filter')
	.mouseover(function(){
		$(this).css('background-image', 'url("' + uni_path_url + '/img/bg-btn-filter-hover.gif")');
	})
	.mouseout(function(){
		$(this).css('background-image', 'url("' + uni_path_url + '/img/bg-btn-filter.gif")');
	});

	$('table.list td.more_cell a img')
	.mouseover(function(){
		$(this).attr('src', uni_path_url + '/img/detail-more-hover.gif');
	})
	.mouseout(function(){
		$(this).attr('src', uni_path_url + '/img/detail-more.gif');
	});

	$('table.list td.cart_cell a img')
	.mouseover(function(){
		$(this).attr('src', uni_path_url + '/img/cart-hover.gif');
	})
	.mouseout(function(){
		$(this).attr('src', uni_path_url + '/img/cart.gif');
	});

	$('table.list td.clear_amount input.amount').click(function(){
		if ($(this).val() == "1") {
			$(this).val('');
		}
	})
	
	$('select#id_wod_select').change(function(){
		var selBox = document.getElementById('id_wod_select');
		var $targetElem = $('#id_pup_table');
		var $targetElem2 = $('#id_delivery_address');
		var sValue = selBox.options[selBox.options.selectedIndex].value;
		
		if (sValue != "") {
			var expl = sValue.split("-");
			var type = expl[0];
			
			if (type == 'odber') {
				$targetElem.show('fast');
				$targetElem2.hide('fast');
			}
			else {
				$targetElem.hide('fast');
				$targetElem2.show('fast');
			}
		}
		else {
			$targetElem.hide('fast');
			$targetElem2.hide('fast');
		}
	})
}

function show_item(id_item) {
	var item = document.getElementById(id_item);
	item.style.display = 'block';
}

function hide_item(id_item) {
	var item = document.getElementById(id_item);
	item.style.display = 'none';
}