$(document).ready(function(){
	showOrientMusicTour();
	initPopins();
	//alert("----");
	Cufon.replace('.eventsStory h1,.eventsStory h2');
});
function showOrientMusicTour(){
	$(".lancerPopup").unbind();
	$(".lancerPopup").bind("click",function(){
		popupcentree($(this).attr("href"),500,440,"menubar=no,scrollbars=no,statusbar=no");
		return false;
	});
}
function showForms(){
	$(".contactWe").unbind();
	$(".contactWe").bind("click",function(){
		$(this).parent().find("ul").hide();
		$(this).parent().find(".formContact").show();
		$(this).hide();
		return false;
	});
}
function popupcentree(page,largeur,hauteur,options){
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}
var indice=1;
function animateTexte(){
	var lii = $(".popup ul").children();
	$(".popup ul li").each(function(i,obj){
		$(obj).fadeOut('slow');
	});
	if($(".popup ul").children().length<=indice){
		indice=0;
	}
	$(lii[indice]).fadeIn('slow');
	
	//alert("--->"+indice);
	indice+=1;
}



/********************************************************/
// Init des popins
/********************************************************/
function initPopins(){
	var dim = getPageSize();
	$('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($(_this).attr('rel'))
		$.ajax({
			type: "GET",
			url: $(_this).attr('href'),
			dataType: "html",
			success: function(data){
				try{
					showPopin(data,$(_this).attr('rel'));
				}catch(e){}
			}
		});
		
		return false;
	});

}
function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizaOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizaOverlay();
	});	
}	

function resizaOverlay(cond){
	var overlay = $('#popinOverlay');
	var scroll = getPageScroll();
	//alert(hi);
	if(cond){
		overlay.css({
			opacity: 0.7,
			display: 'block'
		}).fadeIn('slow',function(){
			var hi = getHeight() + scroll[1];
			overlay.height(hi);
		});
	}else{
		var hi = getHeight() + scroll[1];
		overlay.height(hi);
	}
}

/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){
	//alert("---->"+data);
	$('#popinTemp').html(data);
	$('#popinTemp').removeAttr('style');
	$('#popinTemp').show();
	setPopinPosition(id);
	//setEventSelectLayers();
	$(".popup ul li").hide();
	var li1 = $(".popup ul").children();
	$(li1[0]).fadeIn('slow');
	var timerAnimation= setInterval('animateTexte()', 3000);
	
	showForms();
	validateForms();
	
	
	if($("#popinOverlay").length){
		$("#popinOverlay").unbind();
		$("#popinOverlay").bind('click',function(){	
				 clearInterval(timerAnimation);
				$("#popinOverlay").fadeOut();
				$("#popinTemp").fadeOut();	
				$("#popinOverlay").remove();
				$("#popinTemp").remove();		 
		 });		
	}
}
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function refreshPositionPopin(popin){
	

	$(window).bind('resize',function(){
		var dim = getPageSize();
			popin.css({
			left: (dim[2]-popin.width())/2
		})
	});

	
}

function setPopinPosition(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+id);
	resizaOverlay(1);
	popin.css({
		top: scroll[1] + (dim[3]/4),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	refreshPositionPopin(popin);
	
	popin.find('a.close').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();		
		$("#popinOverlay").remove();
		$("#popinTemp").remove();		
		return false;
	});	
}

function loadPopin(id){
	if(!$('#popinOverlay').length)
		$('#main').before('<div id="popinOverlay"></div>');
			
	if(!$('#popinTemp').length)
		$('#main').before('<div id="popinTemp"></div>');
	$('#popinTemp').html("<div id='"+id+"' >Loading..</div>");
	setPopinPosition("popinTemp");			
}

function setWidthForLayers(){
	$('#onglets > li').each(function(){
		var resultshearch = $('.resultSearch > div',$(this))
		nbrItems = $('ul',resultshearch).length;
		var wiFinale = Math.ceil((nbrItems * 135.3) + ( nbrItems * 14.85));
		resultshearch.width(wiFinale);
	});
}

/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

/******isEmail*****/
function isEmail(field){ 
	var val = field.val();
	if (!val.match (/^[a-z0-9\-\._]+@[a-z0-9\-_\.]+\.[a-z]{2,4}$/gi)){
	 	return false;
	}
	return true;
}

function validateForms(){
	$("#submitBtn").unbind();
	$("#submitBtn").bind("click",function(){
		var flag=false;
		$("#yorEmail").parent().find("span").remove();
		$("#messageEmail").parent().find("span").remove();
		if(!isEmail($("#yorEmail"))){
			$("#yorEmail").after("<span>Email invalid</span>");
			flag=true;
		}else{
			$("#yorEmail").parent().find("span").remove();
		}
		if($("#messageEmail").val()==''){
			$("#messageEmail").after("<span>Message vide</span>");
			flag=true;
		}else{
			$("#messageEmail").parent().find("span").remove();
		}
		if(flag==false){
			get(this.parentNode);
		}
		
		//get(document.getElementById('myform'));
		//get(this.parentNode);
		
		return false;
	});
}
/****ajax******/
	var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
           $('#etatSms').html(result);            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "email=" + encodeURI($("#yorEmail").val()) +
                    "&message=" + encodeURI($("#messageEmail").val());
      makePOSTRequest('post.php', poststr);
   }
