$(document).ready(function(){

	// news mouse over/out
	$('#home-news a.news-title, #home-news .news-summary a').hover(function() {
		$(this).parents('li').addClass('news-over');
	}, function() {
		$(this).parents('li').removeClass('news-over');
	});
	
	var home_carousel = {
	
		// heros container
		container: null,
		
		// current hero index
		current_index: 0,
		// number of heros
		heros_max: 5,
		
		// array of heros, they should have class="ivillage" and be saved inside templates/homepage_heros/ as ivillage.html
		heros: [
		    { id: 'pepsi', img: '/static/imgs/homepage/heros/image-pepsi.png', url:'/heros/pepsi/' },
		    //{ id: 'one', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-one.png', url:'/heros/one/' },
		    //{ id: 'history', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-history.png', url:'/heros/history/' },
		    { id: 'ikea', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-one.png', url:'/heros/ikea/' },
		    { id: 'jetblue', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-jetblue.png', url:'/heros/jetblue' },
		    { id: 'ivillage', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-ivillage.jpg', url:'/heros/ivillage/' },
		    //{ id: 'adultswim', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-adultswim.png', url:'/heros/adultswim/' },
		    //{ id: 'nutrisystem', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-nutrisystem.png', url:'/heros/nutrisystem/' },
		    { id: 'cnn', img: 'http://static.hugeinc.com/imgs/homepage/heros/cnn.png', url:'/heros/cnn/' }
		    //{ id: 'broadway', img: 'http://static.hugeinc.com/imgs/homepage/heros/image-broadway.png', url:'/heros/broadway/', fixIE: true }
		    // if the hero should go above the news are, like the broadway hero, we need fixIE: true to fix the zIndex position 
		],
		
		// fix zIndex position for IE6
		fixIE: function(hero) {
			if(hero.fixIE) { 
				this.container.css({zIndex:10050});
			} else {
				this.container.css({zIndex:999});
			}
		},
		
		// check if hero is loaded, if not, load it
		checkHero: function(hero) {
			var div = $('div.'+hero.id);
			var len = div.length;
			this.fixIE(hero);
			if(len < 1) {
				// hide all heros
				$('div.home-hero').hide();
				this.container.addClass('loading');
				// load new hero
				$.get(hero.url, function(data){
					// preload image before show content
					var img = new Image();
  					$(img).load(function () {
      					$(this).hide();
						home_carousel.container.append(data).removeClass('loading');
						$('div.home-hero').hide();
						$('div.home-hero:last').show();
    				}).attr('src', hero.img);
				});			
			} else {
				// hide other heros and show active one
				$('div.home-hero').hide();
				div.show();
			}
		},

		// previous function
		prev: function() {
			this.current_index--;
			if(this.current_index < 0) this.current_index = this.heros_max-1;
			this.checkHero( this.heros[this.current_index] );
		},
			
		// next functions	
		next: function() {
			this.current_index++;
			this.current_index = this.current_index%this.heros_max;
			this.checkHero( this.heros[this.current_index] );
		},

		init: function() {
		
			// add nex/prev click events
			$('#home-hero-navigation a.next').click(function(e) {
				e.preventDefault();
				home_carousel.next();
			});
			$('#home-hero-navigation a.previous').click(function(e) {
				e.preventDefault();
				home_carousel.prev();
			});
			
			// get current loaded hero
			var current_hero = $('div.home-hero-content:first div');
			var current_hero_class = current_hero.get(0).className;
			var temp_heros = [];
			temp_heros.push('');
			// recreate the heros array according to the loaded hero ()
			for(var i=0,len=this.heros.length;i<len;i++) {
				var hero = this.heros[i];
				if(current_hero_class.indexOf(hero.id) > -1) temp_heros[0] = hero;
				else temp_heros.push(hero);
			}
			this.heros = null;
			this.heros = temp_heros;
			this.heros_max = this.heros.length;
			this.container = $('#home-hero-wrapper');
			this.fixIE(this.heros[0]);
		}
	
	};
	
	// begin home carousel
	home_carousel.init();
	
});
