//Making this baby sing

$(document).ready(function() {


	//Clouds continuously spin
	if ( !$.browser.webkit ) {
		setInterval(function() {
			$('#illi2 img').animate({rotate: '-=0.5deg'});
		}, 100);
	} else if ( $.browser.webkit ) {
		$('#illi2').animate({rotate: '-=36000deg'}, 72000000);
	}

	//Side carousels
	$("a.back").addClass('disabled');
	$("#newsScroll").jCarouselLite({
    	btnNext: "#newsScrollForward",
    	btnPrev: "#newsScrollBack",
    	vertical: true,
    	visible: 2,
    	circular: false
	});
	$("#twitter_div").jCarouselLite({
    	btnNext: "#twitterScrollForward",
    	btnPrev: "#twitterScrollBack",
    	vertical: true,
    	visible: 1,
    	circular: false
	});
	$("#caseStudies").jCarouselLite({
    	btnNext: "#csForward",
    	btnPrev: "#csBack",
    	vertical: true,
    	visible: 1,
    	circular: false
	});

	
	// ---- Magic Lever ---- //
	
	//IE browser sniff
	if ( $.browser.msie ) {
		$('#illi1').css('background-image','url(/new/images/illi1a.gif)');
	}
	
	//Fade globe in out
	var s = 0;
	function switchGlobe() {
		if ( s == 0 ) {
			$('#illi1').css('background-image','url(/new/images/illi1b.gif)');
			s++;
		} else if ( s == 1) {
			$('#illi1').css('background-image','url(/new/images/illi1c.gif)');
			s++;
		} else if ( s == 2 ) {
			$('#illi1').css('background-image','url(/new/images/illi1a.gif)');
			s = 0;	
		}
	}
	
	//Spin the world!
	function spinGlobe() { $('#illi1').animate({rotate: '+=120deg'}); };
	
	//Flip lever
	function flipLever() {
		$('#leaver').animate({backgroundPosition: '0 -228px'}, 1).animate({backgroundPosition: '0 0'}), 0;
	};
	
	var i = 0;
	function introBox(list) {
		if ( i == 2 ) {
			$('li.active', list)
				.fadeOut(300).removeClass('active');
				$('li:first', list).fadeIn(300).addClass('active');
				i = 0;	
				return false;
		} else {
				$('li.active', list)
	    		.fadeOut(300).removeClass('active').next('li').fadeIn(300).addClass('active');
				i++;	
				return false;
		}
	}
	
	//Shake the jamjar
	function shakeJar(newJar) {
		$(newJar).css('background-position','0 -162px')
		.animate({backgroundPosition: '0 -120px'}, 1)
		.animate({backgroundPosition: '0 -90px'}, 1)
		.animate({backgroundPosition: '0 -60px'}, 1)
		.animate({backgroundPosition: '0 -25px'}, 1)
		.animate({backgroundPosition: '0 0'}, 1)
		//.animate({backgroundPosition: '-=5px -162px'}, 100)
		.animate({rotate: '-=3deg'}, 150)
		//.animate({backgroundPosition: '+=5px -162px'}, 100)
		.animate({rotate: '+=6deg'}, 150)
		//.animate({backgroundPosition: '-=8px -162px'}, 100)
		.animate({rotate: '-=5deg'}, 150)
		//.animate({backgroundPosition: '+=6px -162px'}, 100)
		.animate({rotate: '+=4deg'}, 150)
		//.animate({backgroundPosition: '-=4px -162px'}, 100)
		.animate({rotate: '-=3deg'}, 100)
		//.animate({backgroundPosition: '+=1px -162px'}, 100)
		.animate({rotate: '+=2deg'}, 100)
		.animate({rotate: '-=1deg'}, 100)
	};
	
	//Throw that lever and watch the magic happen
	$('#leaver').click(function() {
		if ( $.browser.msie ) {
			switchGlobe();
		} else {
			spinGlobe();
		}
		flipLever();
		introBox('ul#introBox');
		shakeJar('ul#introBox li.active .dropJar');
	});
	

//End
});


