

$(document).ready(function(){
     sliderize('div.slider');
});

// 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 = 5000; // 0 for no autoplay
var easingType = 'easeOutCirc'; //'linear' or 'swing' unless you have JQuery easing installed
var showNums = true; //Generate the number navigation at the top right of the widget? (true/false)
var showControls = true; //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;
var baseElementDiv;
var totalItems;
var itemWidth;
var videoPlaying=false;
var clicks=0;

function sliderize(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();
     videoPlace();
     window.t=setTimeout(function(){moveNext(autoPlayTime)},autoPlayTime);
};

function buttonFunctions(){

     $('span.sliderButtons a.prev').click(function(){
     
     	if(videoPlaying==true){
			$(".featureVidPlacement").fadeOut();
			$(".featureVidPlacement").empty();
			$(".slider li:eq("+currentItem+") .featureVidLoader").fadeIn(600,function(){movePrev('clicked')});
			videoPlaying=false;
		}else{
        	movePrev('clicked');
           	
        }
    sliderautoPlayTime="7000";
    return false;
          
     });
     
     
     $('span.sliderButtons a.next').click(function(){
     	
          
     	if(videoPlaying==true){
			$(".featureVidPlacement").fadeOut();
			$(".featureVidPlacement").empty();
			$(".slider li:eq("+currentItem+") .featureVidLoader").fadeIn(600,function(){moveNext('clicked')});
			videoPlaying=false;
		}else{
          moveNext('clicked');}
           sliderautoPlayTime="7000";
          return false;
     });
     
     $('span.sliderButtons a.play').click(function(){
          playPause();
          return false;
     });
     
     $('div.numbers a').click(function(){
	  	var slideNumber = $(this).attr('href');
	  	
	  	if(videoPlaying==true){
			$(".featureVidPlacement").fadeOut();
			$(".featureVidPlacement").empty();
			$(".slider li:eq("+currentItem+") .featureVidLoader").fadeIn(600,function(){jumpToSlide(slideNumber)});
			videoPlaying=false;
		}else{
	  	
		 jumpToSlide(slideNumber);}
          return false;
     });
    
};
function moveNext(autoPlayTime){

	if(window.t){
	       clearTimeout(t);
	  };
	
		
     if(autoPlayTime == 'clicked'){
          
	    
          if(currentItem +1 < totalItems){
               $(baseElementDiv).children('ul').stop();
               currentItem = currentItem +1;
               var position = itemWidth * currentItem;
               $('div.numbers a').removeClass('active');
               $('div.numbers a').eq(currentItem).addClass('active');
               animation($(baseElementDiv).children('ul'),position);
	       $('span.sliderButtons a.play').html(playImage);
	       $('span.sliderButtons a.play').addClass('paused');
               return false;
          }else{return false};    
     }else{
          if(window.t){
	       clearTimeout(t);
	  }
          if(currentItem +1 < totalItems){
               currentItem = currentItem +1;
               var position = itemWidth * currentItem;
               $('div.numbers a').removeClass('active');
               $('div.numbers a').eq(currentItem).addClass('active');
               animation($(baseElementDiv).children('ul'),position);
               window.t=setTimeout(function(){moveNext(autoPlayTime)},autoPlayTime); 
          }else{
               currentItem = 0;
               var position = itemWidth * currentItem;
               $('div.numbers a').removeClass('active');
               $('div.numbers a').eq(currentItem).addClass('active');
               animation($(baseElementDiv).children('ul'),position);
               window.t=setTimeout(function(){moveNext(autoPlayTime)},autoPlayTime); 
          };    
     };
     
};
function movePrev(){
	
	if(window.t){
	       clearTimeout(t);
	}
	
	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');
               animation($(baseElementDiv).children('ul'),position);
	       $('span.sliderButtons a.play').html(playImage);
	       $('span.sliderButtons a.play').addClass('paused');
               return false;
          }else{return false};
       
};


function playPause(){
	if($('span.sliderButtons a.play').is('.paused')){
		moveNext(autoPlayTime);
		$('span.sliderButtons a.play').html(pauseImage);
		$('span.sliderButtons a.play').removeClass('paused');
	}else{
		if(window.t){
			clearTimeout(t);
			$('span.sliderButtons a.play').html(playImage);
			$('span.sliderButtons a.play').addClass('paused');
		};
	};
};

function jumpToSlide(slideNumber){
	if(window.t){
	       clearTimeout(t);
	  };
        currentItem = parseInt(slideNumber);
        $(baseElementDiv).children('ul').stop();
        var position = itemWidth * currentItem;
        $('div.numbers a').removeClass();
        $('div.numbers a').eq(currentItem).addClass('active');
        animation($(baseElementDiv).children('ul'),position);
	$('span.sliderButtons a.play').html(playImage);
	$('span.sliderButtons a.play').addClass('paused');
        return false;
};

function hoverFunction(){
        $(baseElementDiv).hover(function(){
            if(window.t){
	       clearTimeout(t);
            };
        },function(){
        	if(videoPlaying == false){
            window.t=setTimeout(function(){
	         $('span.sliderButtons a.play').html(pauseImage);
	         $('span.sliderButtons a.play').removeClass('paused');
                 moveNext(autoPlayTime)
            },autoPlayTime*2);
        }});
};

function animation(element,position,f){
	if(f != undefined){
		$(element).animate( { left:"-"+position+"px" }, animationTime, easingType, f());
	}else{
		$(element).animate( { left:"-"+position+"px" }, animationTime, easingType);
	};
};
function fadeAnimation(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);});
			};
		});
};

function videoPlace(){

	$('.featureVidLoader').click(function(){
			
			if(window.t){
	      		 clearTimeout(t);
	  		}	  		
			$(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();
			$(this).siblings(".featureVidPlacement").append("<div>video loading</div>");
			$(this).siblings(".featureVidPlacement").children().attr("id",vidID);
			swfobject.embedSWF(vidURL, vidID,vidW, vidH, "8", null, null, params, atts);
			$(this).siblings(".featureVidPlacement").show();
			videoPlaying=true;	 
});

}


