/* COMMON JAVASCRIPT */

/**
  * Get element by id
  * @param string id, the element id to get
  * @return wanted element or false if not founded
*/
if (typeof 'getE' != 'function') {
	function getE(id) {
		if (el = document.getElementById(id)) { 
			return el;
		} else {
			return false;
		}
	}
}

/**
  * Add an event on a given object
  * @param html object obj, the object to add event on
  * @param string evType, the event type to add (click, mouseover, blur, etc.)
  * @param string fcn, the function name to call on event
  * @return boolean true on success, false on failure
  */
function addEvent(obj, evType, fcn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fcn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fcn);
		return r;
	} else {
		return false;
	}
	return true;
}

/**********************************************
* Browser definition                          *
**********************************************/
var browserOK = false;
if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
	var isIE = true;
	var menutimer;
	//get IE version
	if (navigator.userAgent.toLowerCase().indexOf("msie 5.5") != -1) {
		var isIE55 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 6") != -1)  {
		var isIE6 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 7") != -1)  {
		var isIE7 = true;
		browserOK = true;
	}
	var timerdiv;
	var timerli;
	var elementdiv;
	var elementli;
} else if (navigator.userAgent.toLowerCase().indexOf('opera') != -1) {
	//not compatible for now (tried with opera 8 & 9)
	var isOp = true;
} else if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1 || 
		  (navigator.userAgent.toLowerCase().indexOf('mozilla') != -1 && (navigator.userAgent.toLowerCase().indexOf('rv:1.7') != -1 || navigator.userAgent.toLowerCase().indexOf('rv:1.8') != -1))) {
	browserOK = true;
	var isMoz = true;
}
/*add some events on menu for IE compatibility only*/
function startMenu() {
	if (isIE /* && !isIE7	*/) {
		var liMenu = document.getElementById("nav1").getElementsByTagName("LI");
		
		for (var i=0; i<liMenu.length; i++) {
		  	li = liMenu[i];
			
		  	if (li.nodeName=="LI") {
		  		li.onmouseenter=function() {
					if(this.className != 'start-menu' && this.className != 'end-menu'){
			  			this.className += " over";
					}
		    	}
		  		li.onmouseleave=function() {
		  			this.className = this.className.replace(" over", "");
				}
			}
		}
		var liMenuTop = document.getElementById("top").getElementsByTagName("LI");
		
		for (var i=0; i<liMenuTop.length; i++) {
		  	li = liMenuTop[i];
		  	if (li.nodeName=="LI") {
		  		li.onmouseenter=function() {
			  		this.className += " over";
					
		    	}
		  		li.onmouseleave=function() {
		  			this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}
function addSwitchBox(element,box) {
	addEvent(element,'mouseenter',function() {
		if(element && element == getE('menu6')){
			element.style.backgroundImage = 'url(/img/sicorp/home/menu_backgr_new2end.jpg)';
	  	} else if(element) {
			element.style.backgroundImage = 'url(/img/sicorp/home/menu_backgr_new2.jpg)';
		}
		if(box){
			box.style.display = 'block';
		}
    });
  	addEvent(element,'mouseleave',function() {
		if(element && element == getE('menu6')){
			element.style.backgroundImage = 'url(/img/sicorp/home/menu_backgr_new1end.jpg)';
	  	} else if(element) {
			element.style.backgroundImage = 'url(/img/sicorp/home/menu_backgr_new1.jpg)';
		}
		if(box){
  			box.style.display = 'none';
		}
    });
}
addEvent(window, 'load', function(){
	startMenu();
	if(getE("q")){
		addEvent(getE("q"), 'focus', function(){
			getE("q").value = "";
		});
		addEvent(getE("q"), 'blur', function(){
			if(getE("q").value == "") getE("q").value = getE("q").title;
		});
	}

});

/** Default main window name (used by plan du site & liste de produits)
*/
window.name = "spotimage";

/** Default string to appear in status bar
*/
var strDefaultStatus = "SPOT IMAGE" ;
window.status = strDefaultStatus;

/* function to open Navigator Windows
* parameters available: resizable (r), location (l), directories (d), menubar (m),
*	status (st), toolbar (t), scrollbars (s), copyhistory (c)
*/
function openWindow(url, name, w, h, r, s, m,left, top){
	var popupWin = window.open(url, name, 'width=' + w + ',height=' + h + ',resizable=' + r + ',scrollbars=' + s + ',menubar=' + m + ',left=' + left + ',top=' + top);
}

/** 
  * Opens pop up window with term definition
  *
  * @param integer _id, the ID of desired glossary Term
  * @param String _lang the current language codename for interface
  */
function openGlossary(_id, _lang)
{
	//Close any opened definition window
	if (typeof(glossaire)!='undefined'){
		glossaire.close();
	}
	openWindow('/popup-glossaire.php?lang='+_lang+'&id='+_id, 'glossaire', 550, 350, 'no', 'yes', 'no', 40, 40);
}

/**
  * Swaps a picture between given statuses
  * Picture must be preloaded
  * 
  * @param String imgName, image "name" attribute value in HTML page
  * @param String status, current status to show
  * @return void
  */
function swap(imgName, status) {
    if (document.images) {
    	document[imgName].src = eval(imgName + status + '.src');
    }
    window.status = strDefaultStatus;
}

/**
  * Instanciates an image with its src
  *
  * @param String src, the image source
  * @return Image object created
  */
function newImage(arg) {
	if (document.images && arg!="") {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

/**
  * Emptying and filling search field
  *
  * @param Field field, the field object itself
  * @return void
  */
function clearField(field) {
	if (field.value == field.defaultValue) {
		field.value = "";
	}
}

/**
  * Setting default value to search field
  *
  * @param Field field, the field object itself
  * @return void
  */
function checkField(field) {
	if (field.value == "") {
		field.value = field.defaultValue;
	}
}


/**
  * Open a pop up window for Commercial Network
  * Show the details of an agency
  * Asked from flash "swf/*-rescom-index.swf"
  * 
  * @param integer id, the id of wanted agency
  * @param String lang , the language codename
  * @return void
  */
openedAgencyDetails = null;

function openAgencyWindow(id, lang) {
	if (openedAgencyDetails == null || openedAgencyDetails.closed) {
	} else {
		openedAgencyDetails.close();
	}
	if (parseInt(id)>0 && lang!='') {
		openedAgencyDetails = window.open('/rescom-popup-agence.php?lang='+ lang +'&id='+ id, 'anAgencyDetails', "width=430,height=550,resizable=yes,scrollbars=yes,menubar=no,left=20,top=20");
	}
}

/**
  * Open a pop up window for Commercial Network
  * Show the details of SPOT CONTACTS for a given country
  * Asked from flash "swf/*-rescom-index.swf"
  * 
  * @param integer id, the id of wanted country
  * @param String lang , the language codename
  * @return void
  */
openedContactDetails = null;

function openSpotContactWindow(id, lang) {
	if (openedContactDetails == null || openedContactDetails.closed) {
	} else {
		openedContactDetails.close();
	}
	if (parseInt(id)>0 && lang!='') {
		var _x = 200 ;
		if (screen.width > 1000) {
			_x = 450;
		}
		openedContactDetails = window.open('/rescom-popup-contactspot.php?lang='+ lang +'&id='+ id, 'aCountryDetails', "width=430,height=550,resizable=yes,scrollbars=yes,menubar=no,left="+ _x +",top=20");
	}
}

/**
  * Function to open a CMS page into a pop-up
  * 
  * @param String href, location of page to open
  * @param integer id, page ID, for naming pop-up
  * @param integer width, pop-up width
  * @param integer height, pop-up height
  * @return window
  */
function CMS_openPopUpPage(href, id, width, height)
{
	width = (width<=0) ? 500 : width;
	height = (height<=0) ? 500 : height;
	if (href != "") {
		pagePopupWin = window.open(href, 'CMS_page_'+id, 'width='+width+',height='+height+',resizable=yes,menubar=no,left=0,top=0');
	}
}
