var expandedHeight = 436;
var contactsStartHeight = 48;
var speed = 600;
var topInfoStartHeight = 0;
var topFormStartHeight = 0;
var contactsStartBottom = 0;
/*------------------------------------------------------------------
home page Nivo Silder
------------------------------------------------------------------*/
function initNivoSlider() {
	$('#slider').nivoSlider({
		effect:'fold', //Specify sets like: 'fold,fade,sliceDown'
		slices:8,
		animSpeed:1500, //Slide transition speed
		pauseTime:6000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:false, //Only show on hover
		controlNav:false, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:false, //Use left & right arrows
		pauseOnHover:false, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8 //Universal caption opacity
	});
};
/*------------------------------------------------------------------
document on ready
------------------------------------------------------------------*/
$("document").ready(function () {
	initContacts();
	initLastWorks();
	initTopLinks();
});
/*------------------------------------------------------------------
initiaization for the contacts sliding block
------------------------------------------------------------------*/
function initContacts()
{
	var contactsOn=false;
	topInfoStartHeight = $("#top-info").height();
	topFormStartHeight = $("#top-form").height();
	contactsStartBottom = parseInt($("#top-info").css('bottom'));
	
	$("#top-visible a.home")
	.fadeTo(0, 0.7)
	.mouseenter(function() {
		$(this).stop().fadeTo(200, 1.0);
	})
	.mouseleave(function() {
		$(this).stop().fadeTo(250, 0.7);
	});
	
	$("#top-button-contacts")
	.fadeTo(0, 0.7)
	.mouseenter(function() {
		$(this).stop().fadeTo(200, 1.0);
	})
	.mouseleave(function() {
		$(this).stop().fadeTo(250, 0.7);
	})
	.click( function() {
		if(contactsOn==false) {
			$("#layout-top").stop().animate({"height" : expandedHeight+"px"}, speed, "easeOutBack");
			$(this).css({"background-position" : "0 -20px"});
			contactsOn=true;
		}
		else {
			$("#layout-top").stop().animate({"height" : contactsStartHeight+"px"}, speed, "easeOutBack");
			$(this).css({"background-position" : "0 0px"});
			contactsOn=false;
		}
	});
	
	var tempName = $("#contact-edit-name").attr("value");
	var tempEmail = $("#contact-edit-email").attr("value");
	
	$("#contact-edit-name")
	.focus(function() {
		if( $(this).attr("value") == tempName ) $(this).attr({"value" : ""});
	})
	.blur(function() {
		if( $(this).attr("value") == "" ) $(this).attr({"value" : tempName});
	});
	$("#contact-edit-email")
	.focus(function() {
		if( $(this).attr("value") == tempEmail ) $(this).attr({"value" : ""});
	})
	.blur(function() {
		if( $(this).attr("value") == "" ) $(this).attr({"value" : tempEmail});
	});
	$("#contact #submit")
	.click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		$("#contact input#submit").stop().fadeTo(300, 0.0, function() {
			$("#sending").stop().fadeTo(0,0).show().fadeTo(150, 1.0, function() {
				$.ajax({
				  	url: "/email/send",
				    global: false,
				    type: "POST",
				    data: ({name : $("#contact-edit-name").attr("value"),
							email : $("#contact-edit-email").attr("value"),
							message : $("#contact-edit-message").attr("value")}),
				    dataType: "html",
				    async: false,
				    success: function(msg){
						if(msg=="sent") {
							cleanErrors();
							$("#contact #sending").fadeTo(300, 0.0, function() {
								$("#sending").hide();
								$("#contact #sent").stop().fadeTo(0,0).show().fadeTo(300, 1.0, function() {
								})
							});
						} else {	
							showErrors(msg);
							$("#contact #sending").fadeTo(300, 0.0, function() {
								$("#sending").hide();
								$("#contact input#submit").fadeTo(300, 1.0, function() {
								})
							});
						}
					}
				});
			});
		})
		
	});
}
function showErrors(errors)
{
	errors = errors.split("|");
	var height = 0;
	
	if(errors[0]=="name")
	{
		$("#contact ul#name-errors li").text("Введите Ваше имя");
		height += $("#contact ul#name-errors").height();
	}
	
	if(errors[1]=="email-empty") {
		$("#contact ul#email-errors li").text("Введите Ваш email");
		height += $("#contact ul#email-errors").height();
	}
	else if(errors[1]=="email-wrong") {
		$("#contact ul#email-errors li").text("Вы ввели неверный email");
		height += $("#contact ul#email-errors").height();
	}
	
	if(errors[2]=="message")
	{
		$("#contact ul#message-errors li").text("Введите сообщение");
		height += $("#contact ul#message-errors").height();
	}
	
	$("#layout-top").css({height: expandedHeight+height+"px"});
	$("#top-info").css({height: topInfoStartHeight+height+"px"});
	$("#top-form").css({height: topFormStartHeight+height+"px"});
	
	if(errors[0]!="name-clear") {
		$("#contact #contact-edit-name").css({marginBottom: 18+$("#contact ul#name-errors").height()+"px"});
		$("#contact ul#name-errors").show();
	} else {
		$("#contact #contact-edit-name").css({marginBottom: 18+"px"});
		$("#contact ul#name-errors").hide();
	}
	
	if(errors[1]!="email-clear") {
		$("#contact #contact-edit-email").css({marginBottom: 18+$("#contact ul#email-errors").height()+"px"});
		$("#contact ul#email-errors").show();
	} else {
		$("#contact #contact-edit-email").css({marginBottom: 18+"px"});
		$("#contact ul#email-errors").hide();
	}
	
	if(errors[2]!="message-clear") {
		$("#contact #contact-edit-message").css({marginBottom: 23+$("#contact ul#message-errors").height()+"px"});
		$("#contact ul#message-errors").show();
	} else {
		$("#contact #contact-edit-message").css({marginBottom: 18+"px"});
		$("#contact ul#message-errors").hide();
	}
}
function cleanErrors()
{
	$("#layout-top").css({height: expandedHeight+"px"});
	$("#top-info").css({height: topInfoStartHeight+"px"});
	$("#top-form").css({height: topFormStartHeight+"px"});
	
	$("#contact #contact-edit-name").css({marginBottom: 18+"px"});
	$("#contact ul#name-errors").hide();
	$("#contact #contact-edit-email").css({marginBottom: 18+"px"});
	$("#contact ul#email-errors").hide();
	$("#contact #contact-edit-message").css({marginBottom: 18+"px"});
	$("#contact ul#message-errors").hide();
}
/*------------------------------------------------------------------
initiaization for the last works
------------------------------------------------------------------*/
function initLastWorks()
{
	var activeLastWork = 0;
	var maxLastWork = $("#lastworks > ul > li").length-3;
	
	$("#lastworks-navigation > a.right")
		.click(function(event) {
			event.preventDefault();
			event.stopPropagation();
			activeLastWork++;
			if(activeLastWork>maxLastWork) activeLastWork=maxLastWork;
			$("#lastworks > ul").stop().animate({ marginLeft: -(307*activeLastWork)+'px' }, 500, "easeOutBack");
		});
	$("#lastworks-navigation > a.left")
		.click(function(event) {
			event.preventDefault();
			event.stopPropagation();
			activeLastWork--;
			if(activeLastWork<0) activeLastWork=0;
			$("#lastworks > ul").stop().animate({ marginLeft: -(307*activeLastWork)+'px' }, 500, "easeOutBack");
		});
}
/*------------------------------------------------------------------
initiaization for the top link
------------------------------------------------------------------*/
function initTopLinks()
{
	$("a.top").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		$.scrollTo( 0, 500 );
	});
}
