var FEATURE_COUNT = 0;

function selectFeature(featureID) {
	jQuery('div.feature.selected').css('z-index', '1');
	jQuery('div.featureButton.selected').bind('click', function(featureID) {
			selectFeature(jQuery(this).attr('featureID'));
		}
	);
	jQuery('div.featureButton.selected').removeClass('selected');
	
	jQuery('div.feature[featureID="' + featureID + '"]').css('z-index', '5').addClass('selected');
	jQuery('div.featureButton[featureID="' + featureID + '"]').addClass('selected').unbind('click');
}

function cycleFeatures() {
	var currentFeature = jQuery('div.featureButton.selected').attr('featureID');
	var nextFeature = (1 * currentFeature) + 1;

	if(nextFeature > 1) { nextFeature = 0; }

	// change which feature button is selected
	jQuery('div.featureButton[featureID="' + currentFeature + '"]').removeClass('selected');
	jQuery('div.featureButton[featureID="' + nextFeature + '"]').addClass('selected');

	// show next feature
	jQuery('div.feature[featureID="' + nextFeature + '"]').css('z-index', 2);

	// hide current feature
	jQuery('div.feature[featureID="' + currentFeature + '"]')
		.fadeOut('slow', function() {
			// finish hiding current feature
			jQuery(this).removeClass('selected')
				.css({'display':'block', 'z-index':'1'});

			jQuery('div.feature[featureID="' + nextFeature + '"]').addClass('selected').css('z-index', 5);

		});

	window.setTimeout(cycleFeatures, 8000);
}

function initFeaturesCycle() {
	window.setTimeout(cycleFeatures, 8000);
}

jQuery(document).ready(function() {

	// Set up the features buttons
	jQuery('div.featureButton').bind('click', function(featureID) {
		selectFeature(jQuery(this).attr('featureID'));
	});

	// Count how many features there are
	var fCount = jQuery('div.featureBox div.featureButton').length;

	if(fCount > 1) {
		// Set up a timer to cycle through the features
		initFeaturesCycle();
		FEATURE_COUNT = fCount;
	}

/*
	// Clear the searchbox when clicked if it has its default value or is empty
	jQuery('#topSearch').bind('click', function() {
		if(jQuery(this).attr('isDirty') == 0) {
			jQuery(this).attr('isDirty', '1').val('');
		}
	});
	// Reset the searchbox text on blur only if the searchbox is empty
	jQuery('#topSearch').bind('blur', function() {
		if(jQuery(this).val() == '') {
			jQuery(this).attr('isDirty', '0').val('Search...');
		}
		else {
			jQuery(this).attr('isDirty', '1');
		}
	});
*/
});
