/*******************************************
 * Appel de fonction & fonctions principales
 ******************************************/

/**
* hoverIntent is similar to jQuery's built-in "hover" function except that
* instead of firing the onMouseOver event immediately, hoverIntent checks
* to see if the user's mouse has slowed down (beneath the sensitivity
* threshold) before firing the onMouseOver event.
*
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
*
* hoverIntent is currently available for use in all personal or commercial
* projects under both MIT and GPL licenses. This means that you can choose
* the license that best suits your project, and use it accordingly.
*
* // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
* $("ul li").hoverIntent( showNav , hideNav );
*
* // advanced usage receives configuration object only
* $("ul li").hoverIntent({
*	sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
*	interval: 50,   // number = milliseconds of polling interval
*	over: showNav,  // function = onMouseOver callback (required)
*	timeout: 100,   // number = milliseconds delay before onMouseOut function call
*	out: hideNav    // function = onMouseOut callback (required)
* });
*
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @return    The object (aka "this") that called hoverIntent, and the event object
* @author    Brian Cherne <brian@cherne.net>
*/
(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 70,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);


function makeToggleMenus()
{
	$('dd').hide();
	$('dt a').click(function(){
		$(this).parent().next('dd').attr('title','click');
		$('dt a').css({color: '#0564A6'});
		$(this).css({color : '#F48010'});
		$('dd:visible').each(function(){
			if($(this).attr('title') != "click")
				$(this).slideUp('slow');
		});
		$(this).parent().next('dd').slideToggle('slow');
		$(this).parent().next('dd').attr('title','');		
	});
}

function hoverImg(elemImg)
{
	/*$('div#needs img.active').each(function()
	{
		outImg($(this));
	});*/
    var directory = elemImg.parents('ul').attr('id');
	iid = elemImg.attr('id');
	src = 'images/content/' + directory + '/' + iid + '-Hover.png';
	elemImg.attr('src',src);
	/*elemImg.attr('title','active');*/
	/*makeActiveLeftMenu();*/
}

function makeActiveLeftMenu()
{
    var directory = $('div#needs').children('ul').attr('id');
	$('div#needs img.active-menu-left').unbind();
	id = $('.active-menu-left').attr('id');
	src = 'images/content/' + directory + '/' + id + '-Hover.png';
	$('div#needs img.active-menu-left').attr('src',src);
}

function outImg(elemImg){
    var directory = elemImg.parents('ul').attr('id');
	if(elemImg.attr('class') != "active-menu-left"){
		iid = elemImg.attr('id');
		src = 'images/content/' + directory + '/' + iid + '.png';
		elemImg.attr('src',src);
		//elemImg.attr('title','');
	}
	/*makeActiveLeftMenu();*/
}

function makeActiveLeftMenuByID (id) {
	var directory = $('div#needs').children('ul').attr('id');
	src = 'images/content/' + directory + '/' + id + '-Hover.png';
	$('li#need-'+id+' img').attr('src',src);
	$('li#need-'+id+' img').attr('class','active-menu-left');
}

$(document).ready(function() {		

    manage_subMenu();

    $('div#needs a').hover(function(){
		hoverImg($(this).children('img'));
	},function(){
		outImg($(this).children('img'));
	});
	
	
	makeToggleMenus();
	//makeActiveLeftMenu();
	
	//makeActiveLeftMenuByID(4);
	
});





/*******************************************
 * Fonctions de gestion des rubriques
 ******************************************/

// Fonction principale
function manage_Sections() {
	var reduced = 0;
	
	/*$('div#needs img').each(function() {
		spec = $(this).attr('class');
		if(spec != "active")
			//hoverHandler($(this));
	});*/
	
	/*$('div#needs a').each(function(){
		url = $(this).attr('href');
		$(this).attr('title',url);
		$(this).attr('href','javascript:void(0)');
	});*/
	
	$('div#needs a').click(function() {
		url = $(this).attr('href');				
		if(reduced == 0) {
			$(this).attr('href','javascript:void(0)');
			reduced = 1;
			animateRtoL($(this), url);
		} else {
			/*url = url + '&stopAjax=1';*/
			document.location = url;
			return false;
			loadContent(url,null,1);
			return false;
			/*$('div#needs img').each(function() {
				$(this).parents('a').children('span').css({color:"#917F7F"});
				$(this).parents('a:hover').children('span').css({color:"#FFF"});
				resetSource($(this));
				hoverHandler($(this));
			});	
			$(this).children('img').unbind();
			$(this).children('img').attr('src',"images/content/" + directory + "/" + id + "-Hover.png");
			$(this).children('span').css({color:"#FFF"});						
			loadContent('./views/' + $(this).attr('id') + '-a.php', '60', 2);*/
		}
	});
}

// Fonction d'animation des rubriques de la DROITE vers la GAUCHE
function animateRtoL(element, url) {
	var directory = $(element).parents('ul').attr('id');
	var id = $(element).children('img').attr('id');
			
	
	$('ul#breadcrumb').append('<li><span class="raquo">&raquo;</span>' + element.children('span').attr('id') + '</li>');
	
	$('div#needs a#produits-1').animate({
		fontSize: "4px",
		left: "-220px",
		top: "200px"
	}, 300, 'swing');
	
	$('div#needs a#produits-2').animate({
		fontSize: "4px",
		left: "-125px",
		top: "200px"
	}, 300, 'swing');
	
	$('div#needs a#produits-3').animate({
		fontSize: "4px",
		left: "-220px",
		top: "320px"
	}, 300, 'swing');
	
	$('div#needs a#produits-4').animate({
		fontSize: "4px",
		left: "-125px",
		top: "320px"
	}, 300, 'swing');
	
	$('div#needs a#produits-5').animate({
		fontSize: "4px",
		left: "-220px",
		top: "440px"
	}, 300, 'swing');
	
	$('div#needs a#produits-6').animate({
		fontSize: "4px",
		left: "-125px",
		top: "440px"
	}, 300, 'swing');

    $('div#needs a#produits-7').animate({
		fontSize: "4px",
		left: "-125px",
		top: "560px"
	}, 300, 'swing');
	
	$('div#needs h1').html('&nbsp;').css({fontSize:"1.2em",fontWeight:"bold",position:"absolute",left:"-200px",top:"180px"});
	
	setTimeout("clickHandler('" + url + "')",250);
	$(element).hover(function(){},function(){});
	$(element).unbind();
	hoverImg($(element));
	/*$(element).attr('class','active-menu-left');
	$('div#needs img').css('width','64px').css('height','104px');*/	
}

// Fonction de setting du hover sur les vignettes
function clickHandler(url) {
	$('div#needs h1').addClass('enlarge')
	.html('Mouse over to enlarge');
	
	/**$('div#content').append('<div id="contentData"></div><div class="cleaner"></div>');
	$('div#contentData').css({
		left: '240px',
		top: '50px',
		position: 'absolute'
	});**/
	
	/*$('div#needs a').click(function(){
			id = $(this).attr('id');
			$('div#needs span').css('color','black');
			$('div#needs img').each(function(){
				iid = $(this).attr('id');
				$(this).attr('src','images/content/produits/' + iid + '.png');
			});								
			$('div#needs img').each(function() {
				hoverHandler($(this));
			});
			iid = $(this).children('img').attr('id');
			$(this).children('img').attr('src','images/content/produits/' + iid + '-Hover.png');
			loadContent('./views/products/' + id + '.php','60',1)
			loadContent('./views/products/' + id + '-a.php','60', 2);
			$(this).children('img').unbind();
		});*/
	
	/*$('div#needs a').each(function() {
		leftHoverHandler($(this));	
	});*/
		
	
	document.location = url;/* + '&stopAjax=1';*/
	return false;
	loadContent(url, '60', 1);
}

//Fonction de reset des sources images
function resetSource(element) {
	var directory = $(element).parents('ul').attr('id');
	var id = $(element).attr('id');
	$(element).attr('src','images/content/' + directory + '/' + id + '.png');
}

// Fonction de gestion du survol des rubriques
function hoverHandler(element) {
	return;
	element.hover(function() {		
		hoverImg($(this));
		//$(this).attr('src', "images/content/" + directory + "/"+ id + "-Hover.png");
	}, function() {
		/*var directory = $(this).parents('ul').attr('id');
		var id = $(this).attr('id');
		$(this).attr('src', "images/content/" + directory + "/"+ id + ".png");*/
		outImg($(this));
	});
}


var gardeFou = 0;
var inZoomOut = 0;

// Fonction de gestion du survol des rubriques r�duites
function leftHoverHandler(element) {
	var leftValue = element.css('left');
	var topValue = element.css('top');
	var heightValue = element.css('height');
	var widthValue = element.css('height');    
	/*if (element.children('img').attr('class') != "active") {
		element = element.children('img');
		hoverHandler(element);
	}
	return;*/

	element.hoverIntent( function() {
            $(this).css('z-index', '100');
            setIndex($(this),'10');
            $(this).children('img').css('width','100%').css('height','100%');
            $(this).animate({
    			left: parseInt(leftValue.substr(0,leftValue.length-2)) - parseInt((widthValue.substr(0,widthValue.length-2)/2)),
        		top: parseInt(topValue.substr(0,topValue.length-2)) - parseInt((heightValue.substr(0,heightValue.length-2)/2)),
            	fontSize: '10px'
            }, 200, 'swing', clearGardeFou());


	}, function() {

            $(this).animate({
                left: leftValue,
                top: topValue,
                fontSize: '4px'
            }, 200, 'swing',function(){
                $(this).css('z-index', '1');
                $(this).children('img').css('width','64px').css('height','104px');
            });
            setIndex($(this),'9');
            $(this).css('z-index', '100');
	});


		
}

function clearGardeFou(){
    gardeFou = 0;
}

// Fonction de r�glage du z-index
function setIndex(element, indexPosition) {
	element.css({ zIndex:indexPosition });
}




/*******************************************
 * Fonction de gestion des menus d�roulants
 ******************************************/

// Fonction principale
function manage_Menu() {
	$('dd').hide();	
	/*$('div#contentData').append('<div id="subContentData"></div>');
	$('div#subContentData').css({
		marginTop:'45px',
		width:'690px',
		marginLeft:'20px'
	});*/
	
	//loadContent('./views/products/' + $('dl').attr('class').substr(0,$('dl').attr('class').length) + '-a.php','60',2);
	
	/*$('dt a').click(function(){
		$('dt a').css({color: '#064BF8'});
		$(this).css({color : '#F48010'});
		$('dd:visible').slideUp('slow');
		$(this).parent().next().slideDown('slow');
		loadContent('./views/products/' + $(this).attr('class') + '.php',$(this).attr('title'),2);
		return false;
	});
	
	$('dd a').click(function(){
		$('dd a').css({color: '#064BF8'});
		$(this).css({color : '#F48010'});
		loadContent('./views/products/' + $(this).attr('class') + '.php',$(this).attr('title'),2);
		return false;
	});*/
}

// Fonction de chargement du contenu
function loadContent(url,heightValue,container){
	if(url == "./views/.php" || url == "./views/.php") return;	
	$('div#subContentData').html('<img src="images/loading.gif" alt="" />');
	if (heightValue != null) {
		$('ul#submenu').css({
			height: heightValue + 'em'
		});
	}
	
	$.get(url, null, function(response){
		if (response == '' && url != './views/blank.php') {
			response = 'Error loading content...';
		}		
		if (container == 1) {
			$('div#contentData').html(response);
		} else {
			$('div#subContentData').html(response);
		}
		makeToggleMenus();
	}, 'html');	
}




/*******************************************
* Fonction de gestion des sous-menus 
******************************************/

// Fonction principale
function manage_subMenu() {
	$('ul.buttonList a').click(function(){		
		$('ul.buttonList a.active').removeClass('active');
		$(this).addClass('active');
		$('div#subContentData div.active').attr('class','unactive');
		
		
		if ($(this).attr('id').substr(4,1)=="A") {
		 id = 'data' + $(this).attr('id').substr(5,1);
		 $('div#subContentData div#' + id).attr('class','active');
		}
		else {
		 id = 'data' + $(this).attr('id').substr(4,1);
		 $('div#subContentData div#' + id).attr('class','active ovh');
		}
		
		
		
		
		
	});
}




/*******************************************
* Fonction d'animation des vignettes 
******************************************/

// Fonction principale
function animate_thumbnails(blockId) {
	$('div.imgHoverBlock' + blockId + ' img').each(function(){
		var element = $(this);
		
		var leftValue = element.css('left');
		var topValue = element.css('top');
		var heightValue = element.css('height');
		var enlargeHeight = (parseInt(heightValue)*2) + 'px';
		var widthValue = element.css('width');
		var enlargeWidth = (parseInt(widthValue)*2) + 'px';
		
		$(this).hoverIntent(function(){
			$('div.imgHoverBlock img').each(function(){
				setIndex($(this),'9');
			});
			
			setIndex($(this),'10');
			
			element.animate({
				left: parseInt(leftValue.substr(0,leftValue.length-2)) - parseInt((widthValue.substr(0,widthValue.length-2)/2)),
				top: parseInt(topValue.substr(0,topValue.length-2)) - parseInt((heightValue.substr(0,heightValue.length-2)/2)),
				height: enlargeHeight,
				width: enlargeWidth
			}, 200, 'swing');
		}, function(){
			element.animate({
				left: leftValue,
				top: topValue,
				height: heightValue,
				width: widthValue
			}, 200, 'swing');
		});
	});
}
