$(document).ready(function(){
	//console.log(window.location.pathname);

	$('#menu ul li ul').hide();
	$('#menu li a').click(function() {
		//changeHeaderImage($(this).attr('href'));
		if(!$(this).next()[0]) {
			return true;
		}
		$(this).parent().siblings().each(function(i) {
			$(this).children('ul:first').hide().prev().removeClass('clicked');
			$(this).children('a.clicked').removeClass('clicked');
		});
		$('#menu ul.current').removeClass('current');
		if($(this).hasClass('clicked'))	{
			$(this).removeClass('clicked').next().hide().removeClass('current');
		} else {
			$(this).addClass('clicked').next().show().addClass('current');
		}
		return false;
	});

	$("div.photos ul a").addClass("thickbox");
	$("div.photos ul a").attr("rel", "gall");	
	$("div.gallery a").addClass("thickbox");
	$("div.gallery a").attr("rel", "gall");
	$("div.gallery").append('<div class="cleaner"></div>');
	
	$('a','#main div.gallery ul li:nth-child(5n)').css('margin-right', '0');
	
	//$("#main a:has(img)").addClass('thickbox').attr("rel", "gall");
	$("#main ul:has(a:has(img))").each(function (i){
		$("a:has(img)", $(this)).addClass('thickbox').attr("rel", "gall"+i);
	});

	TB_init();

	showCurrentSubmenu();
	
	// external links in new window
	$("#main a[href*='http://']:not([href*='hotelpinot.cz'])").click(function(){
		if(!$(this).hasClass('newbrowser'))	{
			window.open(this.href);
			return false;
		}
	});

	// links with class newbrowser in new window
	$("#main a.newbrowser").click(function(){
		window.open(this.href);
		return false;
	});
	
	changeHeaderImage(window.location.pathname);
	
	$('form.mailform').ajaxForm({ 
		dataType	: 'json', 
		success		: mailformProcessJson,
		data		: {json : 1}  
    });

	//$('form.mailform table tr td:first').css('background', 'red');
	$('form.mailform table tr').each(function(i){
		$('td:first', $(this)).addClass('label');
	});	
	$('form.mailform table tr td.label').hjustify();
	$('form.mailform table tr td.label').css('width', $('form.mailform table tr td.label:first').width()+10+'px');

	//$.datepicker.setDefaults($.extend($.datepicker.regional['fr']));
	//$.datepicker.setDefaults($.datepicker.regional['en']); 
	//$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['fr']));

	$('form input.datepicker').datepicker({
		dateFormat		: 'dd.mm.yy',
		showAnim		: 'slideDown',
		showOn			: 'button',
		buttonImage		: 'images/design//calendar.gif',
		buttonImageOnly	: true
	});
	
	$('#main table tr td:first-child').addClass('firstCol');
	$('#main table tr th:first-child').addClass('firstCol');
});

function showCurrentSubmenu()
{
	// find current menu item and set it's class
	$('#menu a').each(function(i) {
		//console.log($(this).attr('href'));
		if($(this).attr('href') == window.location.pathname)	{
			// make sure there is max just one a.clicked
			$('#menu a.clicked').removeClass('clicked');
			$(this).addClass('clicked');
		}
	});
	// open submenus and set parent's class clicked
	$('#menu a.clicked').parents().each(function (i){
		// only on UL and not the menu root
		if((this.tagName == 'UL') && ($(this).parent().attr('id') != 'menuIn')) {
			//console.log($(this).parent().attr('id'));
			$(this).show().prev().addClass('clicked');
			if($('#menu ul.current').length == 0)	{
				// only 1 ul can be current
				$(this).addClass('current');				
			}
		}
		if(this.tagName == 'DIV') {
			return false;
		}
		//console.log(this.tagName);
	});
}

function changeHeaderImage(url)
{
	if(typeof(headerImages) == 'undefined' || url == '/')	{
		return null;
	}

	var imageUrl = '';
	for (var i in headerImages) { 
		if(i == url)	{
			//console.log(i+':'+headerImages[i]);
			//console.log(url);
			imageUrl = headerImages[i];
		}
	}
	
	if(imageUrl == '')	{
		// not exactly set, check parents
		$("#menu a[href*='"+url+"']").parents().each(function (i){
			// only on UL and not the menu root
			if((this.tagName == 'UL') && ($(this).parent().attr('id') != 'menuIn')) {
				//console.log($(this).prev().attr('href'));
				var hrefUrl = $(this).prev().attr('href');
				for(var i in headerImages) {
					if(imageUrl == '' && i == hrefUrl)	{
						imageUrl = headerImages[i];
					}
				}
			}
		});
	}
	
	if(imageUrl == '')	{
		imageUrl = '/images/design/headerBanner.jpg';
	}
	
	$('#header').css('background-image', 'url("'+imageUrl+'")');
}

function mailformProcessJson(data) { 
	$('form.mailform div.messageBox').remove();
	$('form.mailform').prepend('<div class="messageBox"></div>');
	var messageBox = $('form.mailform div.messageBox');

    if(data.errorMessage && data.errorMessage.length > 0)	{
    	messageBox.addClass('error');
    	mailformProcessMessages('error', messageBox, data.errorMessage);    	
    } else {
    	messageBox.addClass('ok');
		mailformProcessMessages('ok', messageBox, data.okMessage);
    }
    
    $.scrollTo(messageBox, 100);
    var bgrColor = messageBox.css('background-color');
    messageBox.animate({backgroundColor: '#ffff88'}, 50).animate({backgroundColor: bgrColor}, 3000);
}

function mailformProcessMessages(style, box, messages) {
	box.append('<ul></ul>');
	jQuery.each(messages, function() {
		$('ul', box).append('<li>' + this + '</li>');
	});

	if(style == 'ok')	{
		$('form.mailform').resetForm();
	}
}