$.fn.equalheight = function() {
	$(this).each(function() {
		var currentTallest = 0;

		$(this).children().each(function(i) {
			if ($(this).height() > currentTallest) currentTallest = $(this).height();
		});

		$(this).children().css({'height': currentTallest + 'px'});
	});

	return this;
};

$(function() {
	$('.wm').each(function() {
		$(this).watermark($(this).attr('title'), {className: 'dim'});
	});

	$('a[href="#"]').click(function() {
		return false;
	});

	$('.equal:visible').equalheight();

	$('.par-siz').each(function() {
		$(this).width($(this).parent().width());
		$(this).height($(this).parent().height());
	});

	$('.box-lnk').each(function() {
		var href = $('a:last', this).attr('href');
		$('img:first', this).click(function() {
			if (href != '#') location.href = href;
		}).css('cursor', 'pointer');
	});

	$('.ctabs li a').not(".hzozn").click(function() {
		var rel = $(this).attr('rel');
		var parent = $(this).parents('.ctabs');
		var tabs = $('ul:first', parent);

		$('.ctab, .cbox', parent).addClass('hide');
		$('#tab-' + rel, parent).removeClass('hide');
		$('li', tabs).removeClass('sel');
		$(this).parents('li:first').addClass('sel');
	});

	$("a.hzozn").click(function(event) {
		var next = $(this).next();
		if (next.is("ul")) {
			event.preventDefault();
			next.css("display", next.css("display") == "none" ? "" : "none");
		}
	});

	if (window.location.hash != "") {
		$(".ctabs li a[href=" + window.location.hash + "]").click();
	}

	$("#shop_r1").click(function() {
		$("#checker").hide();
	});

	$("#shop_r2").click(function() {
		$("#checker").show();
		$('.equal:visible').equalheight();
	});

	$('a').click(function() { $(this).blur(); });

	$('form[action^="/formulare/"]').each(function() {
		$(this).submit(function() {
			var action = $(this).attr("action");
			var match = /formular=(\d+)/.exec(action)

			$.ajax({
				url: "/formulare/kontrola",
				type: "POST",
				dataType: "json",
				data: match[0] + "&" + $(this).serialize(),
				context: $(this),
				success: function(data) {
					var prev = $(this).prev();
					var prev_was = prev.attr("class") == "error-box";

					if (data.length > 0) {
						var html = "";
						for (var i=0; i<data.length; i++)
							html += data[i] + "<br />";

						if (prev_was)
							prev.html(html);
						else
							$(this).before("<div id=\"form" + match[1] + "\" class=\"error-box\" style=\"margin-bottom: 15px; width: 720px\">" + html + "</div>");

						$("html, body").animate({scrollTop: $("#form" + match[1]).offset().top}, 0);
					} else {
						$(this).unbind("submit");
						$(this).submit();
					}
				}
			});

			return false;
		});
	});
});

///////////stare

function open_window(url,w,h) 
{
  var twidth = w;
  var theight = h;

  if(twidth > screen.width - 40) twidth = screen.width - 40;
  if(theight > screen.height - 40) theight = screen.height - 40;

  var pos_x = (screen.width - twidth - 10)/2;
  var pos_y = (screen.height - theight - 29)/2;

  if(typeof(c1window) == "undefined" || c1window.closed)  
    c1window = window.open(url,'c1window','menubar=0,toolbar=0,location=0,scrollbars=1,screenX='+pos_x+',screenY='+pos_y+',left='+pos_x+',top='+pos_y+',width='+twidth+',height='+theight+',resizable=1');  
  else c1window.location.href = url;

	c1window.focus();
}

function open_window_ns(url,w,h) 
{
  var twidth = w;
  var theight = h;

  if(twidth > screen.width - 40) twidth = screen.width - 40;
  if(theight > screen.height - 40) theight = screen.height - 40;

  var pos_x = (screen.width - twidth - 10)/2;
  var pos_y = (screen.height - theight - 29)/2;

  if(typeof(c1window) == "undefined" || c1window.closed)  
    c1window = window.open(url,'c1window','menubar=0,toolbar=0,location=0,scrollbars=0,screenX='+pos_x+',screenY='+pos_y+',left='+pos_x+',top='+pos_y+',width='+twidth+',height='+theight+',resizable=1');  
  else c1window.location.href = url;

	c1window.focus();
}

function open_window_ek(url,w,h) 
{
  var twidth = w;
  var theight = h;

  if(twidth > screen.width - 40) twidth = screen.width - 40;
  if(theight > screen.height - 40) theight = screen.height - 40;

  var pos_x = (screen.width - twidth - 10)/2;
  var pos_y = (screen.height - theight - 29)/2;

  if(typeof(c1window) == "undefined" || c1window.closed)  
    c1window = window.open(url,'c1window','menubar=0,toolbar=0,location=0,scrollbars=0,screenX='+pos_x+',screenY='+pos_y+',left='+pos_x+',top='+pos_y+',width='+twidth+',height='+theight+',resizable=0');  
  else c1window.location.href = url;

	c1window.focus();
}

function UkazSkrySMS ()
{
	var obj = document.getElementById ( "item24" );

	if ( obj == null ) return;

	var sms1 = document.getElementById ( "item42" );
	var sms2 = document.getElementById ( "label42" );

	if ( sms1 == null || sms2 == null ) return;

	if ( obj.checked )
	{
		var em = document.getElementById ( "item41" );
		em.click ();
	}

	sms1.style.display = obj.checked ? 'none' : 'inline';
	sms2.style.display = obj.checked ? 'none' : 'inline';

}

max_chars = 900;

function cl ( elem )
{
	var name = elem.name;
	var max = max_chars;

	var obj = document.getElementById ( name );
	var strlen = obj.value.length;

	if ( strlen > max )
	{
		obj.value = obj.value.substring ( 0, max );
		chars_left = 0;
	}else chars_left = max - strlen;

	var obj2 = document.getElementById ( name + '_celkom' );

	obj2.innerHTML = chars_left;
}

function roundNumber(f, n)
{
	var nn = Math.pow(10, n);

	return Math.round(f * nn) / nn;
}
function formatNumber(nStr)
{
	nStr += '';
	x = nStr.split ( '.' );
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;

	while ( rgx.test ( x1 ) )
	{
		x1 = x1.replace ( rgx, '$1' + ' ' + '$2' );
	}

	nStr = x1 + x2;
	nStr = nStr.replace ( ".", "," );

	return nStr;
}
function formatPrice(price, euro_sign)
{
	var formatted = formatNumber(price);

	if (formatted.indexOf ( "," ) == -1)
		formatted += ",-";

	if (euro_sign)
		formatted += " &euro;";

	return formatted;
}

function UkazPanel(elem)
{
	var ul = elem.parentNode.parentNode;
	var ul_li = ul.getElementsByTagName("li");
	var div = ul.parentNode.nextSibling;

	for (var i=0; i<ul_li.length; i++) {
		if (ul_li[i].firstChild == elem) {
			ul_li[i].className = "sel";
			div.childNodes[i].style.display = "";
		} else {
			ul_li[i].className = "";
			div.childNodes[i].style.display = "none";
		}
	}

	return false;
}

function Group1(what)
{
	document.getElementById("group1_text").style.display = what ? "" : "none";
}

$(document).ready(function() {
	$("a.hzozn").each(function() {
		if ($(this).next().is("ul"))
			$(this).next().css("display", "none");
	});

	$("a.colorbox").colorbox({transition: "none", minWidth: "300px", minHeight: "100px", current: "{current} / {total}", opacity: 0.6});
	$("a.colorboxf").colorbox({innerWidth:"560px", innerHeight:"440px", iframe: true, opacity: 0.6});
});
