/** 
 * @author 	Mediagong 2010
 * @version 	1.4 
 */

function trace(obj){
	if (!jQuery.browser.msie) {
		console.log(obj);
	}else{
		//alert(obj);
	}
}

var aNav 		= new Array();
var accroche 	= jQuery('#accroche');
var content		= jQuery('#content');
var body		= jQuery('body');
var nav 		= jQuery('.nav');

var myUrl 		= document.location.href;		
var temp 		= myUrl.lastIndexOf("/");
myUrl 			= myUrl.substr(temp+1);
var myUrlId 	= myUrl.split(".")[0];

jQuery.fn.customFadeIn = function(speed, callback) {
	jQuery(this).fadeIn(speed, function() {
		if(!jQuery.support.opacity)
			jQuery(this).get(0).style.removeAttribute('filter');
		if(callback != undefined)
			callback();
	});
};
jQuery.fn.customFadeOut = function(speed, callback) {
	jQuery(this).fadeOut(speed, function() {
		if(!jQuery.support.opacity)
			jQuery(this).get(0).style.removeAttribute('filter');
		if(callback != undefined)
			callback();
	});
};
jQuery.fn.customFadeTo = function(speed,to,callback) {
	return this.animate({opacity: to}, speed, function() {
		if (to == 1 && jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};

/**
 * @classDescription MG main Javascript Class
 */				
var MG 			= {	
	/**
	* Initialization of MG class
	*/
	init: function (choice){
		var Manage = {
			Init: function(){
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
				}else{
					accroche.css({
						width: "0px"
					});
					
					body.removeClass('js');
					
					content.customFadeOut(0);
					body.customFadeOut(0);					
				}
			},
			Action: function(choice){
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
					MG.toJpg('img.tojpg');					
				}else{
					MG.navigation(choice);	
					if (choice == 0) {
						MG.intro();
					}
				}
			}
		}
		if(choice == 0)
			Manage.Init();
		Manage.Action(choice);
	}
	,
	toJpg: function (obj){
		var Manage = {
			Init: function(){								
			},
			Action: function(obj){
				jQuery(obj).each(function(){
					var t = jQuery(this);					
					trace('from :'+t.attr('src'));
					t.attr('src', t.attr('src').replace('.png', '.jpg'));				
					trace('to :'+t.attr('src'));				
				});
			}
		}
		Manage.Init();
		Manage.Action(obj);
	}
	,/**
	* Navigation Animation of MG class
	*/
	navigation: function (choice){
		var Manage = {
			Init: function(){				
			}
			,
			Action: function(choice){	
				nav.each(function(){		
					aNav.push(jQuery(this));
					var itemNav = jQuery(this);
							
					var jQuerytexte = itemNav.find('.texte a');
					var jQuerymoins = itemNav.find('.moins');
					
					if (choice == 0) {
						jQuerytexte.css({
							backgroundPosition: "0 -30px"
						})
						jQuerymoins.css({
							backgroundPosition: "-15px -15px"
						})
					}
				});		
				nav.mouseenter(function(){
					MG.hoverIt(jQuery(this),1);
				})
				nav.mouseleave(function(){
					MG.hoverIt(jQuery(this),0);
				});
				
				nav.bind("click",function(event) {
					if(jQuery(this).hasClass('currentNav')){
						event.preventDefault();
					}else{
						event.preventDefault();
						
						MG.currentIt(nav,0);
						MG.hoverIt(nav,0);
						
						MG.currentIt(jQuery(this),1);
						MG.hoverIt(jQuery(this),1);
						
						jQuerytoLoad = jQuery(this).find('a.ajax');
						if(jQuery(this).hasClass('direct')){
							document.location = jQuerytoLoad.attr('href');
						}else{
							content.customFadeOut("normal", function(){
								MG.loadContent(jQuerytoLoad.attr('href'));
							});
						}
					}
				});					
			}
		}
		Manage.Init();
		Manage.Action(choice);
	}	
	,
	/**
	* Intro Animation of MG class
	*/
	intro: function (){
		var Manage = {
			Init: function(){				
			}
			,
			Action: function(){		
				
				body.removeClass('js');
				
				body.customFadeIn(2000,function(){
					accroche.animate({width:"247px"},{duration:1000, easing:"easeInOutQuint", complete:
						function(){
							nav.each(
								function(i){									
									var jQueryli_current= jQuery(this);
									var jQuerytexte = aNav[i].find('.texte a');
									var jQuerymoins = aNav[i].find('.moins');
									if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
										jQuerymoins.stop().css({backgroundPosition:"(0 -15px)"});
										jQuerytexte.stop().css({backgroundPosition:"(0 0)"});
										if(i == 0){
											MG.currentIt(jQueryli_current,1);
											MG.hoverIt(jQueryli_current,1);											
										}
										if(i == 2){
											content.empty();
											var myUrlDefault = 'vision.html';
											MG.loadContent(myUrlDefault);
											content.customFadeTo(1000, 1, 'easeInOutQuint');
										}
									}else{
										jQuerymoins.stop().animate({backgroundPosition:"(0 -15px)"}, {duration:700+i*200, easing:'easeInOutQuint'});
										jQuerytexte.stop().animate({backgroundPosition:"(0 0)"}, {duration:700+i*200, easing:'easeInOutQuint', complete:
											function(){
												if(i == 0){
													MG.currentIt(jQueryli_current,1);
													MG.hoverIt(jQueryli_current,1);											
												}
												if(i == 2){
													content.empty();
													var myUrlDefault = 'vision.html';
													MG.loadContent(myUrlDefault);
													content.customFadeTo(1000, 1, 'easeInOutQuint');
												}
											}
										});
									}									
									
							});//each							
							
						}//animMenu
					});//accroche
				});//animIntro		
			}
		}
		Manage.Init();
		Manage.Action();
	}
	,
	/**
	* hoverIt of MG class
	*/
	hoverIt: function (obj,action){
		var Manage = {
			Init: function(){					
			}
			,
			addHover: function (obj){
				obj.addClass("hover");
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
					obj.find('.moins').stop().css({backgroundPosition:"(0 0)"});
					obj.find('a').stop().css({backgroundPosition:"(0 -15px)"});
				}else{
					obj.find('.moins').stop().animate({backgroundPosition:"(0 0)"}, {duration:300, easing:'easeInOutQuint'});
					obj.find('a').stop().animate({backgroundPosition:"(0 -15px)"}, {duration:300, easing:'easeInOutQuint'});
				}
			}
			,
			removeHover: function (obj){
				if(obj.hasClass('currentNav')){			
				}else{
					obj.removeClass("hover");
					if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
						obj.find('.moins').stop().css({backgroundPosition:"(0 -15px)"});
						obj.find('a').stop().css({backgroundPosition:"(0 0)"});
					}else{
						obj.find('.moins').stop().animate({backgroundPosition:"(0 -15px)"}, {duration:300, easing:'easeInOutQuint'});
						obj.find('a').stop().animate({backgroundPosition:"(0 0)"}, {duration:300, easing:'easeInOutQuint'});
					}
				}
			}
			,
			Action: function(obj,action){	
				if(action == 1)
					Manage.addHover(obj);
				else if(action == 0)
					Manage.removeHover(obj);			
			}
		}
		Manage.Init();
		Manage.Action(obj,action);		
	}
	,
	/**
	* Current of MG class
	*/
	currentIt: function (obj,action){
		var Manage = {
			Init: function(){					
			}
			,
			setCurrent: function (obj){
				obj.addClass('hover').addClass('currentNav');
			}
			,
			unsetCurrent: function (obj){
				obj.removeClass('hover').removeClass('currentNav');		
			}
			,
			Action: function(obj,action){	
				if(action == 1)
					Manage.setCurrent(obj);
				else if(action == 0)
					Manage.unsetCurrent(obj);			
			}
		}
		Manage.Init();
		Manage.Action(obj,action);		
	}
	,
	/**
	* loadContent of MG class
	*/	
	loadContent: function(href){		
		var Manage = {
			Init: function(){					
			}
			,
			Action: function(link){	
				jQuery.ajax({
					type:"GET",
					url:href,
					dataType:"html",
					success: function(data){
						var myData = jQuery(data).find("#datas");
						jQuery("#content").empty().append(myData).customFadeIn("normal");
						ManageForm.init();
					}
				});					
			}
		}		
		Manage.Action(href);			
	}	
} 

/* launching scripts */
jQuery(document).ready(function(){
	var choice = 0;
	if(myUrlId == 'contact'||myUrlId == 'vision'||myUrlId == 'clients'||myUrlId == 'presse')
		choice = 1;
	MG.init(choice);
});
