// JavaScript Document
$(document).ready(function() {	
	//Hide all .nav links
	$(".nav").hide();
	$(".emptylegs .nav").show();
	
	//Clear excess menu borders
	$("#service_menu li:first").css("border-top", "none");
	$("#service_menu li:last").css("border-bottom", "none");
	
	// Clear right borders
	$("#content ul").each(function() {
		$("li:last", this).css("border-right", "none");
	});

	$(".articles li").each(function() {
		if(($(this).index()+1)%3 == 0) {
			$(this).css("border-right", "none");	
		}
	});

	$(".events li").each(function() {
		if(($(this).index()+1)%3 == 0) {
			$(this).css("border-right", "none");	
		}
	});

	$(".histyle li").each(function() {
		if(($(this).index()+1)%2 == 0) {
			$(this).css("border-right", "none");	
		}
	});

	$(".planes_view li").each(function() {
		if(($(this).index()+1)%3 == 0) {
			$(this).css("border-right", "none");	
		}
	});
	
	$(".emptylegs li").each(function() {
		if(($(this).index()+1)%3 == 0) {
			$(this).css("border-right", "none");	
		}
	});
	
	$("#emptyleg li").each(function() {
		if(($(this).index()+1)%5 == 0) {
			$(this).css("border-right", "none");	
		}		
	});
	
	$("#available li").each(function() {
		if(($(this).index()+1)%5 == 0) {
			$(this).css("border-right", "none");	
		}		
	});

	//Fix long titles
	$("h1").each(function() {
		if($(this).text().split(" ").length < 4) {
			$(this).css("text-transform", "uppercase");
		}
	});
	
	//Make interview badges
	$(".interview .question:first").prepend('<div class="question_badge">'+$(".question_title").text()+'</div>');

	$(".interview .answer:first").prepend('<div class="answer_badge">'+$(".answer_title").text()+'</div>');
	
	//Set equal block sizes
	$(".sale ul, .focus ul, .press ul, .special ul, .more_articles ul, .planes_view ul, #emptyleg .items").equalHeights();
	
	//Set equal block sizes advanced
	$(".articles ul").setEqualHeights(3);
	$(".events ul").setEqualHeights(3);
	$(".histyle ul").setEqualHeights(3);
	
	//
	$(".list li").each(function() {
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css('height', 'auto'); }
		$(this).css("min-height", 0);
	});
	
	//Clear repeated news dates
/*
	datetext = "";
	$(".news .date, .list .date").each(function(index) {
		if($(this).text() == datetext) {
			$(this).css("display", "none");
		} else {
			datetext = $(this).text();	
		}
	});
*/
	//Hide vertical banner if article is too short
	if($("#article").height() && $("#article").height() < 400 && !$(".news").height()) {
		$(".vertical").hide();	
	}

	//Hide same banner news are too low
	/*
	if($(".news_archive").height() && $(".news").height() < 400) {
		$(".vertical").hide();	
	}
	*/
	if($(".news_archive").height() && $(".news").height() < 600) {
		$(".top-flight").hide();	
	}

	//Set viewmode left position
	var lp = 620 - $(".viewmode").width();
	$(".viewmode").css("margin-left", lp);
		
	//Navigation setup
	$(".news, .sale, .focus, .articles, .press, .special, .events, .more_articles, .results, .planes_view, .planes, .histyle").assignEvents();
	$(".persons").assignPersonEvents();
	
	$(".emptylegs tr").each(function() {
		$(this).hover(
				function () {
				    $(this).addClass("hover");
				},
				function () {
				    $(this).removeClass("hover");
				});
		$(this).click(function(e) {
		//	alert(location.host);
			if(e.target.nodeName == "A") {
				location.href = "http://"+location.host+"/"+e.target.attr("href");	
			} else {
				location.href = "http://"+location.host+"/"+$("a.nav", this).attr("href");
			}
		});
	});
	
	//Services menu events
	$("#service_menu li").hover(
  		function () {
		    $(this).addClass("hover");
  		},
  		function () {
		    $(this).removeClass("hover");
		}
	);

	//Special offers design
	
	var maxHeight = 0;
	$(".offers li")	.each(
							function() {
								maxHeight = Math.max(maxHeight, $(this).height());
							})
						.each(
							function() {
								var result = maxHeight - $(this).find("h3").height() - $(this).find(".price").height() - $(this).find(".type").height() - 150;
								$(this).find(".text").css("min-height", result);
							})
						.hover(
							function() {
								$(".offer_details").addClass("hover");
							},
							function() {
								$(".offer_details").removeClass("hover");
							})
						.click(
							function() {
								$(".offer_details")	.animate({marginTop: -10, marginBottom: 10}, "fast")
															.animate({marginTop: 0, marginBottom: 0}, "fast")
							});

	//BackOffice actions
	$("#backoffice .navigation li")	.hover(
											function () {
												if(!$(this).hasClass("active")) {
													$(this).addClass("hvr");
												}
											},
											function () {
												$(this).removeClass("hvr");		
											})
										.click(
											function() {
												if(!$(this).hasClass("active")) {
													location.href = "http://"+location.host+"/"+$("a", this).attr("href");
													event.preventDefaults();
												}
											});
	
	$("#backoffice .switcher a").click(function() {
		$("#backoffice .switcher").toggle();
		$("#backoffice .switcher input")	.attr("value","")
												.focus();
		event.preventDefault();
	});
	
	//Fix long labels positions
	$("#backoffice .form label").each(function() {
		if(!$(this).parent().hasClass("checkbox")) {
			if($(this).width() > 180) {
				$(this)	.css("margin-left", -400)
							.css("min-width", 390);
			}
		}
	});	

	// Menu events
	$("#menu .services").click(function() {
		if($("#index ul:last").hasClass("about")) {
			$("#index ul:last").removeClass("about");
			$("#service_menu").fadeOut();
		} else {
			$("#index ul:last").addClass("about");
			$("#service_menu").fadeIn();
		}
		return(false);
	});
		
	$("#menu .contents").hover(
  		function () {
			$(this).addClass("hover");
			if (navigator.appVersion.indexOf("Mac")==-1) {
				$(".vertical object").hide();
			}
			$("#index")	.stop(true, true)
							.css("top", $("#header").offset().top + 32)
							.slideDown(400, function() {
								$("#menu .contents").addClass("hover");					
							});
  		},
  		function () {
			$("#index")	.delay(200)
							.slideUp(400, function() {
								$("#menu .contents").removeClass("hover");
								if (navigator.appVersion.indexOf("Mac")==-1) {
									$(".vertical object").show();
								}
							});
		}
	);
	
	$("#menu .contents").click(function() {
		return(false);
	});
	
	$("#index").hover(
		function() {
			$(this).stop(true, true).slideDown(400, function() {
				$("#menu .contents").addClass("hover");
			});
		},
		function() {
			$(this).delay(200).slideUp(400, function() {
				$("#menu .contents").removeClass("hover");		
			});
		}
	);
	
	//Search workflow
	var searchInProgress = false;
	var searchText = '';
	$("#menu .search").click(function(e) {
		if(!searchInProgress)	{
			var searchLink = $(this).html();
			$(this).css('width', $(this).width());
			$(this).html('<form action="search" method="post" id="searchform"><input type="text" value="'+searchText+'" name="searchword" id="searchword" /><img src="i/icon-search-dark.png" width="16" height="16" alt="Искать" /></form>');
			$("input", this).focus();
			searchInProgress = true;
			$(this).animate({	minWidth: '125px'}, 500);
			
			$("img", this)		.click(
									function() {
										$("#searchform").submit();
									})
								.hover(
									function() {
										$("#searchform input").unbind("focusout");
									},
									function() {
										$("#searchform input").focusout(function() {
											$("#menu .search").html(searchLink);
											$("#menu .search").animate({minWidth: '0'}, 500);
											searchInProgress = false;
											searchText = $(this).attr('value');
										});
									})
								.mouseleave();
			
			$("#searchform").submit(function() {
				if($("#searchword").val().length>2) {
					return true;
				}
				else{
					alert("Type 3 and more symbols!"); 
					return false;
				}
			});
		}
	});
		
	//Search Results Params movement
	if($(".search_result .params").height() > 0) {
		var minTop = $(".search_result .params").offset().top;
		$(window).scroll(function() {
			var klop = Math.max(minTop, $(window).scrollTop() + 30);
			klop = Math.min(klop, $(".search_result").height() - 30);
			$(".search_result .params").css("top", klop);
		});
	}
	
	// Survey events
	$(".survey li").click(function() {
		$("input", this).attr("checked", "checked");
	});
	
	//Charter events
	$("#charter .submit input").click(function() {
		$(this).hide();
		$("#charter .contacts").slideDown();
		$("#charter .contacts input:first").focus();
		$("html,body").animate({scrollTop:250});
	});
	
	$("#charter .options div a").hover(
			function() {
				$(".leg:last").addClass("todelete");
			},
			function() {
				$(".leg:last").removeClass("todelete");
			}
	);
		
	/*
	$('#charter .contacts input[type="text"]').keyup(function() {
		var regex = /^[-0-9a-z_.]+@[-a-z_.]+\.\w{2,5}$/i;
		if(regex.test($('#charter .contacts input[name="email"]').attr("value")) && $('#charter .contacts input[name="uname"]').attr("value").length > 0 && $('#charter .contacts input[name="tel"]').attr("value").length > 6 )	{
			$('#charter .contacts input[type="submit"]').removeAttr("disabled");
		} else {
			$('#charter .contacts input[type="submit"]').attr("disabled", "disabled");
		}
	});
	*/
	
	//PlanetJet Button
	$("#encyclopaedia .planetJet input").click(
			function() {
				location.href = "http://planetjetguide.com/";
			});

	//Emptyleg events
	$("#emptyleg tr")		.hover(
			function() {
				if( !$(this).hasClass("selected") ) {
					$(this).addClass("hover");
				}
			},
			function() {
				$(this).removeClass("hover");
			}
	)						.click(
			function(e) {
				if($("td",this).size() > 0) {
					if(e.target.nodeName == "A" && !$(e.target).parent().hasClass("order")) {
						location.href = "http://"+location.host+"/"+e.target.attr("href");						
					} else {
						$("#mask").css("min-height", $(document).height());
						$("#mask").fadeIn();
						var top = $(window).scrollTop() + ($(window).height() - $("#order").height())/2 - 50;
						$("#order")	.css("top", top)
										.fadeIn();
					}
				}
			}
	);
	
	$("#emptyleg #order .close").click(
			function() {
				$("#mask").fadeOut();
				$("#order").hide();	
			}
	);
	
	
	
	$("#available tr")		.hover(
			function() {
				if( !$(this).hasClass("selected") ) {
					$(this).addClass("hover");
				}
			},
			function() {
				$(this).removeClass("hover");
			}
	)						.click(
			function(e) {
				if($("td",this).size() > 0) {
					if(e.target.nodeName == "A" && !$(e.target).parent().hasClass("order")) {
						location.href = "http://"+location.host+"/"+e.target.attr("href");						
					} else {
						$("#mask").css("min-height", $(document).height());
						$("#mask").fadeIn();
						var top = $(window).scrollTop() + ($(window).height() - $("#order").height())/2 - 50;
						$("#order")	.css("top", top)
										.fadeIn();
					}
				}
			}
	);
	
		
		$("#available #order .close").click(
			function() {
				$("#mask").fadeOut();
				$("#order").hide();	
			}
	);
		
	
	//Catalog events
	$("#catalog tr")		.hover(
			function() {
				if(!$("td", this).hasClass("section")) {
					$(this).addClass("hover");
				}
			},
			function() {
				$(this).removeClass("hover");
			}
	)						.click(
			function(e) {
				if(!$("td", this).hasClass("section") && e.target.nodeName != "A") {
					location.href = "http://"+location.host+"/"+$("a", this).attr("href");
				}
			}
	);
	
	//For sale gallery
	$("#galleryPopup .close").click(
			function () {
				$("#galleryPopup").fadeOut();
			});
	$("#for_sale .images li").click(
			function() {
				$("#galleryPopup .gallery .paging a").eq($(this).index()).click();
				$("#galleryPopup").fadeIn();
				$("#galleryPopup .paging").css("margin-left", -$(".paging").width()/2);
			});
	
	
	//Image Slider Init
	$active = 0;
	$total = $("#slider .imageFrame img").size();
	
	$("#slider .imageFrame").after("<div class='bottomPanel'></div>");
	$("#slider .previews li:first").addClass("active");
	$("#slider .descriptions li:first").show();
	$("#slider .imageFrame img:first").show();
	
	$("#slider .previews li").click(function() {
		showImage($(this).index());
	});
	
	$("#slider .previews li").click(function() {
		showImage($(this).index());
	});
	
	$("#slider .descriptions, #slider .previews").hover(
		function() {
			clearInterval(play);
		},
		function() {
			startPlayback();
		}
	);
	
	startPlayback();

	//Gallery
	$(".gallery").each(function() {
		$(this).galleryInit();
	});
	
	//Survey
	normalizeSurvey();

	//"Ears" positions
	$(".navigate").css("margin-top", -$(".navigate").height());
//	var ep = document.getElementById("encyclopaedia").offsetTop - 40 - $(".navigate").height();
//	$(".navigate").css("top", ep);


	//News shortage
	$(".news").each(function() {
		if(!$(this).hasClass("news_archive")) {
			if($(this).height() > 400) {
				$("li:last-child", this).hide();
			};
		}
	});
	
});

	normalizeSurvey = function() {
		var maxValue = 0;
		$(".survey .bar").each(function() {
			if($(this).width() > maxValue) {
				maxValue = $(this).width();
			}
		});
		$(".survey .bar").each(function() {
			var newWidth = 200*$(this).width()/maxValue;
			$(this).width(newWidth);
		});		
	}

	$.fn.setEqualHeights = function(count) {
		var i = 0;
		var j = 0;
		while (i < $(this).children().size()) {
			var currentTallest = $(this).children().eq(i).height();
			j = i+1;
			while((j-i) < count) {
				if($(this).children().eq(j).height() > currentTallest) {
					currentTallest = $(this).children().eq(j).height();	
				}
				j++;	
			}
			while(j > i) {
				$(this).children().eq(j-1).css("min-height", currentTallest);
				j--;
			}
			i += count;
		}
	}

//Gallery Functions

	$.fn.galleryInit = function() {

		if($("img", this).size() > 1) {
			$(this).html("<div class='wrapper'><div class='image_reel'>"+ $(this).html() +"</div></div>");	
			$(this).prepend("<div class='button prev'></div>")
			$(this).prepend("<div class='button next'></div>")
			
			var imageSum = $(".image_reel img", this).size();
			$(".image_reel", this).css({'width' : 750 * imageSum});
			
			var paging = '';
			for (var i=0; i< imageSum; i++) {
				paging += "<a href='#' onclick='return false;'></a>"
			}
			$(this).append("<div class='paging'>"+paging+"</div>");
			$(".paging a:first", this).addClass("active");
			$(".paging", this).css("margin-left", 25-$(".paging").width()/2);
		
			$(".paging a", this).click(
									function(e) {
										$("a", $(this).parent()).removeClass("active");
										$(this).addClass("active");
										$(this).parent().parent().galleryMove();
										event.preventDefault();
									});
									
			$(".button", this).hover(
									function() {
										$(this).animate({opacity: 1}, 500);
									},
									function() {
										$(this).animate({opacity: 0}, 250);
									})
								.click(
									function() {										
										if($(this).hasClass("next")) {
											$(this).parent().find('a[class="active"]').next().click();
										} else {
											$(this).parent().find('a[class="active"]').prev().click();
										}
									});
			$(this).hover(
						function() {
							$activegallery = $(this);
							$(document).keydown(function(event) {
								if(event.keyCode == 37) {
									$activegallery.find('a[class="active"]').prev().click();	
								}
								if(event.keyCode == 39) {
									$activegallery.find('a[class="active"]').next().click();	
								}
							});
						},
						function() {
							$(document).unbind("keydown");
						});
		}
		
		$(this).prepend("<div class='border'></div>");
	}

	$.fn.galleryMove = function(){
		var triggerID = $(".paging", this).find('a[class="active"]').index();
		var image_reelPosition = triggerID * 750;

		if(triggerID != 0) {
			$(".prev", this).show();	
		} else {
			$(".prev", this).hide();
		}

		if(triggerID == ($(".paging a", this).size()-1)) {
			$(".next", this).hide();	
		} else {
			$(".next", this).show();
		}

		//Slider Animation
		$(".image_reel", this).animate({
		 		left: -image_reelPosition
		 }, 500 );
	}; 



startPlayback = function() {
	play = setInterval(function() {
		var next = ($active+1)%$total;
		showImage(next);
	}, 7000);
}

showImage = function(index) {
	if($active != index) {
		$("#slider .imageFrame img").eq($active).fadeOut("slow");
		$("#slider .imageFrame img").eq(index).fadeIn("slow");

		$("#slider .previews li").removeClass("active");
		$("#slider .previews li").eq(index).addClass("active");
		
		$("#slider .descriptions li").eq($active).hide();
		$("#slider .descriptions li").eq(index).fadeIn("slow");	
		
		$active = index;
	}
}

$.fn.assignEvents = function() {
	$("li", this).each(function(index) {
		$(this).hover(
				function () {
				    $(this).addClass("hover");
				},
				function () {
				    $(this).removeClass("hover");
				});
		$(this).click(function(e) {
			if(e.target.nodeName == "A") {
				location.href = "http://"+location.host+"/"+e.target.attr("href");	
			} else {
				location.href = "http://"+location.host+"/"+$("a.nav", this).attr("href");
			}
		});
	});
}

$.fn.assignPersonEvents = function() {
	$(".person", this).each(function(index) {
		$(this).hover(
				function () {
				    $(this).addClass("hover");
				},
				function () {
				    $(this).removeClass("hover");
				});
		$(this).click(function() {
			location.href = "http://"+location.host+"/"+$("a.nav", this).attr("href");
		});
	});
}

$.fn.equalHeights = function() {
	$(this).each(function()	{
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css('height', currentTallest); }	
		$(this).children().css("min-height", currentTallest);
	});
	return this;
};

function make_vote(){
	$("input.voterad").each(function(i){
		if(this.checked){
			sel = $(this).val();
		}
	});
	$.post("poll/vote",{ ajaxvote: sel}, function(html){
		$("#vote_content").addClass("result");
		$("#vote_content").html(html);
	});
    return false;
}

ieClick = function() {
//	event.preventDefaults();
	return false;	
}
