/*****************************************************************************************************/
/*                                                                                                   */
/*                                           'FEATURES'                                            	 */          
/*                                                                                                   */
/*****************************************************************************************************/

function FEATURES(){
	var JSObject = this;
	this.type = "features"; 
	
	this.DOMDoc; //document object from thickbox window
	this.no_pictures;
	this.current_photo = 1;
	this.startingButton = 1;
	this.endingButton = 6;
	this.enabled_buttons = false;
	
	this.container;
	this.container_links;
	this.up_btn;
	this.down_btn;
		
	this.step = 501;     		// number of pixels for an image and the spacer
	this.step_scroll = 37;    	// height of a number cell
	this.visible_area = 222;
	this.visible_buttons = 6;
	
	this.features;		 	// JSON with features (image, title)
	
	this.seconds = 7;
	this.autoplay = true;
	this.animation = false;
	this.animation_scroll = false;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		this.container =		$('#features_container',this.DOMDoc).get(0);
		this.container_links = 	$('#features_links_container',this.DOMDoc);
		
		this.no_pictures = this.features.length;
		
		if (this.no_pictures >= 1){
			this.createFeatures();	
			this.initButtons();					
			this.initCounter();
		}
		
		if (this.no_pictures > this.visible_buttons){
			this.enabled_buttons = true;
			this.initScrolling();
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      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 imageIndex = 1;
		
		// attach click functions for the numbers list
		$('#features_links > ul > 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 CHANGE IMAGE                                     	 */
	/*                                                                                                   */
	/*****************************************************************************************************/
	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);				
					
			// ----------------------------------------- //
			
			// make settings for current div and link
			currentDiv = $('#features_container > div').eq(this.current_photo - 1);
			
			currentLink = $('#features_links > ul > li > a').eq(this.current_photo - 1);
			currentLink.attr("class", "");
			
			// ----------------------------------------- //
			
			// make settings for next div and link
			nextDiv = $('#features_container > div').eq(imageIndex - 1);
			nextDiv.css('left',this.step+'px');
			
			nextLink = $('#features_links > ul > li > a').eq(imageIndex - 1);
			nextLink.attr("class", "selected");
			
			// ----------------------------------------- //
			
			// start animations
			this.animation = true;
			currentDiv.animate({'left': -this.step},{duration:1000, easing: 'easeInOutCubic'});
			nextDiv.animate({'left': 0},1000,'easeInOutCubic',function(){JSObject.animation = false});
		}
		

		// calculate new visible buttons
		if (this.enabled_buttons == true) {
			this.startingButton =  	Math.abs($('#features_links').position().top / JSObject.step_scroll) + 1;
			this.endingButton = 	this.startingButton + this.visible_buttons - 1;
		}		
		
		// enable / disable buttons and calculate starting / ending buttons
		this.enableUpDownButtons();

		
		// move links container up or down
		this.current_photo = imageIndex;
		
		if (this.enabled_buttons == true) {
			
			if (this.current_photo < this.startingButton)
				this.moveLinksContainer("up", this.startingButton - this.current_photo);
			
			if (this.current_photo > this.endingButton)
				this.moveLinksContainer("down", this.current_photo - this.endingButton);
		}
	}
	
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT SCROLLING                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/

	this.initScrolling = function(){
		
		this.up_btn = 	$('#'+this.type+'_up_btn',this.DOMDoc).get(0);
		this.down_btn = $('#'+this.type+'_down_btn',this.DOMDoc).get(0);
				
		$(this.up_btn).click(function(){
											JSObject.moveLinksContainer("up",1);
										});
		
		
		$(this.down_btn).click(function(){
											JSObject.moveLinksContainer("down",1);
										});
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION MOVE LINKS		                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	
	this.moveLinksContainer = function(direction,steps){
		
		// alert(direction + " " + steps);
		
		if (this.animation_scroll == true || this.enabled_buttons == false) return;
		
		var new_position = 0;

		if (direction == 'up'){
			
			new_position = $('#features_links').position().top + JSObject.step_scroll * steps;
																				
			if ( new_position <= 0 ) { 
				JSObject.animation_scroll = true;
			}
			
		} else {
		
			new_position = $('#features_links').position().top - JSObject.step_scroll * steps;
			
			// alert(new_position + " " + $('#features_links').height() + " " + JSObject.visible_area);
			
			if ( Math.abs(new_position) <= ( $('#features_links').height() - this.visible_area) ) { 
				JSObject.animation_scroll = true;
			}
		}
		
		if (JSObject.animation_scroll == true)
			$('#features_links').animate({'top': new_position},500,'easeInOutCubic',function(){
				JSObject.animation_scroll = false
			
				// calculate new visible buttons
				JSObject.startingButton =  	Math.abs($('#features_links').position().top / JSObject.step_scroll) + 1;
				JSObject.endingButton = 	JSObject.startingButton + JSObject.visible_buttons - 1;
				
				// enable / disable buttons and calculate starting / ending buttons
				JSObject.enableUpDownButtons();
				
			});
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION ENABLE UP/DOWN BUTTONS                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	
	this.enableUpDownButtons = function(){
		
		if (this.enabled_buttons) {
			
			// disable move up button
			if (this.startingButton == 1) {
				$("img",this.up_btn).attr("src",JSInterface.localpath + 'includes/images/buttons/btn_up_grey.png');			
			} else {
				$("img",this.up_btn).attr("src",JSInterface.localpath + 'includes/images/buttons/btn_up.png');
			}
			
			// disable move down button
			if (this.endingButton == this.no_pictures) {
				$("img",this.down_btn).attr("src",JSInterface.localpath + 'includes/images/buttons/btn_down_grey.png');			
			} else {
				$("img",this.down_btn).attr("src",JSInterface.localpath + 'includes/images/buttons/btn_down.png');
			}
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      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);	
	}
	
}
