var 	div,
		disp = 4,
		bottom,
		mbottom,
		speed = "slow";

function anim(e)
{
	$(this).animate( { 'opacity': '+=0.'+disp }, speed  );
	$(this).animate( { 'opacity': '-=0.'+disp }, speed, anim );
};

function fuckinIE()
{
	bottom.each( function(e){ h=$(this).parent().height(); $(this).css({top:(h-h%2)+'px'}); });
	mbottom.each( function(e){ h=$(this).parent().height(); $(this).css({top:(h-h%2-70)+'px'}); });
}

$(document).ready(function() {

	bottom = $(".bottom");
	mbottom = $(".mainbottom");

	if( $('#mail').length>0 )
	{
		// Ажели какая спам-программа научится трактовать сия накрутку -- то по исходу десяти дней небо рухнет на головы окоянные наши
		var mail = 'info'+'@';
		mail += 'fordot.com.ua';
		$('#mail').eq(0).html( mail );
		$('#mail').attr( 'href', 'mailto:' + mail );
	}

	toggle = function(e){ 
		var center = $('#center');
		if( jQuery.browser.msie )
			center.toggle();
		else
			if( center.data('show')!=false )
				center.css({display:'block', opacity:0}).animate({opacity:1},speed).data('show',false);
			else
				center.animate({opacity:0},speed,function(e){$(this).css('display','none')}).data('show',true);
		return false;
	}		

	$('#stripe_link').click( toggle );
	
	$(document.createElement('div')).appendTo('body').attr('id','center').load('/addToStripe'+'.'+'html',function(e){
		$(this).find('a').click( toggle );
		$(this).find('input:submit').click( function(){
			if($('input[name=name]').val().length==0) 
			{ 
				$('input[name=name]').val('Ананимус').focus().select(); 
				return false; 
			};
			if($('textarea[name=txt]').val().length==0) 
			{ 
				$('textarea[name=txt]').val('Ничего не скажу').focus().select(); 
				return false; 
			}
		});
	});
	
	$(".img img").hover(
		function (e) { $(this).animate( { 'opacity': '-=0.'+disp }, speed, anim ); },
		function (e) { $(this).stop().stop().css( { 'opacity':'0.99' } ); }
	);
	
	$(".portfolio_page img").hover(
		function (e) { $(this).animate( { 'opacity': '-=0.'+disp }, speed, anim ); },
		function (e) { $(this).stop().stop().css( { 'opacity':'1' } ); }
	);
	
	$(".portfoliocontent ul a").click(
	  function (e)
		{
			if( !($(this).parent().is(".here")) )
			{
				$.ajax({
					url: $(this).attr('href')+'short/',
					beforeSend: function(){
						$(".portfoliocontent .here").removeClass("here");
						if( $.browser.msie && parseInt($.browser.version) < 7 )
						{
							$("#pageinfo").css({height: ($("#pageinfo").height()+30)+'px'}).addClass('loading');
							$("#pageinfo").html('');
						}
						else
						{
							$("#pageinfo").css({height: ($("#pageinfo").height())+'px'}).addClass('loading');
							$("#pageinfo").children().animate({opacity:'0'},speed); 
						}},
					success: function(answ){
						$("#pageinfo").html('');
						var div_elem = $(document.createElement('div'));
						div_elem.html(answ).css({position:'relative',visibility:'hidden'}).appendTo('#pageinfo');
						show = function(e){
							if( jQuery.browser.msie )
							{
								$("#pageinfo").css({height: 'auto'});
								div_elem.css({visibility:'visible'});
								fuckinIE();
							}
							else
							{
								div_elem.css({visibility:'visible',opacity:0}).animate({opacity:'1'},speed);
								$("#pageinfo").animate({height: (div_elem.height()+30)+'px'},speed);
							}
						};
						if( div_elem.find("img").length != 0 )
							div_elem.find("img").load(show);
						else show;
					}
				});
				$(this).parent().addClass("here");
				
				s = $(".portfoliocontent .here").nextAll().eq(0);
				if( s.length == 0 ) s = $(".portfoliocontent li:first-child");
				$("#next").attr('href', s.find('a').attr('href'));
				
				s = $(".portfoliocontent .here").prevAll().eq(0);
				if( s.length == 0 ) s = $(".portfoliocontent li:last-child");
				$("#prev").attr('href', s.find('a').attr('href'));
			}
			return false;
	  }
	);
	
	$("#next").click( function (e) {
		s = $(".portfoliocontent .here").nextAll().eq(0);
		if( s.length == 0 ) s = $(".portfoliocontent li:first-child");
		s.find('a').trigger("click");
		return false;
	});
	
	$("#prev").click( function (e) {
		s = $(".portfoliocontent .here").prevAll().eq(0);
		if( s.length == 0 ) s = $(".portfoliocontent li:last-child");
		s.find('a').trigger("click");
		return false;
	});
});