var on_top_action = 0;
var on_top_next = 0;

var pitchCounter = 1;
setInterval(function(){
	pitchCounter++;
	if(pitchCounter > $("#pitch-images img").length)
		pitchCounter = 1;
		
	var counter = 0;
	$("#pitch-images img").hide().each(function(){
		counter++;
		if(counter == pitchCounter)
			$(this).fadeIn(4000);
	});
}, 10000);

function setup_anims()
{
	/*
	$('#pitch-images').jqFancyTransitions({ 
		width: 700, 
		height: 347,
		strips: 5,
		effect: 'ziper'
	});
	*/
	
	$(".submenu-left a").hover(function(){
		var id = $(this).attr('id');
		id = id.split("-");
		id = id[1];
		$(".submenu-container").hide();
		$("#submenu-page-info-"+id).show();
	}, function(){
		
	});
	
	/*
	$("#footer").hover(function(){
		$("#footer").animate({
			'height' : '160px'
		}, 500);
	}, function(){
		$("#footer").animate({
			'height' : '16px'
		}, 500);
	});
	*/
	
	$("#top").bind("mouseenter", function(){
		if(on_top_action != 0)
		{
			if(on_top_action == -1)
				on_top_next = 1;
				
			return;
		}
		on_top_action = 1;
			
		$(this).animate({
			'height': '66px',
			'margin-top': '-78px'
		}, 500, 'linear', function(){
			on_top_action = 0;
			if(on_top_next == -1)
			{
				on_top_next = 0;
				$("#top").mouseleave();
			}
		});
	});
	$("#top").bind("mouseleave", function(){
		if(on_top_action != 0)
		{
			if(on_top_action == 1)
				on_top_next = -1;
				
			return;
		}
		on_top_action = -1;

		$(this).animate({
			'height': '18px',
			'margin-top': '-30px'
		}, 500, 'linear', function(){
			on_top_action = 0;
			if(on_top_next == 1)
			{
				on_top_next = 0;
				$("#top").mouseenter();
			}
		});
	});
	$("#top").bind("mousedown", function(){
		$('html, body').animate({
			'scrollTop' : '0',
		}, 1000);
	});
	
	$(".awards-holder > img.top").hover(function(){
		if(!$(this).hasClass('animated')) 
			$(this).dequeue().stop().animate({ opacity: 1 }, 1000);
	}, function(){
		$(this).addClass('animated').animate({ opacity: 0 }, 500, "linear", function() {
			$(this).removeClass('animated').dequeue();
		});
	});
	
	$(".footer-links-holder > a").hover(function(){
		if(!$("img.top", this).hasClass('animated')) 
			$("img.top", this).dequeue().stop().animate({ opacity: 1 }, 1000);
	}, function(){
		$("img.top", this).addClass('animated').animate({ opacity: 0 }, 500, "linear", function() {
			$(this).removeClass('animated').dequeue();
		});
	});	
	
	$(".flag-holder").hover(function(){
		if(!$("img.top", this).hasClass('animated')) 
			$("img.top", this).dequeue().stop().animate({ opacity: 1 }, 1000);
	}, function(){
		$("img.top", this).addClass('animated').animate({ opacity: 0 }, 500, "linear", function() {
			$(this).removeClass('animated').dequeue();
		});
	});	
	
	$("#premenu li").hover(function(){
		$("div", this).slideDown();
	}, function(){
		$("div", this).slideUp();
	});
}

var flag_in_action = '';
var flag_status = '';

function fadeFlag(id)
{
	if(flag_in_action == id)
		return;
		
	if(flag_in_action != '')
	{
		$("#"+id).animate({
			'opacity':0
		}, 100);
	}
	
	flag_in_action = id;
	flag_status = 1;

	$("#"+id).stop().animate({
		'opacity':1
	}, 300);
}

function fadeOutFlag(id)
{
	if(flag_in_action == id)
	{
		flag_in_action = '';
		flag_status = 0;
	}
	
	$("#"+id).animate({
		opacity:0
	}, 100);
}

function specialMoveStart(id)
{
	$("#"+id).stop().animate({
		left: '-51px'
	}, 5000, 'linear');
}

function specialMoveEnd(id)
{
	$("#"+id).stop().animate({
		left: '0px'
	}, 500);
}

var gallery_action = false;
function gallery_move(id, cnt, dir, w, m)
{
	if(gallery_action)
		return;
		
	w = (w) ? w : 210;
	m = (m) ? m : 15;
	
	var curr = document.getElementById("gallery-"+id+"-holder").style.left;
	curr = parseFloat(curr);
	var max_p = parseInt($("#gallery-"+id+"-holder").css('width'));

	if(isNaN(curr))
		curr = 0;
	if(dir > 0)
	{
		if(curr >= 0)
			return;
	}
	else
	{
		if(curr + max_p - 3 * (w + m) <= 0)
			return;
	}

	gallery_action = true;
	var offset = w+m;

	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#gallery-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {gallery_action = false;}}
	);
}

function check_room(id)
{
	if($('#room-check-'+id).hasClass('notchecked'))
	{
		$('#room-check-'+id).removeClass('notchecked').addClass('checked');
		$('#room-'+id).attr('checked', true);
		$('#room-spaces-'+id).fadeIn(600);
	}
	else
	{
		$('#room-check-'+id).removeClass('checked').addClass('notchecked');
		$('#room-'+id).attr('checked', false);
		$('#room-spaces-'+id).fadeOut(600);
	}
}

function room_count(id, c)
{
	var valutes = Array();
	$(".valute").each(function(){
		var id = $(this).attr('id');
		id = id.split("-");
		id = id[1];
		valutes[valutes.length] = {'valuta' : id, 'val' : $(this).val()};
	});
	
	var val = $("#room-"+id+"-"+c).val();
	var vals = val.split("|");
	var p = parseFloat(vals[1]);
	$("#room-"+id+"-"+c+"-current-price").html(p.toFixed(2)+" kn");
	
	for(var i = 0; i < valutes.length; i++)
	{
		var v = valutes[i];
		var pr = p / v.val;
		$("#room-"+id+"-"+c+"-current-price-"+v.valuta).html(pr.toFixed(2)+" kn");
	}
	
	var total = 0;
	$('.room-persons').each(function(){
		val = $(this).val();
		vals = val.split("|");
		total += parseFloat(vals[1]);
	});
	
	var days = $("#days").val();
	
	total = parseInt(days) * total;
	
	$("#total-price").html(total.toFixed(2)+" kn");
	$("#total-price-h").val(total);
	
	for(var i = 0; i < valutes.length; i++)
	{
		var v = valutes[i];
		var p = total / v.val;
		$("#total-price-"+v.valuta).html(p.toFixed(2)+" kn");
	}	
}

function check_booking()
{
	if($(".checked").length > 0)
			return true;
			
	alert("Morate izabrati barem jedan od ponuđenih smještaja");
	return false;
}

function sendContact()
{
	var data = $("#contact_form > form").serialize();

	$.ajax({
		type: "POST",
		url: "index.php",
		data: data,
		cache: false,
		success: function(msg){}
	});
	
	$("#contact_form").fadeOut(1000, function() {
		$("#message_sent").slideDown(500);
	});
	
$('html, body').animate({
			'scrollTop' : '0',
		}, 1000);
			
	return false;
}

var captcha_a = Math.ceil(Math.random() * 10);
var captcha_b = Math.ceil(Math.random() * 10);       
var captcha_c = captcha_a + captcha_b;
function generate_captcha(id)
{
	var id = (id) ? id : 'lcaptcha';
	$("#"+id).html(captcha_a + " + " + captcha_b + " = ");
}

function check_info()
{
	var pass = true;
	var id = "";
	
	id = "ime"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "email"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}
	
	id = "prezime"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "adresa"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "grad"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "drzava"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "broj-kreditne-kartice"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "mjesec-isteka-kartice"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "godina-isteka-kartice"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

	id = "ccv"
	if(!$("#"+id).val()){$("#l"+id).css('color', '#A52A2A');pass = false;} else {$("#l"+id).css('color', '#333');}

		
	if(!pass)
	{
		alert("Morate popuniti sva polja kako bi nastavaili dalje.");
		return false;
	}
	
	return true;
}

