(function($) { 
	$(function() {
	  Schweppes.Carousel.init();
	});

	var Schweppes = Schweppes || {},
		Carousel = {};
	
	Schweppes.Carousel = Carousel;
	
	Carousel.localOptions = {};

	Carousel.globals = {
		autoplayEnabled: true,
		
		rotationInterval: 6000,
		restartTime: 24000,

		timer: null,
		jwplayerPath: "/App_Themes/SchweppesMain/flash/player.swf"
	}
	
	Carousel.init = function() {
		tabOptions = {
			fx: [
				{opacity:"toggle", duration:"normal"}, 
				{opacity:"toggle", duration:"fast"}
			],
			show: function(event, ui){
				Carousel.globals.container.find(".ui-tabs-hide object").remove(); //remove flash/videoplayers
			},
			select: function(event, ui) {
				$(ui.panel).find("object").remove(); //remove flash/videoplayers
				Carousel.transform($(ui.panel).find("a"));

			}
		}
		
		Carousel.globals.container = $("#homeCarousel");
		Carousel.globals.container.tabs(tabOptions);
		
		Carousel.startAutoplay();

		var totalTabs = Carousel.globals.container.tabs("length") - 1;
		
		$("#homeCarousel a.btn").click(function(e) {
			e.preventDefault();
			e.stopPropagation();
			activeTab = Carousel.globals.container.tabs('option', 'selected');
			if ($(this).hasClass("hcprev")) {
				if (activeTab == 0) {
					nextTab = totalTabs;
				}
				else {
					nextTab = activeTab - 1;
				}
			}
			if ($(this).hasClass("hcnext")) {
				if (activeTab == totalTabs) {
					nextTab = 0;
				}
				else {
					nextTab = activeTab + 1;
				}
			}


			Carousel.globals.container.tabs('option','selected',nextTab);

			
			return false;
		});
		

		/* add click handlers to all the navigation handlers that will allow us to
		   stop/restart the autoscrolling.
		*/
		$("#homeCarousel a.btn, #homeCarousel .navContainer a").click(function(e) {
			Carousel.stopAutoplay();
			Carousel.restartAutoplay();	
		});
			
		
		$(".tabContent a").each(function(k,v) {
			Carousel.transform($(v));
		});
	}
	
	/**
	 * Sets some options for the carousel and decides what sort of element to embed
	 * @param $el jQuery element reference
	 * 
	 */
	Carousel.transform = function($el) {
			
			var theClass = $el.attr("class");
			Carousel.localOptions = {
				tab_id: $el.closest(".tabContent").attr("id"),
				contentURL: $el.attr("href"),
				contentImageURL: $el.find("img").attr("src")
			};


			switch(theClass) {

				case "video":
					Carousel.videoPlayer($el);
					break;

				case "flash":
					Carousel.swfPlayer($el);
					break;

				default:
					//don't worry about it
					break;
			}
	};
	
	/** Builds a JW Video Player next to the anchor tag */
	Carousel.videoPlayer = function($el) {
		var o = Carousel.localOptions;
		//insert a JW Player
		$el.after("<div id='"+o.tab_id+"_playercontainer' />");
		$el.hide();
		var flashvars = {
			"file": o.contentURL,
			"provider": "video",
			"autostart": "false",
			"image": o.contentImageURL,
			"controlbar": "over",
			"controlbar.idlehide": true,
			"playerready": "Carousel_jwplayer_ready"

		};
		var params = {
			allowfullscreen:"true",
			allowscriptaccess:"always",
			wmode: "transparent"
		};
		var attributes = {
			id:o.tab_id+"_player", name:o.tab_id+"_player"
		};
		
		swfobject.embedSWF(Carousel.globals.jwplayerPath,o.tab_id+"_playercontainer" ,"665", "310" ,"9.0.115","false", flashvars, params, attributes);

	}

	/**
	 * Builds a Flash player next to the anchor tag
	 * @param $el object - jQuery Link Element
	 * @returns void
	 */
	Carousel.swfPlayer = function($el) {
		var o = Carousel.localOptions;
		//insert a SWF Player
		$el.after("<div id='"+o.tab_id+"_playercontainer' />");
		$el.hide();
		
		var flashvars = { };
		var params = {
			allowfullscreen:"true",
			allowscriptaccess:"always",
			wmode: "transparent"
		};
		var attributes = {
			id:o.tab_id+"_player", name:o.tab_id+"_player"
		};
		
		swfobject.embedSWF(o.contentURL,o.tab_id+"_playercontainer" ,"665", "310" ,"9.0.115","false", flashvars, params, attributes);

	}
	
	/** Stop Autoplaying the tabs
	 * clear the global timer
	 */
	Carousel.stopAutoplay = function() {
		if (Carousel.globals.autoplayEnabled === false) { return; }
		clearTimeout(Carousel.globals.timer)
		Carousel.globals.container.tabs("rotate", null);
	}
	
	/** Restart Autoplaying
	  * set the global timer to start autoplay after a particular interval
	  */
	Carousel.restartAutoplay = function() {
		if (Carousel.globals.autoplayEnabled === false) { return; }
		//if there is still a "tab" that's focused - unfocus it.
		Carousel.globals.container
		clearTimeout(Carousel.globals.timer);
		Carousel.globals.timer = setTimeout(Carousel.startAutoplay, Carousel.globals.restartTime);
	}

	/** Execute the autoplay for the tabs */
	Carousel.startAutoplay = function() {
		if (Carousel.globals.autoplayEnabled === false) { return; }
		clearTimeout(Carousel.globals.timer);
		Carousel.globals.container.tabs("rotate", Carousel.globals.rotationInterval, false);
	}
	
	/**
	 * JWPlayer API calls NameSpace
	 */
	Carousel.jwplayer = {};
	
	/**
	 * JWPlayer Ready monitor
	 * Waits for the player to send a ready notification
	 */
	Carousel.jwplayer.ready = function (obj) {
		player = document.getElementById(obj['id']);
		//register play / stop listeners
		//player.addControllerListener("PLAY","Carousel_jwplayer_playTracker");
		player.addModelListener("STATE","Carousel_jwplayer_stateTracker");
	}

	/**
	 * JWPlayer Play Tracker
	 * Event listener that will track the playing state of a video
	 */
	Carousel.jwplayer.playTracker = function (obj) {
		//stop auto-rotating when we're playing a video:
		if (obj.state === true) {
			Carousel.stopAutoplay();
		}
		else {
			Carousel.restartAutoplay();
		}
	}

	/**
	 * JWPlayer State tracker
	 * Event listener that will track the state of the video
	 */
	Carousel.jwplayer.stateTracker = function(obj){

		//stop auto-rotating when we're playing a video:
		if (obj.newstate === "PLAYING" || obj.newstate === "BUFFERING") {
			Carousel.stopAutoplay();
		}
		if (obj.newstate === "COMPLETED") {
			Carousel.restartAutoplay();
		}

		
	}

	//set up necessary aliases for passing along to flash
	Carousel_jwplayer_ready = Carousel.jwplayer.ready;
	Carousel_jwplayer_playTracker = Carousel.jwplayer.playTracker;
	Carousel_jwplayer_stateTracker = Carousel.jwplayer.stateTracker;



})(jQuery);		

	

	

