function show_child(father)
{
	var obj = document.getElementsByTagName('tr');
	var father = 'father_' + father + 'a';
	var hidden = father + ' hidden';
	var hidden2 = 'hidden';
	for(i=0; i<obj.length; i++)
	{
		var test1 = obj[i].className.search(father);
		var test2 = obj[i].className.search(hidden);
		if(test1 != -1
			&& test2 == -1)
		{
			obj[i].className = obj[i].className.replace(father, hidden);
			// nastaveni cookie
			if(obj[i].className.search(hidden2) == -1)
			{
				setCookie(father, 1);
			}
			else
			{
				setCookie(father, -1);
			}
		}
		else if(test1 != -1
			&& test2 != -1)
		{
			obj[i].className = obj[i].className.replace(hidden, father);
			// nastaveni cookie
			if(obj[i].className.search(hidden2) == -1)
			{
				setCookie(father, 1);
			}
			else
			{
				setCookie(father, -1);
			}
		}
	}
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString())
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}

function tisk()
{
	window.print();
}

function insert_comment(id, title)
{
	document.getElementById('comment_title').value = title;
	document.getElementById('comment_father').value = id;
}

function check_comment_form(error)
{
	if((document.getElementById('comment_name').value + 'x') == 'x'
		|| (document.getElementById('comment_title').value + 'x') == 'x'
		|| (document.getElementById('comment_comment').value + 'x') == 'x')
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

$(document).ready(function(){
	var height;
	var width;
	height=$('table#calendar_week').height() - $('table#calendar_week tr:first').height();

	$('table#calendar_week tr td').each(function(index) {
		if(index >= 8)
		{
			return false;
		}
		if(index > 0)
		{
			width=$(this).width()-2;
			$(this).find('span.event').each(function() {
				vals=$(this).attr('title').split(',');
				w=width/vals[1]-2;
				x=width/vals[1]*(vals[0]-1)-(vals[0]-1)*2;
				y=height*vals[2]/100;
				h=height*vals[3]/100-2;
				$(this).css('left', x + 'px');
				$(this).css('top', y + 'px');
				$(this).css('width', w + 'px');
				$(this).css('height', h + 'px');
				$(this).attr('title', '');
			}
			)
		}
	}
	)

	$('div#events span.event').mouseover(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left + 10;
		var y = e.pageY - pos.top + 10;
		$(this).find('span').css('left', x + 'px');
		$(this).find('span').css('top', y + 'px');
		$(this).find('span').css('display', 'block');
	}
	)

	$('div#events span.event').mousemove(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left - 210;		var y = e.pageY - pos.top + 10;
		$(this).find('span').css('left', x + 'px');
		$(this).find('span').css('top', y + 'px');
	}
	)

	$('div#events span.event').mouseout(function(){
		$(this).find('span').css('display', 'none');
	}
	)
	$('div.column').each(function(){
		$(this).append('<div class="end-column">&nbsp;</div>');
	})
	$('div.nav_child').each(function(){
		$(this).append('<div class="end-column">&nbsp;</div>');
	})

	$('table.calendar tr td.event_day').mouseover(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left + 10;
		var y = pos.top - e.pageY + 25;
		$(this).find('span.show_event').css('left', x + 'px');
		$(this).find('span.show_event').css('bottom', y + 'px');
		$(this).find('span.show_event').css('display', 'block');
	})
	$('table.calendar tr td.event_day').mousemove(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left + 10;
		var y = pos.top - e.pageY + 25;
		$(this).find('span.show_event').css('left', x + 'px');
		$(this).find('span.show_event').css('bottom', y + 'px');
		$(this).find('span.show_event').css('display', 'block');
	})
	$('table.calendar tr td.event_day').mouseout(function(){
		$(this).find('span.show_event').css('display', 'none');
	})
})


$(document).ready(function(){
	$('form.login_form input@[name=login]').focus(function(){
		var val1 = $(this).attr('title');
		var val2 = $(this).attr('value');
		if( val1==val2)
		{
			$(this).attr('value', '');
		}
	})

	$('form.login_form input@[name=login]').blur(function(){
		var val1 = $(this).attr('title');
		var val2 = $(this).attr('value');
		if( val2=='')
		{
			$(this).attr('value', val1);
		}
	})

	$('form.login_form input@[name=password_show]').focus(function(){
		$(this).addClass('hidden');
		$('form.login_form input@[name=password]').removeClass('hidden');
		$('form.login_form input@[name=password]').focus();
	})

	$('form.login_form input@[name=password]').blur(function(){
		if($(this).attr('value') == ''
			&& $('form.login_form input@[name=password_show]').attr('name') != undefined)
		{
			$(this).addClass('hidden');
			$('form.login_form input@[name=password_show]').removeClass('hidden');
			$(this).blur();
		}
	})

	$(window).load(function(){
		$("div.[category|galery|photo] a.image img").each(function (){
			var x = Math.round(($(this).parent().height() - $(this).height()) / 2);
			if(x > 0)
			{
				$(this).css('top', x).css('position', 'relative');
			}
		});
	});

	// nastaveni zobrazeni page_cmds v administraci navigace
	set_show_page_cmds();
	$('td.page_priv input@[name=page_priv]').change(function(){
		set_show_page_cmds();
	});

	$('table#admin_metody select').each(function(){
		var select_name = $(this).attr('id');
		var param_name = $(this).next().attr('id');
		start_ajax('select', select_name, param_name, 'get_module_param', param_name + '_hidden');
	});
})

// nastaveni zobrazeni page_cmds v administraci navigace
function set_show_page_cmds()
{
	$('td.page_priv input@[name=page_priv]:checked').each(function(){
		if($(this).val() == 0)
		{
			$('tr.page_cmds').hide();
		}
		else
		{
			$('tr.page_cmds').show();
		}
	});
}

function select_all(name)
{
	$('form input.'+name).each(function()
	{
		$(this).attr('checked', true);
	});
}

function invert_all(name)
{
	$('form input.'+name).each(function()
	{
		if($(this).attr('checked') == true)
		{
			$(this).attr('checked', false);
		}
		else
		{
			$(this).attr('checked', true);
		}
	});
}

// *************************** ESHOP START ***************************

$(document).ready(function(){
	$('#products_main_right select').change(function() {
		var dop_price = parseFloat($('#dop_cena').attr('title'));
		var price = parseFloat($('#cena').attr('title'));
		var vat = parseFloat($('#cena_s_dph').attr('title'));
		var extra = 0;
		$("#products_main_right select option:selected").each(function () {
			extra += parseFloat($(this).attr('title'));
		});
		price = price + extra;
		discount = dop_price - price;
		price2 = Math.round(price * (vat + 100) / 100);
		price = formatNumber(price) + ' Kč';
		price2 = formatNumber(price2) + ' Kč';
		discount = formatNumber(discount) + ' Kč';
		$('#cena').text(price);
		$('#cena_s_dph').text(price2);
		$('#sleva').text(discount);
	});

	$("a.order_addr").click(function(e){
		var obj = $(this).parent().next();
		$('div#dodaci_udaje>div').each(function(){
			if($(this).text() != obj.text())
			{
				$(this).hide();
			}
		})
		obj.toggle();
		e.preventDefault();
	})

	$("input#delivery_addr").change(function(){
		delivery_addr();
	})
	$("input#delivery_addr").each(function(){
		delivery_addr();
	})
	$("input#delivery_addr2").change(function(){
		delivery_addr2();
	})
	$("input#delivery_addr2").each(function(){
		delivery_addr2();
	})
})

function delivery_addr()
{
	var e = $("input#delivery_addr");
	if(e.attr('checked'))
	{
		e.parent('p').next('div').removeClass('hidden')
	}
	else
	{
		e.parent('p').next('div').addClass('hidden')
	}
}

function delivery_addr2(e)
{
	var e = $("input#delivery_addr2");
	if(e.attr('checked'))
	{
		e.parent('p').next('div').removeClass('hidden')
	}
	else
	{
		e.parent('p').next('div').addClass('hidden')
	}
}

function formatNumber(number) {
	var formattedNumberString = strpad((number%1000).toString(), 3, '000');
	var x = parseInt(number/1000);
	while(x > 0) {
	  if(x > 999)
	  {
			formattedNumberString = strpad((x%1000).toString(), 3, '000') + ' ' + formattedNumberString;
		}
		else
		{
			formattedNumberString = x%1000 + ' ' + formattedNumberString;
		}
		x = parseInt(x/1000);
	}
	return formattedNumberString;
}

function strpad(inputString, chars, padSting) {
	result = padSting+inputString;
	remFromLeft=result.length-chars;
	return result.substr(remFromLeft);
}

$(document).ready(function(){
	check_shipment_payment();
	$('div#payment input').change(function(){
		uncheck_other_inputs('div#payment input', $(this).val());
		check_shipment_payment();
	});
	$('div#payment input').click(function(){
		uncheck_other_inputs('div#payment input', $(this).val());
		check_shipment_payment();
	});
	$('div#shipment input').change(function(){
		uncheck_other_inputs('div#shipment input', $(this).val());
		check_shipment_payment();
	});
	$('div#shipment input').click(function(){
		uncheck_other_inputs('div#shipment input', $(this).val());
		check_shipment_payment();
	});
});

function uncheck_other_inputs(path, val)
{
	$(path).each(function(){
		if($(this).val() != val)
		{
			$(this).attr('checked', false);
		}
	});
}

function check_shipment_payment()
{
	var show_pay = new Array();
	var show_ship = new Array();
	var pay = $('div#payment input:checked').attr('value');
	var ship = $('div#shipment input:checked').attr('value');
	var n = 0;
	if(typeof(payment) == 'undefined')
	{
		return false;
	}
	for(var i in payment)
	{
		if(pay != undefined
			&& pay == i)
		{
			show_ship = payment[i];
		}
		if(in_array(ship, payment[i]))
		{
			show_pay[n++] = i;
		}
	}
	$('div#shipment input').each(function(){
		if(pay == undefined
			|| in_array($(this).val(), show_ship))
		{
			$(this).removeAttr('disabled');
		}
		else
		{
			$(this).attr('disabled', true);
		}
	});
	$('div#payment input').each(function(){
		if(ship == undefined
			|| in_array($(this).val(), show_pay))
		{
			$(this).removeAttr('disabled');
		}
		else
		{
			$(this).attr('disabled', true);
		}
	});
	//alert('shipment:'+show_ship+' / payment:'+show_pay);
	var add_price = '<p>Zvolte způsob dopravy a platby.</p><p>';
	var length = add_price.length;
	var add_desc = '';
	if(ship != undefined)
	{
		add_price += '<p>Doplatek za dopravu: ' + shipment_price[ship] + ' ';
		add_desc += '<h3>Popis dopravy</h3>' + shipment_desc[ship];
	}
	if(pay != undefined)
	{
		if(add_price.length == length)
		{
			add_price += '<p>';
		}
		add_price += 'Doplatek za způsob platby: ' + payment_price[pay] + ' ';
		add_desc += '<h3>Popis platby</h3>' + payment_desc[pay];
	}
	if(add_price.length > length)
	{
		add_price += '(bez DPH)</p>';
	}
	else
	{
		add_price = add_price.substr(0, length-3);
	}
	$('div#add_price').html(add_price);
	$('div#add_desc').html(add_desc);
}

function in_array(needle, haystack)
{
	for(key in haystack)
	{
		if(haystack[key] == needle)
		{
			return true;
		}
	}
	return false;
}

function check_order_step1()
{
	if($('div#payment input:checked').attr('value') == undefined
		|| $('div#shipment input:checked').attr('value') == undefined)
	{
		alert('Musíte nejdříve vybrat způsob dopravy a platby.');
		return false;
	}
	return true;
}

function encode(val)
{
	val = encodeURIComponent(val);
	return val;
}


// *************************** ESHOP END ***************************

$(document).ready(function(){
	$('span.show_help').mouseover(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left + 10;
		var y = - e.pageY + pos.top + 30;
		$(this).find('span').css('left', x + 'px');
		$(this).find('span').css('bottom', y + 'px');
		$(this).find('span').css('display', 'block');
	})
	$('span.show_help').mousemove(function(e){
		var pos = $(this).offset();
		var x = e.pageX - pos.left + 10;
		var y = - e.pageY + pos.top + 30;
		$(this).find('span').css('left', x + 'px');
		$(this).find('span').css('bottom', y + 'px');
		$(this).find('span').css('display', 'block');
	})
	$('span.show_help').mouseout(function(){
		$(this).find('span').css('display', 'none');
	})
})
