$(document).ready(function(){
	/*VARS*/
	
	var login_window = $("#login_window");
	var list_time = 6000;
	var showcase_groups = $("#showcase .bottom_block a");
	var showcase_timer;
	var recommendation_prev = $("#recommendation .left_block");
	var recommendation_next = $("#recommendation .right_block");
	var recommendation_count = $("#recommendation .item").length;
	var recommendation_per_page = 5;
	var recommendation_cur_item = 0;
	
	/*FUNCTIONS*/
	
	function hide_menu_all(){
		$("#main_menu .menu1").removeClass("menu1_selected");
		$("#main_menu .menu2").removeClass("menu2_selected");
		$("#main_menu .menu3").removeClass("menu3_selected");
		$("#main_menu .menu4").removeClass("menu4_selected");
		$("#main_menu .menu5").removeClass("menu5_selected");
		$("#main_menu .menu6").removeClass("menu6_selected");
		$("#main_menu .menu_extended").hide().shadowDestroy();
		//show fucking select
		$(".advanced_search_category").show();
	}
	
	function extend_menu(id){
		var same = $("#main_menu .menu" + id + "_selected").length > 0 ? true : false;
		//hide all
		hide_menu_all();
		//display current if not same
		if(same == false){
			$("#main_menu .menu" + id).addClass("menu" + id + "_selected");
			$("#main_menu .menu" + id + "_extended").show().shadow({ color: "#444", offset: 4, opacity: 0.2 });
		}
		//hide fucking select
		$(".advanced_search_category").hide();
	};
	
	function list_showcase()
	{
		if(showcase_groups.length <= 1){
			clearInterval(showcase_timer);
			return;
		}
		var flag = true;
		var prevSelected = false;
		$.each(showcase_groups, function(i, item){
			if(flag){
				if(prevSelected == true){
					$(item).click();
					flag = false;
					return;
				}
				if(i == showcase_groups.length - 1)
				{
					$("#showcase .bottom_block a.group1").click();
					flag = false;
					return;
				}
				if($(item).hasClass("selected")){
					prevSelected = true;
				}
			}
		});
	}
	
	/*EVENTS*/
	
	//menu hovers
	$("#main_menu .menu_item").hover(
		function(e){
			extend_menu($(this).attr('class').substring(14, 15));
		},
		function(e){
		}
	);
	$("#main_menu .menu_extended").hover(
		function(e){
		},
		function(e){
			hide_menu_all();
		}
	);
	$("#main_wrapper").hover(
		function(e){
		},
		function(e){
			hide_menu_all();
		}
	);
	$("#header").hover(
		function(e){
			hide_menu_all();
		},
		function(e){
			
		}
	);
	
	//entrance click
	$("#topbar #entrance_link").click(function(e){
		if(login_window.css("display") == "none")
			login_window.show().shadow({ color: "#444", offset: 4, opacity: 0.2 });
		else
			login_window.hide().shadowDestroy();
		e.preventDefault();
		return false;
	});
	
	//login window close click
	$("#login_window .close").click(function(e){
		login_window.hide().shadowDestroy();
		e.preventDefault();
		return false;
	});
	
	//image thumb click
	$("#content .item_full .visual .image_holder_thumb").click(function(e){
		$("#content .item_full .visual .image_holder_full img").attr({
			src : $("a", this).attr("href")
		});
		$("#content .item_full .visual .image_holder_full a").attr({
			href : $("a", this).attr("alt")
		});
		e.preventDefault();
		return false;
	});
	
	//feature hover
	$("#content .item_full .features div").
	add("#content .item_full .main_features div").hover(
		function(){
			$(this).css("background", "#d1f2ab");
		},
		function(){
			$(this).css("background", $(this).hasClass("odd") ? "#fff" : "#f5f5f5");
		}
	);
	
	//search sample click
	$("#search_samples .sample").click(function(){
		$(".search_field input").val($(this).text());
		$(".search_place select").val(0);
	});
	
	//recommendation prev-next clicks
	$("a", recommendation_prev).click(function(){
		var item = $("#recommendation .item").get(--recommendation_cur_item);
		$(item).show();
		recommendation_next.show();
		if(recommendation_cur_item == 0)
			recommendation_prev.hide();
	});
	$("a", recommendation_next).click(function(){
		var item = $("#recommendation .item").get(recommendation_cur_item++);
		$(item).hide();
		recommendation_prev.show();
		if(recommendation_cur_item == recommendation_count - recommendation_per_page)
			recommendation_next.hide();
	});
	
	//showcase group click
	$("#showcase .bottom_block a").click(function(e){
		clearInterval(showcase_timer);
		showcase_timer = setInterval(list_showcase, list_time);
		var group_id = $(this).attr("class").substr(5, 1);
		var prev_group_id = $("#showcase .bottom_block a.selected").attr("class").substr(5, 1);
		$("#showcase .showcase"+prev_group_id).fadeOut();
		$("#showcase .bottom_block a").removeClass("selected");
		$("#showcase .showcase"+group_id).fadeIn();
		$(this).addClass("selected");
		e.preventDefault();
		return false;
	});
	
	//list showcase
	showcase_timer = setInterval(list_showcase, list_time);
	
	//recommendation setup
	if(recommendation_count > recommendation_per_page)
		recommendation_next.show();
	
});
