var currentPortrait = null;
var first = true;
var gallery_width = 0;
var gallery_height = 0;

var currentMenu = null;
	

/*----------------------------------------------------
					CONTENTS
---------------------------------------------------- */

function btnOn(Element){ 
	$(Element).css("background-position","0px -13px").delay(1000);
}

function btnOff(Element){
	$(Element).css("background-position","0px 0px")
}

function showContent(idTitre, idElement){

	var t = idTitre;
	var e = idElement;
		
	//Remove Events
	$(t).unbind('mouseover');
	$(t).unbind('click');
	
	//Affiche -
	btnOn($('.btn', t))	
	
	//Animation Content
	$(e).css('display', 'block')	
	
	//Specs
	if ($(t).is("#titre_ventes")) {
		$('.content_ventes').unbind('click');
		$('.conteneur_inscription').css('display','block');
		$(e).stop().animate({top:0}, 3000, 'easeOutExpo', function(){$(t).click(function(){hideContent($(t), '.inscription')})}); 
		$('#acces_ventes_mail').focus()
	} else {
		$('.content_boutiques').unbind('click');
		//$(e).stop().animate({top:0}, 3000, 'easeOutExpo', function(){$(t).click(function(){hideContent($(t), '.menu_boutiques')})}); 
		$(e).stop().animate({top:0}, 0, 'easeOutExpo'); 
		var menu = $('.menus', '.menu_boutiques')
		menuOver($('#'+premiereCat, menu), 0)
	}
			
}

function hideContent(idTitre, idElement){

	var t = idTitre;
	var e = idElement;
	
	//RemoveEvents
	$(t).unbind('click');
		
	//Affiche +
	btnOff($('.btn', t))	
	
	//Animation Content
	$(e).stop().animate({top:-425}, 2000, 'easeOutExpo', function(){
		$(t).mouseover(function(){showContent($(this), e)}).click(function(){showContent($(t), e)})
		$(e).css('display', 'none')
		
		if ($(t).is("#titre_ventes")) {
			$('.content_ventes').click(function(){showContent('#titre_ventes', '.inscription')});
			$('.conteneur_inscription').css('display','none');
		} else {
			$('.content_boutiques').click(function(){showContent('#titre_boutiques', '.menu_boutiques')});
		}
	});
	

}



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


function setupMenu(){

	var menu = $('.menus', '.menu_boutiques')

	$('a', menu).each(function(index) {
		$(this).i = index;
		$(this).click(function(){menuClick($(this))}).mouseover(function(){menuOver($(this), index)});
	});

}

function menuOver(bouton, index){
	
	if (currentMenu != null){ menuOut(currentMenu, currentMenu.i);}
	currentMenu = bouton;
	currentMenu.i = index;
	$(currentMenu).unbind('mouseover')	
	
	var cssObjBG = {'background' : 'url('+BACKGROUNDS[index]+') no-repeat',
				   'opacity' : '0'}
	$('.pic', '.menu_boutiques').stop().css(cssObjBG).animate({opacity:1}, 1000);
	
	$('.pic', '.menu_boutiques').attr('href', bouton.attr('href')) 
	
	var cssObjBouton = {'color' : '#ffffff',
				   'background-color' : '#a9a9a9'}
	$(bouton).css(cssObjBouton);

}


function menuOut(bouton, index){
	
	var cssObjBouton = {'color' : '#2a2a2a',
				   'background-color' : '#f3f3f3'}
	$(bouton).css(cssObjBouton);
	
	$(bouton).mouseover(function(){menuOver($(this), index)});
		
}



/*----------------------------------------------------
						GALLERY
---------------------------------------------------- */


function setupGallery(){

	var li_width = 138;
	
	$('li', '.gallery_designers').each(function(index) {
		gallery_width+=li_width;
		var li_height = $('li', '.gallery_designers').height();
		$(this).click(function(){showFiche(this, '.content_fiche', index)}).hover(function(){portraitOver(this)},function(){portraitOut(this)});
	});
		
	
		
	$('#gallery').css('width', gallery_width);	

	if (gallery_width > 966){
		SetupSlider(gallery_width);
	} else {
		$('#gallery').css('left', (930 - gallery_width + 38)/2)
		$("#slider_barre").css('display', 'none');
	}	

	gallery_height = $('.gallery_designers').height()
	
}


function portraitOver(Portrait){
	$('.selected', Portrait).stop().animate({height:5}, 1000, 'easeOutExpo');	
}

function portraitOut(Portrait){
	$('.selected', Portrait).stop().animate({height:0}, 1000, 'easeOutExpo');	
}


function showGallery(idTitre, idElement){

	var t = idTitre;
	var e = idElement;
	
	//Changement BTN
	btnOn($('.btn', t))	
	$(t).unbind('click');
	$(t).unbind('mouseover');

	//Show Conteneur	
	$(e).stop().css('height', '0').animate({height:gallery_height+5, marginTop:20}, 2500, 'easeOutExpo');
	//Alpha Images
	$('#gallery').css('opacity', '0').stop().animate({opacity:1}, 3000, 'linear', function(){$(t).click(function(){
		hideGallery($(this), '.gallery_designers')})
		});	
	//Alpha slider
	$('#slider_barre', e).css('opacity', '0').stop().delay(2000).animate({opacity:1}, 2000, 'easeOutExpo');
	
	
	//IE
	if (window.navigator.appName == 'Microsoft Internet Explorer')
	{
		$('img', '#gallery').each(function(index) {
			$('img', '#gallery').css('opacity', '0').stop().animate({opacity:1}, 3000, 'linear');
		});
		
		$('.slider', e).css('opacity', '0').stop().delay(2000).animate({opacity:1}, 2000, 'easeOutExpo');
		$('.ui-slider-handle', e).css('opacity', '0').stop().delay(2000).animate({opacity:1}, 2000, 'easeOutExpo');
	}	
	
	
	//Windows focus
	$(window).scrollTo(500, 2000, 'easeOutExpo');
}


function hideGallery(idTitre, idElement){
	
	var t = idTitre;
	var e = idElement;
	$(t).unbind('click');
	
	btnOff($('.btn', t))

	//HideConteneur
	$(e).stop().animate({height:0, marginTop:0}, 1500, 'easeInOutCubic', function(){
		//$(e).css('display', 'none')
		$(t).mouseover(function(){showGallery(this, e)})
		$(t).click(function(){showGallery(this, e)})
	});
	
	//alphas Images
	$('#gallery', e).stop().animate({opacity:0}, 1000, 'easeOutExpo');
		
	//Alpha Slider
	$('#slider_barre', e).stop().animate({opacity:0}, 500, 'easeOutExpo');
	
	//IE
	if (window.navigator.appName == 'Microsoft Internet Explorer')
	{
		$('img', '#gallery').each(function(index) {
			$('img', '#gallery').stop().animate({opacity:0}, 1000, 'easeOutExpo');
		});
		
		$('.slider', e).stop().animate({opacity:0}, 500, 'easeOutExpo');
		$('.ui-slider-handle', e).stop().animate({opacity:0}, 500, 'easeOutExpo');
	}
	
	//Hide Fiche
	if (currentPortrait != null)
	{
		hideFiche(currentPortrait, '.content_fiche')
	}
	
}


function showFiche(idDesigner, idFiche, index){

	if (idDesigner != currentPortrait){

			//Retour dernier portrait
			portraitOut(currentPortrait);
			$(currentPortrait).hover(function(){portraitOver(this)},function(){portraitOut(this)});
			currentPortrait = idDesigner;
			portraitOver(idDesigner);
			$(idDesigner).unbind('mouseenter mouseleave');
			
			
			$('.acces_objets', idFiche).css('display', 'block')
			$(idFiche).css('display', 'block')
				
			//Update
			if (first == false){
				
				$('#content', idFiche).stop().animate({opacity:0}, 500, 'easeOutExpo', function(){
				
					$('.fiche_designers', idFiche).css('height', '');
					
					remplissageFiche(index);
					
					if ($('.fiche_designers', idFiche).height() <= 120){
						$('.fiche_designers', idFiche).css('height', '120px');
					} else {
						$('.fiche_designers', idFiche).css('height', '');
					}
					
					$(idFiche).animate({height:$('.fiche_designers', idFiche).height() + 62, marginTop:30}, 2500, 'easeOutExpo');
					$('#content', idFiche).css('opacity', '0').delay(1000).animate({opacity:1}, 2000, 'easeOutExpo');
				
				})
			
				if (window.navigator.appName == 'Microsoft Internet Explorer')	{
					$('.acces_objets', idFiche).stop().animate({opacity:0}, 500, 'easeOutExpo').animate({opacity:1}, 2000, 'easeInOutExpo')
				}
			//Première Ouverture
			} else {
			
				first = false;
				
				remplissageFiche(index);
				
				if ($('.fiche_designers', idFiche).height() <= 120){
						$('.fiche_designers', idFiche).css('height', '120px');
					} else {
						$('.fiche_designers', idFiche).css('height', '');
					}

				//Affiche Fiche	
				$(idFiche).stop().animate({height:$('.fiche_designers', idFiche).height() + 62, marginTop:30}, 2500, 'easeOutExpo');
				$('#content', idFiche).stop().css('opacity', '0').delay(2000).animate({opacity:1}, 2000, 'easeOutExpo');
				
				
				if (window.navigator.appName == 'Microsoft Internet Explorer')	{
					$('.acces_objets', idFiche).stop().css('opacity', '0').delay(2000).animate({opacity:1}, 2000, 'easeOutExpo');
				}
				
			}
			
			
			//windows Focus
			$(window).scrollTo(610, 1000, 'easeOutExpo');	
		
		}
}


function remplissageFiche(index) {
	
	var designerBox = $('#content', '#content_fiche');
	var fiche = $('.fiche_designers', designerBox);
	var designerName = $('h2', fiche);
	var designerTexte = $('p', fiche);
		
	document.getElementById('link_objets').href = PORTRAITS_LINK[index];
	designerTexte.html(PORTRAITS_TEXTE[index]);
	designerName.text(PORTRAITS_NAME[index]);
	fiche.css('background', '#2a2a2a url('+PORTRAITS_IMG[index]+') no-repeat left top');
	
}


function hideFiche(idDesigner, idFiche){
	var p = idDesigner;
	var f = idFiche;
	
	//Delete fiche
	$(f).stop().animate({height:0, marginTop:0}, 1500, 'easeInOutCubic', function(){
		$(f).css('display', 'none')
		$('.acces_objets', f).css('display', 'none')
	});	
	
	$('#content', f).clearQueue();
	$('#content', f).stop().animate({opacity:0}, 500, 'easeOutExpo');
	
	if (window.navigator.appName == 'Microsoft Internet Explorer')	{
		$('.acces_objets', f).stop().animate({opacity:0}, 500, 'easeOutExpo');
	}

	//Reset Portraits
	portraitOut(currentPortrait);
	$(currentPortrait).hover(function(){portraitOver(this)},function(){portraitOut(this)});
	currentPortrait = null;
	
	first = true;
	
	//windows Focus
	$(window).scrollTo(475, 1000, 'easeOutExpo');
}




/*----------------------------------------------------
					SLIDER
---------------------------------------------------- */


function SetupSlider(){

	var ul = $('#gallery');
	var portraitsDiff = (gallery_width - 968);
		
		
	$("#slider").slider({
		animate : true,
		slide: function (event, ui) {
			var xdiff =  (9.3 * ui.value * portraitsDiff / 930)
			ul.stop().animate({left: (-xdiff + 3) +'px'}, 2500, 'easeOutExpo');
		},
		change: function (event, ui) {
			var xdiff =  (9.3 * ui.value * portraitsDiff / 930)
			ul.stop().animate({left: (-xdiff + 3) +'px'}, 2500, 'easeOutExpo');
		}, 
		stop: function (event, ui) {
			var xdiff =  (9.3 * ui.value * portraitsDiff / 930)
			ul.stop().animate({left: (-xdiff + 3) +'px'}, 2500, 'easeOutExpo');
		}	 
	});

}



/*----------------------------------------------------
					LIGHTBOX
---------------------------------------------------- */


function setupLightBox(){

	$('.link_inscription_newletter').click(function() {
		$('.inscription_newsletter').lightbox_me({
			centered: true,
			lightboxSpeed: 'normal', 
			closeSelector: '#inscription_close', 
			onLoad: function() {
				$('#newsletter_inscription_mail').focus()
				}
			});
		return false;
	});
}

function setup(){
	
	setupGallery();
	setupLightBox();
	setupMenu();
	$('#login_mail').focus()	
	//$('#titre_boutiques').mouseover(function(){showContent(this, '.menu_boutiques')});
	showContent($('#titre_boutiques'), '.menu_boutiques')
  $('#titre_ventes').mouseover(function(){showContent(this, '.inscription')});
	$('#titre_designers').mouseover(function(){showGallery(this, '.gallery_designers')}).click(function(){showGallery(this, '.gallery_designers')});
	//$('.content_boutiques').click(function(){showContent('#titre_boutiques', '.menu_boutiques')});
	$('.content_ventes').click(function(){showContent('#titre_ventes', '.inscription')});
}