function adjustScroll() {
	var biggest = ($('#page').height() > $(window).height()) ? $('#page') : $(window);
	if (biggest.height() < 1009) {
		var diff = 1009 - biggest.height();
		$('#stains1').css('height', 859-diff);
	}
}

function stains() {
	if ($('#page').width() > $(window).width()) {
		$('#stains1').css('height', 0);
		$('#stains2').css('height', 0);
	}
	else {
		$('#stains2').css('right', '0');
		$('#stains2').css('left', 'auto');
	}
}

function slideLogos() {
	$('#clients_inner').animate({
		marginLeft: '-=105'
	}, 250, function() {
		var first = $('#clients_inner img:first');
		$('#clients_inner').append(first);
		$(this).css('margin-left', 0);
	});
}

function rollover(target, from, to) {
	target.mouseenter(function() {
		$(this).stop();
		$(this).animate({
			backgroundPosition: '0 -'+to+'px'
		}, 100, 'easeOutQuad');
	});
	target.mouseleave(function() {
		$(this).stop();
		$(this).animate({
			backgroundPosition: '0 -'+from+'px'
		}, 100, 'easeInQuad');
	});
}

$(document).ready(function() {
	
	// COMMON
	
	$('nav a').mouseenter(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: $(this).width()
		}, 250);
	});
	$('nav a').mouseleave(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: 0
		}, 250);
	});
	$('nav_vault a').mouseenter(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: $(this).width()
		}, 250);
	});
	$('nav_vault a').mouseleave(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: 0
		}, 250);
	});
		$('nav_language a').mouseenter(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: $(this).width()
		}, 250);
	});
	$('nav_language a').mouseleave(function() {
		var span = $(this).find('span');
		span.stop();
		span.animate({
			width: 0
		}, 250);
	});
	$('.visu').mouseenter(function() {
		if ($(this).parent().attr('id') != 'pics_visu' || $(this).parent().attr('id') != 'pics_visu_small' || $(this).parent().attr('id') != 'vault_visu' || $(this).parent().attr('id') != 'main_visu') {
			$(this).find(':first').stop();
			$(this).find(':first').animate({
				marginTop: -186
			}, 250, 'easeOutQuad');
		}
	});
	$('.visu').mouseleave(function() {
		if ($(this).parent().attr('id') != 'pics_visu' || $(this).parent().attr('id') != 'pics_visu_small' || $(this).parent().attr('id') != 'vault_visu' || $(this).parent().attr('id') != 'main_visu') {
			$(this).find(':first').stop();
			$(this).find(':first').animate({
				marginTop: 0
			}, 250, 'easeInQuad');
		}
	});
	
	rollover($('.more'), 199, 214);
	rollover($('#newsletter form input.image'), 444, 467);
	rollover($('#contact_social a#facebook'), 1, 34);
	rollover($('#contact_social a#twitter'), 67, 100);
	rollover($('#contact_social a#youtube'), 133, 166);
	rollover($('#contact_social a#home'), 400, 433);
	rollover($('.post_comment_link'), 229, 249);
	rollover($('.goforit'), 269, 294);
	rollover ($('.gobackhome'), 319, 360);
	rollover ($('.download'), 400, 440);
	rollover($('.goforitposts'), 480,520);	
	
	if ($('body').attr('id') != 'posts' & $('body').attr('id') != 'main'){
		$('body').append('<div id="stains1"></div>');
		$('body').append('<div id="stains2"></div>');
		$('body').append('<div id="stains3"></div>');
	}
	adjustScroll();
	stains();
	
	$(window).resize(function() {
		adjustScroll();
		stains();
	});
	
	// HOME
	
	function heights() {
		var h = new Array();
		var ids = ['hot_from_the_blog', 'other_news_counter', 'misc'];
		
		h[0] = $('#'+ids[0]).height();
		h[1] = $('#'+ids[1]).height();
		h[2] = $('#'+ids[2]).height()+22;
		
		var biggest = (h[0] >= h[1]) ? 0 : 1;
		biggest = (h[2] >= h[biggest]) ? 2 : biggest;
		
		for (var i=0; i<3; i++) {
			if (i != biggest) {
				var bh = h[biggest] - h[i] - 3;
				$('#'+ids[i]).append('<div style="background:#FFF;height:'+bh+'px;margin-top:3px;"></div>');
			}
		}
	}
	 
	heights();
	
	if ($('body').attr('id') == 'main') {
	
		$('.main_visu').nivoSlider({
			effect: 'fade',
			slices: 6,
			animSpeed: 500,
			pauseTime: 2500
		});
	}
	

	if ($('body').attr('id') == 'home') {
	
		$('.pics_visu').nivoSlider({
			effect: 'sliceUpDown',
			slices: 6,
			animSpeed: 500,
			pauseTime: 2500
		});
		$('.pics_visu_small').nivoSlider({
			effect: 'sliceUpDown',
			slices: 6,
			animSpeed: 500,
			pauseTime: 2500
		});	

	}
	
	// POSTS
	
	if ($('body').attr('id') == 'posts') {
		 $('.visu_expand').css('height', 0);
		 
			$('.vault_visu').nivoSlider({
			effect: 'fade',
			slices: 6,
			animSpeed: 500,
			pauseTime: 2500
		});
	}

	
	// VAULT
	
	if ($('body').attr('id') == 'vault') {
		 $('.visu_expand').css('height', 0);
		 
			$('.vault_visu').nivoSlider({
			effect: 'fade',
			slices: 6,
			animSpeed: 500,
			pauseTime: 2500
		});
		
		$('.vaultbox').mouseenter(function() {
			var expandable = $(this).find('.visu_expand');
			expandable.stop();
			expandable.animate({
				height: 199
			}, 250);
		});
		
		$('.vaultbox').mouseleave(function() {
			var expandable = $(this).find('.visu_expand');
			expandable.stop();
			expandable.animate({
				height: 0
			}, 250);
		});
	}
	
	// 404
	
	if ($('#too_bad').length > 0) {
		
		rollover ($('#too_bad a'), 50, 91);
		
	}
	
});
