/**
 * @author Stéphane Roucheray 
 * @extends jquery
 */


jQuery.fn.carousel = function(previous, next, options){
	var sliderList = jQuery(this).children()[0];
	
	if (sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		
		for (i = 0; i < shownInViewport; i++) {
			jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
		}
		
		jQuery(previous).click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort == 1) {
					jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
					firstElementOnViewPort = numElmts;
				}
				else {
					firstElementOnViewPort--;
				}
				
				jQuery(sliderList).animate({
					left: "+=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
			
		});
		
		jQuery(next).click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 2;
					jQuery(sliderList).css('left', "0px");
				}
				else {
					firstElementOnViewPort++;
				}
				jQuery(sliderList).animate({
					left: "-=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
		});
	}
};

jQuery(function(){
	//Define a function to hack the footer to fill the rest of the page.
	var viewport = {}, footer = {}, $window = jQuery(window), $footer = jQuery('#footerwrap');
	var adjust_footer = function () {
		viewport.height = $window.height();
		footer.y = $footer.offset().top;
		footer.height = viewport.height - footer.y;
		if (footer.height > 272) {
			$footer.css('height', footer.height);
		} else {
			$footer.css('height', '272px');
		}
	};
	//Bind the function to the window.resize and document.ready events.
	$window.resize(adjust_footer);
	//Fire it manually aswell to make sure we resize it as soon as possible.
	adjust_footer();

	//initialize slider
	var $slider = jQuery('#splashwrap');
	if ($slider.length) {
		$slider.carousel('#slide_previous', '#slide_next');
	}

	//The auto-scrolling function
	function slide(){
		jQuery('#slide_next').click();
	}
	//Launch the scroll every 5 seconds
	var intervalId = window.setInterval(slide, 5000);

	//On user click deactivate auto-scrolling
	jQuery('#slide_previous, #slide_next').click(function(event){
		if(event.originalEvent){
			window.clearInterval(intervalId);
		}
	});

});

var hatchandbloom = {
	scroll: {
		domElm: document.getElementById('background'),
		initialize: function() {
			if (lightCore.browser.getName() == 'IE' && lightCore.browser.getVersion() <= 6) {
				var me = this;
				window.onscroll = function() {
					me.browserScrolled();
				};
			}
		},
		browserScrolled: function() {
			this.domElm.style.top = document.documentElement.scrollTop+'px';
		}
	}
};

lightCore.registerInit(hatchandbloom.scroll);


