var currentProductId = 0;
var currentVersionId = 0;
var currentToscalianId = 0;
var prix_solo = '1.40';
var prix_super = '1.70';
var prix_maxi = '2.10';
var prix_moyenne = '1.70';

var heure_fermeture = 22.30;//Attention : Heure de dernière commande et non livraison

function ajaxloader( status, message ) {
	$('message_loader').setHTML( message );
	if ( status ) {
		hideAll();
		$('async_loader').setStyle('display', 'block');
	} else {
		$('async_loader').setStyle('display', 'none');
	}
}

function tempPop() {
	$('async_content').setStyle('opacity', '0');
	$('async_content').setStyle('display', 'block');
	var myCoord = $('async_content').getCoordinates();
	$('async_content').setStyle( 'top', ( window.getScrollTop() + (window.getHeight() - myCoord.height) / 2) +'px' );
	$('async_content').setStyle('opacity', '1');
}

function Maintenant() {
	var now = new Date();
	var hours = now.getHours()+0.01*now.getMinutes();
	return hours;
}

function displayComponentView( version, productid, versionid, toscalian ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		currentProductId = productid;
		currentVersionId = versionid;
		currentToscalianId = toscalian;
		$$('#compose_pizza input').setProperty('checked', '');
		$('version_harlem').setHTML(version);
		switch(version) {
			case 'Solo': 
				prix = prix_solo;
				$('row-toscalian').setStyle('display', 'none');
				break; 
			case 'Super': 
				prix = prix_super; 
				$('row-toscalian').setStyle('display', 'inline');
				break;
			case 'Maxi': 
				prix = prix_maxi; 
				$('row-toscalian').setStyle('display', 'none');
				break;
			case 'Moyenne': 
				prix = prix_moyenne; 
				$('row-toscalian').setStyle('display', 'none');
				break;
         case 'MaxiF':
                prix = prix_maxi;
                $('toscalian').setStyle('display', 'none');
                break;
            case 'SoloF':
                prix = prix_solo;
                $('toscalian').setStyle('display', 'none');
                break;
		}
		$('prix_ingredient').setHTML(prix);
		$('component_total').setHTML('0.00');
		$('compose_pizza').setStyle('display', 'block');
	}
}

function displayMenuView( menuid ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		var myCoord = $('compose_menu').getCoordinates();
		$('compose_menu').setStyle( 'top', ( window.getScrollTop() + (window.getHeight() - 500) / 2) +'px' );
		$('compose_menu').setStyle('display', 'block');
		$('menucontent').setHTML('<div id="menuloader"><img id="menuloaderimage" src="/medias/loader.gif" width="220" height="19" alt="" title="" /><div id="menuloadermessage">Mise à jour...</div></div>');
		var arq = new Ajax('/front/fr/shop_menu/async_selectmenu/' + menuid, {
			method: 'post',
			evalScripts: true,
			update: $('menucontent'),
			onComplete: function(){
				$('menu_validation').addEvent('click', function() {
					var items = new Array();
					$$('#formmenu select').each( function(e) {
						items.push( e.getValue() );
					} );
					addMenuToCart( menuid, items, false );
				});
			}
		}).request();
	}
}

function displayPromoView( couponid ) {
	$('coupons').setStyle('display', 'none');
	var myCoord = $('compose_coupon').getCoordinates();
	$('compose_coupon').setStyle( 'top', ( window.getScrollTop() + (window.getHeight() - myCoord.height) / 2) +'px' );
	$('compose_coupon').setStyle('display', 'block');
	$('couponcontent').setHTML('<div id="couponloader"><img id="couponloaderimage" src="/medias/loader.gif" width="220" height="19" alt="" title="" /><div id="couponloadermessage">Mise à jour...</div></div>');
	var arq = new Ajax('/front/fr/shop_coupon/async_selectcoupon/' + couponid, {
		method: 'post',
		evalScripts: true,
		update: $('couponcontent'),
		onComplete: function(){
			$('coupon_validation').addEvent('click', function() {
				var items = new Array();
				$$('#formcoupon select').each( function(e) {
					items.push( e.getValue() );
				} );
				addCouponToCart( couponid, items, false );
			});
		}
	}).request();
}

function displayOptionsView( productid, versionid, key, version, toscalian ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		$$('#options_pizza input').setProperty('checked', '');
		switch(version) {
			case 'Solo': 
				prix = prix_solo; 
				$('toscalian').setStyle('display', 'none');
				break; 
			case 'Super': 
				prix = prix_super;
				$('toscalian').setStyle('display', 'block');
				break;
			case 'Maxi': 
				prix = prix_maxi;
				$('toscalian').setStyle('display', 'none');
				break;
			case 'Moyenne': 
				prix = prix_moyenne;
				$('toscalian').setStyle('display', 'none');
				break;
			case 'MaxiF':
                prix = prix_maxi;
                $('toscalian').setStyle('display', 'none');
                break;
            case 'SoloF':
                prix = prix_solo;
                $('toscalian').setStyle('display', 'none');
                break;
		}
		$('prix_options').setHTML(prix);
		var myCoord = $('options_pizza').getCoordinates();
		$('options_pizza').setStyle( 'top', ( window.getScrollTop() + (window.getHeight() - 400) / 2) +'px' );
		$('options_pizza').setStyle('display', 'block');
		
		$('submitoptions').removeEvents('click');
		$('submitoptions').addEvent('click', function() {
			$('options_pizza').setStyle('display', 'none');
			var components = false;
			var v = versionid;
			if ($('optionfromage').getValue() == 'on') {
				components = new Array();
				components.push('298');
			}
			if ($('optiontoscalian').getValue() == 'on') {
				v = toscalian;
			}
			addToCart( productid, v, key, components );
		} );
	}
}

function addMenuToCart( menuId, items, key ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_addmenutocart/0', {
			onComplete: function( response ){
				if ($chk($('compose_menu'))) $('compose_menu').setStyle('display', 'none');
				if ( response.display == false ) checkAccount();
				else initCart();
			}
		}).send({'menu': menuId, 'items': Json.toString( items ), 'key':key});
	}
}

function addCouponToCart( couponId, items, key ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_addcoupontocart/0', {
			onComplete: function( response ){
				if ($chk($('compose_coupon'))) $('compose_coupon').setStyle('display', 'none');
				if ( response.display == false ) checkAccount();
				else initCart();
			}
		}).send({'coupon': couponId, 'items': Json.toString( items ), 'key':key});
	}
}

function addToCart( product, version, key, components ) {
	if ( Maintenant()> heure_fermeture ){
		tempPop();
	}else{
		var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_addtocart/0', {
			onComplete: function( response ){
				if ($chk($('compose_pizza'))) $('compose_pizza').setStyle('display', 'none');
				if (response.display == false) {
					checkAccount();
				}
				else {
					initCart();	
				}
			}
		}).send({'product': product, 'version': version, 'key': key, 'components': components});
	}
}

function removeFromCart( product, version, key ) {
	var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_removefromcart/0', {
		onComplete: function( cart ){
		    if ($chk($('compose_pizza'))) $('compose_pizza').setStyle('display', 'none');
			initCart();
		}
	}).send({'id': product + '.' + version + '.' + key});
}

function removeMenuFromCart( menu, key ) {
	var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_removefromcart/0', {
		onComplete: function( cart ){
		    if ($chk($('compose_menu'))) $('compose_menu').setStyle('display', 'none');
			initCart();
		}
	}).send({'id': 'MENU.' + menu + '.' + key});
}

function removeCouponFromCart( coupon, key ) {
	var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_removefromcart/0', {
		onComplete: function( cart ){
		    if ($chk($('compose_coupon'))) $('compose_coupon').setStyle('display', 'none');
			initCart();
		}
	}).send({'id': 'COUPON.' + coupon + '.' + key});
}

function showHideComponents ( str ) {
	$(str).toggleClass('invisible');
}

function removeComponent( product, version, key, component ) {
	var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_removecomponent/0', {
		onComplete: function( cart ){
		    initCart();
		}
	}).send({'id': product + '.' + version + '.' + key, 'component':component});
}

function addComponent( product, version, key, component ) {
	var jSonRequest = new Json.Remote('/front/fr/shop_customer/async_addcomponent/0', {
		onComplete: function( cart ){
		    initCart();
		}
	}).send({'id': product + '.' + version + '.' + key, 'component':component});
}

function initCart() {
	$('cartcontent').setHTML('<div id="cartloader"><img id="cartloaderimage" src="/medias/cartloader.gif" width="24" height="24" alt="" title="" /><div id="cartloadermessage">Mise à jour...</div></div>');
	var arq = new Ajax('/front/fr/shop_customer/async_initcart/0/', {
		method: 'post',
		evalScripts: true,
		update: $('cartcontent'),
		onComplete: function(){ updateCart(); }
	}).request();
}

function initStaticCart() {
	var arq = new Ajax('/front/fr/shop_customer/async_getstaticcart/0/', {
		method: 'post',
		evalScripts: true,
		update: $('cartcontent')
	}).request();
}

function updateCart() {
	if ( $('cartcontent').getText() == chk ) {
		checkAccount();
	}
}

function hideAll() {
	$('async_login').setStyle('display', 'none');
	$('async_account').setStyle('display', 'none');
	$('async_mode').setStyle('display', 'none');
	$('async_address').setStyle('display', 'none');
}

function checkAccount() {
	var arq = new Ajax('/front/fr/shop_customer/async_checkaccount/0/', {
		method: 'post',
		evalScripts: true,
		onComplete: function(){ 
			if ( this.response.text == '2' ) {
				$('async_content').setStyle('display', 'block');
				ajaxloader( false, '' );
				$('async_login').setStyle('display', 'block');
			} else {
				updateBulle();
				ajaxloader( true, 'Sauvegarde des paramètres...' );
				checkMode();
			}
		}
	}).request();
}

function checkMode() {
	var arq = new Ajax('/front/fr/shop_customer/async_checkmode/0/', {
		method: 'post',
		evalScripts: true,
		onComplete: function(){ 
			if ( this.response.text == '2' ) {
				$('async_content').setStyle('display', 'block');
				ajaxloader( false, '' );
				$('async_mode').setStyle('display', 'block');
			} else {
				ajaxloader( true, 'Vérification de la disponibilité de votre magasin...' );
				checkAddress();
			}
		}
	}).request();
}

function checkAddress() {
	var arq = new Ajax('/front/fr/shop_customer/async_checkaddress/0/', {
		method: 'post',
		evalScripts: true,
		onComplete: function(){ 
			if ( this.response.text == '2' ) {
				$('async_content').setStyle('display', 'block');
				ajaxloader( false, '' );
				$('async_address').setStyle('display', 'block');
				ajaxloader( true, 'Mise à jour...' );
				var a = new Ajax('/front/fr/shop_customer/async_setavailability/0/', {
					method: 'post',
					evalScripts: true,
					onComplete: function(){
						ajaxloader( false, '' );
						if ( this.response.text == '1' ) {
							$('async_content').setStyle('display', 'none');
							var arq = new Ajax('/front/fr/shop_customer/async_getcart/0/', {
								method: 'post',
								evalScripts: true,
								update: $('cartcontent')
							}).request();
						} else if ( this.response.text == '2' ) {
							$('async_address').setStyle('display', 'block');
							$('checkavailability').setHTML('L\'adresse choisie n\'est pas reconnue. Merci de la vérifier dans la partie "Mon Compte".');					
						} else {
							$('async_address').setStyle('display', 'block');
							$('checkavailability').setHTML('Votre magasin n\'est pas joignable.<br />Veuillez réessayer ou contacter directement ce dernier par téléphone.<br />' + this.response.text);
						}
					}
				}).request();
			} else {
				ajaxloader( false, '' );
				updateCart();
			}
		}
	}).request();
}

function split_num(num) {
	separator = num.indexOf(".");
	if (separator != -1) {
		intpart_op = num.slice(0, separator);
		floatpart_op = num.slice(separator + 1);
		if (floatpart_op.length == 1) floatpart_op += "0";
	} else {
		intpart_op = num;
		floatpart_op = "00";
	} 
	return Array(intpart_op, floatpart_op);
}

// -- Addition sans approximation
function noapprox_add(num1, num2) {
	buffer = 0;	
	num1 = split_num(num1);
	num2 = split_num(num2);
	floatpart = Number( num1[1] ) + Number( num2[1] );
	if (floatpart >= 100) {
		buffer = 1;
		floatpart -= 100;
		floatpart = floatpart.toString();
		if (floatpart.length == 1) floatpart = "0" + floatpart;
	} else {
		floatpart = floatpart.toString();
		if (floatpart.length == 1) floatpart += "0";
	}	
	intpart = Number( num1[0] ) + Number( num2[0] ) + buffer;
	intpart = intpart.toString();
	return intpart + "." + floatpart;
}
