/*****************************************************************************************************/
/*                                                                                                   */
/*                                           'FEATURES'                                            	 */          
/*                                                                                                   */
/*****************************************************************************************************/

function FEATURES(){
	var JSObject = this;
	this.type = "features"; 
	
	this.DOMDoc; //document object from thickbox window
	this.no_pictures;
	this.current_photo = 1;
	
	this.container;
	this.left_btn;
	this.right_btn;
		
	this.step = 424;     // number of pixels for an image and the spacer
	this.features;		 // JSON with features (image, title)
	
	this.seconds = 7;
	this.autoplay = true;
	this.animation = false;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		this.container = $('#features_container',this.DOMDoc).get(0);
		this.no_pictures = this.features.length;
		
		if (this.no_pictures >= 1){
			this.createFeatures();
			this.initButtons();
			this.initCounter();
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION CREATE FEATURES                                     */
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.createFeatures = function(){
		
		var default_item = $('#features_container').html();
		
		for (i=0; i<this.no_pictures-1; i++){
			$('#features_container').append(default_item);
			$('#features_container > div').eq(i+1).css('left',this.step+'px');
		}
		
		for (i=0; i<this.no_pictures; i++){
			var features_item = $('#features_container > div').get(i);
			
			if (this.features[i].link != '') {
				$('a:eq(0)',features_item).attr('href',this.features[i].link);
				$('a:eq(0)',features_item).attr('target',this.features[i].target);
			} else {
				$('a:eq(0)',features_item).css("cursor","default");
			}
			
			$('img:eq(0)',features_item).attr('src',this.features[i].image);
			$('img:eq(0)',features_item).attr('alt',this.features[i].title);
		}
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION BUTTON LINKS                                     	 */
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initButtons = function(){
		
		var links_container = $('#features_links',this.DOMDoc);		
		var imageIndex = 1;
		
		// attach click functions for the numbers list
		$('#features_links > li',this.DOMDoc).each(
			function(){
				
				$('a:eq(0)',$(this)).data("imageIndex",imageIndex);
				
				$('a:eq(0)',$(this)).click(
					function(){						
						// alert("aici " + $(this).data("imageIndex"));
						JSObject.changeImage($(this).data("imageIndex"));
					}
				);
				
				imageIndex++;
			}
		);
	}
	
	
		
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT SCROLLING                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.changeImage = function(imageIndex){
		
		if (this.animation == true) return;
		
		// we have a valid index, so change pictures
		if (imageIndex != this.current_photo && imageIndex >= 1 && imageIndex <= this.no_pictures) {
			
			//reset autoplay timer
			$(this.container).data("seconds",this.seconds);				
					
			currentDiv = $('#features_container > div').eq(this.current_photo - 1);
			
			nextDiv = $('#features_container > div').eq(imageIndex - 1);
			nextDiv.css('left',this.step+'px');
			
			// start animations
			this.animation = true;
			currentDiv.animate({'left': -this.step},{duration:1000, easing: 'easeInOutCubic'});
			nextDiv.animate({'left': 0},1000,'easeInOutCubic',function(){JSObject.animation = false});
		}
		
		this.current_photo = imageIndex;		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      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);
																	
																	var new_index = JSObject.current_photo + 1;
																	if (new_index > JSObject.no_pictures)
																		new_index = 1;
																		
																	JSObject.changeImage(new_index);
																}
															 })

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