$(document).ready(function() {

	field_1=false;
	field_2=false;

	$(".unusual_link").click(
		function() {
			if ($(this).children("a")) {
				//alert("unusual link, have children(a). near 9. Value: \nno" );
				location.href=$(this).children("a").attr("href");
			}
		}
	);
	
	function findPosX(obj) {
		var curleft = 0;
		if(obj.offsetParent)
			while(1) {
				curleft = curleft + obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft = curleft + obj.x;
		return curleft;
	}

	function findPosY(obj) {
		var curtop = 0;
		if(obj.offsetParent)
			while(1) {
				curtop= curtop+ obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.y)
			curtop= curtop+ obj.y;
		return curtop;
	}


//	************************ Ховер в верхнем меню ************************
	$("#top_menu ul li.text").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);


//	************************ Ховер и клик в первом левом меню городов ************************	
	$(".first_block .bottom_backgr ul li").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);
	
	$(".first_block .bottom_backgr ul li").click(
		function() {
			location.href=$(this).children(".cont").children(".text").children("a").attr("href")
		}
	);


//	************************ Ховер и клик во втором левом меню городов ************************
	$(".second_block .bottom_backgr ul li").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);
	
	$(".second_block .bottom_backgr ul li").click(
		function() {
			location.href=$(this).children(".cont").children(".text").children("a").attr("href")
		}
	);


//	************************ Клик по иконкам в третьем левом меню (билетов) ************************	
	$(".third_block .bottom_backgr .content .content_inner .content_bottom_backgr .discount .img").click(
		function() {
			location.href=$(this).children("a").attr("href")
		}
	);


//	************************ Ховеры и клики в первом верхнем поле поиска ************************
	
	$("#content_container .search_form .input input").blur(
		function() {
//			$(this).parent().parent().children(".ul_wrapper").children("ul").removeClass("active")
		}
	);

	$("#content_container .search_form .input li").hover(
		function() {
			$(this).addClass("active");
		},
		function() {
			$(this).removeClass("active");
		}
	);

	$("#content_container .search_form .input li, .inner .search_form .input li").click( function() { clickLi(this); } );

	function clickLi(obj) {
			assosiated_input = $(obj).parents().children(".input_field").children("input");
			assosiated_input.attr("value", $(obj).text());

			$.getScript( '/ajax/?target=' + $(obj).parents('form').attr('action') 
						+ '&form='+$(obj).parents('form').attr("name") 
						+ '&field='+assosiated_input.attr("name") 
						+ '&value=' + encodeURI($(obj).text()) , 
				function(){
					$("#content_container .search_form .input li, .inner .search_form .input li").unbind('click').click( function() { 
						clickLi(this);
					});
			});
	}

	$("#content_container .search_form .input").click(
		function() {
			if ( $(this).children(".ul_wrapper").children("ul").hasClass("active")) {
				$(this).children(".ul_wrapper").children("ul").removeClass("active")
			}
			else {
				$("#content_container .search_form .input ul.active").removeClass("active");
				$(this).children(".ul_wrapper").children("ul").addClass("active");
				$(this).children(".input_field").children("input").focus()
			}
		}
	);

	$("#content_container .search_form .last_line .button").hover(
		function() {
			$(this).css("background-position", "bottom")
		},
		function() {
			$(this).css("background-position", "top")
		}
	);

	$("#content_container .search_form .last_line .button").click(
		function() {
return true;
			location.href="#"
		}
	);


//	************************ Ховеры и клики в ячейке контента ************************	
	$(".cell .middle_backgr .cont .right_block .button").hover(
		function() { 
			$(this).addClass("active")
		},
		function() { 
			$(this).removeClass("active")
		}
	);


//	************************ Ховер в нижнем меню ************************		
	$("#bottom_container .menu li a").hover(
		function() {
			$(this).css("text-decoration", "underline")
		},
		function() {
			$(this).css("text-decoration", "none")
		}
	);

//	$("#hotline").click(
//		function() {
//			a=getPos( this );
//			alert( a.left );
//			alert( findPosX(this) );
//			alert( findPosY(this) )
			a = document.getElementById("left_end_marker");
			b = document.getElementById("content_end_marker");
			c = document.getElementById("right_end_marker");
			d = document.getElementById("bottom_container");
			a_top = findPosY(a);
			b1 = $("#pages").css("height");
			b_top = findPosY(b) + parseInt(b1);
//			alert(findPosY(b) + ' + ' + parseInt(b1) + ' = ' + b_top);
			c1 = $("#shell").css("height");
			c_top = findPosY(c) + parseInt(c1);
			d_top = findPosY(d) + 1;
			e = Math.max(Math.max(a_top,b_top),c_top);
			f = e - d_top + 1;
			if (e > d_top) {
				$("#bottom_container.strani").css("margin-top", f);
//				alert("corrected на "+f+"!")
			}


//	************************************************ Страница Горящих ************************************************	
	$("#content_container .goryash_top_form_container .input li").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);

	$("#content_container .goryash_top_form_container .input li").click(
		function() {
			$(this).parent().parent().parent().children(".input_field").children("input").attr("value", $(this).text())
		}
	);

	$("#content_container .goryash_top_form_container .input").click(
		function() {
			if ( $(this).children(".ul_wrapper").children("ul").hasClass("active") ) {
				$(this).children(".ul_wrapper").children("ul").removeClass("active")
			}
			else {
				$("#content_container .goryash_top_form_container .input ul.active").removeClass("active");
				$(this).children(".ul_wrapper").children("ul").addClass("active");
				$(this).children(".input_field").children("input").focus()
			}
		}
	);

	$("#content_container .goryash_top_form_container .button").hover(
		function() {
			$(this).css("background-position", "bottom")
		},
		function() {
			$(this).css("background-position", "top")
		}
	);

	$("#content_container .goryash_top_form_container .button").click(
		function() {
//alert('l248');
			location.href="#"
		}
	);

	$("#goryash_table tbody tr").hover(
		function() {
			$(this).children("td").css("background-position","left -204px")
		},
		function() {
			$(this).children("td").css("background-position","left top")
		}
	);

	$("#stropis_table tbody tr").hover(
		function() {
			$(this).children("td").css("background-position","left -204px")
		},
		function() {
			$(this).children("td").css("background-position","left top")
		}
	);


	$("#strani_content_container .cell .button").hover(
		function() {
			$(this).css("background-position", "bottom")
		},
		function() {
			$(this).css("background-position", "top")
		}
	);

	$("#pages a").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);


//	************************************************ Страница Внутренних ************************************************	
	$("#vnutr_content_container .top_form_cont .input li").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);

	$("#vnutr_content_container .top_form_cont .input li").click(
		function() {
			$(this).parent().parent().parent().children(".input_field").children("input").attr("value", $(this).text())
		}
	);

	$("#vnutr_content_container .top_form_cont .input").click(
		function() {
			if ( $(this).children(".ul_wrapper").children("ul").hasClass("active") ) {
				$(this).children(".ul_wrapper").children("ul").removeClass("active")
			}
			else {
				$("#vnutr_content_container .top_form_cont .input ul.active").removeClass("active");
				$(this).children(".ul_wrapper").children("ul").addClass("active");
				$(this).children(".input_field").children("input").focus()
			}
		}
	);

	$("#vnutr_content_container .top_form_cont .button").hover(
		function() {
			$(this).css("background-position", "bottom")
		},
		function() {
			$(this).css("background-position", "top")
		}
	);

	$("#vnutr_content_container .top_form_cont .button").click(
		function() {
//alert('l331');
		location.href="#"
		}
	);

	$("#vnutr_table tbody tr").hover(
		function() {
			$(this).children("td").css("background-position","left -204px")
		},
		function() {
			$(this).children("td").css("background-position","left top")
		}
	);

	$("#short_field_1").click(
		function() {
			if (!field_1) {
				field_1=true;
				$(this).attr("value","")
			}
		}
	);

	$("#short_field_1").blur(
		function() {
			if ( $(this).attr("value")=="" ) {
				field_1=false;
				$(this).attr("value","От")
			}
		}
	);

	$("#short_field_2").click(
		function() {
			if (!field_2) {
				field_2=true;
				$(this).attr("value","")
			}
		}
	);

	$("#short_field_2").blur(
		function() {
			if ( $(this).attr("value")=="" ) {
				field_2=false;
				$(this).attr("value","До")
			}
		}
	);


//	************************************************ Страница Зарубежных ************************************************	
	$("#zarubeg_content_container .top_form_cont .input li").hover(
		function() {
			$(this).addClass("active")
		},
		function() {
			$(this).removeClass("active")
		}
	);

	$("#zarubeg_content_container .top_form_cont .input li").click(
		function() {
			$(this).parent().parent().parent().children(".input_field").children("input").attr("value", $(this).text())
		}
	);

	$("#zarubeg_content_container .top_form_cont .input").click(
		function() {
			if ( $(this).children(".ul_wrapper").children("ul").hasClass("active") ) {
				$(this).children(".ul_wrapper").children("ul").removeClass("active")
			}
			else {
				$("#zarubeg_content_container .top_form_cont .input ul.active").removeClass("active");
				$(this).children(".ul_wrapper").children("ul").addClass("active");
				$(this).children(".input_field").children("input").focus()
			}
		}
	);

	$("#zarubeg_content_container .top_form_cont .button").hover(
		function() {
			$(this).css("background-position", "bottom")
		},
		function() {
			$(this).css("background-position", "top")
		}
	);

	$("#zarubeg_content_container .top_form_cont .button").click(
		function() {
//alert(l422);
			location.href="#"
		}
	);

	$("#zarubeg_table tbody tr").hover(
		function() {
			$(this).children("td").css("background-position","left -204px")
		},
		function() {
			$(this).children("td").css("background-position","left top")
		}
	);	
/*

//	************************ Любимый PNGFIX ************************	
	if (ie6) { 
		$(document).pngFix();
	}

    $(".jqbookmark").jFav();
*/
});

function pre_search(f) {
//alert('pre_search()');
	link = $("form[name="+f+"]").attr('action') + '?';
	$("form[name="+f+"] input").each(function() {
		//alert( $(this).val().substr(0,3) );
		if ( ($(this).val().substr(0,3) != 'Все') ) {
			if ( (($(this).attr('name')=='price_from') && ($(this).val()=='От')) || (($(this).attr('name')=='price_to') && ($(this).val()=='До')) ) {
				//это просто праздник какой-то! =(
			} else {
				link += $(this).attr('name') + "=" + $(this).val() + "&";
			}
		}
	});
//alert("pre_searched. link: \n"+link);
	document.location.href = link;
	return false;
}