
/********* HERE STARTS THE SCRIPT **********/
jSlideShowIntArray = [];
function initAllSlideShows(){
	
  $(".jslideshow").each(function(k){
    
	/*** SET UP DEFAULTS ***/
	
	var type = "fade";
	var animatespeed = 250;
	var speed = 10000;
	var auto = false;
	var startnum = 0;
	
	/*** AUTOMATIC DEFAULTS ***/
	
	var pagelength = $("div.frame", this).eq(0).width();
	var total = $("div.slides", this).eq(0).children("div.slide").length; 
	var option = $(this).attr("option");
	
	/*** END OF DEFAULTS ***/
	
	
	
	$(this).attr("type")?type = $(this).attr("type"):$(this).attr("type", type);
	if(type=="slideV"){pagelength = $(this).find("div.frame").height();}
	$(this).attr("speed")?speed = $(this).attr("speed"):$(this).attr("speed", speed);
	$(this).attr("animatespeed")?animatespeed = $(this).attr("animatespeed"):$(this).attr("animatespeed", animatespeed);
	$(this).attr("auto")?auto =eval($(this).attr("auto")):$(this).attr("auto", auto);
	$(this).attr("startnum")?startnum =eval($(this).attr("startnum")):$(this).attr("startnum", startnum);
	$(this).attr("current", "-1");
	$(this).attr("total", total);
	$(this).attr("ssid", k);
	$(this).attr("pagelength")?pagelength = parseInt($(this).attr("pagelength")):$(this).attr("pagelength",pagelength);
	
	var sH = $("div.slides", this).eq(0); // Slides Holder
	sH.children("div.slide").each(function(i){
			$(this).css("visibility", "visible").height($(this).height());	
			//$(this).css("background-image", "none");
	});
	
	if(type=="slideH"){
	  if(option=="cycle"){
      sH.width(pagelength*(total+1));
			$("div.slide", this).eq(0).clone().appendTo(sH);
	  }else{
	    sH.width(pagelength*total);
	  }
		sH.children("div.slide").each(function(i){
			$(this).css("left", (i*pagelength)+"px");
		});
	}else if(type=="slideV"){
	  if(option=="cycle"){
      sH.height(pagelength*(total+1));
			$("div.slide", this).eq(0).clone().appendTo(sH);
	  }else{
	    sH.height(pagelength*total);
	  }
		sH.children("div.slide").each(function(i){
			$(this).css("top", (i*pagelength)+"px");
		});
	}else if(type=="fade"){
	  sH.children("div.slide:gt(0)").hide();
	}else if(type=="crossfade"){
	  sH.children("div.slide:gt(0)").hide();
	}else if(type=="slideDown"){
	  sH.children("div.slide:gt(0)").hide();
	};
	
	
	/*** in case there is no nav and it's just autoscroll, i'm plopping these elements in to give it an invisible button ***/
	if(auto){  
      if(!$(this).children("div.nav").get(0)){$(this).append('<div class="nav" style="display:none;"></div>');}
      if(!$(this).children("div.nav").find("a.next").get(0)){$(this).children("div.nav").eq(0).append('<a style="display:none;" class="next"></a>');}
	}
	
	$("div.nav:last", this).find("a").each(function(){
	  $.data(this, "root", $(this).parents("div.jslideshow").eq(0));
	});
	
	$("div.nav:last", this).find("a.next").click(function(){
	  var current = parseInt($.data(this, "root").attr("current"));
	  (current<(total-1))?current++:current=0;
	  animateSlideShow($.data(this, "root"), current, type, animatespeed,option);
	  return false;
	});
	
	$("div.nav:last", this).find("a.prev").click(function(){
	  var current = parseInt($.data(this, "root").attr("current"));
	  (current==0)?current=(total-1):current--;
	  animateSlideShow($.data(this, "root"), current, type, animatespeed,option);
	  return false;
	})
	
	$("div.nav:last", this).find("a.btn").each(function(i){
	  $(this).attr("num", i);
		if(option=="hover"){
			$(this).mouseover(function(){
				var current = parseInt($(this).attr("num"));
				animateSlideShow($.data(this, "root"), current, type, animatespeed, "pass");
				return false;
			});
		}else{
			$(this).click(function(){
				var current = parseInt($(this).attr("num"));
				animateSlideShow($.data(this, "root"), current, type, animatespeed, "pass");
				return false;
			});
		}
	});
	
	if(option=="hover"){
	}
	
	if(auto){
	  jSlideShowIntArray[k] = setTimeout('$("div.jslideshow").eq('+k+').children("div.nav").eq(0).find("a.next").click();', speed);
	  $("div.nav:last", this).find("a.start").eq(0).click(function(){
        var ssid=parseInt($.data(this, "root").attr("ssid"));
        clearTimeout(jSlideShowIntArray[ssid] );
	    jSlideShowIntArray[ssid] = setTimeout('$("div.jslideshow").eq('+ssid+').children("div.nav").eq(0).find("a.next").click();', parseInt($.data(this, "root").attr("speed")));
			return false;
	  });
	  $("div.nav:last", this).find("a.stop").eq(0).click(function(){
        var ssid=parseInt($.data(this, "root").attr("ssid"));
        clearTimeout(jSlideShowIntArray[ssid] );
			return false;
	  });
	};
	
	/*** THIS CLICKS ON THE FIRST BTN TO MAKE IT LOOK "active" IF THERE ARE BTNS FOR EACH SLIDE ***/
	if($("div.nav:last", this).find("a.btn").get(startnum)){
		if(option=="hover"){
			$("div.nav:last", this).find("a.btn").eq(startnum).mouseover();
		}else{
			$("div.nav:last", this).find("a.btn").eq(startnum).click();
		}
	}else{
		$("div.nav:last", this).find("a.next").click();
	}
  
  });
	
	$(".jslideshow .slide").each(function(){
		if($(this).attr("url-under")){
			$(this).prepend('<a href="'+$(this).attr("url-under")+'"><img src="/images/beachbody/en_us/products/programs/p90x/sellingsafety/spacer.gif" class="spacer"/></a>');
			
		}else if($(this).attr("url")){
			$(this).css("cursor", "pointer");
			$(this).click(function(){
				window.location = $(this).attr("url");										 
			});
		}
	});
};  
  
function animateSlideShow(target, num, method, speed, option){
  var ssid=parseInt(target.attr("ssid"));
	var total=parseInt(target.attr("total"));
  if(eval(target.attr("auto"))){
    clearTimeout(jSlideShowIntArray[ssid] );
    jSlideShowIntArray[ssid] = setTimeout('$("div.jslideshow").eq('+ssid+').children("div.nav:last").find("a.next").click();', parseInt(target.attr("speed")));
  }
	
  var current = parseInt(target.attr("current"));
  var diff =  current - num; 
	if(diff==0){ return false;};
	(current==-1)?current=0:null;
  var absdiff = Math.abs(diff);
	var sH = $("div.slides", target).eq(0); // Slides Holder
	var pL = parseInt(target.attr("pagelength"));
	var fL = (-(pL)*num)+"px"; // Final Location
	var cL = (-(pL*total))+"px"; // Cycle Location
	var shS = sH.children("div.slide");

  $("div.nav:last", target).eq(0).find("a.btn").removeClass("active").end().find("a.btn").eq(num).addClass("active");
  
	
  target.attr("current", num);
  if(method=="slideH"){
    if(option=="cycle" && diff>=(total-1)){
	  sH.stop().animate( { "left":cL}, speed, "swing", function(){$(this).css("left", fL)});
		}else if(option=="cycle" && diff<=(1-total)){
	  sH.stop().css("left", cL).animate( {"left":fL}, speed, "swing");
    }else{
	  sH.stop().animate( { "left":fL}, (speed*absdiff), "swing");
		}	
  }else if(method=="slideV"){
		
    if(option=="cycle" && diff>=(total-1)){
	  sH.stop().animate( { "top":cL}, speed, "swing", function(){$(this).css("top", fL)});
    }else if(option=="cycle" && diff<=(1-total)){
	  sH.stop().css("top", cL).animate( { "top":fL}, speed, "swing");
    }else{
	  sH.stop().animate({"top":fL}, (speed*absdiff), "swing");
    }	
  }else if(method=="instant"){
    shS.hide();
    shS.eq(num).show();
  }else if(method=="fade"){
    shS.stop().css("opacity", 1).hide();
    shS.eq(num).fadeIn(speed);
  }else if(method=="crossfade"){
    if(total<shS.length){
	  shS.eq(0).remove();
		}
		shS.eq(current).clone().prependTo(sH).css("opacity", 1).show();
		shS.eq(current+1).hide();
    shS.eq(num+1).fadeIn(speed);
	
  }else if(method=="slideDown"){
		if(total<shS.length){
			shS.eq(0).remove();
		}
		shS.eq(current).clone().prependTo(sH).show();
		sH.children("div.slide:gt(0)").hide();
    shS.eq(num+1).slideDown(speed);
	
  }
	  
	$("div.nav:last", target).eq(0).find("a.disabled").removeClass("disabled");
	if(num==0){
			$("div.nav:last", target).eq(0).find("a.prev").addClass("disabled");
	}else if(num==(total-1)){
			$("div.nav:last", target).eq(0).find("a.next").addClass("disabled");
	}
	
	
	
	
	
	
};



function gotoSlide(target, num){
	if(isNaN(num)){
		if(num.toLowerCase() == "next"){
			$("div#"+target+" div.nav:last").find("a.next").eq(0).click();
		}else if(num.toLowerCase()=="back"){
			$("div#"+target+" div.nav:last").find("a.prev").eq(0).click();
		}
	}else{
	$("div#"+target+" div.nav:last").find("a.btn").eq(num).click();
	}
	return false;
}



/********* HERE ENDS THE SCRIPT ************/
