$(document).ready(function() {
	carousel.init();
});

carousel = {
	
	ROTATION_TIME: 3500,
	
	intID: null,
	
	openedFromCarousel: false,
	
	instance: null,
  
	static_url: POKE.url_maps.static_url(),
  
	init: function() {;
		
		var buttons = Array();
		var total = $('#home_carousel_nav').find('li').length;
		for(var i = 0; i < total; i++) {
			buttons.push("#home_carousel_nav ."+(i+1));
		}
		
		var nextID = 1;
		var manualNav = false;
		
		$('#home_carousel_nav').find('a').click(function() {
			nextID = $(this).attr('class');
			manualNav = true;
		});
		
		/**
		 * IE6 HACKS!
		 */
		$('#home_carousel_nav').find('a:first').css('background', 'url(' + carousel.static_url + 'images/carousel_nav_on.png) no-repeat').addClass('on').supersleight();
		
		$('#home_carousel_nav').find('a').hover(function() {
			$(this).css('background', 'url(' + carousel.static_url + 'images/carousel_nav_hover.png) no-repeat');
			$(this).supersleight();
		});
		
		$('#home_carousel_nav').find('a').mouseout(function() {
			if($(this).hasClass('on')) {
				$(this).css('background', 'url(' + carousel.static_url + 'images/carousel_nav_on.png) no-repeat');
			} else {
				$(this).css('background', 'url(' + carousel.static_url + 'images/carousel_nav_off.png) no-repeat');
			}
			$(this).supersleight();
		});
		
		var c = $(".jCarouselLite").jCarouselLite({
				btnNext: ".next",
				scroll: 1,
				visible: 1,
		    //auto: 3000,
		    speed: 1500,
				easing: "easeInOutQuad",
			  btnGo: buttons,
				beforeStart: function(item) {
					
					$('#home_carousel_nav').find('a').removeClass('on').css('background', 'url(' + carousel.static_url + 'images/carousel_nav_off.png) no-repeat').supersleight();
					
					if(!manualNav) {
						nextID = parseInt($(item).attr('id').replace('item', ''))+1;
						if(nextID > buttons.length) {
							nextID = 1;
						}
					}
					$("#home_carousel_nav ."+nextID).addClass('on').css('background', 'url(' + carousel.static_url + 'images/carousel_nav_on.png) no-repeat').supersleight();
				},
				afterEnd: function() {
					clearInterval(carousel.intID);
					carousel.restartCarousel();
					manualNav = false;
				}
				
		});
		
		carousel.intID = setInterval("$('#carousel_items .next').trigger('click');", carousel.ROTATION_TIME);
				
		carousel.bindEvents();
		
	},
	
	bindEvents: function() {
		$('#home_carousel').find('a').bind('click', function() {
			switch($(this).attr('rel')) {
				case 'v':
					clearInterval(carousel.intID);
					carousel.embedVideo($(this).attr('href'));
					break;
				case 'g':
					clearInterval(carousel.intID);
					carousel.openedFromCarousel = true;
					openSetInDialog($(this).attr('href'));
					break;
				default:
					window.open($(this).attr('href'), $(this).attr('href'),'');
					break;
			}
			return false;
		});
		
	},
	
	restartCarousel: function() {
		carousel.intID = setInterval("$('#carousel_items .next').trigger('click');", carousel.ROTATION_TIME);
	},
	
	embedVideo: function(video) {
		$('#carousel_items').hide();
		var videoContainer = $('<div/>').attr('id', 'videoWrapper');
		videoContainer.prependTo($('#feature'));
		var so = new SWFObject(carousel.static_url + "/flash/carousel/player.swf", "player", "640", "360", "9", "#000");
		so.addParam('allowfullscreen','true');
		so.addParam('quality', 'best');
		so.addParam('wmode', 'window');
		so.addParam('menu', 'false');
		so.addParam('allowScriptAccess','always');
		so.addParam('salign', 'tl');
		so.addVariable('file', video);
		so.addVariable('autostart', 'true');
		so.addVariable('stretching','uniform');
		so.addVariable('controlbar','over');
		so.addVariable('backFunction','carousel.loadCarouselBack');
		so.addVariable('forwardFunction','carousel.loadCarouselForward');
		so.write("videoWrapper");
	},
	
	loadCarouselBack: function() {
		carousel.destoryVideo();
	},
	
	loadCarouselForward: function() {
		carousel.destoryVideo();
	},
	
	destoryVideo: function() {
		$('#videoWrapper').remove();
		$('#carousel_items').show();
		carousel.restartCarousel();
	}

}
