var animTime1 = 400;
var animTime2 = 400;
var ssAnimTime = 1000;
var ssTimeout = 8000;
var ssStopped = false;
var scrollSettings = {
		duration: 1000, 
		offset: {left: 0, top: -10}
	}
	
var lightBoxSettings = {
		imageLoading: 'templates/images/lightbox-ico-loading.gif',
		imageBtnClose: 'templates/images/lightbox-btn-close.gif',
		imageBtnPrev: 'templates/images/lightbox-btn-prev.gif',
		imageBtnNext: 'templates/images/lightbox-btn-next.gif',
		imageBlank : 'templates/images/lightbox-blank.gif',
		txtImage: 'Zdjęcie',
		txtOf: 'z'
	}	

var currentHouse = null;
	
function openHouse()
{
	if ($(this).attr('class') == currentHouse)
	{
		$('div.buttons div.description').hide();
		$('div.buttons').css({height: 234});
		$(this).siblings('a').animate({opacity: 1});
		currentHouse = null;
	}
	else
	{
		$('div.buttons').css({height: 450})
		$(this).siblings('a').animate({opacity: 0.5});
		$(this).animate({opacity: 1})
		$('div.buttons div.description').fadeOut();
		$(this).next().fadeIn();
		currentHouse = $(this).attr('class');
	}
	return false;
}

$(document).ready(function(){

	$('table').find('tr').find('td:last, th:last').addClass('no_border_right');
	$('table').find('tr:last').find('td').addClass('no_border_bottom');

	$('a.new_window').click(function(){
		var newWindow = window.open($(this).attr('href'), '_blank');
		newWindow.focus();
		return false;
	})

	$('div.calendar a.prev').click(function(){
		var cur = $('div.calendar div.current')
		$('div.calendar div.controls a').show();
		if ($(cur).prev('div.month').length > 0)
			$(cur).hide().toggleClass('current').prev('div.month').fadeIn('slow').toggleClass('current');
		if ($(cur).prevAll('div.month').length <= 1)
			$(this).hide();
			
		return false;
	});
	
	$('div.calendar a.next').click(function(){
		var cur = $('div.calendar div.current')
		$('div.calendar div.controls a').show();
		if ($(cur).next('div.month').length > 0)
			$(cur).hide().toggleClass('current').next('div.month').fadeIn('slow').toggleClass('current');
		if ($(cur).nextAll('div.month').length <= 1)
			$(this).hide();
		return false;
	});
	
	
		
	$("form").submit(function(){
		return checkForm($(this));
	});
	
	$('a.reserveSubmit').click(checkReservation);
	
	$('div.description').hide();
	
	$('div.buttons > a').click(openHouse)
	
	$('div.buttons p.arrow a').click(function(){
		$(this).parents('div.description').hide();
		$('div.buttons > a').fadeIn();
		return false;
	})

	$(document.body).height($(document).height());
	
	$('a[@rel=lightbox]').lightBox(lightBoxSettings);
	
	$('a[@rel="lightbox[szalas]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[kr-80]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[kr-60]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[kociewie-i]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[kociewie-ii]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[wiezyca]"]').lightBox(lightBoxSettings);
	$('a[@rel="lightbox[pawilon]"]').lightBox(lightBoxSettings);

	$('span.border').css('opacity',0.30);
	
	if (!($.browser.mozilla && $.browser.version == '1.9'))
	{
		$('span.border').each(function(){
			var h = $(this).parent().height() - 20;
			$(this).height(h);
			$(this).css('top',10);
		});
	}


	$('#footer').prepend('<div class="bg"></div>');
	$('#footer div.bg').css('opacity',0.35);

	$('img[@src$=.png]').ifixpng();
	  
	$('div.image_right').ifixpng(); 
	$('div.image_left').ifixpng(); 
		
	$('div.menu_content li:gt(0)').prepend('<span class="button_bar1"></span>')
	$('div.menu_content li:gt(0)').prepend('<span class="button_bar2"></span>')
	$('div.menu_content li span.button_bar1').css('opacity',0.5);
	$('div.menu_content li').prepend('<span class="button_bg"></span>')
	$('div.menu_content li span.button_bg').css('opacity',0.5);
	
	if ($.cookie('animation') == 'off')
	{
		animationOff();
		$('a.animationSwitch').toggle(animationOn,animationOff)
	}
	else
	{
		animationOn();
		$('a.animationSwitch').toggle(animationOff,animationOn)
	}
	
	$('a.slideshowSwitch').toggle(function(){
		ssStopped = true;
		$('div.slideshow div.cycle').cycle('pause');
		$(this).text('odtwarzaj')
	},function(){
		ssStopped = false;
		$('div.slideshow div.cycle').cycle('resume');
		$(this).text('stop')
	})
	
	$('div.menu_content').hover(function(){
		$('div.slideshow div.cycle').cycle('pause');
	},function(){
		if (!ssStopped)
			$('div.slideshow div.cycle').cycle('resume');
	})
});

function animationOn()
{
	$('div.slideshow div.cycle').cycle('stop');
	$('div.slideshow div.cycle').cycle({ 
	    fx:    'fade',
	    cssFirst: {opacity: 1, display: 'block'},
	    speed:  ssAnimTime,
	    timeout: ssTimeout,
	    startingSlide: Math.floor(Math.random()*$('div.slideshow div.cycle img').length)
	});
	$('div.menu_content a').hoverIntent(function(){
		if ($(this).hasClass('current'))
			return;
		$(this).animate({
			paddingTop: 62,
			height: 238,
			color: '#000'
		},animTime1)
		$(this).siblings('span.button_bg').animate({
			height: 300,
			backgroundColor: '#fff'
		},animTime1)
	},function(){
		if ($(this).hasClass('current'))
			return;	
		$(this).animate({
			paddingTop: 33,
			height: 47,
			color: '#fff'
		},animTime2)
		$(this).siblings('span.button_bg').animate({
			height: 80,
			backgroundColor: '#000'
		},animTime2)
	})
	$('a.animationSwitch').text('wyłącz animacje')
	$.cookie('animation','on')
}

function animationOff()
{
	$('div.slideshow div.cycle').cycle('stop');
	$('div.slideshow div.cycle').cycle({ 
	    fx:    'custom', 
	    cssFirst: {opacity: 1, display: 'block'},
	    cssBefore: {opacity: 1, display: 'block', position: 'absolute'},
	    animIn: {opacity: 1},
	    animOut: {opacity: 1},
	    cssAfter: {opacity: 0},
	    speed:  1,
	    timeout: ssTimeout + ssAnimTime,
	    startingSlide: Math.floor(Math.random()*$('div.slideshow div.cycle img').length)
	});
	$('div.menu_content a').hoverIntent(function(){
		if ($(this).hasClass('current'))
			return;
		$(this).css({
			paddingTop: 62,
			height: 238,
			color: '#000'
		},animTime1)
		$(this).siblings('span.button_bg').css({
			height: 300,
			backgroundColor: '#fff'
		},animTime1)
	},function(){
		if ($(this).hasClass('current'))
			return;
		$(this).css({
			paddingTop: 33,
			height: 47,
			color: '#fff'
		},animTime2)
		$(this).siblings('span.button_bg').css({
			height: 80,
			backgroundColor: '#000'
		},animTime2)
	})
	$('a.animationSwitch').text('włącz animacje')
	$.cookie('animation','off')
}

function currentPage(p)
{
	$(document).ready(function(){
		$('div.menu_content a[href*='+p+']').css({
			paddingTop: 62,
			height: 238,
			color: '#000'
		}).addClass('current').siblings('span.button_bg').css({
			height: 300,
			backgroundColor: '#fff'
		});
	});
}

function checkForm(f)
{
	var err = false;
	$(f).find('input').each(function(){
		if ($(this).attr('alt'))
		{
			if ($(this).attr('alt').match('req') && !checkFormEmpty($(this)))
		 		err = true;
		}
	});
	if (err)
	{
		$(f).find('span[rel=form]').text('Błędy w formularzu.').show().animate({opacity: 1.0}, 2000).fadeOut(1000);
	}
	return !err;
}

function checkFormEmpty(obj)
{
	var err = false;
	var name = $(obj).attr('name');
	var type = $(obj).attr('type');
	var value = $(obj).val();
	
	if (type == 'text' && value == '')
		err = true;

	if (type == 'radio')
	{
		if ($('input[type=radio][name="'+name+'"]:checked').length == 0)	
			err = true;
	}
	
	$('span[rel="'+$(obj).attr('name')+'"]').text('');
	$(obj).css('borderColor','');
	if (err)
	{
		$('span[rel="'+$(obj).attr('name')+'"]').text('To pole jest wymagane.');
		if (type != 'radio')
			$(obj).css('borderColor','#f00');
		return false;
	}
	return true;
}

function checkReservation()
{
	if (!checkForm($(this).parents('form')))
		return false

	var house_id = parseInt($('[name=domek]').attr('value'));
	var count = parseInt($('[name=osoby]').attr('value'));
	var from = $('[name=fromYear]').attr('value')+'-'+$('[name=fromMonth]').attr('value')+'-'+$('[name=fromDay]').attr('value');
	var to = $('[name=toYear]').attr('value')+'-'+$('[name=toMonth]').attr('value')+'-'+$('[name=toDay]').attr('value');
	//$.get('lib/reservation/Reservation.php', {house_id: house_id, count: count, from: from, to: to}, parseReservation);
	//$('#rezerwacja p.status').text('Sprawdzanie stanu domków, proszę czekać...').show();
	$('#rezerwacja').submit();
		
	return false;
}

function parseReservation(data)
{
	eval("result = ("+data+")");
	if (result['status'] == true)
		$('#rezerwacja').submit();
	else
	{
		$('#rezerwacja p.status').text('W podanym terminie nie ma odpowiedniej liczby wolnych domków.').show();
	}
}

