/*****************************************************************************************************/
/*                                                                                                   */
/*                                           'CLIENTS LOGOS'                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function CLIENTS(){
	var JSObject = this;
	this.type = "clients"; 
	
	this.DOMDoc; //document object from thickbox window
	this.no_pictures;
	this.current_photo = 1;
	
	this.container;
	this.left_btn;
	this.right_btn;
		
	this.step = 172;     //number of pixels for an image and the spacer
	this.no_visible = 5; //the number of pictures visible simultaneous
	
	this.seconds = 7;
	this.autoplay = true;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		this.container = $('#clients_container',this.DOMDoc).get(0);
		this.no_pictures = $('#clients_container > div').get().length;
		
		//set container width for x overflow
		var width = 0;
		for (var i=0; i<this.no_pictures; i++){
			width += parseInt($('#clients_container > div').eq(i).width());
		}
		
		$(this.container).width(width);
		this.step = width / this.no_pictures ;
		
		
		if (this.no_pictures >= this.no_visible){
			this.initScrolling();
			this.initCounter();
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT SCROLLING                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initScrolling = function(){
		this.left_btn = $('#clients_left_btn',this.DOMDoc).get(0);
		this.right_btn = $('#clients_right_btn',this.DOMDoc).get(0);
		
		$(this.left_btn).click(function(){
											//reset autoreplay timer
											$(JSObject.container).data("seconds",JSObject.seconds);	
											
											if (JSObject.current_photo-1 > 1){
												JSObject.current_photo--;	
											}
											else{
												JSObject.current_photo = 1;
												$(this).parent().css('display',"none");
											}
											
											if (JSObject.current_photo == JSObject.no_pictures - JSObject.no_visible){
												$(JSObject.right_btn).parent().css('display',"block");
											}
											
											$(JSObject.container).stop();
											$(JSObject.container).animate({'left': -JSObject.step*(JSObject.current_photo-1)},{duration:500, easing: 'easeOutSine'});
										})
		
		
		$(this.right_btn).click(function(){
											//reset autoreplay timer
											$(JSObject.container).data("seconds",JSObject.seconds);	
											
											if (JSObject.current_photo + JSObject.no_visible < JSObject.no_pictures){
												JSObject.current_photo++;	
											}
											else{
												JSObject.current_photo = JSObject.no_pictures-(JSObject.no_visible-1);
												$(this).parent().css('display',"none");
											}
											
											if (JSObject.current_photo == 2){
												$(JSObject.left_btn).parent().css('display',"block");
											}
											
											
											$(JSObject.container).stop();
											$(JSObject.container).animate({'left': -JSObject.step*(JSObject.current_photo-1)},{duration:500, easing: 'easeOutSine'});
										})
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION REPLAY                                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.replay = function(){
		JSObject.current_photo = 1;
		$(this.left_btn).parent().css('display',"none");
		$(this.right_btn).parent().css('display',"block");
		
		$(this.container).stop();
		$(this.container).animate({'left': -JSObject.step*(this.current_photo-1)},{duration:800, easing: 'easeOutSine'});
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION COUNTER                                             */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCounter = function(){
		$(this.container).unbind("countTimer");
		$(this.container).bind("countTimer", function(){
																var seconds = $(this).data("seconds");
																
																if (seconds - 1 >= 0){
																	$(this).data("seconds",seconds-1)
																}
																else{
																	$(this).data("seconds",JSObject.seconds);
																	//JSObject.newFeature();
																	
																	if ($(JSObject.right_btn).parent().css('display') == "block"){
																		$(JSObject.right_btn).trigger("click");	
																	}
																	else{
																		JSObject.replay();	
																	}
																}
															 })

		$(this.container).data("timerInterval", setInterval(function(){if (JSObject.autoplay == true) $(JSObject.container).trigger("countTimer")},1000));
		$(this.container).data("seconds",this.seconds);	
	}
	
}
