/**
 * @brief			Contient des fonctions communes à tout le site
 * @to 17/05/2010	Renan	Mise en place pour Secteur18
 * @to 12/07/2010	Renan	Navigation ajax (ajout pageLoad)
 */
var isIe = /*@cc_on!@*/false;


var $lienMenus = $('#menuCentral > span > a, #menuPrincipal > a, .ficheOptions > a, .ficheTexte > a, #post-it-2 > div > ul > li > a, #forum > a').filter('[href^=/game/#]');



// Quick fix indexOf IE 6/7/8
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}



function pageLoad(hash,onglet)
{
	$(document).ready(function(){
		//stopSoundFlash('secteur18_sound', null); //on stope les sons en cours	
		setTimeout( "stopSoundFlash('secteur18_sound',null)", 500 );
	});
	
	
	var lienFinal;						//le lien final de la page
	hash = hash.replace(/^.*#\//, ''); 	// suppression de tout ce qu'il y a avant #/ + #/

	// affiche le chargement en cours
	$('#loading').show();
	$('#profil-joueur').hide();
	$('#content').html('');
	$(document).unbind('.click');
	viewCaserne(false);	
	

	//=> Module Messagerie
	if(hash.indexOf('messagerie')>= 0) 
	{
		var details = hash.replace('/messagerie', '').split('/');
		
		if(details[1] == 'index' || typeof(details[1]) == 'undefined')
		{
			hash = '/messagerie/index';
			lienFinal = '/fr/game/messagerie/index.php';
		}
		else if(details[1] == 'signalement')
		{			
			if(details[2] >0)
			{
				id= details[2];
				if(details[3] == "ok")
				{
					hash = '/messagerie/signalement/'+id;
					lienFinal = '/fr/game/messagerie/signalement.php?id='+id+"&ok=1";
				}
				else
				{
					hash = '/messagerie/signalement/'+id;
					lienFinal = '/fr/game/messagerie/signalement.php?id='+id;
				}
			}
			else
			{
				hash = '/messagerie/signalement';
				lienFinal = '/fr/game/messagerie/signalement.php';
			}
		}
		
	}
	
	
	//=> Module Pompier
	else if (hash.indexOf('pompier') >= 0) {
		var details = hash.replace('/pompier', '').split('/');
		lienFinal = '/fr/game/pages/pompier/';
		
		if (details[1] == 'voir' || typeof(details[1]) == 'undefined') {
			if (is_numeric(details[details.length - 1])) {
				var tri = details[details.length - 2];
				var numPage = details[details.length - 1];
				lienFinal += 'voir.php?tri=' + tri + '&numPage=' + numPage;
			}
			else {
				var tri = details.length > 1 ? details[details.length - 1] : 'nom';
				lienFinal += 'voir.php?tri=' + tri;
			}
		}
		else if (details[1] == 'licencier') {
			if (is_numeric(details[details.length - 1])) {
				var tri = details[details.length - 2];
				var numPage = details[details.length - 1];
				lienFinal += 'licencier.php?tri=' + tri + '&numPage=' + numPage;
			}
			else {
				var tri = details.length > 2 ? details[details.length - 1] : 'nom';
				lienFinal += 'licencier.php?tri=' + tri;
			}
		}
		
		else 
			lienFinal += details[1] + '.php'; //lien par défault
	}
	//=> Module Caserne
	else if (hash.indexOf('caserne') >= 0) {
		lienFinal = '/fr/game/pages/';
		
		var details = hash.split('/');
		if( details.length > 2 && details[1]=='collector')
		{
			var numPage = is_numeric(details[2]) ? details[2]:1;
			lienFinal += 'caserne/collector.view.php?p='+numPage;
		}
		else if( details.length > 2 && details[1]=='intervention')
		{
			if(details[2]=='supprimer')
				lienFinal += 'caserne/intervention.php?supprimer='+details[3];
			else
				lienFinal += 'caserne/intervention.php?action='+details[2];			
		}
		else if (typeof(details[1]) == 'undefined' || details[1] == '') {
			lienFinal += details[0] + '/accueil.php';
		}
		else 
			lienFinal += hash + '.php';
	}
	
	//=> Module Marché
	// on demande à afficher un magasin en particulier (le marché de l'occasion est cahrgé directement)
	else if(hash.indexOf('marche') >= 0 && hash.indexOf('marche/occasion')<0) { 
		var options=hash.replace('marche/rayon/', '').split('/');
		
		var nomMagasin 	= 	options[0];
		var pageNum		=	parseInt(options[1]);
		if( isNaN(pageNum)) pageNum=1;
		
		hash = '/fr/game/pages/marche/rayon';
		lienFinal=  hash + '.php?mag='+nomMagasin+'&page='+pageNum;
	}
	
	//=> Module Marché de l'occasion
	else if(hash.indexOf('marche/occasion')>=0)
	{
		var options=hash.replace('marche/occasion', '').split('/');
		var nomMagasin 	= (typeof(options[1])=='undefined' || options[1]=='') ? 'vehicules':options[1];
		var pageNum		= is_numeric(options[2]) ? options[2]:(is_numeric(options[3]) ? options[3]:1);
		var tri			= (!is_numeric(options[2]) && typeof(options[2])!='undefined') ? '&tri='+options[2]:'';
		
		hash = '/fr/game/pages/marche/occasion';
		lienFinal=  hash + '.php?rayon='+nomMagasin+'&page='+pageNum+''+tri;
	}
	
	//=> Aire de feu
	else if(hash.indexOf('aire_de_feu/')>=0)
	{
		var options=hash.replace('aire_de_feu/', '').split('/');
		hash = '/fr/game/pages/aire_de_feu';
		lienFinal=  hash + '.php?view='+options[0];
	}
	
	//=> VIP
	else if(hash.indexOf('vip')>=0)
	{		
		var options=hash.replace('/vip', '').split('/');
				
		hash = '/fr/game/pages/vip/index';
		lienFinal=  hash + '.php';
		if(options.length > 1) 
			lienFinal += '?go='+options[1];
		
		if( is_numeric(options[2]) )
			lienFinal += '&pompier='+options[2];
	}
	
	//=> Module Alliance
	else if (hash.indexOf('alliance') >= 0) {
		var details = hash.replace('/alliance', '').split('/');
		
		if (details.length > 2 && details[1] == 'rejoindre') 
		{
			if( is_numeric(details[2]))
				lienFinal = '/fr/game/pages/alliance/accueil.php?action=rejoindre&alliance=' + details[2];
			else
				lienFinal = '/fr/game/pages/alliance/accueil.php?action=rejoindre&decision='+details[2]+'&membre=' + details[3];
		}
		else if (details.length > 2 && details[1] == 'actVote') {
			lienFinal = '/fr/game/pages/alliance/accueil.php?action=actVote&decision='+details[2]+'&id=' + details[3];
		}
		else if ( hash.indexOf('alliance/voir/') >= 0 )
		{
			var numPage = details[details.length - 1] > 1 ? details[details.length - 1]:1;
			lienFinal = '/fr/game/pages/alliance/accueil.php?action=voir&numPage=' + numPage;
		}
		
		else {
			var action = details.length > 1 ? details[1] : null;
			
			if( action != null )
			{
				var numPage = details[details.length - 1];
				var nHash = '/alliance/' + action;
				
				hash = nHash;
				lienFinal = '/fr/game/pages/alliance/accueil.php?action=' + action;
				if (!is_numeric(numPage)) 
					numPage = 1;
				
				if (action == 'chat') 
					lienFinal += '&numPage=' + numPage;
			}
			else
				lienFinal = '/fr/game/pages/alliance/accueil.php';
		}
	}
	
	//=> Historique
	else if(hash.indexOf('administration/historique')>=0)
	{
		var options=hash.replace('administration/historique', '').split('/');
		var filtre = (options[1] == undefined)? "all":options[1];
		var pageNum		= is_numeric(options[2]) ? options[2]:1;
		
		hash = '/fr/game/pages/administration/historique';
		lienFinal=  hash + '.php?page='+pageNum+'&filtre='+filtre;
	}

	
	//=> Gestion des modules ayant un repertoire avec une page par défault (acceuil)
	//---	Administration
	else if (hash.indexOf('administration') >= 0) 
	{
		var details = hash.split('/');
		lienFinal = '/fr/game/pages/';
		
		if (typeof(details[1]) == 'undefined' || details[1] == '') {
			lienFinal += details[0] + '/accueil.php';
		}
		else 
			lienFinal += hash + '.php';
	}
	
	
	//=> Sport
	else if(hash.indexOf('sport/olympiade')>=0)
	{
		var options	= hash.replace('sport/olympiade', '').split('/');
		var pageNum	= is_numeric(options[1]) ? options[1]:1;
		
		hash = '/fr/game/pages/sport/olympiade';
		lienFinal=  hash + '.php?page='+pageNum;
	}
	
	
	//=> DEFAULT
	else
		lienFinal = '/fr/game/pages/' + hash + '.php'; 

	//--- CHARGEMENT DE LA PAGE SOUHAITE ---
	$('#content').load(lienFinal, function()
	{
		$('#infobulleSecteur18').css('display','none');
		var h1 = $('#content > h1').text();
		if(h1.length > 0)
			document.title = h1;
		else
			document.title = 'Secteur18';
		
		//on retire le loading après 500ms du chargement total de la page
		setTimeout(function(){
			$('#loading').hide();
			reloadEcouteur();
		}, 500);
		
		
	});
}

function reloadEcouteur( firstLoad ){
	
	//--- Affecte le bon style de bouton au input type submit et button, + button
	$('input[type=button], input[type=submit]').each(function(){ classButton('input', this); });
	$('button').each(function(){ classButton('button', this); });

	
	
	
	//--- TRAITEMENT DES FORMULAIRE AJAX
	$('form.ajaxForm input[name=submit][type=button]').die('click').live('click', function(event){
		var formulaire	= $(this).parents('form.ajaxForm');
		
		var nameOfForm 	= formulaire.attr('name');
		var textConfirm = $('form[name='+ nameOfForm +'] > input[name=ajaxFormConfirm]').val();
		$.alerts.okButton 		= 'Oui';
		$.alerts.cancelButton 	= 'Non';
	
		//demande confirmation ?
		if( (typeof(textConfirm)!='undefined') )
		{
			jConfirm(textConfirm, 'Confirmation', function(r){
				if(r)
					ajaxForm(nameOfForm);
			});
		}
		else
			ajaxForm(nameOfForm);
	});
	
	
	//thickbox
	tb_init('.thickbox');//pass where to apply thickbox
	imgLoader 		= new Image();// preload image
	imgLoader.src 	= '/fr/static/template/chargement.gif';
	
	/* Infobulle */
	$('* .infobulle').live('mouseover', function(){
		$('.infobulle').each(function(){
			if( $(this).data('qtip') ) { return true; }
			
			$(this).qtip({ 
				position: { target:'mouse', adjust:{ mouse:true, x:15, y:5, resize:true } },
				show	: { delay:0 },
				style	: { width: { max:5000 } }	
			});
		});		
	});
	
}


/**
 * FONCTION : Afficher la caserne (flash)
 * @param {Object} voir
 */
function viewCaserne( voir ){
	$villeSecteur = $('#ville_secteur18');

	if(voir)	// mode on force l'affichage
	{
		$villeSecteur
				.css('height','500px')
				.show()
				.removeClass('cacher');
			$('#viewcaserne-button').addClass('activate');
		
		playSoundFlash( 'secteur18_sound', 'ambianceVille', 1000 );
		
	}
	else if(voir == false)
	{
		$villeSecteur
				.hide()
				.addClass('cacher');
		$('#viewcaserne-button').removeClass('activate');
		
		stopSoundFlash( 'secteur18_sound', 'ambianceVille' );
	}
	else
	{
		if( $villeSecteur.hasClass('cacher'))
		{
			viewCaserne(true);
		}
		else
		{
			$('#viewcaserne-button').removeClass('activate');
			$villeSecteur.animate({height: '-=500'}, 800, function(){
				$villeSecteur.hide().addClass('cacher');
			});
			
			stopSoundFlash( 'secteur18_sound', 'ambianceVille' );
		}
	}
}

function mavilleLoad()
{
	pageLoad('accueil');
	viewCaserne(true);
}

$('document').ready(function(){
	
	//--- NAVIGATION AJAX
	if (window.location.hash.substring(0,2) == '#/' && window.location.toString().indexOf('/game/')>=0 )
	{	
		pageLoad( window.location.hash.substring(2) );
	}
	else if(window.location.hash.substring(0,2) == '#/')
	{
		window.location.hash = null;
	}
	
	reloadEcouteur();
	
	
	//--- MENU dynamique pour les modos
	$("ul.subnavModo").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	$("ul.navModo li span").bind('mouseover', function(){
		$(this).parent().find("ul.subnavModo").slideDown('fast').show(); //Drop down the subnav on click
		
		$(this).parent().hover( function() {}, function(){       
			$(this).parent().find("ul.subnavModo").slideUp('slow'); 
		});
		
	}).hover(function() {
		$(this).addClass("subhover");
	}, function(){        //On Hover Out
		$(this).removeClass("subhover");
	});
});

