$(document).ready(function(){
    $(".sortFilter").change(function() {
        $(location).attr("href", $(this).val());
    });
    
    $(".newcollec input[type=checkbox]").click(function() {
        $(location).attr("href", $(".newcollec a").attr("href"));
    });
});



var mouseX, mouseY;
var dispTimeout;

function triggerDispMoreInfo(event, pid)
{   
    if ($('#moreInfo'+pid).css('display') == 'none') {
        getMousePos(event);
        dispTimeout = window.setTimeout('dispMoreInfo('+pid+')', 1500);
    }
}

function dispMoreInfo(pid)
{
    if (testIntersection(document.getElementById('vignetteCatalogue'+pid))) {
        $('#vignetteCatalogue'+pid).css("backgroundColor", "#F5F5F5");
        $('#moreInfo'+pid).fadeIn();
    }
}

function hideMoreInfo(event, pid)
{
    getMousePos(event);
    
    if (!testIntersection(document.getElementById('vignetteCatalogue'+pid))) {
        clearTimeout(dispTimeout);

        $('#moreInfo'+pid).fadeOut();
        $('#vignetteCatalogue'+pid).css("backgroundColor", "#FFFFFF");
    }
}

function getMousePos(e)
{
    e = e || window.event;
        
    if (e.pageX || e.pageY) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    } else {
        var de = document.documentElement;
        var b = document.body;
        mouseX = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        mouseY = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
}

function testIntersection(elem)
{
    var x1 = elem.offsetLeft;
    var y1 = elem.offsetTop;
    var x2 = x1 + elem.offsetWidth;
    var y2 = y1 + elem.offsetHeight;
        
    if (mouseX > x1 && mouseX < x2 && mouseY > y1 && mouseY < y2)
        return true;
    else
        return false;
}

function applyParamFilter(url) {
	if (location.hash && location.hash!="#") {

		productGridHeight = $("div.productGrid").height();
		$('#waitingPopup').show();
		$("div.productGrid").css('height', productGridHeight);
		

		$("ul.productList").fadeOut(function(){
			paramString = location.hash.substring(1);
		
			$.ajax({
			  url: "ajax/catalog/filter.php",
			  type: "POST",
			  data: paramString + "&url=" + url,
			  success: function(result) {
			  	$("div.catalogContent").html(result);
			  	scrollTo(0,0);
			  	$('#waitingPopup').hide();
			  }
			});
		});
	}
}

function setParamUrl(param)
{

	if (param.lastIndexOf('_')!=-1) {
		name   = param.substring(0, param.lastIndexOf('_'));
		valeur = param.substring(param.lastIndexOf('_')+1);
	} else {
		name   = param.substring(0);
		valeur = '';
	}
	
	
	var newParam = new Object();
	newParam['taille'] = '';
	newParam['page'] = '';
	newParam['nco'] = '';

	// Récup des parametre actuel
	if (location.hash.length > 0) {

		hashArray = location.hash.substring(location.hash.indexOf('#')+1).split('&');
		
		$.each(hashArray, function(key, value){
			newParam[value.substring(0, value.lastIndexOf('='))] = value.substring(value.lastIndexOf('=')+1);
		});
	}
	
	newParam['taille'] = newParam['taille'].replace(" ", "");

	// Modif du nouveau parametre
	newParam[name] = valeur;
	
	if (name!='page') {
		newParam['page'] = '';
	}
	
	var newHashArray = new Array();
	var newHash = '';
	var allEmpty = true;

	$.each(newParam, function(key, value){
		
		if (value!='') {
			allEmpty = false;
		}
		
		newHashArray.push(key + '=' + value);
	});

	if (allEmpty) {
		return '';
	} else {
		return '#' + newHashArray.join('&');
	}
}
