/*
	Name: home-scripts.js
	Date: March 2010
	Description: Homepage Scripts for Chaudigital.
	Version: 1.0
	Coder: Enrique Ramirez
	Coder URI: http://enrique-ramirez.com
*/

/* Setting NoConflict mode */
var $j = jQuery.noConflict();

$j(document).ready(function(){
	
	/* Home Slides
	-----------------------------------------------*/
	// Adding Buttons
	$j("#home-slides").prepend('<span class="left"></span><span class="right"></span>');
	
	// Adding Wrapper
	$j("#home-slides .slides").wrap('<div id="slider-wrap" />');
	
	// Adding some CSS
	$j("#home-slides .slides").css('position','absolute');
	
	// Getting total width
	var totalWidth = 0;
	$j("#home-slides .slide").each(function() {
		totalWidth += $j(this).outerWidth(true);
	});
	
	// Setting total width to list
	$j("#home-slides .slides").width(totalWidth);
	
	// Defining first slide as active
	$j("#home-slides .slide:first-child").addClass('active');
	
	/* Sliding
	*****************************************/
	// Right Button
	$j("#home-slides .right").live('click', function() {
		var checkCurrent = $j("#home-slides .active");
		
		// If it's the last slide
		if(checkCurrent.is(':last-child')){
			// Setting new active slide
			$j("#home-slides .active").removeClass('active');
			$j("#home-slides .slide:first-child").addClass('active');
			
			// Animating
			$j("#home-slides .slides").stop().animate({marginLeft: '0'}, 1000, 'easeOutCubic');
		} else {
			// Getting position
			var position = $j("#home-slides .active").next('.slide').position().left;
			var nextSlide = $j("#home-slides .active").next('.slide');
		
			// Setting new active slide
			$j("#home-slides .active").removeClass('active');
			$j(nextSlide).addClass('active');
		
			// Animating
			$j("#home-slides .slides").stop().animate({marginLeft: -position}, 1000, 'easeOutCubic');
		}
	});
	
	// Left Button
	$j("#home-slides .left").live('click', function() {
		var checkCurrent = $j("#home-slides .active");
		
		// If it's the first slide
		if(checkCurrent.is(':first-child')){
			var position = $j("#home-slides .slide:last-child").position().left;
			
			// Setting new active slide
			$j("#home-slides .active").removeClass('active');
			$j("#home-slides .slide:last-child").addClass('active');
			
			// Animating
			$j("#home-slides .slides").stop().animate({marginLeft: -position}, 1000, 'easeOutCubic');
			
		} else {
			// Getting position
			var position = $j("#home-slides .active").prev('.slide').position().left;
			var nextSlide = $j("#home-slides .active").prev('.slide');
		
			// Setting new active slide
			$j("#home-slides .active").removeClass('active');
			$j(nextSlide).addClass('active');
		
			// Animating
			$j("#home-slides .slides").stop().animate({marginLeft: -position}, 1000, 'easeOutCubic');
		}
	});
	
	/* Sliding through arrow keys
	*****************************************/
	function checkKey(e){
		switch (e.keyCode) {
			// Pressing Left Arrow Key:
			case 37:
				$j("#home-slides .left").click();
				break;

			// Pressing Right Arrow Key
			case 39:
				$j("#home-slides .right").click();
				break;
		}     
	}

	// Now triggering it all
	$j(document).keydown(checkKey);

	// Preventing event to launch on inputs/textareas/selects
	$j("input").focus(function() {$j(document).unbind('keydown');}).blur(function() {$j(document).keydown(checkKey);});
	$j("textarea").focus(function() {$j(document).unbind('keydown');}).blur(function() {$j(document).keydown(checkKey);});
	$j("select").focus(function() {$j(document).unbind('keydown');}).blur(function() {$j(document).keydown(checkKey);});
});
