/**
 * Custom jQuery code for SISTR
 * 
 * @author Stijn Van Minnebruggen
 * @website www.donotfold.be 
 * 
 * @copyright These Days
 * @website www.thesedays.com
 * 
 */

$(document).ready(function() {
	
	// png fix
		$(document).pngFix();
	
	// set standard easing
		jQuery.easing.def = "easeOutQuart";
	
	// start animation on the left navigation
		if($('body').hasClass('home')) {
			if($.support.opacity) $('#navigation').hide().delay(1000).fadeIn(1000);
			$('#navigation .wrapper').css({'marginLeft': '165px'}).delay(1000).animate({'marginLeft': '57px'}, {duration: 800, easing: 'easeOutBack'});
			$('#flap').css({'top': '-28px', 'left': '-28px'}).delay(1800).animate({'top': '-9px', 'left': '-11px'}, {duration: 800, easing: 'easeOutElastic'});
		} else {
			$('#flap').css({'top': '-28px', 'left': '-28px'}).delay(1000).animate({'top': '-9px', 'left': '-11px'}, {duration: 800, easing: 'easeOutElastic'});
		}
	
	// start animation on the twitter flap
		if($('body').hasClass('home')) {
			if($.support.opacity) $('#twitter').hide().delay(1500).fadeIn(1000);
			$('#twitter .wrapper').css({'marginLeft': '-90px'}).delay(1500).animate({'marginLeft': '-30px'}, {duration: 800, easing: 'easeOutBack'});
		}
	
	// start mini animation on the twitter balloon
		var twitAnim = function() {
			$('#twitter .wrapper p')
				.animate({'top': '22px'}, 500, 'easeOutQuad')
				.animate({'top': '24px'}, 500, 'easeInQuad')
				.animate({'top': '25px'}, 500, 'easeOutQuad')
				.animate({'top': '24px'}, 500, 'easeInQuad', function() { twitAnim(); });
		};
		setTimeout(twitAnim, 3000);
	
	// add twitter actions
		$('#twitter .wrapper').hover(function() {
			$('#twitter .wrapper p').show();
			$('#twitter .wrapper').animate({'marginLeft': '-25px'}, {duration: 300, easing: 'easeOutBack'});
		}, function() {
			$('#twitter .wrapper p').hide();
			$('#twitter .wrapper').animate({'marginLeft': '-30px'}, {duration: 300, easing: 'easeOutBack'});
		});
	
	// reposition the flaps
		$(window).scroll(function() { setFlapsY(); }).resize(function() { setFlapsY(); });
		setTimeout(function() { setFlapsY(); }, 500);
	
});

function setFlapsY() {
	
	// get vars
		var docH = $('#container').height();
		var winH = $(window).height();
		var scrollY = $(window).scrollTop();
	
	// set vars
		var navY = scrollY;
		var twitY = ((scrollY + winH) < (docH - 100)) ? scrollY + winH - 200 : docH - 300;
	
	// set styles
		$('#navigation').css('top', navY);
		$('#twitter').css('top', twitY);
	
}



/**
 * Delay plugin inspired by learningjquery.com/2007/01/effect-delay-trick
 * @author : Jp Siffert
 * @param :  delay interval, if not given default to 1000ms
 * @return : jquery chain object
 */

$.fn.delay = function(delay) {
	if(typeof delay === "undefined") delay = 1000;
	return this.animate({ 'void': 0 }, delay);
}
