var Gallery = {
	current_img: 0,
	total_imgs: null,
	animating: false,
	margin: 10,
	autoPlay: false,
	autoPlayTimer: 6000,
	
	autoPlayer: function(){
		var _this = this;
		if(this.autoPlay == true && this.total_imgs > 0){
			this.interval = setInterval(function() { _this.scrollNext(); }, _this.autoPlayTimer);
		}
	},
	
	returnCurrentImageWidth: function() {
		return $('#gallery ul li:eq('+this.current_img+')').width() + parseInt($('#gallery ul li').css("margin-right"));
	},
	
	setCurrentImageNumber: function() {
		$('#gallery ul li').addClass("off");
		$('#gallery ul li:eq('+this.current_img+')').removeClass("off");
		$('#gallery p.count').empty().append((this.current_img + 1) + '/' + (this.total_imgs + 1));
	},
	
	scrollNext: function() {
		if(this.current_img < this.total_imgs && !this.animating) {
			this.animating = true;
			this.current_img++;
			this.setCurrentImageNumber();

			this.animateScroll();

			if(this.current_img == this.total_imgs) {
				return "last";
			}
		}
	},
	
	scrollPrevious: function() {
		if(this.current_img > 0 && !this.animating) {
			this.animating = true;
			this.current_img--;
			this.setCurrentImageNumber();
			
			this.animateScroll();
			
			if(this.current_img == 0) {
				return "first";
			}
		}
	},
	
	animateScroll: function() {
		$('#gallery ul').animate({left: - (this.current_img) * this.returnCurrentImageWidth() }, 700, "easeOutQuint", function() { Gallery.animating = false; });
	}
}

// DOM ready Events
$(function () {

	Cufon.replace('#content #gallery h2', { fontFamily: 'intlightcon' });

	// setup gallery
	Gallery.total_imgs = $('#gallery ul li').length - 1;
	Gallery.setCurrentImageNumber();
	if(Gallery.autoPlay){Gallery.autoPlayer();}
	
	if($('#gallery ul li').length == 1){ $('#gallery a.controls').hide(); }
	
	if($('#gallery ul li').length == 1 && $('#gallery ul li img').attr('src') == '/templates/nkp_website/images/content/gallery_blank_placeholder.gif' && $('#gallery ul li .caption_wrap h2').text().length < 2 ){
		$('#gallery ul li').hide();
	}
	
	
	$('#content #gallery ul li').hoverIntent(
		function(){ if($('.caption_wrap h2', this).text().length > 2){ $('.caption_wrap', this).fadeIn(800); } $('#gallery').addClass('hover'); },
		function(){ $('.caption_wrap', this).fadeOut();$('#gallery').removeClass('hover'); }
	);

	// events
	$('a#btn_prev').css("visibility", "hidden");
	
	$('a#btn_next').click(function() {
		if(Gallery.scrollNext() == "last") {
			$(this).css("visibility", "hidden");
			$('a#btn_prev').css("visibility", "visible");
		} else {
			//$('a#btn_prev').css("visibility", "visible");
		}
		return false;
	});
	$('a#btn_prev').click(function() {
		if(Gallery.scrollPrevious() == "first") {
			$(this).css("visibility", "hidden");
			$('a#btn_next').css("visibility", "visible");
		} else {
			$('a#btn_next').css("visibility", "visible");
		}
		return false;
	});
});
