// Retrieve submenu using JSON
var menutree;

// Don't execute AJAX request in HTTPS mode
if (site.https == 'off') {
	$.getJSON(
		pagesurl+'site_map.cfm?ajax=yes',
		function(data, textStatus) {
			menutree = data;
		}
	);
}


$(document).ready(function() {
	// Hide extra text
	$('#more-tools-content').hide();
	
	$("#path span.titre4").each(function() {
		$(this).text($(this).capitalize());
	});
	
	if (site.https == 'off') {
		$("#menu li").hover(
			function() {
				$('div.submenu').hide().remove();
				
				var currentCat = $(this).attr('id');
				var currentCatId = (currentCat.slice(3, currentCat.length));
				
				if (menutree) {
					$.each(menutree.tree, function(i, item) {
						if (item.c.id == currentCatId) {
							root = item.c;
							count = item.count;
						}
					});
					
					$(this).buildMenu(root, count);
				}
			}, function() {
				$(this).children('div.submenu').hide();
				$(this).children('div.submenu').remove();
			}
		);
	}
	
	$('#frmSearch input[name=libelle]').click(function() {
		if ($(this).val() == keywords)
			$(this).val('');
	}).blur(function() {
		if ($(this).val() == '')
			$(this).val(keywords);
	});
	$('#frmMailing input[name=email]').click(function() {
		if ($(this).val() == label_votre_email)
			$(this).val('');
	}).blur(function() {
		if ($(this).val() == '')
			$(this).val(label_votre_email);
	});
	
	$('#more-tools h5').click(function() {
		$(this).parent().children('div').toggle();
	});
	
	$('a[rel=cart]').click(function() {
		ColdFusion.navigate($(this).attr('href') + '&v3=1', 'mini_panier', mycallBack, myerrorhandler);
		
		return false;
	});
	$('a[rel=external]').click(function() {
		window.open($(this).attr('href'));
		
		return false;
	});
	
	$(this).changeServiceDecoration();
});


jQuery.fn.extend({
	capitalize: function() {
		var str = $(this).text();
		
	    return str.replace(/\w+/g, function(el) {
	        return el.charAt(0).toUpperCase() + el.substr(1).toLowerCase();
	    });
	},
	changeServiceDecoration: function() {
		$('#navServices ul li').each(function() {
			if ($(this).attr('id').slice(8, $(this).attr('id').length) == site.page) {
				$(this).toggleClass('off');
			}
		});
	},
	buildMenu: function(data, total) {
		var currentLi = "li#cat"+data.id;
		var currentHref = $(currentLi + ' > h1 a').attr('href');
		var currentTitle = $(currentLi + ' > h1 a').attr('title');
		
		var submenu = '<div class="submenu" style="display:none;"><div class="content"><div class="main"><div class="wrapper"><div class="col">';
		
		var count = 0; // Init counter
		var max_items_per_col = 13;
		var max_cols = 4;
		var sursis = 0;
		var tolerance = 0;
		
		if (total > parseInt(max_items_per_col * max_cols)) {
			max_items_per_col = parseInt(Math.round(total / max_cols) + 1);
		}
		
		var nb_cols = Math.round(total / max_items_per_col);
		
		var sep = false; // Separate tp from their titles (h2)
		var doSep = false;
		
		$.each(data.i, function(key, item) {
			if (count == max_items_per_col || doSep) {
				if (count > 0)
					submenu += '</div>';
				submenu += '<div class="col">';
				
				doSep = false;
				count = 0;
			}
			
			submenu += '<h2><a href="'+pagesurl + item.us+'" title="'+item.n+'">'+item.n+'</a></h2>';
			sursis = 0;
			
			$.each(item.tp, function(tpi, tpdata) {
				count++;
				
				if (count >= max_items_per_col) {
					if (!sep && tolerance != 0 && parseInt(sursis + count) > parseInt(max_items_per_col + tolerance)) {
						submenu += '</div><div class="col">';
						count = 0;
						sursis = 0;
					}
					else if (!sep) doSep = true;
					else {
						submenu += '</div><div class="col">';
						count = 0;
					}
				}
				submenu += '<p><span>-</span><a href="'+pagesurl+tpdata.u+'">'+tpdata.l+'</a></p>';
				
				sursis++;
			});
			
			sursis = 0;
			count++;
		});
		
		if (nb_cols == 1) {
			submenu += '</div><div class="col">&nbsp;';
		}
		submenu += '</div></div></div><div class="see-all"></div><img src="ximg/v3/menu-submenu-degr-tr.png" alt="" class="corner-tr" /></div></div>'; // End col + wrapper + main + content + submenu
		
		$(currentLi).append(submenu);
		
		var position = $(currentLi).position();
		var maxWidth = $('#page').width();
		var merge = 27 + 8 + 8 + (nb_cols * 8); // Padding + margin merged
		if (nb_cols == 1) {
			nb_cols = 2;
		}
		var w = nb_cols * 157 + merge;
		
		$(currentLi).children('div.submenu').width(nb_cols * 157 + merge);
		var currentWidth = $(currentLi).children('div.submenu').width();
		
		if ((position.left + 3) > (maxWidth / 2)) {
			$(currentLi).children('div.submenu').css('left', $(currentLi).width() - currentWidth + 12 + 3 + 1); // 12=padding
		}
		
		$(currentLi).find('div.col h2 a').each(function() {
			$(this).text($(this).capitalize());
		});
		
		$(currentLi).children('div.submenu').show();
		$(currentLi + ' div.see-all').append('<p><a href="'+currentHref+'">Voir toute l\'offre '+currentTitle+'</a></p>');
	}
});