


// Jquery Slider widget
// Developer: Eddie Johnson or Red Snapper LTD
// URL: http://www.redsnapper.net
// <div class='slider'><ul><li>Slide 1 content</li></li>slide 2 content</li></ul></div> sets slider up in html
// sliderize('div.slider') will attach the slider js.

var animationTime = 1000; // 0 for no animation
var autoPlayTime = 2000; // 0 for no autoplay
var easingType = 'easeInQuart'; //'linear' or 'swing' unless you have JQuery easing installed
var showNums = false; //Generate the number navigation at the top right of the widget? (true/false)
var showControls = false; //Show prev/playpause/next controls (true/false)
var playImage = ''; //Leave blank to turn this button off, or insert text or image
var pauseImage = ''; //Leave blank to turn this button off, or insert text or image
var nextImage;// = '<img class="editor" width="25" height="25" src="/media/images/rightarrow_63.gif" alt="Next" />' //Leave blank to turn this button off, or insert text or image
var prevImage;// = '<img class="editor" width="25" height="25" src="/media/images/leftarrow_62.gif" alt="previous" />' //Leave blank to turn this button off, or insert text or image


var currentItem = 0, baseElementDiv, totalItems, itemWidth, position, videoPlaying=false;


var playPause = function(){
	if($('span.sliderButtons a.play').is('.paused')){
		moveToNext(autoPlayTime);
		$('span.sliderButtons a.play').html(pauseImage);
		$('span.sliderButtons a.play').removeClass('paused');
	} else {
		if(window.sliderTimeout){
			clearTimeout(window.sliderTimeout);
			$('span.sliderButtons a.play').html(playImage);
			$('span.sliderButtons a.play').addClass('paused');
		}
	}
};


var hoverFunction = function(){
	$(baseElementDiv).hover(function(){
		if(window.sliderTimeout){
			clearTimeout(window.sliderTimeout);
		}
	},function(){
		if(videoPlaying === false){
			window.sliderTimeout=setTimeout(function(){
				$('span.sliderButtons a.play').html(pauseImage);
				$('span.sliderButtons a.play').removeClass('paused');
					moveToNext(autoPlayTime);
			},autoPlayTime*2);
	}});
};

var doAnimation = function(element,position,f){
	if(f !== undefined){
		$(element).animate( { left:"-"+position+"px" }, animationTime, easingType, f());
	}else{
		$(element).animate( { left:"-"+position+"px" }, animationTime, easingType);
	}
};


var jumpToSlide = function(slideNumber){
	if(window.sliderTimeout){
		clearTimeout(window.sliderTimeout);
	  }
	currentItem = parseInt(slideNumber,10);
	$(baseElementDiv).children('ul').stop();
	var position = itemWidth * currentItem;
	$('div.numbers a').removeClass();
	$('div.numbers a').eq(currentItem).addClass('active');
	doAnimation($(baseElementDiv).children('ul'),position);
	$('span.sliderButtons a.play').html(playImage);
	$('span.sliderButtons a.play').addClass('paused');
	return false;
};

var movePrev = function(){
		
	videoPlaying=false;
	if(window.sliderTimeout) {
		clearTimeout(window.sliderTimeout);
	}
	if(currentItem > 0){
		$(baseElementDiv).children('ul').stop();
		currentItem = currentItem -1;
		var position = itemWidth * currentItem;
		$('div.numbers a').removeClass();
		$('div.numbers a').eq(currentItem).addClass('active');
		doAnimation($(baseElementDiv).children('ul'),position);
		$('span.sliderButtons a.play').html(playImage);
		$('span.sliderButtons a.play').addClass('paused');
		return false;
	} else {
		return false;
	}
};

var fadeAnimation = function(element,position,f){
		var anmationTimef = animationTime / 2;
		$(element).children('li').fadeOut(anmationTimef,function(){
			if(f !== undefined){
				$(element).animate({ left:"-"+position+"px" },000000000000001,easingType,function(){$(element).children('li').fadeIn(anmationTimef, f());});
			}else{
				$(element).animate({ left:"-"+position+"px" },000000000000001,easingType,function(){$(element).children('li').fadeIn(anmationTimef);});
			}
		});
};

var moveToNext = function(autoPlayTime){

	videoPlaying=false;
	 if(autoPlayTime === 'clicked'){
		  if(window.sliderTimeout){
		   clearTimeout(window.sliderTimeout);
	  }
	  if(currentItem +1 < totalItems){
			$(baseElementDiv).children('ul').stop();
			currentItem = currentItem +1;
			position = itemWidth * currentItem;
			$('div.numbers a').removeClass('active');
			$('div.numbers a').eq(currentItem).addClass('active');
			doAnimation($(baseElementDiv).children('ul'),position);
			$('span.sliderButtons a.play').html(playImage);
			$('span.sliderButtons a.play').addClass('paused');
			return false;
	} else {
		return false;
	}
	} else {
		if(window.sliderTimeout){
			clearTimeout(window.sliderTimeout);
		}
		if(currentItem +1 < totalItems){
			currentItem = currentItem +1;
			position = itemWidth * currentItem;
			$('div.numbers a').removeClass('active');
			$('div.numbers a').eq(currentItem).addClass('active');
			doAnimation($(baseElementDiv).children('ul'),position);
			window.sliderTimeout=setTimeout(function(){
				moveToNext(autoPlayTime);
			},autoPlayTime); 
		}else{
			currentItem = 0;
			position = itemWidth * currentItem;
			$('div.numbers a').removeClass('active');
			$('div.numbers a').eq(currentItem).addClass('active');
			doAnimation($(baseElementDiv).children('ul'),position);
			window.sliderTimeout=setTimeout(function(){
				moveToNext(autoPlayTime);
			},autoPlayTime); 
		}
	}
};


var buttonFunctions = function(){
	 $('span.sliderButtons a.prev').click(function(){
		  if(videoPlaying===true){
			$(".featureVidPlacement").fadeOut(300,
				function(){$(".featureVidLoader").fadeIn(600,function(){movePrev('clicked');});
					$(".featureVidPlacement").children().empty();
					return false;
				});
		}else{
		  movePrev('clicked');}
		  return false;
	 });
	 
	 $('span.sliderButtons a.next').click(function(){
		
		if(videoPlaying===true){
			$(".featureVidPlacement").fadeOut(300,
				function(){$(".featureVidLoader").fadeIn(600,function(){moveToNext('clicked');});
					$(".featureVidPlacement").children().empty();
					return false;
				});
		}else{
		  moveToNext('clicked');}
		  return false;
	 });
	 $('span.sliderButtons a.play').click(function(){
		  playPause();
		  return false;
	 });
	 $('div.numbers a').click(function(){
	  var slideNumber = $(this).attr('href');
		  jumpToSlide(slideNumber);
		  return false;
	 });
	 
	 $('.featureVidLoader').click(function(){
			
			if(window.sliderTimeout){
				 clearTimeout(window.sliderTimeout);
			}
						
			$(this).hide();
			var params = { allowScriptAccess: "always", allowfullscreen: "true" };	var atts = { id: "myytplayer", autoplay: "1" }; 
			
			vidURL=$(this).siblings(".vidData").children(".vidURL").text();
			vidID=$(this).siblings(".vidData").children(".vidID").text();
			vidW=$(this).siblings(".vidData").children(".vidW").text();
			vidH=$(this).siblings(".vidData").children(".vidH").text();
			swfobject.embedSWF(vidURL, vidID,vidW, vidH, "8", null, null, params, atts); 
			$(this).siblings(".featureVidPlacement").show();
			videoPlaying=true;
			 
	 });
	 
};

var sliderize = function(baseElement){
	 baseElementDiv = baseElement;
	 totalItems =  $(baseElementDiv+' ul li').length;
	 itemWidth = $(baseElementDiv+' ul li').eq(0).width();
	 $(baseElementDiv +' ul').width(totalItems * itemWidth);
	 if(showControls === true){
		  $(baseElementDiv).append('<span class="sliderButtons"><a class="prev" href="#">'+prevImage+'</a><a class="play" href="#">'+pauseImage+'</a><div class="numbers"><!--Numbers go here--></div><a class="next" href="#">'+nextImage+'</a></span>');
	 }else{
		  $(baseElementDiv).append('<div class="numbers"><!--Numbers go here--></div>');
	 }
	 if(showNums === true){
		  $(baseElementDiv+' ul li').each(function(indIndex){
			  $('div.numbers').append('<a href="'+indIndex+'"></a>');
		  });
	 }
	 $('div.numbers a').eq(0).addClass('active');
	 buttonFunctions();
	 hoverFunction();
	 window.sliderTimeout=setTimeout(function(){
		moveToNext(autoPlayTime);
	},autoPlayTime);
};

$(document).ready(function(){
	 sliderize('div.shoppingSlider');
});
