(function($) {
 		
 	$.fn.contentSlide = function(settings) {
 	
		var config = {
			autoHeight: false,
			autoHeightEaseDuration: 1000,
			autoHeightEaseFunction: "easeInOutExpo",
			slideEaseDuration: 500,
			slideEaseFunction: "easeInOutExpo",
			contentDiv: false,
			defaultContent: 1,
			autoSlide: false,
			autoSlideInterval: 4000,
			autoSlideStopWhenClicked: true,
			fbLikeButton: true,
			pickerElement: false,
			navElement: false,
			locationBar: false
		};
		
		//merge any passed in settings with the default config
		if(settings) $.extend(config, settings);
				
		//hide the default content div... still there though for SEO goodness.
		$("#" + config.contentDiv).hide();
		
		//make sure the page is set to the top
		 $('html, body').animate({scrollTop:0}, 'slow');
		
		var slider = $(this);
		var structure = "<div id=\"carousel-panels\"><div class=\"active\"></div><div class=\"inactive\"></div></div>";
		slider.append(structure);
		slider.show();
		
		/* set up some global values */
		var contentCount = $("#"+config.contentDiv).children("div").size();
		var panelWidth = $("#carousel-panels .active").width();
		var totalPanelWidth = panelWidth*2;	
		var currentSlide = config.defaultContent;
		var timer;
				
		//load up the selected content or default
		if(location.hash){
			contentId = location.hash.slice(1);
			currentSlide = $('#' + config.contentDiv + ' #' + contentId).index()+1;
		} else {
			//make sure the requested content is within the scope
			if(contentCount < currentSlide){
				currentSlide = 1;
			}
			contentId = $("#"+config.contentDiv+ " div:eq("+ (currentSlide-1) +")").attr("id");
		}
					
		content = $("#"+contentId).html();
				
		updateNav(contentId);		
		
		$("#carousel-panels .active").html(content);
				
		//resize the height
		if (config.autoHeight) {
			panelHeight = slider.find("#carousel-panels .active").height();
			slider.animate({ height: panelHeight }, config.autoHeightEaseDuration, config.autoHeightEaseFunction);			
		};

		//check the electric auto slide setting
		if (config.autoSlide) {			
			slider.ready(function() {timer = setTimeout(autoSlide, config.autoSlideInterval);});
		};
				
		if(contentId == "trailer"){
			//tie click event to load up swf
			$("#ytapiplayer a").click(function(event){
				video_id = $(this).children("img").attr("id");
				setSWFObj(video_id);
				cancelDefault(event);
			});
		}
		
		this.each(function() {
	
			//tie click event to each picker item
			if(config.navElement){	
				obj = $('#' + config.pickerElement + ', #' + config.navElement);
			} else {
				obj = $('#' + config.pickerElement);
			}
			
			obj.click(function(event){
				
				cancelDefault(event);
				
				stopAutoSlide();
							
				contentId = $(this).attr("hash").slice(1);
								
				if(config.locationBar){
					// update the location hash
					location.hash = contentId;
				}									
				
				// fill the inactive div with the selected content				
				selected = $('#' + config.contentDiv + ' #' + contentId);
				currentSlide = selected.index()+1;
				content = selected.html();
				$('#carousel-panels .inactive').html(content);
					
				if (config.autoHeight) {
					panelHeight = slider.find("#carousel-panels .inactive").height();
					slider.animate({ height: panelHeight }, config.autoHeightEaseDuration, config.autoHeightEaseFunction);
				}
				
				$('#carousel-panels .active', slider).animate({marginLeft: - panelWidth}, config.slideEaseDuration, config.easeInOutExpo, function(){
					resetPanels();
				});				
				
				updateNav(contentId);
				
				if(contentId == "trailer"){
					//tie click event to load up swf
					$("#ytapiplayer a").click(function(event){
						video_id = $(this).children("img").attr("id");
						setSWFObj(video_id);
						cancelDefault(event);
					});
				}
								
			});		
		});

		
		function autoSlide() {
			
			if(currentSlide == contentCount){
				currentSlide = 1;
			} else {
				currentSlide += 1;
			};

			contentId = $("#" + config.contentDiv).children("div").eq(currentSlide-1).attr("id");
			content = $("#" + contentId).html();
			
			updateNav(contentId);
			
			$("#carousel-panels .inactive").html(content);
			
			if(config.locationBar){
				location.hash = contentId;
			}
					
			if (config.autoHeight) {
				panelHeight = $("#carousel-panels .inactive").height();
				slider.animate({ height: panelHeight }, config.autoHeightEaseDuration, config.autoHeightEaseFunction);
			};

			$('#carousel-panels .active', slider).animate({marginLeft: - panelWidth}, config.slideEaseDuration, config.easeInOutExpo, function(){
				resetPanels();
			});
			
			if(contentId == "trailer"){
				//tie click event to load up swf
				$("#ytapiplayer a").click(function(event){
					video_id = $(this).children("img").attr("id");
					setSWFObj(video_id);
					cancelDefault(event);
				});
			};
			
			timer = setTimeout(autoSlide, config.autoSlideInterval);
			
		};
		
		function stopAutoSlide(){
			//cancel the auto timeout if we need to....			
			if(config.autoSlideStopWhenClicked && config.autoSlide){
				clearTimeout(timer);
			}		
		};

		function resetPanels(){
			//remove the active div from the DOM
			$('#carousel-panels .active').remove();
			//set the inactive to active
			$('#carousel-panels .inactive').addClass("active").removeClass("inactive");				
			//create a new inactive div
			$('#carousel-panels').append("<div class=\"inactive\"></div>");

		};
		
		function updateNav(activeId){			
			if(config.pickerElement){
				//IF IE < 8, update the parent div to have a high z-Index
				if ($.browser.msie && (parseInt($.browser.version) < 8)) {
					$("#"+config.pickerElement).parents('div:eq(0)').css('zIndex', 9999);
				}
				$("#"+config.pickerElement).removeClass("active");
				$("#"+config.pickerElement).each(function(){
					//find the pickerElement that has a matching hash tag
					($(this).attr("hash").slice(1) == activeId) ? $(this).addClass("active") : "";
				});
			}			
		};
						
		function cancelDefault(event){
			(event.preventDefault) ? event.preventDefault() : event.returnValue = false;
		};
		
		function setSWFObj(video_id_to_load){
			
			stopAutoSlide();

			var video_width = '945';
			var video_height = '556'
			var params = { allowScriptAccess: 'always', hd: 1 };
			var atts = { id: 'myytplayer' };
			var target_id = 'ytapiplayer';
			swfobject.embedSWF('http://www.youtube.com/v/'+video_id_to_load+'?enablejsapi=1&playerapiid=video_player&hd=1', target_id, video_width, video_height, '9.0.115', '/swfs/expressInstall.swf', null, params, atts);
		
		};
		
		return this;
	
	};
	
 })(jQuery);

function onYouTubePlayerReady(playerId) {
	video_player = document.getElementById("myytplayer");
	video_player.playVideo();			
}
