/**
 * @author Kovacs Gabriel
 * @author Andrei Pfeiffer
 */

// Quantity spin buttons
function increase_by_one(field) {
	nr = parseInt(document.getElementById(field).value);
	document.getElementById(field).value = nr + 1;
}

function decrease_by_one(field, min_quantity) {
	nr = parseInt(document.getElementById(field).value);
	if (nr > min_quantity) {
		document.getElementById(field).value = nr - 1;
	}
}

function addValue(field, increment) {
	nr = parseInt(document.getElementById(field).value);
	
	var value = _getValue(field);
	var currentValue = _getCurrentValue(field);
	
	var newValue = String((currentValue + value));
	newValue = _setFloat(newValue, 2);
	
	$('#assortment-customize div.price span em').text(newValue);
	
	if (increment==true) {
		increase_by_one(field);
	}
	
	// update total quantity
	currentQuantity += 1;
	
	$('#assortment-customize #nr-items em').text(currentQuantity);
}

function substractValue(field, decrement, min_quantity) {
	nr = parseInt(document.getElementById(field).value);
		
	if (((nr > min_quantity) && decrement==true) || decrement==false) {
		var value = _getValue(field);
		var currentValue = _getCurrentValue(field);
		
		var newValue = String((currentValue - value));
		newValue = _setFloat(newValue, 2);
		
		$('#assortment-customize div.price span em').text(newValue);
		
		// update total quantity
		currentQuantity -= 1;
		
		$('#assortment-customize #nr-items em').text(currentQuantity);
	}
	
	if (decrement==true) {
		decrease_by_one(field, min_quantity);
	}
}

// extract element individual value (price)
function _getValue(field) {
	var value = String($($($('#' + field).parent()).find('span.price em').get(0)).text());
	value = parseInt(value.replace('.', ''));
	
	return value;
}

// extract current total value (price)
function _getCurrentValue(field) {
	var currentValue = String($('#assortment-customize div.price span em').text());
	currentValue = jQuery.trim(currentValue);
	currentValue = parseInt(currentValue.replace('.', ''));
	//alert(currentValue)
	return currentValue;
}

// gets a String and an Integer, and creates a Float with the Integer number of decimals
function _setFloat(stringValue, nrDecimals) {
	var parteIntreaga = stringValue.substr(0, stringValue.length-nrDecimals);
	var parteZecimala = stringValue.substr(parteIntreaga.length, stringValue.length);
	
	floatValue = String(parteIntreaga) + '.' + String(parteZecimala);
	
	return floatValue;
}






// on page load complete, fire off a jQuery 
$(document).ready(function() {
	
	$(".arrow-up").click( function() {
		/*$("#quantity").hide();
		increase_by_one('quantity')
		$("#quantity").fadeIn(500);*/
		return false;
	});
	
	$(".arrow-down").click( function() {
		/*$("#quantity").hide();
		decrease_by_one('quantity');
		$("#quantity").fadeIn(500);*/
		return false;
	});
	
	//$('#more-details > ul').tabs({ fx: { opacity: 'toggle' } });

});
