﻿var speed=400; // задаем скорость перемотки 
var minsize=0.5; // задаем процент высоты при которой мы будем добавлять еще один пост твитера
				// если не хочешь чтобы была пустая область когда не вмещается последний пост можешь закомментировать 111 строку (выделена слешами)
$(document).ready(function(){
	Cufon.replace($('#menu span'),  {fontFamily: 'HelveticaNeueLTPro'});
	Cufon.replace($('#menu .sub_menu a'),  {fontFamily: 'Helvetica Narrow', hover: true});
	Cufon.replace($('#discover_ergo h6'),  {fontFamily: 'Lobster 1.4'});
	Cufon.replace($('#discover_ergo .text'),  {fontFamily: 'HelveticaNeueLTPro'});
	Cufon.replace($('#discover_ergo .button'),  {fontFamily: 'HelveticaNeueLTPro'});
	Cufon.replace($('#posts .item .date'),  {fontFamily: 'HelveticaNeueLTPro', textShadow: "0px 1px 0 #fff"});
	Cufon.replace($('footer .top a'),  {fontFamily: 'Lobster 1.4'});
	Cufon.replace($('a.open_close'),  {fontFamily: 'Helvetica Narrow'});
	Cufon.replace($('#grey_section .main_part article .contact_block span'),  {fontFamily: 'Helvetica Narrow'});
	Cufon.replace($('#grey_section .main_part article .contact_block strong'),  {fontFamily: 'Helvetica Narrow'});
	$('#menu ul li.blue:first').addClass('first');
	$('#menu a.parent').mouseover(function(){ Cufon.replace($(this).find('.main'), {fontFamily: 'HelveticaNeueLTPro', textShadow: "-1px -1px 5px rgba(255, 255, 190, 0.7)"});});
	$('#menu a.parent').mouseout(function(){ Cufon.replace($(this).find('.main'), {fontFamily: 'HelveticaNeueLTPro'});});
	
	$('#discover_ergo .link_sticker').mouseover(function(){ Cufon.replace($(this).parent().find('.button'), {fontFamily: 'HelveticaNeueLTPro', color: "#eb4e00"});});
	$('#discover_ergo .link_sticker').mouseout(function(){ Cufon.replace($(this).parent().find('.button'), {fontFamily: 'HelveticaNeueLTPro', color: "#3776aa"});});
	$('#discover_ergo .go_back').click(function(){
		var id=$(this).attr('href');		
		$(this).parents(".inner_html").eq(0).parent().animate({top: -290},  speed, "linear", function(){ $(this).addClass('hide')});
		$('#discover_ergo .main_header h6').animate({top: -46},  speed, "linear", function(){ $(this).text($(id).find('h6').text()).css("top","46px"); Cufon.replace($(this), {fontFamily: 'Lobster 1.4'}); $(this).animate({top: 0},  100, "linear");});
		$(id).css("top","290px").removeClass("hide");
		$(id).animate({top: 0},  speed, "linear");				
		return false;
	});
	
	$('#discover_ergo .link_sticker').click(function(){
		var id=$(this).attr('href');		
		$(this).parents(".inner_html").eq(0).parent().animate({top: 290},  speed, "linear", function(){ $(this).addClass('hide')});
		$('#discover_ergo .main_header h6').animate({top: 46},  speed, "linear", function(){ $(this).text($(id).find('h6').text()).css("top","-46px"); Cufon.replace($(this), {fontFamily: 'Lobster 1.4'}); $(this).animate({top: 0},  100, "linear");});
		$(id).css("top","-290px").removeClass("hide");
		$(id).animate({top: 0},  speed, "linear");				
		return false;
	});
	
	
	//event  click .open_close
	$('a.open_close').click(function(){		
		var button=$(this);
		if($(this).hasClass('open'))
		{
				$("#content_discover").removeClass('hide');
			    $("#content_discover").stop().animate({ height:280 }, 200, "linear", function(){
					button.removeClass('open');
					button.addClass('close');
					button.text('Close');
					Cufon.replace(button,  {fontFamily: 'Helvetica Narrow'});
					setTwitterHeight();				
			});
		}
		else
		{			
			$("#content_discover").stop().animate({ height:0 }, 200, "linear", function(){
				$("#content_discover").addClass('hide');
				button.removeClass('close');
				button.addClass('open');
				button.text('Discover Ergo');
				Cufon.replace(button,  {fontFamily: 'Helvetica Narrow'});
				setTwitterHeight();				
			});
		}		
		return false;
	});

	$('a.open_close').trigger('click');
	$('#posts .item').mouseover(function(){ Cufon.replace($(this).find('.date'), {fontFamily: 'HelveticaNeueLTPro', textShadow: "0px 1px 0 #fff", color: "#eb4e00"});});
	$('#posts .item ').mouseout(function(){ Cufon.replace($(this).find('.date'), {fontFamily: 'HelveticaNeueLTPro', textShadow: "0px 1px 0 #fff", color: "#555555"});});
	$('#posts .item:first').addClass('first');
	
	$('#menu .sub_menu').each(
		function(){
			var width_=0;
			var item=$(this).find('.item');
			width_=item.size()*127;
			$(this).width(width_);
			var pos_parent = $(this).parent('li').position().left;
			if(width_>0)
			{
		    	var offset=(width_+30-136)/2;
				if(offset>pos_parent)
					$(this).css({'left':-pos_parent});
				else	
					if(offset>(960-pos_parent-136))
					{
						$(this).css({'left':-(width_-136+30)});
					}
					else
						$(this).css({'left': -offset});
			}
					
	});
	
	function setTwitterHeight(){
		//set height twitter block			
		$('#twitter .content .item').removeClass('show');
		$("#calculator").css('marginTop',"0px");
		var height_twitter=$('#grey_section .main_part').height()-parseInt($('#grey_section .right_part h3').css('paddingTop'))- parseInt($('#grey_section .right_part h3').css('paddingBottom'))- $('#grey_section .right_part h3').height()-parseInt($('#twitter').css('paddingTop'))-parseInt($('#twitter .top').css('paddingTop')) -parseInt($('#twitter .bottom').css('paddingBottom'))-15;	
		var i=0, height=-5, count;		
		$('#twitter .content .item').eq(i).addClass('show');	
		while(((height+($('#twitter .content .item').eq(i).height()+16)*minsize<height_twitter)||i<3)&&i<$('#twitter .content .item').size())
		{
			height=height+$('#twitter .content .item').eq(i).height()+16;						
			$('#twitter .content .item').eq(i).removeClass('show');
			i++;
			$('#twitter .content .item').eq(i).addClass('show');
		}	
		$('#twitter .content .item').eq(i).removeClass('show');		
		count=i;
		/////////////////////////////////////////////////
		if(height_twitter>height) 
			height=height_twitter;
		else
			$("#calculator").css('marginTop',parseInt(height-height_twitter+count*2)+"px");
		/////////////////////////////////////////////////
		$('#twitter .content').stop().animate( { height: height } , 200,"linear", function(){if($.browser.msie && $.browser.version <= 8) boxShadowIE();});	
		for(i=0; i<count && i<$('#twitter .content .item').size(); i++)
		{
			$('#twitter .content .item').eq(i).addClass('show');			
		}
	};
	$('#twitter .content .item:first').addClass('first');
	
	window.onload=setTwitterHeight;
	
	// gallery work bind (carousel & lightBox)
	$("#work_gallery .gallery").jCarouselLite({
        btnNext: "#work_gallery .prev",
        btnPrev: "#work_gallery .next",
		visible: 1,
		scroll: 1
    });
	//$('#work_gallery .gallery a').lightBox({fixedNavigation:false});
	

	function boxShadowIE()
	{		
		$('img.shadow').each(function(){
			if($(this).attr('align'))
				$(this).css('float',$(this).attr('align'));});
		$('img.shadow').each(function(){
			if($(this).next().css('z-index')==-1)								
				$(this).next().remove();
				$(this).boxShadow('0 0 15px #aaa');});
		$('span.img_cover').each(function(){
			if($(this).next().css('z-index')==-1)
				$(this).next().remove();
				$(this).boxShadow('0 0 15px #aaa');});
			
	}
	
	if($.browser.msie && $.browser.version <= 8)
	{
		$('#menu .sub_menu a').mouseover(function(){ $(this).css('border-bottom-style','solid') });
		$('#menu .sub_menu a').mouseout(function(){ $(this).css('border-bottom-style','none') });
		//boxShadowIE();
	}
	

});
