/* Javascript - PagesIntérim */
var isIE=navigator.userAgent.toLowerCase().indexOf("msie")>-1;
var isMoz=document.implementation && document.implementation.createDocument;
var isSafari=((navigator.userAgent.toLowerCase().indexOf("safari")!=-1) && (navigator.userAgent.toLowerCase().indexOf("mac")!=-1)) ? true : false;
var divBackup = new Array();

function openPopup(url,name,w,h,o){
	window.open(url,name,'width='+w+',height='+h+',resizable=0,'+o);
}
function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}
function save(id){
	window.open(base_url + '/candidats/selection/ajout/id/' + id,
		'selection',
		'width=330,height=370,resizable=0,0');
}

function open_popup(src, popup_name, iframeWidth, iframeHeight){

	// Centre la div
	
	if(window.popup_name){
		$('#' + popup_name).remove();
		$('#popup_' + popup_name).remove();
	}
	
	//Ajoute une div après l'élément BODY
	insideDiv = document.createElement("DIV");
	document.body.appendChild(insideDiv);
	
	var pageSize = getPageSize();
	
	// Dimension de l'élément BODY
	bgdiv_width = pageSize.width;
	bgdiv_height = pageSize.height;
	
	insideDiv.innerHTML = '<div class="DXP_filigrane" id="popup_'+popup_name+'" style="height:'+bgdiv_height+'px;width:'+bgdiv_width+'px;" onclick="close_popup(\''+popup_name+'\');"></div>' +
	'<div name="'+popup_name+'" id="'+popup_name+'" style="height:'+iframeHeight+'px;width:'+iframeWidth+'px"></div>';
	
	$("#" + popup_name).load(base_url + src,
		'',
		function() { 
			if(popup_name == 'add_selection'){
				update_selection_quicklist();
			}
		}
	);
	
	posMiddleScreenDiv(popup_name);
	
	return true;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {'scrOfX':scrOfX, 'scrOfY':scrOfY };
}

function open_popup_localiser(e, lat, lng, jobId) 
{
	popup_name = 'localiser_singleMap';
	
	// Suprime une instance d'une autre carte
	if ($('#'+popup_name)) {
		$('#'+popup_name).parent().remove();
	}
	
	var scroll = getScrollXY();
	
	// Récupère la position de la souris lors du clic
	if (!e) e = window.event;	
	if (e) {
		mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
		mouseY = isIE ? (e.clientY + scroll.scrOfY) : e.pageY;
	}
	
	
	//Ajoute une div après l'élément BODY
	mapDiv = document.createElement("DIV");
  jobDiv = document.getElementById(jobId);
	jobDiv.appendChild(mapDiv);
	
	html_content = '<div name="'+popup_name+'" id="'+popup_name+'" style="background: #fff;width: 100%;border: 5px solid #E99D00;color:black;">';
	html_content += '<p style="text-align:right; margin-right:8px;"><a href="javascript:void(0);" onclick="close_popup_localiser(\''+popup_name+'\');">Fermer <img src="'+ base_url +'/public/skin/default/images/btn-close.png" /></a><p>';
	html_content += '<div id="'+popup_name+'_map" style="overflow: hidden; height:360px;margin:auto;"></div>';
	html_content += '</div>';
	// Insère la div dans la page
	mapDiv.innerHTML = html_content;
	if(lat != 0 
	&& lng != 0){
		initSingleGmap($('#'+popup_name+'_map'),lat,lng);
	} else {
		$('#'+popup_name+'_map').html('Carte indisponible pour cette offre.');
	}
	
	/*var scrollvalue = getScrollvalue();
	
	divElt = $('#' + popup_name);
	divHeight = divElt.height();
	
	posY = ((divHeight + mouseY) > (scrollvalue + getWindowSize().height)) ? (scrollvalue + getWindowSize().height - (divHeight + 10)) : (mouseY - 30);
	posX = 650;
	
	$('#' + popup_name).css('left',posX);
	$('#' + popup_name).css('top',posY);*/
	
	return true;
}

function getScrollvalue(){
	return document.documentElement.scrollTop > document.body.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop; // Position de la scrollbar
}

function getWindowSize(){
	windowWidth =  window.innerWidth;
	windowWidth = (windowWidth)? windowWidth : document.documentElement.clientWidth;
	windowWidth = (windowWidth)? windowWidth : document.body.clientWidth;
	windowHeight =  window.innerHeight;
	windowHeight = (windowHeight)? windowHeight: document.documentElement.clientHeight;
	windowHeight = (windowHeight)? windowHeight: document.body.clientHeight;  
	return {'width': windowWidth, 'height': windowHeight};
}
function getPageSize(){
	var windowSize = getWindowSize()
	var xScroll = document.body.scrollWidth;
	var yScroll = (window.innerHeight && window.scrollMaxY)? window.innerHeight + window.scrollMaxY : document.body.scrollHeight;
	var pageWidth = (xScroll < windowSize.width)? windowSize.width : xScroll;  
	var pageHeight = (yScroll < windowSize.height)? windowSize.height : yScroll;
	return {'width': pageWidth, 'height': pageHeight};
}
function posMiddleScreenDiv(element){
var c = 0;
	var windowSize = getWindowSize();
	var scrollvalue = getScrollvalue();
	
	var myDiv = $('#' + element);
	
	var posLeft = (windowSize.width - myDiv.width())/2;
	left = (posLeft<0) ? 0 : posLeft;
	myDiv.css('left',left);
	
	var posTop = (scrollvalue + (windowSize.height/2) - (myDiv.height()) );
  if (posTop<0) {
    posTop = 0;
  }
	myDiv.css('top',posTop);
}

function close_popup_localiser(popup_name){
	$("#" + popup_name).remove();
	$('#popup_'+popup_name).remove();
}

function remove_selection(id){
	$.ajax({
		type: "POST",
		url: base_url + '/selection/remove/id/' + id,
		dataType : "html",
		complete:function()
		{
			$("#maSelectionRowId" + id ).remove();
		}
	});
}

function close_popup(popup_name){
	$("#" + popup_name).remove();
	$('#popup_'+popup_name).remove();
}

function update_selection_quicklist()
{
	var divLoading = $("#chargement");
	
	$.ajax({
		type: "POST",
		url: base_url + '/selection/quicklist', // Url d'envoie de mail - accès sécurisé
		dataType : "html",
		beforeSend:function()
		{
			$(divLoading).show(); // affiche la div de chargement
		},
		success:function(data)
		{
			if(data) {
				$("#selection_content").hide();
				$("#selection_content").html(data);
				$("#selection_content").show();
			}
		},
		complete:function()
		{
			$(divLoading).hide(); // Cache la div de chargement
		}
	});
}

function getRadioValue(nameRadio)
{
	Elt = document.getElementsByName(nameRadio);
	for (i=0;i<Elt.length;i++) {
		if (Elt[i].checked) {
			return Elt[i].value;
			break;
		}
	}
}

function getVar(nomVariable)
{
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&";
	
	if (infos.indexOf("#")!=-1)
	infos = infos.substring(0,infos.indexOf("#"))+"&";
	var variable=0;
	{
		nomVariable = nomVariable + "=";
		var taille = nomVariable.length;
		if (infos.indexOf(nomVariable)!=-1)
		variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"));
	}
	return variable;
}
	
/* Change params */
function urlevt(field,value) 
{
	// Récupère les paramètres dans l'url qui concerne les variables JS
	var params = new Array('dp','tc','sort','eid','ne','exp','vm','ag','pg','vid','loc');
	var params_string = '';
	
	for(i=0;i<params.length;i++) {
		// alert(params[i]);
		if(params[i] == field) {
			if(value != '') {
				params_string += '&' + field + '=' + value;
			}
		} else if(getVar(params[i]) != '') {
			params_string += '&' + params[i] + '=' + getVar(params[i]);
		}
	}
	document.location = url + '&aff=1' + params_string;
}

/*
* Fonction de clonage
* @author Keith Devens
* @see http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone
*/
function clone(srcInstance)
{
    /*Si l'instance source n'est pas un objet ou qu'elle ne vaut rien c'est une feuille donc on la retourne*/
    if(typeof(srcInstance) != 'object' || srcInstance == null)
    {
        return srcInstance;
    }
    /*On appel le constructeur de l'instance source pour crée une nouvelle instance de la même classe*/
    var newInstance = srcInstance.constructor();
    /*On parcourt les propriétés de l'objet et on les recopies dans la nouvelle instance*/
    for(var i in srcInstance)
    {
        newInstance[i] = clone(srcInstance[i]);
    }
    /*On retourne la nouvelle instance*/
    return newInstance;
}

function pushDivContent2div(divSender,divReceiver,backup,focus) {
  if (backup && backup!='no') {backupDivContent(divReceiver)};
  divSenderHl=document.getElementById(divSender);
  divReceiverHl=document.getElementById(divReceiver);
  targetFocus=document.getElementById(focus);
  height = divReceiverHl.offsetHeight;
  width = divReceiverHl.offsetWidth;
  divReceiverHl.innerHTML=divSenderHl.innerHTML;
  divReceiverHl.style.height = height;
  divReceiverHl.style.width = width;
  if (focus) {
    targetFocusS=document.getElementsByName(focus);
    targetFocus = targetFocusS.item(0);
    targetFocus.focus();
    targetFocus.select();
  }
  if (!isIE) new ElementMaxHeight();
}

function backupDivContent(divId) {
  divHl=document.getElementById(divId);
  divBackup[divId] = divHl.innerHTML;
}

function restoreDivContent(divId) {
  if (divBackup[divId]) {
    divHl=document.getElementById(divId);
    divHl.innerHTML = divBackup[divId];
    if (!isIE) new ElementMaxHeight();
  }
}
