function LogConsole(text, args)
{
	if (window.console)
	{
		if (args == null)
			console.log(text);
		else
			console.log(text + ': ' + args);
	}
}
var oXml;
var soupStart = 1;
$(document).ready(function()
{
	$.ajax(
	{
		url: jsXmlUrl,
		type: 'GET',
		async: false,
		dataType: "xml",
		success: function(data)
		{
			oXml = data;
		}
	});
	
	CheckForm();
	Galerie();
	DetImg();
	
	$('#soupO').jcarousel(
	{
		start: soupStart,
		scroll: 3
	});
	$('.clueNote').cluetip(
	{
		cluetipClass: 'iHelp',
		attribute: 'data-upx-clue',
		titleAttribute: '',
		showTitle: false,
		local: true,
		positionBy: 'mouse',
		clickThrough: true,
		width: '500',
		cursor: ''
	});
	$('a[rel=fanIn]').fancybox({
		titleShow: false,
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: 0,
		autoScale: false
	});

	$("#dSwitch").click(function (ev)
	{
		ev.preventDefault();
		$("#dSwitchC").toggle();
	});
	if ($("#dSwitch").attr('data-upx-noc') != '1')
		$('#dSwitch').trigger('click');

	$("#dSwitch1").click(function (ev)
	{
		ev.preventDefault();
		$("#dSwitchC1").toggle();
	});
	if ($("#dSwitch1").attr('data-upx-noc') != '1')
		$('#dSwitch1').trigger('click');

	if ($.browser.msie)
		if (parseInt($.browser.version, 10) < 9)
			$(".imgT:nth-child(3n+3)").addClass("imgTIE");

	$('#lsDum').bind('change', function ()
	{
		var url = $(this).val();
		if (url)
			window.location = url;
		return false;
	});
});

function XmlFind(co)
{
	return $(oXml).find('texty ' + co.replace('/', ' ')).text();
}

function CheckForm()
{
	$.validator.addMethod('czDecimal', function(value, element)
	{
		if (value == '')
			return true;
		else
		{
			value = value.replace(',', '.');
			return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
		}
	}, 'X');

	$.validator.addMethod('integer', function(value, element)
	{
		return this.optional(element) || /^-?\d+$/.test(value);
	}, 'X');

	CheckNap();
	CheckDsObjKat();
	CheckHle();
	CheckHleR();
}

function CheckNap()
{
	$("#chNap").validate(
	{
		rules:
		{
			email: { required: true, email: true },
			predmet: { required: true },
			zprava: { required: true }
		},
		messages:
		{
			email: { required: XmlFind('frmNapiste/errEmail'), email: XmlFind('frmNapiste/errEmail') },
			predmet: { required: XmlFind('frmNapiste/errPredmet') },
			zprava: { required: XmlFind('frmNapiste/errZprava') }
		}
	});
}

function CheckDsObjKat()
{
	$("#chDsObjKat").validate(
	{
		rules:
		{
			jmeno: { required: true},
			ulice: { required: true },
			mesto: { required: true },
			psc: { required: true },
			kusu: { required: true, min: 1, digits: true }
		},
		messages:
		{
			jmeno: { required: XmlFind('frmObj/errJmeno')},
			ulice: { required: XmlFind('frmObj/errUlice') },
			mesto: { required: XmlFind('frmObj/errMesto') },
			psc: { required: XmlFind('frmObj/errPsc') },
			kusu: { required: XmlFind('frmObj/errKusu'), min: XmlFind('frmObj/errKusu'), digits: XmlFind('frmObj/errKusu') }
		}
	});
}

function CheckHle()
{
	$("#chHle").validate(
	{
		rules:
		{
			cenaOd: { integer: true },
			cenaDo: { integer: true }
		},
		messages:
		{
			cenaOd: { integer: XmlFind('frmHle/errCislo') },
			cenaDo: { integer: XmlFind('frmHle/errCislo') }
		}
	});
}

function CheckHleR()
{
	$("#chHleR").validate(
	{
		rules:
		{
			cenaOd: { integer: true },
			cenaDo: { integer: true },
			hrebenOd: { czDecimal: true },
			hrebenDo: { czDecimal: true },
			sklonOd: { czDecimal: true },
			sklonDo: { czDecimal: true },
			cenaPOd: { integer: true },
			cenaPDo: { integer: true },
			plochaZastavOd: { czDecimal: true },
			plochaZastavDo: { czDecimal: true },
			plochaUzitnaOd: { czDecimal: true },
			plochaUzitnaDo: { czDecimal: true },
			plochaObytnaOd: { czDecimal: true },
			plochaObytnaDo: { czDecimal: true }
		},
		messages:
		{
			cenaOd: { integer: XmlFind('frmHle/errCislo') },
			cenaDo: { integer: XmlFind('frmHle/errCislo') },
			hrebenOd: { czDecimal: XmlFind('frmHle/errDecimal') },
			hrebenDo: { czDecimal: XmlFind('frmHle/errDecimal') },
			sklonOd: { czDecimal: XmlFind('frmHle/errDecimal') },
			sklonDo: { czDecimal: XmlFind('frmHle/errDecimal') },
			cenaPOd: { integer: XmlFind('frmHle/errCislo') },
			cenaPDo: { integer: XmlFind('frmHle/errCislo') },
			plochaZastavOd: { czDecimal: XmlFind('frmHle/errDecimal') },
			plochaZastavDo: { czDecimal: XmlFind('frmHle/errDecimal') },
			plochaUzitnaOd: { czDecimal: XmlFind('frmHle/errDecimal') },
			plochaUzitnaDo: { czDecimal: XmlFind('frmHle/errDecimal') },
			plochaObytnaOd: { czDecimal: XmlFind('frmHle/errDecimal') },
			plochaObytnaDo: { czDecimal: XmlFind('frmHle/errDecimal') }
		}
	});
}

function Galerie()
{
	$('a[rel=gOi]').fancybox({
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: 0,
		autoScale: false,
		titlePosition: 'over',
		titleFormat: function(title, currentArray, currentIndex, currentOpts)
			{
				var txt = '';
				var pop = $(currentArray[currentIndex]).data("upx-popis");
				if (pop == undefined)
					pop = '';
				if (title != '')
				{
					txt = title;
					if (pop != '')
						txt += '<br />' + pop;
				}
				else
				{
					if (pop != '')
						txt = pop;
				}
				if (txt != '')
					txt = '<span id="fancybox-title-over">' + txt + '</span>';
				return txt;
			}
	});
}

function DetImg()
{
	$("#oN a").click(function()
	{
		var puvI = '#oH img';
		var puvV = $(puvI).attr('src');
		var puvN = $(puvI).data("upx-nah");
		var novV = $(this).attr("href");
		var novN = $(this).find('img').attr("src");
		$(puvI).fadeOut(200, function()
		{
			$(puvI).attr('src', novV);
			$(puvI).data('upx-nah', novN);
			$(puvI).fadeIn(400);
		});
		$(this).fadeOut(200, function()
		{
			$(this).attr('href', puvV);
			$(this).find('img').attr('src', puvN);
			$(this).fadeIn(400);
		});
		return false;
	});
}

