// JavaScript Document

$(document).ready()
{
	
		var codeHTML = '<div id="bg_shadow" >fdsfdsdf</div>';
		$(document).append(codeHTML);
}

var popinPasOuverture = new Object();
var popinMagasin = new Object();
var popinContact = new Object();
var popinVideo = new Object();

function showPopin( typePopin, drive, idMag )
{
	switch(typePopin)
	{
		case 'popinPasOuverture' :
		popinPasOuverture = new popin('Support_'+typePopin , null);
		popinPasOuverture.create('popin_pas_ouverture/popin_pas_ouverture.html','');
		break;
		
		case 'popinMagasin' :
		popinMagasin = new popin('Support_'+typePopin, drive, idMag);
		popinMagasin.create('/popin_magasin/popin_magasin-view.jsp', drive, idMag);
		break;
		
		case 'popinContact' :
		popinContact = new popin('Support_'+typePopin , null);
		popinContact.create('popinContact/popin_contact-view.jsp', '');
		break;
		
		case 'popinVideo' :
		popinVideo = new popin('Support_'+typePopin , null);
		popinVideo.create('popinvideo/popin_video.html','video');
		break;	
		
	}
}

function closePopin(typePopin)
{
	switch(typePopin)
	{
		case 'popinPasOuverture' :
		popinPasOuverture.destroy();
		break;
		
		case 'popinMagasin' :
		popinMagasin.destroy();
		break;
		
		case 'popinContact' :
		popinContact.destroy();
		break;
		
		case 'popinVideo' :
		$('#bg_shadow').css('width','10px');
		$('#iframeVideo').attr('src','');
		popinVideo.destroy();
		break;
			
	}
}


function popin(typePopin, drive, idMag)
{
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id',typePopin);
	
	this.create = function(typePopin, drive, idMag)
	{
		var widthPopin = 550;
		//// exception popin video
		if(drive == 'video') { widthPopin = 570; }
		
		$('body').append(newdiv);
		var posix = Math.round( ( $(document).width() / 2) - ( widthPopin / 2) );
		
		
		//// requete ajax pour aller chercher la popin
		
		$.ajax({
		  url: typePopin,
		  context: document.body,
		  success: function(reponse){
			  	var codeHTML = '<div id="bg_shadow" ></div>';
				codeHTML += '<div id="conteneurPopin"></div>';
				$(newdiv).append(codeHTML);
				$('#conteneurPopin').css('position', 'absolute;');
				$('#conteneurPopin').css('left', posix+'px');
				$('#conteneurPopin').css('width', widthPopin+'px');
				$('#bg_shadow').css('width', $(document).width()+'px');
				$('#bg_shadow').css('height', $(document).height()+'px');
				$('#bg_shadow').css('filter', 'alpha(opacity=70)');
				$('#conteneurPopin').css('top',$(document).scrollTop()+75+'px');
				
				
				if(drive) { reponse = reponse.replace("__DRIVE__", drive); }
				$('#conteneurPopin').css('visibility','hidden');
				$('#conteneurPopin').html(reponse);
				
				//pour charger la valeur du champ hidden contenant l'idMag
				$('#prehome_magasin').attr("value", idMag);
				
				/// adaptation du flap à l'image
				var langue = readCookie("langueFront", "fr");
       			if (langue=="nl") {
					var urlimage = $("#flap").attr("src");
					if(urlimage != undefined){
						var reg = new RegExp('img/images', 'gi');
						newURL = urlimage.replace(reg, 'img/images-NL');
						$("#flap").attr("src",newURL);
					}
					if(typePopin=="popinvideo/popin_video.html") {  
						$("#iframeVideo").attr("src","http://www.youtube.com/embed/iOLsATsWzGI");
					}
				}
				$('#conteneurPopin').css('visibility','visible');
			}
		});
	}
	this.destroy = function()
	{
		$('#'+typePopin).remove();
	}
	
	
}

//////popin iphone
function showpopinMag(drive, idMag)
{
	var widthPopin = 550;
	var posix = Math.round( ( $(document).width() / 2) - ( widthPopin / 2) );
	if(posix < 0) { posix=0;  }
	
	$.ajax({
		url: '/popin_magasin/popin_magasin-view.jsp',
		context: document.body,
		success: function(reponse){
			
			if(drive) { reponse = reponse.replace("__DRIVE__", drive); }
		
			var codeHTML = reponse;
			
			$(document.body).html(codeHTML);
			
			//$(newdiv).append(codeHTML);
			$('#conteneurPopin').css('visibility','hidden');
			$('#conteneurPopin').css('width', $(document).width()+'px');
			$('#conteneurPopin').css('height', $(document).height()+'px');
			$(document.body).css('background','url(images/bg_body-mobile.jpg) repeat-x');
			$('#popinMagasin').css('left', posix+'px');
			$('#popinMagasin').css('top', '25px');
			
			var langue = readCookie("langueFront", "fr");
            if (langue == "nl") {
            	$('.btn_fermerPopin').html("X <a href=\"javascript:closepopinMagFromMobile();\">SLUITEN</a>");
            } else {
            	$('.btn_fermerPopin').html("X <a href=\"javascript:closepopinMagFromMobile();\">FERMER</a>");
            }
            $('.btn_fermerPopin').css('color','#CC0000');
			$('.btn_fermerPopin a').css('color','#CC0000');
			
			//pour charger la valeur du champ hidden contenant l'idMag
			$('#prehome_magasin').attr("value", idMag);
			
			/// adaptation du flap à l'image
			var langue = readCookie("langueFront", "fr");
			if (langue=="nl") {
				var urlimage = $("#flap").attr("src");
				if(urlimage != undefined){
					var reg = new RegExp('img/images', 'gi');
					newURL = urlimage.replace(reg, 'img/images-NL');
					$("#flap").attr("src",newURL);
				}
			}
			$('#conteneurPopin').css('visibility','visible');
		}
	});
}

function closepopinMagFromMobile()
{
		location.reload(); 
}

