var HomeGallery = {
	imageArr: null,
	current_img: 0,
	total_imgs: null,
	interval: null,
	timeOut: 6000,
	autoplay: true,
	
	init: function() {
		this.imageArr = $("div#slideshow div.feat_img .slide");
		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);
		}
		
		$("div#slideshow div.feat_img .slide .feat_caption").each(function(){
		  var h3 = $('h3', this);
		  var p = $('p', this);
		  
		  if( !h3.text().match(/([A-Za-z])/g) && !p.text().match(/([A-Za-z])/g))
		  {
		    $(this).remove()
		  }
		});
		
	},
	
	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);
	}
}

// DOM ready Events
// $(function() {
// 	$("div.projects_right div.feat_img .slide:first-child").show();
	// HomeGallery.timeOut = "6000";
	// HomeGallery.init();
// 	
// 	$('a.next').click(function(){
// 		if(HomeGallery.current_img < HomeGallery.total_imgs){
// 			HomeGallery.showNext();
// 			HomeGallery.showCount();
// 			clearInterval(HomeGallery.interval);
// 		} else {
// 			return false;
// 		}
// 		return false;
// 	});
// 	
// 	$('a.prev').click(function(){
// 		if(HomeGallery.current_img > 0  ){
// 			HomeGallery.showPrev();
// 			HomeGallery.showCount();
// 			clearInterval(HomeGallery.interval);
// 		} else {
// 			return false;
// 		}
// 		return false;
// 	});
// 	
// });
