// handles the functionality for the service browser

jQuery(document).ready(function(){

	// build the list
	buildFeatureList();

});

// make them active
function initListFunctionality(){

	// how to find elements
	var serviceTitle_selector              = '.service-full-list-rightcol .title';
	var serviceDescription_selector        = '.service-full-list-rightcol .description';
	var serviceDescriptions_selector       = '.service-list-main ul li p';
	var serviceDescriptionButtons_selector = '.service-list-main ul li a';

	// hide the descs, in case they were shown
	jQuery(serviceDescriptions_selector).hide();

	// for each click, update the desc
	newDesc = jQuery(this).next().html();
	jQuery(serviceDescriptionButtons_selector).click(function(){

		// data for our actions
		var newDesc = jQuery(this).next().html();
		var newTitle = jQuery(this).text();

		// the "active" class
		jQuery(serviceDescriptionButtons_selector).removeClass('active');
		jQuery(this).addClass('active');

		// hide the title
		jQuery(serviceDescription_selector).slideUp(200, function(){
			jQuery(serviceDescription_selector).html(newDesc);

			// show them both
			jQuery(serviceTitle_selector).text( newTitle );
			jQuery(serviceTitle_selector).css( 'visibility', 'visible' );
			jQuery(serviceDescription_selector).slideDown(200);
		});

	});

}

// service list
var features = {};

function buildFeatureList(){
	jQuery.getJSON('/shop/features/enum', function(data){
	
		// save to global scope
		features=data;
		$iconId = 1;

		for(feature in features){
			jQuery('.services-full-list-menu ul').append(
				"<li><a>"+
					"<span><img src='" +
						features[feature].icon + "'>"+
					"</span>" +
					feature +
				"</a></li>"
			);
		}

		// see if we have an item to choose - else choose the first
		var feature = document.URL.split('#')[1];
		if(typeof feature == 'undefined'){
			// choose first feature
			for(feature in features) break;
		}
		else feature=unescape(feature);
		
		chooseFeature(feature, $iconId);

		// add click functionality to featurelist
		jQuery('.services-full-list-menu ul li a').click(function(){
			name = jQuery(this).text();
			for(feature in features){
				if(feature == name){
					chooseFeature(feature, $iconId);
					break;
				}
			}
		});

	});
}

function chooseFeature(featureset, icon) {  
	// disable active and add new active class
	jQuery('.services-full-list-menu ul li a').removeClass('active');
	jQuery('.services-full-list-menu ul li a:contains('+featureset+')').addClass('active');

	jQuery('.service-list-main .inner-nav ul').html('');

	// the items
	var items = features[featureset].items;
	/* var $imgUrl = str_replace("%body%", "black", "<body text='%body%'>"); */
	for(feature in items){
		jQuery('.service-list-main .inner-nav ul').append(
			'<li>'+
				'<a>'+
					'<img style="height:16px;" src="'+
						features[featureset].icon + '" /> '+
					feature+'</a>'+
				'<p>'+items[feature]+'</p>'+
			'</li>'
		);
	}


	// add functionality
	initListFunctionality();

	// click the first one
	jQuery('.service-list-main ul li a').first().click();
}

