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

function FEATURES(){
	var JSObject = this;
	this.type = "features"; 
	
	this.DOMDoc; //document object from current window or thickbox window
	this.no_features;
	this.current_feature = 0;
	
	this.seconds = 20;
	this.autoplay = true;
	
	this.container;
	this.prev_btn;
	this.next_btn;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		this.no_features = $('div[id="feature"]',this.DOMDoc).get().length;
		this.prev_btn = $('#featurePrevButton',this.DOMDoc).get(0);
		this.next_btn = $('#featureNextButton',this.DOMDoc).get(0);
		
		this.initFeatures();
		this.initCounter();
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT FEATURES                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initFeatures = function(){
		
		for (var i=0; i<this.no_features; i++){
			var a = $('a[id="featurePage"]',this.DOMDoc).get(i);
			$(a).data('index',i);	
		}
		
		$('a[id="featurePage"]',this.DOMDoc).each(function(){
			
			
			/*****************************************************************************************************/
			/*                                                                                                   */
			/*                                        BULLETS CLICK                                              */          
			/*                                                                                                   */
			/*****************************************************************************************************/
			$(this).click(function(){
				
				var index = $(this).data('index');
				
				if (index == JSObject.current_feature) return false;
				
				JSObject.newFeature(index);
								
			});
		});
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        PREVIOUS BUTTON CLICK                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		$(this.prev_btn).click(function(){
											JSObject.newFeature(JSObject.current_feature-1);
										})
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        NEXT BUTTON CLICK                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		$(this.next_btn).click(function(){
											JSObject.newFeature(JSObject.current_feature+1);
										})
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION NEW FEATURE                                         */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.newFeature = function(index){
		
		if (index == null){
			if (this.current_feature+1 > JSObject.no_features-1) index = 0;
			else index = this.current_feature+1;
		}
		
		if (index == 0){
			$(JSObject.prev_btn).parent().css('display','none');
			$(JSObject.next_btn).parent().css('display','block');
		}
		else if (index == JSObject.no_features-1){
			$(JSObject.prev_btn).parent().css('display','block');
			$(JSObject.next_btn).parent().css('display','none');
		}
		else{
			$(JSObject.prev_btn).parent().css('display','block');
			$(JSObject.next_btn).parent().css('display','block');	
		}
		
		
		//change next icon
		var icon = $('img',$('a[id="featurePage"]:eq('+index+')',JSObject.DOMDoc).get(0)).get(0);
		var src = icon.src;
		var newsrc = src.replace(/\/bullet_3.png/g,'/red_bullet.png')
		icon.src = newsrc;
		
		//change current icon
		var current_icon = $('img',$('a[id="featurePage"]:eq('+JSObject.current_feature+')',JSObject.DOMDoc).get(0)).get(0);
		var src = current_icon.src;
		var newsrc = src.replace(/\/red_bullet.png/g,'/bullet_3.png')
		current_icon.src = newsrc;
		
		//fade out current feature
		var currentDiv = $("div[id='feature']:eq("+JSObject.current_feature+")",JSObject.DOMDoc);
		currentDiv.animate({opacity:0},1000,function(){currentDiv.css('display','none')});
		
		//fade in next feature
		var nextDiv = $("div[id='feature']:eq("+index+")",JSObject.DOMDoc);
		nextDiv.animate({opacity:0},1,function(){nextDiv.css('display','block'); JSObject.current_feature = index;});
		nextDiv.animate({opacity:1, delay:1},1000,function(){});
		
		$('body',this.DOMDoc).data("seconds",this.seconds);	
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION COUNTER                                             */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCounter = function(){
		$('body',this.DOMDoc).unbind("countTimer");
		$('body',this.DOMDoc).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();
																}
															 })

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