function initPopins(){
	
	var dim = getPageSize();
	$('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($(_this).attr('rel'), this)
		return false;
	});
}

/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){
	$('#popinTemp').contents().html(data);
	$('#popinTemp').removeAttr('style');
	$('#popinTemp').show();
	setPopinPosition(id);
	setEventSelectLayers();

	
}


function loadPopin(id,url){
		
	if(!$('#popinOverlay').length)
		$('#main').before('<div id="popinOverlay""></div>');
			
	if(!$('#popinTemp').length) {
		$('#main').before('<iframe id="popinTemp" class="popin" frameborder="0" scrolling="no"  allowtransparency="true"></iframe>');
		$('#popinTemp').load(setPopinPosition);
		$('#popinTemp').attr('src', url);
	}
	
	$('#popinOverlay').unbind();
	$('#popinOverlay').bind('click',function(){
		$('#popinTemp').remove();
		$(this).remove();
		if(navigator.appVersion.indexOf("MSIE") != -1){
			$("#main select").show();
		}
	})
}


function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizaOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizaOverlay();
	});	
}	

function resizaOverlay(cond){
	var overlay = $('#popinOverlay');
	var scroll = getPageScroll();

	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);
	}
}


function setPopinPosition(id){
	id = 'popinTemp';
	var dim = getPageSize();
	$('#popinTemp').height($('#popinTemp').contents().find('.popin').height() + 12);
	$('#popinTemp').width($('#popinTemp').contents().find('.popin').width() + 15);

	if ($('#popinTemp').width() < 50)
		$('#popinTemp').width(650);

	if ($('#popinTemp').height() < 50)
		$('#popinTemp').height(350);
	
	$('#popinTemp').contents().find('.btnClose').bind('click', function(){
		$('#popinTemp').remove();
		$('#popinOverlay').remove();
		if(navigator.appVersion.indexOf("MSIE") != -1){
			$("#main select").show();
		}
	});
	
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+id);
	resizaOverlay(1);
	if(id=="popin_agenda"){
	popin.css({
		top: scroll[1] + (dim[3] / 10)+200,
		left: ((dim[2]-$(popin).width())/2)-100,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	}else{
	popin.css({
		top: scroll[1] + (dim[3] / 10),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	}
	popin.find('.btnClose').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();
		if(navigator.appVersion.indexOf("MSIE") != -1){
			$("#main select").show();
		}
		return false;
	});	
	
	if(navigator.appVersion.indexOf("MSIE") != -1){
		$("#main select").hide();
	}
}

// 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;
}


$(document).ready(function(){	
	initPopins();
	resizeOverlayForPopin();
	

});
