
jQuery(function($){ 


/**
* Embed Flash
*/
flashembed("mp3player", {src:"player.swf",id:"flashplayer",wmode: 'Transparent',scale: 'noscale'},{
	xmlPath: 'xml/songs.xml',
	autoplay: 'true',

});


/**
 * REPLACE LINKS TO 'javascript:;'
 * won't replace attributes if:
 * 1) /site/ is present in the URI
 * 2) <a tag contains rel="external"
 * 3) <a tag contains target="_blank"
 * --------------------------------------------------------- */
	var docLoc = window.location.href;
	
	//is it a search engine? 
	var isSe = docLoc.indexOf('/site/') != "-1" ? true : false;
	function removeLinks()
	{
		$('a').each(function(){
			if (isSe) {
				//resetNav();
				return false; //is not a search engine...
			}
			var url = $(this).attr('href');
			var rel = $(this).attr('rel').indexOf('external');
			var clas = $(this).attr('class').indexOf('tu_'); //Pour les élémenents Topup
			var ext = $(this).attr('target').indexOf('_blank');
			if (url != "http://www.fabriccreative.com" && url != docLoc && rel != "0" && ext != "0" && clas != "0")
			{	
				$(this).attr('href', 'javascript:;');
			}
		});
	}
	removeLinks();

/**
* BROWSER FIXES (IE5/6)
* --------------------------------------------------------- */
var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32"); 

/**
* RESIZE DOCUMENT
* --------------------------------------------------------- */
$(window).resize(function() {
	resizeBackground();
}); 

/**
 * RESIZABLE BACKGROUND IMAGE (WHEN DOC > 1920x1200)
 * --------------------------------------------------------- */
var objDocBg = $('#background-image img'); //Background image obj
objDocBg.hide(); //don't need show this unless the browser is size > 1920x1200;


function resizeBackground()
{
	//Gather browser and current image size
	var imagewidth = objDocBg.width();
	var imageheight = objDocBg.height();
	var browserwidth = $(window).width();
	var browserheight = $(window).height();
	var offset;
	//Only proceed, if the dimensions are greater than the image
	if ((browserwidth > 1920 || browserheight > 1200) && ! badBrowser ) //don't use this if IE5/6
	{
		//Show the background image
		objDocBg.show();
		//Define image ratio
		var ratio = 1200 / 1920;
		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio){
			objDocBg.height(browserheight);
			objDocBg.width(browserheight / ratio);
			objDocBg.children().height(browserheight);
			objDocBg.children().width(browserheight / ratio);
		} else {
			objDocBg.width(browserwidth);
			objDocBg.height(browserwidth * ratio);
			objDocBg.children().width(browserwidth);
			objDocBg.children().height(browserwidth * ratio);
		}
		//vertical center
		objDocBg.children().css('left', (browserwidth - objDocBg.width())/2);
		objDocBg.children().css('top', (browserheight - objDocBg.height())/2);
	}
	else
	{
		objDocBg.hide();
	}
}
resizeBackground(); 

/**
 * MENU
 * --------------------------------------------------------- */

		// set opacity to nill on page load
		$("ul#menu span").css("opacity","0");
		// on mouse over
		$("ul#menu span").hover(function () {
			// animate opacity to full
			$(this).stop().animate({
				opacity: 1
			}, "speed");
		},
		// on mouse out
		function () {
			// animate opacity to nill
			$(this).stop().animate({
				opacity: 0
			}, "slow");
		});
		
		$("ul#menu span").click(function() {
			loadContent($(this).parent().attr('class'));
		});


/**
 * AJAX LOAD CONTENT
 * --------------------------------------------------------- */
	function loadContent(page, json)
	{
		var objContent = $('#content');
		objContent.fadeTo(200, 0, function() {
			$.post(
				hostname +'site/' + page +"/",
				{
					json : $.toJSON(json)
				},
				function(html)
				{

					objContent
						.html(html)
						.fadeTo(500, 1, function() {
							eval(page)(name); 
							removeLinks();
						});
					
				}
			);
		});
	}

/**
 * CALLBACKS (FROM AJAX)
 * --------------------------------------------------------- */	

	function accueil()		
	{ 
		$('#slider').nivoSlider();
	
		arWidgetItems = $('.widget'); //tableau d'objets widgets
		loadWidgets(0);		
		
		//Liens vers les news
		$('.widget-news a').click(function() {
			//resetNav();
			json = { id : this.id }
			loadContent('news', json);
		});
		//Liens vers les dates de concerts
		$('.widget-agenda a').click(function() {
			//resetNav();
			loadContent('agenda');
		});
		//Boutique, Galerie...
		$('a', '.widget-album, .widget-divers').click(function() {
			var objParent = $(this).parent('div');
			// Boutique
			if (objParent.hasClass('widget-album'))
			{
				loadContent('boutique', { })
			}
			//Galerie
			if (objParent.hasClass('widget-divers'))
			{
				loadContent('galerie', { })
			}
			
		});

		
	}	
	//accueil(); //Page par défaut
	loadContent('accueil', { });
	
	function bio()	
	{ 
	}
	function pros()
	{
	}
	function contact()
	{
	}
	
	function news()
	{
		//Liens de news
		$('#news-headlines a').click(function() {
			json = { id : this.id }
			loadContent('news', json);
		});

	}
	initPage('news');

	
	function disco()
	{	

	}

	function boutique()
	{	

	}
	function agenda()		
	{ 
		$('.tour-dates tr')
			.mouseover(function() {
				if ( $(this).hasClass('tour-heading') ) return false;
				$('td', $(this)).css({'backgroundColor' : '#1e1f19', 'color' : 'white', 'border-top' : ' 1px dashed white'});
			})
			.mouseout(function() {
				$('td', $(this)).animate({ 'backgroundColor' : '#000000', 'color' : '#a2130c', 'border-top' : ' 1px dashed #a2130c'}, 500);
			})
			.click(function() {
				var id = $(this).attr("id");
				var url = 'site/agenda/detail.php?id=' + id;
				TopUp.display (url, {title:'Concert',layout:'dashboard',type:'iframe',width:800,height:600,modal:1,shaded:1,resizable:0});
		}); 
	}
	initPage('agenda');
	
	function galerie()
	{		

	}

/**
 * LOADS HOMEPAGE WIDGETS
 * --------------------------------------------------------- */
	
	//Chargement des items
	var iWidget = 0; //quel item on charge
	var arWidgetItems; 
	function loadWidgets(iWidget)
	{
		var objItem 	 = $(arWidgetItems[iWidget]);
		
		//On sort si tous (6) les items sont chargés 
		if (iWidget == 6 || !objItem) 
		{
			return false;
		}
		 
		//fadeIn to white
		objItem.fadeTo(300, 1, function() { 
			
			//Chargement du prochain...
			++iWidget;
			loadWidgets(iWidget);
		});
	}
	
/**
 * RE-INITIATE CALLBACK IN CASE SE LOADS PAGE
 * --------------------------------------------------------- */
	function initPage(page)
	{
		if (isSe && docLoc.indexOf('/'+ page) != "-1")
		{
			eval(page)(name);
		}
	}
	


	
}); 





	/**
 	* Pause MP3 : Cela peut être utilisé lors du lancement des videos youtube
 	* --------------------------------------------------------- */
	function pauseMP3() {
		var mp3player = document.getElementById('flashplayer');
		mp3player.pause();
	}
	/**
 	* Play MP3 : Cela peut être utilisé lors du lancement des videos youtube
 	* --------------------------------------------------------- */
	function playMP3() {
		var mp3player = document.getElementById('flashplayer');
		mp3player.play();
	}
