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

function stains() {
	if ($('#container').width() > $(window).width()) {
		$('#stains2').css('left', '1033px');
		$('#stains2').css('right', 'auto');
	}
	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');
	});
}

function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;

}


$(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);
	});
	$('.work .visu').mouseenter(function() {
		if ($(this).parent().attr('id') != 'work_visu') {
			$(this).find(':first').stop();
			$(this).find(':first').animate({
				marginTop: -186
			}, 250, 'easeOutQuad');
		}
	});
	$('.work .visu').mouseleave(function() {
		if ($(this).parent().attr('id') != 'work_visu') {
			$(this).find(':first').stop();
			$(this).find(':first').animate({
				marginTop: 0
			}, 250, 'easeInQuad');
		}
	});
	$('.work .fwa').css('cursor', 'pointer').click(function() {
		window.location.href = $(this).prev().attr('href');
	});
	$('.work .fwa').mouseenter(function() {
		if ($(this).parent().attr('id') != 'work_visu') {
			$(this).prev().find(':first').stop();
			$(this).prev().find(':first').animate({
				marginTop: -186
			}, 250, 'easeOutQuad');
		}
	});
	$('.work .fwa').mouseleave(function() {
		if ($(this).parent().attr('id') != 'work_visu') {
			$(this).prev().find(':first').stop();
			$(this).prev().find(':first').animate({
				marginTop: 0
			}, 250, 'easeInQuad');
		}
	});
	
	rollover($('.more'), 414, 429);
	
	$('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>');
			}
		}
	}
	
	if ($('body').attr('id') == 'home') {
		var austDay = new Date("April 1, 2011 00:00:00");
		$('#countdown strong').countdown({until: austDay, format: "DHMS", layout:"{dn}DAYS {hn}HOURS {mn}MIN {sn}SEC"});
		
		$.ajax({url: '/twitter/last', success: function(data) {
			$('#twitter p').html(data);
			heights();
		}});
		
		rollover($('#newsletter form input.image'), 444, 467);
		rollover($('#contact_social a#facebook'), 490, 523);
		rollover($('#contact_social a#twitter'), 556, 589);
		rollover($('#contact_social a#vimeo'), 622, 655);
		rollover($('#hiring'), 0, 14);
		rollover($('#next'), 0, 30);
		
	}
	
	// WORKS
	
	if ($('body').attr('id') == 'works' || $('body').attr('id') == 'work_details') {
	
		$('#work_visu').nivoSlider({
			effect: 'sliceUpDown',
			slices: 6,
			animSpeed: 500,
			pauseTime: 5000
		});
		
	}
	
	if ($('body').attr('id') == 'works') {
		$('.work_expand').css('height', 0);
		
		$('#all_works .work').mouseenter(function() {
			var expandable = $(this).find('.work_expand');
			expandable.stop();
			expandable.animate({
				height: 199
			}, 250);
		});
		
		$('#all_works .work').mouseleave(function() {
			var expandable = $(this).find('.work_expand');
			expandable.stop();
			expandable.animate({
				height: 0
			}, 250);
		});
		
		var url = $('#main_work').attr('href');
	
		$('#main_work').remove();
	
		$('.nivo-slice').css('cursor', 'pointer');
		$('.nivo-slice').click(function() {
			window.location.href = url;
		});
		
		setInterval('slideLogos()', 3000);
		
	}
	
	
	
	if ($('body').attr('id') == 'work_details') {
	
		rollover($('#visit'), 202, 234);
		rollover($('#watch_video'), 688, 720);
		rollover($('#works_pager .previous'), 900, 922);
		rollover($('#works_pager .next'), 944, 966);
		
	}
	
	// BLOG
	
	if ($('body').attr('id') == 'blog' || $('body').attr('id') == 'blog_post') {
	
		$('#blog_nav h4').click(function() {
			if ($(this).parent().attr('id') != 'blog_nav_section_search') {
				if ($(this).parent().hasClass('open')) {
					$(this).next().slideUp(250, function() {
						$(this).parent().removeClass('open');
						$(this).parent().addClass('close');
					});
				}
				else {
					$(this).next().slideDown(250, function() {
						$(this).parent().removeClass('close');
						$(this).parent().addClass('open');
					});
				}
			}
		});
		
		rollover($('.post_content .post_comment_link a'), 266, 286);
		rollover($('#blog_nav_section_search input.image'), 368, 391);
		
	}
	
	// BUZZ

	function adjustBackground() {
		var w = $(window).width();
		if (w < 1117) {
			$('body').css('background-position', '-141px 0');
		}
		else {
			$('body').css('background-position', 'center 0');
		}
	}
	
	if ($('body').attr('id') == 'buzz') {
		
		rollover($('#register-facebook'), 0, 49);
		
		adjustBackground();
		
		$(window).resize(function() {
			adjustBackground();
		});
		
		if (window.location.hash == '#thankyou') {
			$('#thankyou').css('display', 'inline-block');
		}
		
		$.get('/vip.php', function(data) {
			var reg = pad(data, 6);
			reg = reg.substr(0, 3)+'<span>.</span>'+reg.substr(3, 3);
			$('#buzz-be-our-guest-register div').html(reg);
		});
	}

	if ($('body').attr('id') == 'buzz-before-million') {
		
		$.get('/results.php', function(data) {
			var votes = data.split('|');
			var i = 0;
			$('.votes').each(function() {
				$(this).html(votes[i]+' votes');
				i++;
			});
		});
		
		rollover($('#buzz_box .box .go'), 0, 25);
		
		if (window.location.search === '?already') {
			alert('You have already voted! What a cheater!');
		}
		
		var austDay = new Date("April 1, 2011 00:00:00");
		$('#countdown strong').countdown({until: austDay, format: "DHMS", layout:"{dn}DAYS {hn}HOURS {mn}MIN {sn}SEC"});
		
	}
	
	// CONTACT 
	
	if ($('body').attr('id') == 'contact') {
		
		rollover($('#contact_map'), 132, 167);
		rollover($('#contact_email'), 306, 337);
		rollover($('#contact_social a#facebook'), 490, 523);
		rollover($('#contact_social a#twitter'), 556, 589);
		rollover($('#contact_social a#vimeo'), 622, 655);

	}
	
	// 404
	
	if ($('#too_bad').length > 0) {
		
		rollover ($('#too_bad a'), 50, 91);
		
	}
	
	// JOBS
	
	if ($('body').attr('id') == 'jobs') {
		
		rollover($('#apply'), 752, 793);
		
		rollover($('#share_social a#facebook'), 490, 523);
		rollover($('#share_social a#twitter'), 556, 589);
		rollover($('#share_social a#linkedin'), 834, 867);
		
		
		var url = window.location.href;
		
		$('#share_social a#facebook').click(function() {
			window.open('http://www.facebook.com/sharer.php?u='+url,'sharer','toolbar=0, status=0, resizable=1, width=626, height=436');
		});
		$('#share_social a#twitter').click(function() {
			window.open('http://twitter.com/home?status='+encodeURIComponent('1MD is hiring a Creative Web Developer! You? Apply now! Not you? Please RT! (via @1MD)')+' '+url);
		});
		$('#share_social a#linkedin').click(function() {
			window.open('http://www.linkedin.com/shareArticle?mini=true&url='+url+'&title='+encodeURIComponent('1MD is hiring a Creative Web Developer! You? Apply now!')+'&source=1md.be','sharer','toolbar=0,status=0,resizable=1,width=626,height=563');
		});
		
		
	}
	
});
