jQuery(function() {
	var disablehash = false;
	if (!disablehash)
		if (jQuery('#main').is('.catalogue, .article')) {
			window.location = '/#'+(''+window.location).match(/^http[s]?:\/\/[a-zA-Z0-9-\.]+\/(.*)$/)[1];
			return ;
		}
	
	window.makeHumanUnit = function(unit, pack, weight) {
		pack = parseInt(pack);
		return !pack? (unit == 'unit'? ' / pc': ' / '+(unit == 'g'? 'kg': 'litre')):
			' / '+(unit == 'unit'? 'lot de '+parseInt(weight): Renderer[unit == 'g'? 'weight': 'volume'](parseInt(weight)));
	};

	function	HumanMultipleQuantity(tarif, qt) {
		var weight = !parseInt(tarif.pack) && tarif.unit != 'unit'? 1000: (parseInt(tarif.weight) || 1);
		if (parseInt(tarif.pack))
			return Renderer.number(qt)+(!isNaN(qt)? ' '+(tarif.packtype || 'pack').toLowerCase()+(qt>1? 's' :''): '');
		else if (tarif.unit == 'unit')
			return qt*parseInt(weight);
		else
			return Renderer[tarif.unit == 'g'? 'weight': tarif.unit == 'l'? 'volume': 'number'](qt);
	};

	function	HumanMultipleQuantity4(tarif, qt) {
		var weight = !parseInt(tarif.pack) && tarif.unit != 'unit'? 1000: (parseInt(tarif.weight) || 1);
		if (parseInt(tarif.pack))
			return qt;
		else if (tarif.unit == 'unit')
			return qt*parseInt(weight);
		else
			return Renderer[tarif.unit == 'g'? 'weight': 'volume'](qt);
	};
	
	window.HumanMultipleQuantity3 = function(tarif, qt) {
		var s = HumanMultipleQuantity4(tarif, qt);
		var weight = !parseInt(tarif.pack) && tarif.unit != 'unit'? 1000: (parseInt(tarif.weight) || 1);
		if (parseInt(tarif.pack))
			return s;
		else if (tarif.unit == 'unit')
			return s+(qt == 1? ' pc': ' pcs');
		else
			return s;
	};
	
	
if (jQuery.browser.msie && jQuery.browser.version < 7) {
	jQuery('<div class="background"></div>').prependTo('#bandeau');
	jQuery('body').addClass('msie6');
	jQuery('#crocnews').mousedown(function() {
		window.location = this.href;
	});
}


var main = jQuery('#main .data:first'),
	menu = jQuery('#menu'),
	path = (''+window.location).match(/^http[s]?:\/\/[a-zA-Z0-9-\.]+\/(.*)$/)[1],
	win = jQuery(window),
	footer = jQuery('#footer'),
	content = jQuery('#content'),
	mainBottom = main.nextAll('.bottom:last'),
	scrollWith = content.find('> .bars'),
	nav = menu.parents('.bloc.navigation:first'),
	toolbar = jQuery('#toolbar'),
	basket = [];

var band = content.offset().top;
var oldPos = 'absolute';
window.scrollbars = function(time) {
	if (jQuery.browser.msie && jQuery.browser.version < 7)
		return ;
	var top = (document.documentElement.scrollTop || document.body.scrollTop);
	var pos = top <= band? 'absolute': 'fixed';
	if (oldPos == pos) {
		resizeBasket();
		return ;
	}
	oldPos = pos;
	scrollWith.css({position: pos});
	
	resizeBasket();
};
window.resizeBasket = function() {
	if (!basket.length && !(basket = jQuery('#basket')).length)
		return ;
	try {
		var top = (document.documentElement.scrollTop || document.body.scrollTop);
		var basket_top = basket.offset().top;
		var basket_height = basket.height();
		var toolbar_top = toolbar.offset().top;
		var toolbar_height = toolbar.height();
		var wheight = win.height();
	//	var rest = toolbar_height-(toolbar_top-basket.next().offset().top);
	//	console.info('basket_top: '+(wheight-(basket_top-top))+', rest: '+rest);
	//	basket.find('ul:first').css({maxHeight:(wheight+top+toolbar_top-toolbar_height+basket_height-2*basket_top)+'px'});
		basket.find('ul:first').css({maxHeight:(wheight+top+toolbar_top-toolbar_height+basket_height-basket_top-basket_top-40)+'px'});
	//	console.info('begin: '+begin+', rest: '+rest+', height: '+height+', bottom: '+bottom+', max-height: '+(rest-bottom));
	}catch(e){}
};
win.scroll(scrollbars);
win.resize(resizeBasket);

function	minheight() {
	content.css({minHeight:(Math.max(500, nav.height(), toolbar.height())-15)+'px'});
//	resizeBasket();
};

var firstLevel = menu.find('> li > a');
if (path.charAt(0) == '#' || path == '')
firstLevel.click(function() {
	var href = this.href;
	var submenu = jQuery(this).next();
	if (!submenu.is('.submenu'))
		return ;
	if (submenu.is('.collapsed'))
		submenu.removeClass('collapsed').hide();
	if (submenu.is(':visible'))
		submenu.slideUp(minheight);
	else {
		var visible = menu.find('.submenu:visible');
		if (!visible.length)
			submenu.slideDown(minheight);
		else {
			visible.slideUp('fast', function() {
				submenu.slideDown(minheight);
			});
		}
	}
	document.documentElement.scrollTop = document.body.scrollTop = 0;
	scrollbars();
});
firstLevel.each(function() {
	var href = this.href.match(/^http[s]?:\/\/[a-zA-Z0-9-\.]+\/(.*)$/)[1];
	if (path.indexOf(href) === 0 || path.indexOf('#'+href) === 0)
		jQuery(this).next().slideDown(function() {
			jQuery(this).next().removeClass('collapsed');
			minheight();
		});
});
minheight();
if (!disablehash)
	jQuery("#menu a, #main.default .article a").each(function() {
		this.href = '/#'+this.href.match(/^http[s]?:\/\/[a-zA-Z0-9-\.]+\/(.*)$/)[1];
	});

jQuery(document).click(function(e) {
	var el = jQuery(e.target);
	if (el.is('a') && el.attr('href') == path)
		return false;
});

jQuery.loadComponent([
//		'/style/delay.css',
		'/js/catalogue/catalogue.js',
		'/js/moncompte.js',
		'/js/basket.js'
		]
		, function() {
	toolbar_trajet();
	content.removeClass('loading');
	function	done() {
		minheight();
		resizeBasket();
	};
	toolbar.find("h3").click(function(){
		var element = jQuery(this).nextAll('.element:first');
		if (element.is(':visible'))
			element.slideUp(done);
		else
			element.slideDown(done);
	});
	done();
});

if (jQuery('body').is('.admin'))
	jQuery.loadComponent('/js/admin.js');

window.messageAlert = function(obj, timeOut) {
	jQuery.loadComponent(['/js/library/jquery.nmodal.js'], function() {
		jQuery.alert(obj);
	});
	if (timeOut)
		setTimeout(function(){jQuery('#nmodalOverlay').remove(); jQuery('.popup').remove();}, timeOut);
};


});
/**
 * Ajout du bloc Trajet
 */
function	toolbar_trajet() {
	var trajet = jQuery('' +
	'<div class="zone" id="tooltrajet">' +
		'<h3><span>Trajet</span></h3>' +
		'<div class="element">' +
			'<div class="top"></div>' +
			'<form class="data trajet" action="/trajet" method="get">' +
				'<p>Votre trajet en 1 clic !</p>' +
				'<input id="toolbar-voiture" type="radio" class="transport" name="transport" value="voiture"/><label class="transport" for="toolbar-voiture">Voiture</label>' +
				'<input id="toolbar-pieton" type="radio" class="transport" name="transport" value="pieton" checked="checked"/><label class="transport" for="toolbar-pieton">Piéton</label>' +
				'<div>' +
					'<input class="adresse" type="text" name="adresse" />' +
					'<span class="button mauve">' +
						'<span><span><input class="ok" type="submit" value="OK" /></span></span>' +
					'</span>' +
				'</div>' +
				'<br />' +
			'</form>' +
			'<div class="bottom"></div>' +
		'</div>' +
	'</div>'
	);
	
	trajet.find("form:first").submit(function(){
		if (!(trajet.find(".adresse").val().length)) {
			jQuery.errorAlert({text:"Vous n'avez indiqué aucune adresse de départ.", title:"Point de départ."});
			return false;
		}
	});
	
	jQuery('#toolbar #crocnews').before(trajet);
	trajet.find(".adresse").val(jQuery("#trajet .adresse").val());
};

/**
 * Ajout de la recherche
 */
jQuery(function() {
	var form = jQuery('<form class="search" action="/recherche"><input class="area" name="q" /><div><input class="ok" type="submit" value=""/></div></form>');

	jQuery('#bandeau .addons').append(form);
	var input = form.find('.area');
	input.attr({'ajaxcomplete':'/recherche/complete?minchar=2&contains=&q=', 'autocomplete':'off'});
	input.focus(function() {
		jQuery.loadComponent("/js/library/jquery.ncompletion.js", function(){});
	});

	form.submit(function() {
		if (!input.val()) {
			jQuery.errorAlert({text:"Vous n'avez specifié aucun mot clé pour la recherche", title:"Recherche"});
			return false;
		}
	});
});
