var ImageSlideshow = {
	imageArr: null,
	current_img: 0,
	total_imgs: null,
	interval: null,
	timeOut: 6000,
	autoplay: true,
	
	init: function() {
		this.imageArr = $("div.slide_show img");
		this.total_imgs = this.imageArr.length - 1;
		this.setIndexes();
		this.showCount();
		var _this = this;
		if(this.autoplay == true && this.total_imgs > 0){
			this.interval = setInterval(function() { _this.showNext(); }, _this.timeOut);
		}
	},
	
	setIndexes: function() {
		for(var i=0; i < this.imageArr.length; i++) {
			$(this.imageArr[i]).css("z-index", this.imageArr.length - i).css('opacity', 0).filter(":first").css('opacity', 1.0);
		}
	},
	
	showNext: function() {
		if(this.current_img < this.total_imgs) {
			// show next image behind
			$(this.imageArr[this.current_img + 1]).css('opacity', 1.0).css('display', 'block');
			// fade out current image
			$(this.imageArr[this.current_img]).fadeOut(1000);
			// increment counter
			this.current_img++;
			this.showCount();
		} else {
			// fade in first image
			var _this = this;
			
			// reset counter
			_this.current_img = 0;
			_this.showCount();
			
			$(this.imageArr[0]).fadeIn(1000, function() {
				// hide last image
				$(_this.imageArr[_this.imageArr.length-1]).css('opacity', 0);
			});
		}
	},
	
	showPrev: function() {
		if(this.current_img >= 1) {
			// show next image behind
			var _this = this;
			$(this.imageArr[this.current_img - 1]).fadeIn(1000, function(){
			});
			// fade out current image
			$(this.imageArr[this.current_img]).fadeOut(1000);
			
			// increment counter
			this.current_img--;
			this.showCount();
		} else {
			// fade in last image
			var _this = this;
			
			// reset counter
			_this.current_img = _this.total_imgs;
			_this.showCount();
			
			$(this.imageArr[this.imageArr.length-1]).css('opacity', 1.0).css('display', 'block');
			$(this.imageArr[0]).fadeOut(1000, function() {
				// hide last image
			});
		}
	},
	
	showCount: function() {
		var _counter = $('span.count');
		var _current = this.current_img + 1;
		var _total = this.total_imgs + 1;
		_counter.text(_current + "/" + _total);
	},
	
	toggleCaption: function(toggle){
		$caption_holder = $('.slide_caption');
		captionHeight = $caption_holder.height() + 20;
		var capTitleEmpty = $('.slide_caption > span.caption_title').text().replace(" ", "").length < 2;
		var capParaEmpty = $('.slide_caption p').text().replace(" ", "").length < 2;
		
		switch(toggle){
			case "close": 
				$caption_holder.animate({'bottom': -captionHeight},{ queue:false, easing: "easeInOutQuint", duration:1000 });
				break;
				
			case "open":
				if( !capTitleEmpty || !capParaEmpty )
					$caption_holder.animate({'bottom': '0px'},{ queue:false, easing: "easeInOutQuint", duration:1000 });
				
				break;
			
			case "onload":
				this.initTimeout("$caption_holder.animate({'bottom': '0px'},{ queue:false, easing: 'easeInOutQuint', duration:1000 })", "3000");
				break;
		}
	}
};

//DOM ready Events
$(function() {
	//$("div.projects_right div.feat_img .slide:first-child").show();
	ImageSlideshow.timeOut = "6000";
	ImageSlideshow.init();

});
