/* feature vechicle helper functions */
$.fn.featureVechicle = function(options) {
	return this.each(function() {
		$.featureVechicle(this, options);
	});
};

// @@param[options]: maybe object or function that returns object
$.featureVechicle = function(container, options) {
	var container = $(container);
	
	var settings = $.extend({},$.featureVechicle.settings);
	if (options){
		if ($.isFunction(options))
			$.extend(settings, options());
		else
			$.extend(settings, options);
	}
	
	var vechicle		= container.find('#featureList');
	var vechicleItems	= vechicle.find('.feature');
	var control			= container.find('#featureTitles');
	var controlItems	= control.find('.featuretitle');
	var num 			= vechicleItems.length;
/*
	if ($(vechicleItems[0]).width() > 900){
		//define the width of the feature title base on the no. of items
		switch(num){
			case 1:
				var menu_width		= 0;
				var selected_width  = 950; // [ 950 ] + 10 = 960
				break;
			case 2:
				var menu_width		= 340;
				var selected_width  = 600; // [ 340 + 600 = 940 ] + 10 * 2 = 960
				break;
			case 3:
				var menu_width		= 270;
				var selected_width  = 390; // [ 270 + 270 + 390 = 930 ] + 10 * 3 = 960
				break;
			case 4:
				var menu_width		= 200;
				var selected_width  = 320; // [ 200 + 200 + 200 + 320 = 920 ] + 10 * 4 = 960
				break;
			default:
				var menu_width		= 160;
				var selected_width  = 270; // [ 160 * 4 + 270 = 910 ] + 10 * 5 = 960
		}
	} else {
		switch(num){
			case 1:
				var menu_width		= 0;
				var selected_width  = 650; // [ 650 ] + 10 = 660
				break;
			case 2:
				var menu_width		= 260;
				var selected_width  = 380; // [ 260 + 380 = 640 ] + 10 * 2 = 660
				break;
			case 3:
				var menu_width		= 175;
				var selected_width  = 280; // [ 280 + 280 + 300 = 630 ] + 10 * 3 = 660
				break;
			case 4:
				var menu_width		= 130;
				var selected_width  = 230; // [ 150 + 150 + 150 + 230 = 620 ] + 10 * 4 = 660
				break;
			default:
				var menu_width		= 100;
				var selected_width  = 210; // [ 100 * 4 + 210 = 610 ] + 10 * 5 = 660
		}
	}
*/

	// start from zero
	var current = settings.start;
	
	// init styles
	for (var i=0; i < num; i++) {
		var $this = $(vechicleItems[i])
		$this.show().fadeTo(0,0).css({
	//			'z-index' : num-i,
				'position' : 'absolute'
		});
		if (i==current)
			$this.fadeTo(0,1).addClass('selectedFeature');
	}
	
	// Get first child width / height as container width / height
	var $first = $(vechicleItems[0]); // Cache
	
	var vechicleWidth	= $first.innerWidth();
	var vechicleHeight	= $first.innerHeight();
	
	$first = null; //Free
	
	// build box
	vechicle.css({
			'position' : 'relative',
			'height' : vechicleHeight
		});
	
	// init the control bar
	controlItems.each(function(i){
		$(this).removeClass('hidden')
			.addClass( (i==current) ? 'selectedFeature' : '')
			.find('a')
//			.attr('href', 'javascript:;')	//remove original link
			.css(
				(i==current) ? settings.selected : settings.menu
			)
			.click(function(e){
				e.preventDefault();
				_moveTo(i);
			});
		$.data(controlItems[i],'fVindex',i); // Use lowlevel data set for speed
	});

	//run initialisations (move arrow)
	if ($.isFunction(settings.init))
		settings.init(container,current);

//	if (num < 2) return;
	
	// create interval
	container.data('interval',setInterval(function(){_move()}, settings.timeout));

	// Container Hover event
	container.hover(
		function(){
			if (container.data('fVpause')){return}
			container
				.data('fVpause',true)
				.data('interval',clearInterval(container.data('interval')));
		},
		function(){
			if(!container.data('fVpause')){return}  //Prevent double firing of mouseout
			container
				.data('fVpause',false)
				.data('interval',setInterval(function(){_move()}, settings.timeout));
		}
	);
	
	//Resize
	$(window).bind('resize.feature',function(){
		vechicle.height(vechicleItems.filter('.selectedFeature').innerHeight());
	});
	
	//Unload
	$(document).one('content-unload',function(){
		clearInterval(container.data('interval'));
		$.data(container[0],'interval',null);
		$(window).unbind('resize.feature');
	});

	function _move() {
		if(container.data('fVpause')){ return }
		if($.cookie('turn') == 'noani'){ return }

		var current 	= vechicleItems.filter('.selectedFeature');
		var next 		= (current.next().length) ? current.next() : vechicleItems.first();

		var ctlCurent	= controlItems.filter('.selectedFeature');
		var ctlNext		= (ctlCurent.next().length) ? ctlCurent.next() : controlItems.first();

		var n = $.data(ctlNext[0],'fVindex');

		//innerfade
		current
			.removeClass('selectedFeature')
			.fadeTo(settings.speed,0);
		next
			.fadeTo(settings.speed,1)
			.addClass('selectedFeature');

		//callbacks (move arrow)
		if ($.isFunction(settings.callback))
			settings.callback(container,n);

		//animate the title
		ctlCurent
			.removeClass('selectedFeature')
			.find('a')
				.animate(settings.menu, settings.anispeed);
		ctlNext
			.addClass('selectedFeature')
			.find('a')
				.animate(settings.selected, settings.anispeed);
		
		vechicle.animate({ 'height' : next.outerHeight() + 'px' }, settings.speed);
	}
	
	function _moveTo (n) {
		container.data('fVpause',true);
		vechicle.stop(true,true);
		vechicleItems.stop(true,true);
		controlItems.stop(true,true);
		
		var current		= vechicleItems.filter('.selectedFeature');
		var next		= $(vechicleItems[n]);
		
		var ctlCurent	= controlItems.filter('.selectedFeature');
		var ctlNext		= $(controlItems[n]);
		
		if ( $.data(ctlCurent[0],'fVindex') != n ) {

			//innerfade
			current
				.removeClass('selectedFeature')
				.fadeTo(settings.speed,0);
			next
				.fadeTo(settings.speed,1)
				.addClass('selectedFeature');

			//callbacks (move arrow)
			if ($.isFunction(settings.callback))
				settings.callback(container,n);

			//animate the title
			ctlCurent
				.removeClass('selectedFeature')
				.find('a')
					.animate(settings.menu,settings.anispeed);
			ctlNext
				.addClass('selectedFeature')
				.find('a')
					.animate(settings.selected,settings.anispeed);

			vechicle.animate({ 'height' : next.outerHeight() + 'px' }, settings.speed, 'swing', function(e){container.data('fVpause',false)});
		}
	}
};

//initialize default settings
$.featureVechicle.settings = {
	'speed':	'normal',
	'anispeed': '300',
	'timeout': 2000,
	'menu': { 'width': '160px'},
	'selected': { 'width': '270px'},
/*	'init': function(container,start){
		
		n = (start == 0) ? 1 : 0; // Get unselected menu item
		
		var arrow = container.find('#featurearrow')
		var controlItems = container.find('.featuretitle')
		var controlItem_a = $(controlItems[n]).find('a')
		var arrowOptions = {
			'arrow':		container.find('#featurearrow'),
			'menu_padding':	(controlItem_a.innerWidth() - controlItem_a.width()),
			'menu_width':	controlItem_a.width(),
			'arrow_margin': parseInt($(arrow).css('left'))
		}
		
		container.data('arrowOptions',arrowOptions);
	},
	'callback': function(container,current){

		arrowOptions = container.data('arrowOptions');

		var left_space  = (current * (arrowOptions.menu_width + arrowOptions.menu_padding))
								+ arrowOptions.arrow_margin;
		arrowOptions.arrow.animate({ left : left_space + 'px' },300);
	},*/
	'start': 0
};
