/* ############################################################################ */
(function($){
/* ############################################################################ */
/******************************************************************************
* + Public jQuery API
*/
$.fn.extend(
{
	//+ jQuery $('div.tgz-nav').TGZNav({});
	/*jQuery*/TGZNav : function(params)
	{
		params = params ? params : {};
		
		return this.each(function()
		{
			params.container = $(this);
			TGZNav.Instance(params);
		});
	}
});
/******************************************************************************
* # TGZNav (Syngleton)
* 
* @author Antoine RODES arodes@next-idea.fr
* 
* Dependences :
* - jquery.js
* - jquery.hoverIntent.js
* - gabarit.css
* 
* HTML :
* 
*	<div class="tgz-nav">
*		<ol class="tgz-nav-prim">
*			<li>
*				<a href="#">Vôtre Univers</a>
*				<ol>
*					<li><a href="#">Particuliers</a></li>
*					<li><a href="#" class="selected">Entreprises</a></li>
*					<li><a href="#">Collectivités</a></li>
*				</ol>
*			</li>
*		</ol>
*		<ol class="tgz-nav-sec">
*			<li>
*				<a href="#">Item 1</a>
*				<ol>
*					<li>
*						<a href="#">Item 1.1</a>
*						<ol>
*							<li><a href="#">Item 1.1.1</a></li>
*							<li><a href="#" class="selected">Item 1.1.2</a></li>
*							<li><a href="#">Item 1.1.3</a></li>
*						</ol>
*					</li>
*					<li><a href="#">Item 1.2</a></li>
*				</ol>
*			</li>
*			<li>
*				<a href="#">Item 2</a>
*				<ol>
*					<li>
*						<a href="#">Item 2.1</a>
*						<ol>
*							<li><a href="#">Item 2.1.1</a></li>
*							<li><a href="#">Item 2.1.2</a></li>
*							<li><a href="#">Item 2.1.3</a></li>
*						</ol>
*					</li>
*					<li><a href="#">Item 2.2</a></li>
*				</ol>
*			</li>
*			<li><a href="#">Item 4</a></li>
*			<li><a href="#">Item 5</a></li>
*		</ol>
*	</div>
*/
var TGZNav = function(){this.init.apply(this, arguments);};
TGZNav._Instance = null;TGZNav.Instance = function(params){if(null == TGZNav._Instance)TGZNav._Instance = new TGZNav(params);return TGZNav._Instance;};
TGZNav.prototype = 
{
	/*- void*/init : function(params)
	{
		this.opts = $.extend({
			container	: null,
			olNavPrim	: null,
			olNavSec	: null
		}, params );
		
		this.opts.olNavPrim = this.opts.container.find('ol.tgz-nav-prim');
		this.opts.olNavSec	= this.opts.container.find('ol.tgz-nav-sec');
		
		// In this order for chrome ?...
		this.initNavSec();
		this.initNavPrim();
	},
	/*- void*/initNavPrim : function()
	{
		var spaceLink			= this.opts.olNavPrim.find(' > li > a');
		var selectedLink		= (this.opts.olNavPrim.find(' > li > ol > li > a.selected').size() != 0 ) ? this.opts.olNavPrim.find(' > li > ol > li > a.selected') : null;
		var spaceTxt			= spaceLink.text();
		var selectedSpaceTxt	= (null==selectedLink) ? spaceTxt : selectedLink.text();
		var ol					= this.opts.olNavPrim.find(' > li > ol');
		
		spaceLink.text(selectedSpaceTxt);
		spaceLink.attr('href', (null==selectedLink) ? '/' : selectedLink.attr('href'));
		ol.find(' > li:first > a').css('border-top', 'none');
		
		this.opts.olNavPrim.find(' > li').hoverIntent
		(
			{
				over :function()
				{
					spaceLink.text(spaceTxt);
					ol.slideDown(250);
				},
				timeout : 300,
				out : function()
				{
					spaceLink.text(selectedSpaceTxt);
					ol.slideUp(250);
				}
			}
		);
	},
	/*- void*/initNavSec : function()
	{
		// link:first level 1 border
		this.opts.olNavSec.find(' > li:first > a').css('border-left', 'none');
		
		// default ol level2 selected
		var defaultOlLevel2Selected = null;
		if(this.opts.olNavSec.find('a.selected').size() == 1)
		{
			this.opts.olNavSec.find('li:has(a.selected) > a').addClass('selected');
			
			if(this.opts.olNavSec.find(' > li > ol:has(a.selected)').size() == 1)
			{
				defaultOlLevel2Selected = this.opts.olNavSec.find(' > li > ol:has(a.selected)');
			}
			else
			{
				defaultOlLevel2Selected = this.opts.olNavSec.find(' > li:has(a.selected) > ol');
			}
			
			defaultOlLevel2Selected.slideDown(250);
		}
		
		// link level 2
		this.opts.olNavSec.find(' > li > ol > li > ol').each(function()
		{
			var link = $(this).parent().find(' > a');
			link.html(link.text() + '<span class="arrow">&nbsp;&nbsp;▼</span>');
		});
		
		// ol level 2
		this.opts.olNavSec.find(' > li').hoverIntent
		(
			{
				over : function()
				{
					if(null != defaultOlLevel2Selected)
					{
						if(defaultOlLevel2Selected.get(0) == $(this).find(' > ol').get(0) && defaultOlLevel2Selected.css('display') == 'block')return;
						defaultOlLevel2Selected.hide();
					}
					
					$(this).find(' > ol').delay(450).slideDown(250);
				},
				timeout : 700,
				out : function()
				{
					$(this).find(' > ol').hide();
				}
			}
		);
		
		// link level 1
		this.opts.olNavSec.find(' > li').hover
		(
			function()
			{
				if(null != defaultOlLevel2Selected)defaultOlLevel2Selected.prev().removeClass('selected');
				$(this).parent().find('a').removeClass('over');
				$(this).find(' > a').addClass('over');
			},
			function(){}
		);
		
		// ol level 3
		this.opts.olNavSec.find(' > li > ol > li').hoverIntent
		(
			{
				over : function()
				{
					// stop and hide other ol level 3
					$(this).parent().find('ol').stop().hide();
					$(this).find(' > ol').slideDown(150);
				},
				timeout : 1,
				out : function()
				{
					$(this).find(' > ol').hide();
				}
			}
		);
		
		// ol level 1
		this.opts.olNavSec.hoverIntent
		(
			{
				over : function(){},
				timeout : 700,
				out : function()
				{
					if(null != defaultOlLevel2Selected)
					{
						$(this).parent().find('a').removeClass('over');
						defaultOlLevel2Selected.prev().addClass('selected');
						defaultOlLevel2Selected.show(0);
					}
					else
					{
						$(this).parent().find('a').removeClass('over');
					}
				}
			}
		);
	}
};
/* ############################################################################ */
})(jQuery);
/* ############################################################################ */
