
/* - homeFeatures.js - */
/**
 * jQuery Shuffle (http://mktgdept.com/jquery-shuffle)
 * A jQuery plugin for shuffling a set of elements
 *
 * v0.0.1 - 13 November 2009
 *
 * Copyright (c) 2009 Chad Smith (http://twitter.com/chadsmith)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
 *
 * Shuffle elements using: $(selector).shuffleit() or $.shuffleit(selector)
 *
 **/
(function(d){d.fn.shuffleit=function(c){c=[];return 
this.each(function(){c.push(d(this).clone(true))}).each(function(a,b){d(b).replaceWith(c[a=Math.floor(Math.random()*c.length)]);c.splice(a,1)})};d.shuffleit=function(a){return 
d(a).shuffleit()}})(jQuery);


/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);	




$(document).ready(function() {

        /* shuffle the features */
        //$('#featuresBox').shuffle();
        $('div.homeFeature').shuffleit();
 
        /* shuffle the badges */
        //$('#badgeBox').shuffle();
        $('a.badge').shuffleit();
	
	/* show the navigation for the features hidden from non-Javascript borwsers */
	$('.noJS').css('display','block');
	
	var curFeature = 0;
	$('.homeFeature').eq(0).addClass('current');
	
	/* Color switching */
	/* Set the initial color based on the first feature */
	$('#featuresBox').addClass($('.current').attr('rel'));
	
	/* functions to change color */
	function setColorClass() {
		$('#featuresBox').addClass($('.animating').attr('rel'));
	}
	function removeColorClass() {
		$('#featuresBox').removeClass($('.current').attr('rel'));
	}
	
	var numFeatures = ($(".homeFeature").size());
	
	/* set the padding on the featuresNav to center */
	$('#featuresNav').css('padding-left', 90-(numFeatures*12));

        /* Keep the featuresNav above the homeFeatures */
        $('#featuresNav').css("zIndex","101");
     	
	// Build the anchor divs inside featuresNav
	for (i=1;i<=numFeatures;i++) {
		$('#featuresNav').append('<a class="featureLink" id="'+i+'">'+i+'</a>');
	}
	
	/* set the initial selected link */
	$('.featureLink').eq(curFeature).addClass('selectedFeatureLink');
	
	// create a click function that returns the id of the clicked feature link
	// on all featureLinks except the selected one
	function bindFeatureLinks() {
	$(".featureLink:not(.selectedFeatureLink)").click(function(){
   		var clickedFeature = $(this).attr("id");
   		featureChange(clickedFeature);
	});
	}
	
	bindFeatureLinks();
	
	
	// Load all home features behind current
	$(".homeFeature").css("display","block");
	$(".homeFeature:not(.current)").css("zIndex","-500");
	/* move non-current images off to the right */
	$(".homeFeature:not(.current) .featureImgLink").css("left","640px");
	
	
	function featureChange(featureNum) {
		$(".featureLink").unbind('click');
		//alert(featureNum);
		var indexNew = featureNum-1;
		var newFeature = $('.homeFeature').eq(indexNew);
		newFeature.addClass('animating');
		newFeature.css('zIndex','100');
		$('.current').css('zIndex','75');
		removeColorClass();
		$('.featureLink').removeClass('selectedFeatureLink');
		setColorClass();
		$('.featureLink').eq(indexNew).addClass('selectedFeatureLink');
		
		$('.animating .featureImgLink').animate({"left": "-=640px"}, 500);
		$('.current .featureImgLink').animate({left: '-=640'}, 500, function() {
			$('.current').css('zIndex','-500');
			$('.current .featureImgLink').css('left','640px');
			$('.homeFeature').removeClass('current');
			newFeature.addClass('current');
			$('.homeFeature').removeClass('animating');
			bindFeatureLinks();
  		});
  		//$(".homeFeature:not(.current)").css({"zIndex": "-500"});
	}
/* Badges */

	var numBadges = ($(".badge").size());
	
	/* set the padding on the badgeNav to center */
	$('#badgeNav').css('padding-left', 90-(numBadges*12));
	
	var curBadge = 0;
	$('.badge').eq(0).addClass('curbadge');
	
	/* Color switching */
	/* Set the initial color based on the first badge */
	$('#badgeBox').addClass($('.curbadge').attr('rel'));
	
	/* functions to change color */
	function setColorBadge() {
		$('#badgeBox').addClass($('.fading').attr('rel'));
	}
	function removeColorBadge() {
		$('#badgeBox').removeClass($('.curbadge').attr('rel'));
	}
	
	
	// Build the badge divs inside badgeNav
	for (i=1;i<=numBadges;i++) {
		$('#badgeNav').append('<a class="badgeLink" id="'+i+'">'+i+'</a>');
	}
	
	/* set the initial selected link */
	$('.badgeLink').eq(curBadge).addClass('selectedBadgeLink');
	
	// create a click function that returns the id of the clicked badge link
	// on all badgeLinks except the selected one
	function bindBadgeLinks() {
	$(".badgeLink:not(.selectedBadgeLink)").click(function(){
   		var clickedBadge = $(this).attr("id");
   		badgeChange(clickedBadge);
	});
	}
	
	bindBadgeLinks();
	
	// Load all badges behind current and change opacity
	$(".badge").css("display","block");
	$('.badge').removeClass('hidden');
	$(".badge:not(.curbadge)").fadeTo(0,0);
	$(".curbadge").css('z-index','100');
	$(".badge:not(.curbadge)").css('z-index','0');
	
	
	function badgeChange(badgeNum) {
		$(".badgeLink").unbind('click');
		var indexNew = badgeNum-1;
		var newBadge = $('.badge').eq(indexNew);
		newBadge.addClass('fading');
		removeColorBadge();
		$('.badgeLink').removeClass('selectedBadgeLink');
		setColorBadge();
		$('.badgeLink').eq(indexNew).addClass('selectedBadgeLink');
		$(".curbadge").css('z-index','0');
		newBadge.css('z-index','100');
		$('.fading').fadeTo('slow',1);
		$('.curbadge').fadeTo('slow', 0, function() {
			$('.badge').removeClass('curbadge');
			newBadge.addClass('curbadge');			
			$('.badge').removeClass('fading');
			bindBadgeLinks();
  		});
  		
	}
	
}); /* /document ready */



