/*!
 *
 * TERMS OF USE - HAPPY END APPLICATION
 *
 * La présentation et chacun des éléments, y compris les marques, logos, dessins, 
 * illustrations, textes, graphiques et autres fichiers réalisés et transmis par 
 * Happy end, sont protégés par les lois en vigueur sur la propriété intellectuelle, 
 * et appartiennent à Happy End et font l'objet d'une autorisation d'utilisation.
 * 
 * Aucun de ces éléments ne peut être copié, reproduit, modifié, réédité, chargé, 
 * dénaturé, transmis ou distribué de quelque manière que ce soit, sous quelque 
 * support que ce soit, de façon partielle ou intégrale, sans l'autorisation écrite 
 * et préalable d'Happy End à l'exception d'une stricte utilisation pour les besoins 
 * de l'exploitation considéré et sous réserve du respect des droits de propriété 
 * intellectuelle et de tout autre droit de propriété dont il est fait mention.
 * 
 */
 
/* Happyend Bootstrap */

/*
   namespace: He

   bootstrap Happyend

*/

He = 
{
	IsUndefined: function(a)
	{
		return typeof(a) == "undefined";
	},
	
	IsFunction: function(a)
	{
		return typeof(a) == "function";
	},
	
	IsNumber: function(a)
	{
		return typeof(a) == "number" && !isNaN(a);
	},

	IsObject: function(a)
	{
		return a !== null && typeof(a) == 'object';
	},
	
	ApplyParam: function(a)
	{
		return (!He.IsUndefined(a) && a != '')
	},

	Params:
	{
		Version			: 1,
		Debug			: true,
		Idevice			: null,
		BadBrowser		: null,
		PixelDotGif		: '/static/images/artpack/spacer.gif',
		Cufon			: true,
		ViewPort		:
		{
			MinHeight	: 595,
			MinWidth	: 980
		}
	},
	
	EventsDictionary: null,

	/*
		Function: He.Debug
		
		un wrapper de console.log. Evite les erreurs si la console n'est pas ouverte ou n'existe pas (ex: IE < 8).
		*Pour s'affiche la variable He.Params.Debug doit etre egale à TRUE*
		
		Parameters:
		
		_str - une chaine, object, tableau, etc.. a afficher dans la console.
		_force - un boolean qui force l'affiche de _str dans une fenetre modale (chaine uniquement)

	*/

	Debug: function(_str, _force)
	{
		if(He.Params.Debug)
		{
			if(typeof console!="undefined" && console.log)
			{
				console.log(_str);
			}
			else if(_force)
			{
				alert(_str);
			}
		}
	}
};

/*
	namespace: He.Application
	
	@PRIVATE
	Bootstrap des fonctions applicatives 

*/

He.Application =
{
	ViewPort: function(_full)
	{
		var _docWidth		= document.body.offsetWidth || document.documentElement.offsetWidth;
		var _docHeight		= window.innerHeight || document.documentElement.clientHeight;
		var _css			= {};
		var _jewelHeight	= document.getElementById('jewel').offsetHeight;
		var _funnel			= $('#funnel-main-header');
		
		if(_funnel.size() == 0)
		{
			if(_jewelHeight <= He.Params.ViewPort.MinHeight)
			{
				
				if(_docHeight <= He.Params.ViewPort.MinHeight)
				{
					_css['top']			= 0;
					_css['marginTop']	= 0;
				}
				
				if(_docWidth <= He.Params.ViewPort.MinWidth)
				{
					_css['left']		= 0;
					_css['marginLeft']	= 0;
				}
				
				if(_full)
				{
					if(_docHeight >= He.Params.ViewPort.MinHeight)
					{
						_css['top']			= '50%';
						_css['marginTop']	= Math.round((He.Params.ViewPort.MinHeight / 2) * -1);
					}
					
					if(_docWidth >= He.Params.ViewPort.MinWidth)
					{
						_css['left']		= '50%';
						_css['marginLeft']	= Math.round((He.Params.ViewPort.MinWidth / 2) * -1);
					}
				}
			}
			else
			{
				if(_docHeight > _jewelHeight)
				{
					_css['top']			= '50%';
					_css['marginTop']	= Math.round((_jewelHeight / 2) * -1);
				}
				else
				{
					_css['top']			= 0;
					_css['marginTop']	= 0;
				}
			}
			
			$('#jewel').add('#metaNav').css(_css);
		}
		else
		{
			_css['top']			= 0;
			_css['marginTop']	= 0;
			
			_funnel.css(_css);
			$('#jewel').add('#metaNav').css(_css);
			
		}
//		$('#funnel-main-header').css(_css);
	},
	
	FauxColumn : function()
	{
	
		if(jQuery.browser.msie)
		{
			var row	= $('div.fauxtable_row');
			
			if(row.size() > 0)
			{
				row.each(function()
				{
					var cells = $(this).find('div.fauxtable_cell');
					
					var tallest = 0;
					cells.each(function()
					{
						var thisHeight = $(this).height();
						if(thisHeight > tallest)
						{
							tallest = thisHeight;
						}
					});
					cells.height((tallest +1));
				});
			}
		}
	},
	
	FixPng: function(png)
	{
		var src = png.src;		
		
		// set width and height
		if(!png.style.width)
		{
			png.style.width = $(png).width();
		}
		
		if(!png.style.height)
		{
			png.style.height = $(png).height();
		}
		
		// replace by blank image
		png.onload = function() { };
		png.src = He.Params.PixelDotGif; 
		// set filter (display original image)
		png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";

	},
	
	Init: function()
	{
		// Generic Components Launcher
		//He.Components.ScrollBar.Init('.custom-scroll')
		
		if(He.Params.BadBrowser)
		{
			
			$('#container').find('img.png').each(function()
			{
				if (!this.complete)
				{
					this.onload = function() { He.Application.FixPng(this) };
				}
				else
				{
					He.Application.FixPng(this);
				}
			});
		}
	}
};

/*
	namespace: He.Components
	
	Bootstrap des composants

*/

He.Components = {};


// On définie certain params au démarage,
// on corrige l'alignement vertical si heuteur < 595
(function()
{
	if(typeof(He_Cufon) != "undefined")
	{
		He.Params.Cufon = He_Cufon;
	}
	
	He.Application.ViewPort();
	
	// Définitions
	He.Params.Idevice		= (/iPad|iPhone|iPod/.test(navigator.userAgent));
	He.Params.BadBrowser	= ($.browser.msie && ($.browser.version < 7));
	
	// Dictionaire des évenements 
	// selon que l'on soit sur un device tactille ou non
	if(He.Params.Idevice)
	{
		var _events = 
		{
			Click: 'touchstart',
			Hover: 'touchstart'
		}
	}
	else
	{
		var _events = 
		{
			Click: 'click',
			Hover: 'mouseover'
		}
	}
	
	He.EventsDictionary = _events;
})();


// Ajuste la position de l'écrin 
// au resize de la  page
$(window).resize(function()
{
	He.Application.ViewPort(true);
});

if(!He.Params.Idevice && He.Params.Cufon)
{
	Cufon.replace('.times', { fontFamily: 'Times New Roman' , hover: true });
	Cufon.replace('.times-italic', { fontFamily: 'Times New Roman italic' , hover: true });
}

// Quand Cufon est pret on affiche les titres
$(document).bind('CUFON_READY', function()
{
	$('.cufon-fade').fadeIn('slow');
	$('.cufon-go').show();
});

if(He.Params.Idevice || !He.Params.Cufon)
{
	$(document).trigger('CUFON_READY');
}

