$(document).ready(function() {


	// Main Menu effects
	$(function () {
		$('#navigation li')
			.removeClass('no-js')
			.find('a:not(div.reservation-links a)')
			.append('<span class="hover" />').each(function () {
				var $span = $('> span.hover', this).css('opacity', 0);
				$(this).hover(function (){
					$span.stop().fadeTo(400, 1);
				}, function () {
					$span.stop().fadeTo(700, 0);
				});
			});
	});


	// Book Travel Button effects
	$(function () {
		$('#book-travel')
			.removeClass('no-js')
			.find('a')
			.append('<span class="hover" />').each(function () {
				var $span = $('> span.hover', this).css('opacity', 0);
				$(this).hover(function (){
					$span.stop().fadeTo(300, 1);
				}, function () {
					$span.stop().fadeTo(500, 0);
				});
			});
	});
	
	// RSS in news Button effects
	$(function () {
		$('#rss')
			.removeClass('no-js')
			.find('a')
			.append('<span class="hover" />').each(function () {
				var $span = $('> span.hover', this).css('opacity', 0);
				$(this).hover(function (){
					$span.stop().fadeTo(300, 1);
				}, function () {
					$span.stop().fadeTo(500, 0);
				});
			});
	});

	// Menu Tickets effects
	$("#navigation a.tickets").click(function(){
		if(!$(".reservation-links").hasClass("visible")){
			$(".reservation-links").fadeIn();
			$(".reservation-links").addClass('visible');
		}else{
			$(".reservation-links").fadeOut();
			$(".reservation-links").removeClass('visible');
		}
		
	});
	
	$(".inhome .form-button").hover(
		function(){
			//first remove the old ones. we remove them here to not break the fade out
			$(".tooltip-js").remove();
			var t = $(this);
			var title = t.text();
			t.attr("title", "");
			var tip = "<div class='tooltip-js' style='display: none';>"+title+"</div>";
			$(tip).appendTo(t);
			$(".tooltip-js").fadeIn();
		}, function(){
			$(".tooltip-js").fadeOut();
		}
	);
	
	
	//remove the link added for google
	$("#navigation a.destinations").attr("href", "javascript:void(0);");	
	
	//destination-select on news page
	$("#destination-select").change(function(){
		var destination = $("#destination-select").val();
		window.location = '/news/?destination='+destination;
	});
	
	//destination-select on travel page
	$("#travel-destination-select").change(function(){
		var destination = $("#travel-destination-select").val();
		window.location = '/travels/?destination='+destination;
	});
	
	
	//travels carousel
	$(".navigate-next").click(function(){
		var last_id = $("#types-browser .moving-part .type-item:last").attr("id");
		var temp = last_id.split("id");
		var items = temp[1];
		var left = $("#types-browser .moving-part").css("left");
		if(left == '0px'){
			$("#types-browser .moving-part").animate({left: "-812px"}, 750 );
		}else if(left == '-812px'){
			$("#types-browser .moving-part").animate({left: "-1624px"}, 750 );
		}else if(left == '-1624px'){
			if(items > 12 && items < 17){
				var go = -203*(items-4);
				$("#types-browser .moving-part").animate({left: go+"px"}, 750 );
			}else if(items == 12){
				$("#types-browser .moving-part").animate({left: "0px"}, 750 );
			}
		}else if(left == '-2436px'){
			if(items > 16 && items < 21){
				var go = -203*(items-4);
				$("#types-browser .moving-part").animate({left: go+"px"}, 750 );
			}else if(items == 12){
				$("#types-browser .moving-part").animate({left: "0px"}, 750 );
			}
		}else{
			//αν έχουμε items που είναι 13, 14, 15, 17, 18, 19 πάμε στην αρχή
			$("#types-browser .moving-part").animate({left: "0px"}, 750 );
		}
	});
	$(".navigate-previous").click(function(){
		/*
		var left = $("#types-browser .moving-part").css("left");
		if(left == '0px'){
			$("#types-browser .moving-part").animate({left: "-1624px"}, 1500 );
		}else if(left == '-812px'){
			$("#types-browser .moving-part").animate({left: "0px"}, 1500 );
		}else if(left == '-1624px'){
			$("#types-browser .moving-part").animate({left: "-812px"}, 1500 );
		}*/
		var last_id = $("#types-browser .moving-part .type-item:last").attr("id");
		var temp = last_id.split("id");
		var items = temp[1];
		var left = $("#types-browser .moving-part").css("left");
		if(left == '0px'){
			var go = -203*(items-4);
			$("#types-browser .moving-part").animate({left: go+"px"}, 750 );
		}else if(left == '-812px'){
			$("#types-browser .moving-part").animate({left: "0px"}, 750 );
		}else if(left == '-1624px'){
			$("#types-browser .moving-part").animate({left: "-812px"}, 750 );
		}else if(left == '-2436px'){
			$("#types-browser .moving-part").animate({left: "-1624px"}, 750 );
		}else{
			if(items > 16 && items < 21){
				$("#types-browser .moving-part").animate({left: "-2436px"}, 750 );
			}else if(items > 12 && items < 17){
				$("#types-browser .moving-part").animate({left: "-1624px"}, 750 );
			}
		}
	});
	
		
	
	//rotate ταξιδιωτικά νέα
	var news = $("#news a").size();
	new_interval = setInterval(news_rotate, 6000);
	
	var new_displayed = 0;
	function news_rotate(){
		//alert(items);
		if(news == '2'){
			if(new_displayed == 0){
				$("#news a:eq(0)").fadeOut(1000);
				$("#news a:eq(1)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 1;
			}else{
				$("#news a:eq(1)").fadeOut(1000);
				$("#news a:eq(0)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 0;
			}
		}else if(news == '3'){
			if(new_displayed == 0){
				$("#news a:eq(0)").fadeOut(1000);
				$("#news a:eq(1)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 1;
			}else if(new_displayed == 1){
				$("#news a:eq(1)").fadeOut(1000);
				$("#news a:eq(2)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 2;
			}else if(new_displayed == 2){
				$("#news a:eq(2)").fadeOut(1000);
				$("#news a:eq(0)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 0;
			}
		}else if(news == '4'){
			if(new_displayed == 0){
				$("#news a:eq(0)").fadeOut(1000);
				$("#news a:eq(1)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 1;
			}else if(new_displayed == 1){
				$("#news a:eq(1)").fadeOut(1000);
				$("#news a:eq(2)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 2;
			}else if(new_displayed == 2){
				$("#news a:eq(2)").fadeOut(1000);
				$("#news a:eq(0)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 3;
			}else if(new_displayed == 3){
				$("#news a:eq(3)").fadeOut(1000);
				$("#news a:eq(0)").fadeTo(1000, 1).fadeIn(500);
				new_displayed = 0;
			}
		}
	}
	
	
	
	$("#check-also div.moving-part").css("margin-top", '0px')
	check_also_interval = setInterval(check_also_move, 6000);
	function check_also_move(){
		var check_also_height = $("#check-also div.moving-part").height();
		if(check_also_height == 60){
			if($("#check-also div.moving-part").css("margin-top") == '0px'){
				$("#check-also div.moving-part").animate({marginTop: "-30px"}, 1500 );
			}else if($("#check-also div.moving-part").css("margin-top") == '-30px'){
				$("#check-also div.moving-part").animate({marginTop: "0px"}, 1500 );
			}
		}else{
			//do nothing, its 30
		}
	}
	
	$("#useful-links div.moving-part").css("margin-top", '0px')
	useful_links_interval = setInterval(useful_links_move, 6000);
	function useful_links_move(){
		var useful_links_height = $("#useful-links div.moving-part").height();
		if(useful_links_height == 60){
			if($("#useful-links div.moving-part").css("margin-top") == '0px'){
				$("#useful-links div.moving-part").animate({marginTop: "-30px"}, 1500 );
			}else if($("#useful-links div.moving-part").css("margin-top") == '-30px'){
				$("#useful-links div.moving-part").animate({marginTop: "0px"}, 1500 );
			}
		}else{
			//do nothing, its 30
		}
	}
	
	
	
	$("a.login-button").click(function(){
		//alert('yes');
		$("div#tooltip").toggle();
	});
	
	//change links that we added only for google to the normal ones
	//$("#navigation .destinations").attr("href", "javascript:void(0);");
	
	//$(".select-airline").insertBefore("#External_FlightFareSearch_PrefferedAirlines");


	
	//Zebra like lists and tables
	$(".zebra ul").each(function(index){$(this).children("li:odd").addClass('even');});
	$(".zebra ul li").hover(function (){
			$(this).addClass('hovered');
		}, function () {
			$(this).removeClass('hovered');
	});

	//Add class last in list items
	$(".zebra ul").each(function(index){$(this).children("li:last").addClass('last');});

	
	
	$("#trips-form select").change(function(){
		var value = $(this).val();
		if(value == 'all' || value == ''){
			$(this).parent().removeClass("activated");
		}else{
			$(this).parent().addClass("activated");
		}
		check_activation();
	});
	
	$("#trips-form input").change(function(){
		var value = $(this).val();
		if(value == ''){
			$(this).parent().removeClass("activated");
		}else{
			$(this).parent().addClass("activated");
		}
		check_activation();
	});
	
	/*$("#trips-form .duration").change(function(){
		var value = $(this).val();
		if(value == 'all'){
			$(this).parent().removeClass("activated");
		}else{
			$(this).parent().addClass("activated");
		}
		check_activation();
	});
	
	$("#trips-form .port").change(function(){
		var value = $(this).val();
		if(value == 'all'){
			$(this).parent().removeClass("activated");
		}else{
			$(this).parent().addClass("activated");
		}
		check_activation();
	});*/
	
	function check_activation(){
		if($("#trips-form div").hasClass("activated")){
			$("#trips-form .submit-button").attr("disabled", "").addClass("activated");
		}else{
			$("#trips-form .submit-button").attr("disabled", "disabled").removeClass("activated");
		}
	}

});

