var timer;
var manual = false;
var slideIndex = 0;

$(window).load(function() {
	
	//fade in homepage slideshow
	if($("body#home").length > 0){
		
	   $('.slideshow-wrap img:nth-child(1)').css("visibility","visible");
	   $('.slideshow-wrap img:nth-child(1)').css("opacity","0");
		var index = 0;
		$('.slideshow-wrap img:nth-child(1)').delay(500).animate({
		    opacity: 1
		  }, 1000, function() {
			  index++
			  if(index==2){
		    homeSlideshow();
			  }
		  });
		
		
	    /*$('div#love').delay(500).animate({
		    opacity: 1
		  }, 1000, function() {
		    // Animation complete.
		  });
		$('div#life').delay(1000).animate({
		    opacity: 1 
		  }, 1000, function() {
		    // Animation complete.
		  });
		$('div#light').delay(1500).animate({
		    opacity: 1
		  }, 1000, function() {
		  //end animation
		  //run slideshow
		  homeSlideshow();
		  });*/
		
	}
	//end fade in homepage slideshow
	    
   	if($("#gallery-image").length > 0){
		var imgs = $("#gallery-image li").text().split(",");
		imgs.pop();
		$("#next").click(function(){
		  manual = true;
		  slideIndex++;
		  if(slideIndex == imgs.length){
		    slideIndex = 0;	
		  };
		  getImage(imgs);
		  $("#image-count").html((slideIndex+1)+":"+imgs.length);
		  return false;
		});	    

		$("#previous").click(function(){
		  manual = true;
		  slideIndex--;
		  if(slideIndex == -1){
		    slideIndex = imgs.length - 1;	
		  };
		  getImage(imgs);
		  $("#image-count").html((slideIndex+1)+":"+imgs.length);
		  return false;
		});	    
		// initiate slideshow		
		getImage(imgs);
	    
	  $("#image-count").html((slideIndex+1)+":"+imgs.length);
	}
	
});

// slideshow fadein
function homeSlideshow(){
	   $('.slideshow-wrap img').css("visibility","visible");
	   $('.slideshow-wrap img').css("opacity","100");
	   $('.slideshow-wrap').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	   
	
}
// end slideshow fadein

function getImage(imgs){
	var picId = imgs[slideIndex];
	var query = "docId="+$("#page-id").text()+"&picId="+picId;
	$("#image-count").html((slideIndex+1)+":"+imgs.length);
	$.ajax({type:'GET',url:"/index.php?id=18",data:query,
		 success:function(data){
		 $("#gallery-image").html(data);
    	 $("#img"+picId).css({opacity: 0});
		 $("#img"+picId).load(function() { 
		 	$("#img"+picId).animate({opacity: 1}, 500, function() {
				if(!manual){
				  timer = setTimeout(function(){
				  	  slideIndex++ 
		      		  if(slideIndex == imgs.length){
				        slideIndex = 0;	
				      };
					  getImage(imgs);
				  },3000);
	            }
	 			else{
		 			  clearTimeout(timer);
				  }
		    });
		});
		}
	});
};

SLIDER = {};
SLIDER.index = 0;
SLIDER.id;
SLIDER.basepath;
SLIDER.slideshowChildren;
SLIDER.target;
SLIDER.imgIDs;
SLIDER.timer;

SLIDER.initSlideShow = function(o){
	this.index = 0;
	this.target = o.target;
	this.imgIDs = o.images;
	this.id = $("#page-id").text();
	this.basepath = o.url;
	var query = "id="+this.id+"&picId="+this.imgIDs[this.index];
	
	if($("#product-slideshow-nav").length > 0){
		$($("#product-slideshow-nav li")[0]).addClass("active");
	}
	
	$.ajax({type:'GET',url:this.basepath,data:query,
		 success:function(data){
			SLIDER.target.html(data);
			$("#img"+SLIDER.imgIDs[SLIDER.index]).css({opacity: 0});
			$("#img"+SLIDER.imgIDs[SLIDER.index]).load(function() {
			  $("#loader").hide();
			  $("#img"+SLIDER.imgIDs[SLIDER.index]).animate({opacity: 1}, 500, function() {});
			});
			
			SLIDER.setTimer();
		 }
	});

	
}

SLIDER.setTimer = function(){
	this.timer = setTimeout(function(){SLIDER.nextSlide()},5000);
}

SLIDER.clearTimer = function(){
	clearTimeout(this.timer);
}

SLIDER.nextSlide = function(){
	if($("#product-slideshow-nav").length > 0){
		$($("#product-slideshow-nav li")[this.index]).removeClass("active");
	}
	this.index++;
	if($("#product-slideshow-nav").length > 0){
		$($("#product-slideshow-nav li")[this.index]).addClass("active");
	}
	$("#loader").show();
	if(this.index == SLIDER.imgIDs.length){
		this.index = 0;	
	}
	var query = "id="+this.id+"&picId="+this.imgIDs[this.index];
	$.ajax({type:'GET',url:this.basepath,data:query,
		 success:function(data){
			 SLIDER.target.append(data);
			$("#img"+SLIDER.imgIDs[SLIDER.index]).css({opacity: 0});
			$("#img"+SLIDER.imgIDs[SLIDER.index]).load(function() {
			$("#loader").hide();
			$("#img"+SLIDER.imgIDs[SLIDER.index]).animate({opacity: 1}, 500, function() { $($(SLIDER.target).children()[0]).remove()});
			  
			});
			SLIDER.setTimer();
		 }
    });
}

SLIDER.showSlide = function(query,target){
	
	$.ajax({type:'GET',url:this.basepath,data:query,
		 success:function(data){
			 SLIDER.target.append(data);
			$("#img"+SLIDER.imgIDs[SLIDER.index]).css({opacity: 0});
			$("#img"+SLIDER.imgIDs[SLIDER.index]).load(function() {
			$("#loader").hide();
			$("#img"+SLIDER.imgIDs[SLIDER.index]).animate({opacity: 1}, 500, function() { $($(SLIDER.target).children()[0]).remove()});
			  
			});
		 }
    });
}
