jQuery.extend({
	postJSON: function(url, data, callback) {
		$.post(url, data, callback, 'json');
	}	
});



function capitalize(string) {
	return string.charAt(0).toUpperCase() + string.substr(1).toLowerCase();
}

function formatPrice(finalPrice) {
	finalPrice = finalPrice.toString();
	
	if(finalPrice.indexOf('.') > -1) {
		var split = finalPrice.split('.');
		
		if(split[1].length < 2) {
			finalPrice = finalPrice + '0';	
		}			
	}	else {
		finalPrice = finalPrice + '.00';
	}
	
	return finalPrice;			
}  

$(document).ready(function() {
	$('#nav > li').hover(
		function() {
			if(!$(this).hasClass('root')) {
				$(this).addClass('active');
			}
			$('.dropdown',this).show();
		},
		function() {
			if(!$(this).hasClass('root')) {
				$(this).removeClass('active');
			}
			$('.dropdown',this).hide();
		}
	);
	
	setLastNavItemWidth();
	
	
	$('.details .tabNav li').click(function() {
		$('.details .tabs .tab').hide();
		$('.details .tabNav .active').removeClass('active');
		$('.details .tabs .active').removeClass('active');
		$('.details .tabNav #' + $(this).attr('id')).addClass('active');
		$('.details .tabs .' + $(this).attr('id')).addClass('active');
		$('.details .tabs .' + $(this).attr('id')).show();
	});
	
	//on click an option in details
	$('#addOrderOptionCont .close').click(function(){
		$('#addOrderOptionCont').hide();
		$('#addOrderOptionCont .chk').attr('checked', false);
		$('#addOrderOptionCont .option').removeClass('active');
		$('.info .optionChk').attr('checked', false);
	});
	
	$('#addOrderOptionCont .optionHead').click(function() {
		if ($(this).parent().hasClass('active')){
			$('.chk',this).attr('checked', false);
			$('~ .optionBody',this).removeClass('active');
			$(this).parent().removeClass('active');
		}else{
			$('.chk',this).attr('checked', 'checked');
			$('~ .optionBody',this).addClass('active');
			$(this).parent().addClass('active');
		}
	});
	
	
});

function setLastNavItemWidth() {
	var navWidth = $('#nav').width();
	var restWidth = 0;
	var paddingWidth = 30;
	
	//if(navigator.appVersion.indexOf("Mac") != -1) {
		//paddingWidth = 30;
	//}
	
	$('#nav > li:not(:last)').each(function(i) {
		restWidth += $(this).width() + 20;
	});

	$('#nav > li:last').width(navWidth - restWidth - paddingWidth);
}


function loadOptionLayerMulti(aktiveOption) {
	var varsel1 = $('.selectedValues p.varsel1 .content').html();
	
	if(varsel1 != ""){
		$('#addOrderOptionCont').show();
		$('#addOrderOptionCont #' + aktiveOption).addClass('active');
		$('#addOrderOptionCont #' + aktiveOption + ' .chk.' + aktiveOption).attr('checked', 'checked');
		$('#addOrderOptionCont #' + aktiveOption + ' .optionBody').addClass('active');
	}else {
		$('.orderOptionTable .optionChk').attr('checked', false);
		alert("Bitte wählen Sie erst eine Variante aus.");
		return false;
	}
}

function loadOptionLayer(aktiveOption) {
	var amount = 0;
	$('.variant input.amount').each(function(){
		if(isNaN(parseInt($(this).val())) === false) {		
			amount += parseInt($(this).val());
		}
	});
	if(amount > 0) {
		$('#addOrderOptionCont').show();
		$('#addOrderOptionCont #' + aktiveOption).addClass('active');
		$('#addOrderOptionCont #' + aktiveOption + ' .chk.' + aktiveOption).attr('checked', 'checked');
		$('#addOrderOptionCont #' + aktiveOption + ' .optionBody').addClass('active');
	}else {
		$('.orderOptionTable .optionChk').attr('checked', false);
		alert('Bitte wählen Sie eine Variante aus.');
		return false;
	}
}
