if(typeof $.CATMARAN == "undefined") {
	$.CATMARAN = Object;
};

/**
 * Rotator with cycle
 */
$.CATMARAN.showcase = {
	index : 0, // Position
	fCount : 0, // Frame count
	fWidth : 0, // Frame width
	fVisible : 0, // Ammount of frames visible
	cWidth : 0, // Container width
	positioner : '#smallFramesPositioner', // Frames holder
	autoAdvanceTimeout : null,
	autoAdvanceMs : 4000,

	initialize	:	function(){
		this.pager();
	},
	
	pager : function(){
		var _this = this;
		this.fWidth = $('#smallFramesPositioner a:eq(0)').outerWidth(true);
		this.cWidth = $('#smallFrames').width();
		this.fVisible = Math.ceil(this.cWidth/this.fWidth);
		this.fCount = $('#smallFramesPositioner a').length-this.fVisible;
		this.prevNext();
		this.handleFrames();

		/*this.autoAdvanceTimeout = setTimeout(function(){
			_this.autoAdvance(1);
		},_this.autoAdvanceMs);*/

		$('#rotator').bind('mouseenter',function(){
			clearTimeout(_this.autoAdvanceTimeout);
		});
	},
	
	autoAdvance : function(currentFrame){
		var _this = this;
		var frames = this.fCount + this.fVisible;
		currentFrame = currentFrame % frames;
		
		$('#thumbs #smallFramesPositioner a').eq(currentFrame).click();

		this.autoAdvanceTimeout = setTimeout(function(){
			_this.autoAdvance(++currentFrame);
		},_this.autoAdvanceMs);
	},
	
	prevNext : function(){
		var _this = this;
		$('#prev').bind('click',function(e){
			_this.moveLeft();
			_this.manageDisabled();
			e.preventDefault();
		});
		$('#next').bind('click',function(e){
			_this.moveRight();
			_this.manageDisabled();
			e.preventDefault();
		});
		this.manageDisabled();
	},
	
	moveLeft : function(){
		if(this.index > 0){
			$(this.positioner).animate({ left: '+='+this.fWidth });
			this.index-=1;
		}
	},
	
	moveRight : function(){
		if(this.index < this.fCount){
			$(this.positioner).animate({ left: '-='+this.fWidth });
			this.index+=1;
		}
	},
	
	manageDisabled : function(){
		if(this.fCount+this.fVisible <= this.fVisible){
			$('#prev,#next').addClass('disabled');
		} else {
			if(this.index == this.fCount){
				$('#next').addClass('disabled');
			} else {
				$('#next').removeClass('disabled');
			}
			if(this.index == 0){
				$('#prev').addClass('disabled');
			} else {
				$('#prev').removeClass('disabled');
			}
		}
	},
	
	handleFrames : function(){
		$('#frames div:not(:eq(0))').css('display','none');
		$('#frames div:eq(0)').addClass('active');

		$('#smallFramesPositioner a').bind('click',function(e){
			$('#smallFramesPositioner a').removeClass('active');
			$(this).addClass('active');

			$('#frames div:visible').fadeOut('slow').removeClass('active');
			$('#frames div:eq('+$(this).index()+')').addClass('active').fadeIn('slow');

			e.preventDefault();
		});
	}
};

$(document).ready(function(){
	$.CATMARAN.showcase.initialize();
	
	$('a[rel="lightbox"],a.lightbox').lightBox({
		overlayBgColor: "#000",
		overlayOpacity: 0.6,
		imageLoading: "/public/images/lightbox/loading.gif",
		imageBtnClose: "/public/images/lightbox/close.gif",
		imageBtnPrev: "/public/images/lightbox/prev.gif",
		imageBtnNext: "/public/images/lightbox/next.gif",
		imageBlank: "/public/images/lightbox/blank.gif", 
		containerResizeSpeed: 350,
		txtImage: "",
		txtOf: "of"
	});
	
	if($('form.formGenerated').length){
		$('form.formGenerated').formChecker({
			nOkMessage : $('#errMsgContact').val()
		});
	}
});
