function addClick(item) {
	item.click(function(){
		var target = item.attr('href').replace(/#/,'');
			//Replaces existing tab reference and adds current tab reference
		var url = new String(window.location);
			if(url.indexOf("#") != -1) {
				url = url.substr(0,url.indexOf("#"));
				window.location = (url + item.attr('href'));
			} else {
				window.location = (url = item.attr('href'));
			}
		$('div.description > div').each(function(i){
			var div = $(this).attr('id');
			if(div != target && div !=''){
				$(this).addClass('hide').removeClass('here');
				$('a[href^=#]').parent().removeClass('active');
			}
		});
		$('div#'+target).addClass('here').removeClass('hide');
		$('a[href=#'+ target +']').parent().addClass('active');
		return false;	
	});
}

function addTabs() {
	var counter = 1;
	$('div.copy#A > span').each(function(){
		if($(this).attr('id') == 'more') {
			$(document.createElement('a'))
				.attr({id:'more_info'})
				.text('Read More')
				.toggle(
					function(){
						$('span#more').show();
						$(this).text('Read Less');
					},
					function(){
						$('span#more').hide();
						$(this).text('Read More');
					}
				)
				.appendTo($(this).parent());
			
		};
		$(this).hide();
		var href_letter = String.fromCharCode('C'.charCodeAt() + counter);
		var title = $(this).attr('id').replace('_',' ');
		$(document.createElement('li')).appendTo('div.menu > ul').append($(document.createElement('a')).attr({href:'#'+href_letter, style:'text-transform:capitalize'}).text(title));
		$(document.createElement('div')).attr({id:href_letter}).addClass('copy hide').html($(this).html()).appendTo('div.description');
		counter++;
	});
}

$(document).ready(function(){
	//Add any extra tabs listed in long description
	addTabs();
	//Check current url for selected tab and preselect
	var current_url = new String(window.location);
	if(current_url.indexOf("#") != -1) {
		var selected = current_url.slice(current_url.indexOf("#")).replace(/#/,'');
	}
	$('div.tabs a[href^=#]').each(function(){
		var current = $(this).attr('href').replace(/#/,'');
		if(current_url.indexOf('#') != -1) {
			if(current != selected ){
				$('a[href=#' + selected + ']').parent().addClass('active');
				$('a[href=#' + current + ']').parent().removeClass('active');
				$('div#'+selected).addClass('here').removeClass('hide');
				$('div#'+current).addClass('hide').removeClass('here');
			}
		} else {
			$('a[href=#A]').parent().addClass('active');
			if(current != 'A'){ 
				$('div#'+current).addClass('hide').removeClass('here'); 
			}
		}
		addClick($(this));
	});
});

