(function($) {
 		
 	$.fn.powerSlide = function(settings) {
 	
		var config = {
			autoHeight: false,
			autoHeightEaseDuration: 1000,
			autoHeightEaseFunction: "easeInOutExpo",
			slideEaseDuration: 500,
			slideEaseFunction: "easeInOutExpo",
			contentDiv: "products",
			defaultContent: 1,
			autoSlide: false,
			autoSlideInterval: 4000,
			autoSlideStopWhenClicked: true,
			fbLikeButton: true,
			pickerElement: false,
			navElement: false,
			updateLogo: false,
			locationBar: true
		};
		
		//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=\"slider-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 = $("#slider-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);

		if (config.updateLogo) {
			$("#logo a").attr("id", "logo-"+contentId);
		}
				
		$("#slider-panels .active").html(content);
		
		if(contentId == "powerade-commercial"){
			//tie click event to load up swf
			$("#ion4 a").click(function(event){
				setSWFObj();
				cancelDefault(event);
			});
		}
		
		//resize the height
		if (config.autoHeight) {
			panelHeight = slider.find("#slider-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);});
		};
		
		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();
				$('#slider-panels .inactive').html(content);
				
				if (config.updateLogo) {
					$("#logo a").attr("id", "logo-"+contentId);
				}
					
				if (config.autoHeight) {
					panelHeight = slider.find("#slider-panels .inactive").height();
					slider.animate({ height: panelHeight }, config.autoHeightEaseDuration, config.autoHeightEaseFunction);
				}
				
				$('#slider-panels .active', slider).animate({marginLeft: - panelWidth}, config.slideEaseDuration, config.easeInOutExpo, function(){
					resetPanels();
				});
				
				if(contentId == "powerade-commercial"){
					//tie click event to load up swf
					$("#ion4 a").click(function(event){
						setSWFObj();
						cancelDefault(event);
					});
				};
				
				updateNav(contentId);
								
			});		
		});

		
		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);
			
			$("#slider-panels .inactive").html(content);
			
			if(config.locationBar){
				location.hash = contentId;
			}
					
			if (config.autoHeight) {
				panelHeight = $("#slider-panels .inactive").height();
				slider.animate({ height: panelHeight }, config.autoHeightEaseDuration, config.autoHeightEaseFunction);
			};

			$('#slider-panels .active', slider).animate({marginLeft: - panelWidth}, config.slideEaseDuration, config.easeInOutExpo, function(){
				resetPanels();
			});
			
			timer = setTimeout(autoSlide, config.autoSlideInterval);
			
			if(contentId == "powerade-commercial"){
				//tie click event to load up swf
				$("#ion4 a").click(function(event){
					setSWFObj();
					cancelDefault(event);
				});
			}			
		};
		
		function stopAutoSlide(){
			//cancel the auto timeout if we need to....			
			if(config.autoSlideStopWhenClicked && config.autoSlide){
				clearTimeout(timer);
			}		
		}
		
		function setSWFObj(){
			stopAutoSlide();
			var flashvars = {autoPlay:true, flvWidth:'740', flvHeight:'416', flvURL:'/swf/flv/PA_GAMESCIENEv2_740x416.flv', backgroundURL:'/swf/flv/PA_GAMESCIENEv2_740x416.jpg', bufferTime:8};
			var params = {play: 'true', bgcolor: '#000000', width: '740', height: '440', scale: 'showall', allowfullscreen: 'true', wmode: 'transparent', salign: 'lt'}
			var attributes = {};
			swfobject.embedSWF("/swf/player_black.swf", "ion4", "740px", "440px", "9.0.115", "/swfs/expressInstall.swf", flashvars, params, attributes);		
		}

		function resetPanels(){
			//remove the active div from the DOM
			$('#slider-panels .active').remove();
			//set the inactive to active
			$('#slider-panels .inactive').addClass("active").removeClass("inactive");				
			//create a new inactive div
			$('#slider-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);
					if ($("#boost-picker").length != 0) {
						$("#boost-picker").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;
		}	
		
		return this;
	
	};
	
 })(jQuery);

