/*-------------------------------------------------------------------------------------------------------------
	Popin load
---------------------------------------------------------------------------------------------------------------*/

function loadPopin(externalLink,type,closeWording){
	
	// document height for the mask
	myHeight = document.body.offsetHeight+'px';
	
	// IE6 failures
	if($.browser.msie){ 
		$("select").css("visibility","hidden");
	}
	$('body #masque, body #popinContainer').remove();
	
	// container and mask
	$("body").append('<div id="masque"></div><div id="popinContainer" class="'+type+'"><a href="#" id="closeButton">'+closeWording+'</a><div id="popinContent"></div></div>');
	
	if ($.browser.msie) $("#masque").show();
	else $("#masque").fadeIn();
	
	// external HTML load
	jQuery('#popinContainer #popinContent').load(externalLink + " #popinContentToLoad", function(){
		
		if (jQuery("#popinContentToLoad").hasClass("portrait")) {
			jQuery(".popinVisu img").width("232px");
		}/*
		else {
			jQuery(".popinVisu img").width("442px");
		}*/
		
		// Close buttons
		
		jQuery('#closeButton, #masque').click(function(){
		
			jQuery('#popinContainer, #masque').remove();
			if (status != "") {
				status = "";
			}
			
			if (jQuery.browser.msie) {
				jQuery("select").css("visibility", "visible");
			}
			
			return false;
			
		});
		
	});
	
}

/*-------------------------------------------------------------------------------------------------------------
	Zoom load
---------------------------------------------------------------------------------------------------------------*/
// setting up popin link status
var status;
var originHeight;
var originWidth;
var originMargin;
var originImg;





function loadZoom(event) {
	event.preventDefault();
	
	// setting up original data
	// link
	var obj = jQuery(this);
	obj.blur();
	
	// images url
	var zoomImgUrl = obj.attr("href");
	
	// initial popin size
	if (status != "zoomed") {
		// setting up original data
		originHeight = jQuery("#popinContainer").height();
		originWidth = jQuery("#popinContainer").width();
		originMargin = jQuery("#popinContainer").css("margin-left");
		
		// images url
		originImg = obj.prev().attr("src");
		
		
		// applying width and height to prevent popin collapse
		jQuery("#popinContainer").css({
			height: originHeight,
			width: originWidth
		});
	}
	
	
	
	switch(status) {
		case "zoomed": // zoomed
			//hiding previous content
			jQuery("#popinContentToLoad *").fadeOut(350);
		
			jQuery("#popinContainer").animate({height:originHeight}, 500)
									 .animate({width:originWidth,marginLeft:originMargin},500, function(){
									 	// setting back portrait popin values
										if(jQuery("#popinContentToLoad").hasClass("portrait")) {
											jQuery("#popinContentToLoad .popinVisu").css({width:"232px",float:"left",margin:"0 10px 0 0"});
											jQuery(".popinVisu img").width("232px");
										}
										/*else {
											jQuery(".popinVisu img").width("442px");
										}*/
										
										
									 	// replacing image
										jQuery(".popinVisu img").attr("src", originImg);
										obj.toggleClass("minus");
							 			jQuery("#popinContentToLoad .popinVisu, #popinContentToLoad *").fadeIn(350);
							 		});
			// changing link status
			status = "";
			break;
		default:// not zoomed
			// getting new image
			jQuery.ajax({
				type: "GET",
				url: zoomImgUrl,
				complete: function(data) {
					//hiding previous content
					jQuery("#popinContentToLoad *").fadeOut(350, function (){
						// replacing image
						jQuery(".popinVisu img").attr("src", zoomImgUrl);
						obj.toggleClass("minus");
					});
					
					
					
					jQuery("#popinContainer").animate({width:"924px",marginLeft:"-462px"}, 500)
											 .animate({height:"550px"},500, function(){
											 	if(jQuery("#popinContentToLoad").hasClass("portrait")) {
													jQuery("#popinContentToLoad .popinVisu").css({width:"auto",float:"none",margin:"0 auto"});
													jQuery(".popinVisu img").width("auto");
												}
												
												
											 	jQuery("#popinContentToLoad .popinVisu, #popinContentToLoad .popinVisu *").fadeIn(350);
											 });
					
					
					
					
					// changing link status
					status = "zoomed";
				}
			});
	}
	
}



/*-------------------------------------------------------------------------------------------------------------
	Popin init
---------------------------------------------------------------------------------------------------------------*/


function initPopin(){

	$(".popin").click(function(){
		
		//loadPopin(externalLink,type,closeWording)
		//format 2 carroussel image envoyee en get directement dans loutils de gestion des zones media	--- popin grand format
		//format 1 on envoie limage en param get dans le JS --- popin portrait
		
		if($(this).attr('rel').split('-')[0]=='format1')
		loadPopin($(this).attr('href')+"&img="+$(this).children('img').attr('src') , $(this).attr('rel').split('-')[0] , $(this).attr('rel').split('-')[1]);
		else
		loadPopin($(this).attr('href'),$(this).attr('rel').split('-')[0],$(this).attr('rel').split('-')[1]);
			
			
		jQuery("#popinContainer").css("margin-top", jQuery(window).scrollTop());
		$(this).blur();
		
		return false;
		
	});
	
	jQuery("#popinContent form [type=submit]").live("click", function() {	
		var formData = jQuery(this).parents("form").serialize();
		jQuery.ajax({
			type: "POST",
			url: jQuery(this).parents("form").attr('action'),
			data: formData,
			complete: function(data) {
				if(jQuery("#confirmMessage", data.responseText).length > 0){
					jQuery("#popinContentToLoad .titleLevel3 em").fadeOut();
					jQuery("#popinContent .formProfil").html(jQuery(data.responseText).find("#confirmMessage"));
				}else{
					jQuery("#popinContent").html(jQuery(data.responseText).find("#popinContentToLoad"));
				}
				
			}
		});
		jQuery(this).blur();
		return false;
	});
	
	
	
	
	
	
}


/*-------------------------------------------------------------------------------------------------------------
	onLoad
---------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	initPopin();
	jQuery(".btZoom").live("click", loadZoom);
})	
	