/* Plugin jQuery
** Description : Permet de réaliser des sites web simples à navigation dynamique
** Dépendances : plugin History
** Date de création : 3 Mars 2011
** Dernière modification : 5 Mars 2011
** Exemple d'utilisation :
{
	pages : {
		fullPages : ["presentation", "galerie", "references", "contact"],
		integratedPage : ["galerie-view"],
		jsPath : "../pages/$1/javascript.js",
		contentPath : "../pages/$1/$1.html",
		pageInURL : "/$1/.?"
	},
	
	defaultLoadWith : "presentation"
}

$(document).SiteVitrine(pages);

**
*/

(function($){

	$.fn.SiteVitrine = function(o){
		
		/* * * * * * * * * * * * * */
		/* * variables  globales * */
		
		var currentPage = 0;
		var pagesLoaded = [];
	
		/* fin  variables globales */	
		/* * * * * * * * * * * * * */
	
	
		/* * * * * * * * * * * */
		/* fonctions  internes */
		
		
		/*function getDossierFromURI(uri){
			
			uri = uri.substring(0, uri.indexOf('/'));
			
			return uri;
		}*/
		
		function DechargerLaPage(page){
		
			if(!currentPage){ /* premier chargement */
				ChargerLaPage(page);
				return;
			}
				
			if($.isFunction(window[currentPage+"Leave"])){
				window[currentPage+"Leave"]();
			}
			
			$("#contenu").fadeOut(500, function(){
				ChargerLaPage(page);
			});
		}
		
		function ChargerLaPage(page){
			
			currentPage = page;
			
			$("#contenu").load("/"+page+"/"+page+".html?js=1", function(){
				$.getScript("./pages/"+page+"/javascript.js",
					function(){
						if($.isFunction(window[page])){
							window[page]();
						}
						$("#contenu").fadeIn(500, function(){
							loadEnded();
						});
					}
				);
			})
			
			
		}
		
		function loadStarted(){
			
			$("#indicationChargement")
				.animate({
					top: "10px"
				});
		}
		
		function loadEnded(){
			$("#indicationChargement")
				.animate({
					top: "-35px"
				});		
		}
		
		$.address.change(function(event) { 
			
			var hash = event.value;
			
			//On remonte
			$.scrollTo(0,400, {easing:"swing"});
			
			
			if(hash == "galerie"){
				return;
			}
			
			if(galerieOuverte == true){
				FermerLaGalerie();
				return;
			}
			
			if(hash == "/"){
				var page = o.startWith;
			}else
				var page = getDossierFromURI(hash);
			
			if(page == currentPage)
				return;
			
			if($.inArray(page, o.pages) != -1){
				loadStarted();
				DechargerLaPage(page);	
			}
		})
		
		/*function ClicOnLink(hash){

			if(hash == "galerie"){
				return;
			}
			
			if(galerieOuverte == true){
				FermerLaGalerie();
				return;
			}
			
			if(hash == "")
				var page = o.startWith;
			else
				var page = getDossierFromURI(hash);
			
			if(page == currentPage)
				return;
			
			
			if($.inArray(page, o.pages) != -1)
				DechargerLaPage(page);
		}*/
			
		/* * * * * * * * * * */
		/* configuration css */
		
		$("#contenu").hide();
		
		
		/* * * * * * * * * * **/
		/* avant de commencer */
		
		//$.history.init(ClicOnLink);
		
		$("body")/*
			.append('<div id="hidder"></div>')
			.find("#hidder")
				.css({
					position: 'fixed',
					width: "100%",
					height: "100%",
					top: '0px',
					background: "#fff"
				})
				.end()*/
			.find('#header img')
				.one("load", function(){
					$("#hidder")
						.fadeOut(500, function(){
							$(this).remove();
						})
				})
				.each(function(){
					if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
						$(this).trigger("load");
				})
				.end()
			.append('<div id="indicationChargement"><img src="style/images/loading.gif" alt="" /> Chargement</div>')
			.find("#indicationChargement")
				.css({
					position: 'fixed',
					background: '#fff',
					top: "-35px",
					right: '40px',
					border: '1px solid #000',
					padding: '5px 10px',
					color: 'black'
				})

		
		$(this)
		.find('a[class!="external"]')
			.bind("click", function(){
				
				if($.browser.msie){
					var url = $(this).get(0).getAttribute('href', 2);
				}
				else{
					var url = $(this).attr('href');
				}
				
				//$.history.load(url);
				$.address.value($(this).attr('href'));
					
				return false;
			});
		
		//ChargerLaPage(o.startWith);
		
	};
	
	function getDossierFromURI(uri){
		//alert('uri : ' + uri + ' \n 1e arg' + (uri.indexOf('/')+1) + '\n2e arg ' + uri.length)
		uri = uri.substring(uri.indexOf('/')+1, uri.length);
		//alert('uri: ' + uri + '\n 2e arg' + uri.indexOf('/'));
		uri = uri.substring(0, uri.indexOf('/'));
		//alert('uri :' +uri);
		
		return uri;
	}
	
	$(document).ready(function(){
		var pageToDisplay = getDossierFromURI(document.location.pathname);
		if(pageToDisplay == "")
			pageToDisplay = "presentation"
		$(this).SiteVitrine(
			{
				pages : ["presentation", "galerie", "references", "partenaires", "contact"],
	
				startWith : pageToDisplay
			}
		);
	});

})(jQuery);



