$(document).ready(function() {
	
	if($("#index").length) {
		//$("#container-content-pad").addClass("unvisible");
		//$("#container-foot").addClass("unvisible");
		$("<img/>").bind("load", function() {
			
			$("#loader").hide();	
			$("#container-content").addClass('fon');
				
			setTimeout(function(){
				
				if($.browser.msie) {
					$("#container-content-pad").show();
					$("#container-foot").show();

					if(("#menu").length) {
						
						var height =$("#menu ul").height(); 
 						
 						var data = $("#menu ul li.table-col");
						$.each(data, function (key,val) {
							if(val) $(val).css("height",height+"px");
						});
					}
					
					if($('#carousel').length) $('#carousel').glebsn_carousel();
					
				} else {
					$("#container-content-pad").fadeIn();
					$("#container-foot").fadeIn();	
					
					
					if($('#carousel').length) $('#carousel').glebsn_carousel();
				}			
				
			}, 1500);
			

						
		}).attr("src","/i/houseb4.jpg");
	} else {
		if($('#carousel').length) $('#carousel').glebsn_carousel();
	}
	
	if(".colored") {
		$(".colored tr:nth-child(odd)").addClass("odd");
	}
	
	
	var jsonRequest;
	
	$("#years li").hover( function(e) {
		var hovered = $(e.target);
		var offset =  hovered.offset();
		var top = offset.top;
 		var left = offset.left;
 		jsonRequest = $.getJSON(
 			'/',
 			{
 				id: hovered.attr('id'),
  				show_stage_info: true
 			},
 			function(obj) {
 				height = 0;
 				
 				top = top - height - 13;
 				$("#stages-info-block").css({
 					"top": top+"px",
 					"left": left+"px"
 				}).html(obj.html).show();
 				
 				//.corner("10px");
 				
 				var height= $("#stages-info-block").height();
 				top = top - height + 6;
 				$("#stages-info-block").css({
 					"top": top+"px"
 				});
 		});

	}, function() {
		$("#stages-info-block").hide().html().css({ "top": "0px", "left": "0px"});
		jsonRequest.abort();
	});
	
	$(".email").each(function() {
  		var e = $(this).attr("href") + "@" + $(this).text();
  		$(this).text(e);
  		$(this).attr("href", "mailto:" + e);
 	});		
 	
 	/* select */
 		
 	$("#select-arrow, #select-value").click( function() {
		var parent = $(this).parents('div.select-field')
		parent.removeClass('form-field-notice')
			  .toggleClass('select-field-open')
			  .find('ul').toggle();
	});
	
	$("#select_category li").click( function() {
		var el = $(this);
		$("#category").attr("value",el.attr("value"));
		$("#select_category").removeClass('select-field-open').find('ul').toggle();
		$("#select-value").addClass("selected-value").html(el.html());
		$("#select_category li").removeClass('selected');
		$(this).addClass('selected');
	});
	
	$("#select-value-onpage, #select-arrow-onpage").click( function() {
		var parent = $(this).parents('div.select-field')
		parent.removeClass('form-field-notice')
			  .toggleClass('select-field-open')
			  .find('ul').toggle();
	});
	
	$("#select_onpage li").click( function() {
		var el = $(this);
		$("#onpage").attr("value",el.attr("val"));
		$("#select_onpage").removeClass('select-field-open').find('ul').toggle();
		$("#select-value-onpage").addClass("selected-value").html(el.html());
		$("#select_onpage li").removeClass('selected');
		$(this).addClass('selected');
		$("#items").submit();
	});
	
	/* colorbox */
	
 	if($("a[rel='glebsnbox']").length){
		$('a[rel="glebsnbox"]').colorbox({
			"opacity": "0.9",
			"loop": false
			/*
			onOpen:function(){ $('body').addClass('overflow'); },
			onClosed:function(){ $('body').removeClass('overflow'); }
			*/
		});
	}
	
 	
	/* corners */
	$(".rounded").corner("10px");
	$(".rounded_l").corner("10px tl bl");
	$(".rounded_r").corner("10px tr br");
	
	
	
	/* focus fields */
 	if((".form-field").length) {
		var data = $(".form-field");
		$.each(data, function (key,val) {
			if(val.value) $("#"+val.id+"_value").parents('.default-field').hide();
		});
 	}
	
 	$(".default-field").live("click", function() {
	 	/*$(this).hide();*/
	 	var id = new String($(this).find('span').attr('id'));
	 	var field = id.replace("_value","");
	 	$("#"+field).focus();
 	});
 
 	$(".form-field").live("focus", function() {

		$(this).removeClass("form-field-notice"); /* for shop */
		var parent = $(this).parents('span');
		if(parent.length) parent.removeClass("form-field-notice"); /* for textarea */
	
		var id = new String($(this).attr('id'));
	
	
		var parent_ = $("#"+id+"_value").parents('.default-field').hide();
		
 	}).live("blur", function() {
		
		if($(this).attr("value")=='') {
			var id = $(this).attr('id');
			var parent = $("#"+id+"_value").parents('.default-field');
		
			if(parent.hasClass('default-field-notice')) {
				
				$(this).addClass("form-field-notice"); /* for shop */
				var span_parent = $(this).parents('span');
				span_parent.addClass("form-field-notice"); /* for textarea */
			}
			parent.show();
		}
		
 	});
 	
 	$('span.ajax-link a, a.ajax-link').live("click", function(e) {
		var clicked = $(e.target);
		var href = clicked.attr('href');
		fillfon();
		$.getJSON(
 		'/publications/',
 		{
 			href: href,
  			show_publication: true
 		},
 		function(obj) {
 			var top =$(window).scrollTop()+15;
 			$("#bigimg").html(obj.html).find(".dynamic-content").css({top:top+"px"});
 			$("#dynamicloader").hide();
 			// подкрасить фон
 			$("#bigimgfon").css("height",$(document).height()+"px");
 		});
		return false;
	});

 	
 	$("#close, #bigimgfon, #back").live("click", function() {
 		$("#bigimgfon").hide();
 		$("#bigimg").empty().hide();
 		$("#formajaxloader").hide();
  		$("#disabled").hide();	
  		return false;	
	});
	
 	if($("#nextlink").length) {
 		document.onkeydown = NavigateThrough;
	}
	
	if($("#prevlink").length) {
 		document.onkeydown = NavigateThrough;
	}
});

var fillfon = function() {
	var height= $("#container-content").height();	
 	var width = $("#container-content").width();	
 	$("#bigimgfon").css( {
 		width: "100%",
   		height:height+"px"
 	});
 	$("#bigimgfon").show();
 	$("#bigimg").show();
 	if($("#video-box-div").length) $("#video-box-div").hide();
 	
 	var top = Math.max(($(window).height() - 25)/2,0) + $(window).scrollTop();
 	$("#dynamicloader").css("top",top+"px").html("<div></div>").show();
}

function NavigateThrough (event) {
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey) {
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x27:
				var linkNext = $("#nextlink").text();
				href = linkNext;
   				break;			
			case 0x25:
			var linkPrev = $("#prevlink").text();
			   	href = linkPrev;
   				break;
		}
		
		 if ( href ) document.location = href;
	}			
}
