/**
 * Javascripts de la Lightbox.
 * @author David LIMA
 * @date 21/09/2008
 * @lastupdate 21/09/2008
 *
 * @copyright LBA Thivel
 */

/*
 * Fonction qui établi le contenu de la lightbox depuis un composant HTML.
 * @param String head: titre du lightbox
 * @param Object value: composant HTML
 */
function lightboxAppendContent(head, value) {
	var lightbox_header  = document.getElementById('lightbox_header');
	var lightbox_content = document.getElementById('lightbox_content');
	lightbox_header.innerHTML = head;
	lightbox_content.appendChild(value);
}

/*
 * Affiche la lightbox.
 */
function lightboxDisplay() {
	var backbox  = document.getElementById('backbox');
	var lightbox = document.getElementById('lightbox');
	backbox.style.display  = 'block';
	lightbox.style.display = 'block';
	lightboxAdjustSize();
}

/*
 * Masque la lightbox.
 */
function lightboxClose() {
	var backbox  = document.getElementById('backbox');
	var lightbox = document.getElementById('lightbox');
	var lightbox_content = document.getElementById('lightbox_content');
	var nodes = lightbox_content.childNodes;
        for (var i = 1; i <= nodes.length; i++) {
		lightbox_content.removeChild(nodes[i]);
	}
	backbox.style.display  = 'none';
	lightbox.style.display = 'none';
}

/*
 * Ajuste la taille du lightbox à son contenu et le centre sur la page.
 */
function lightboxAdjustSize() {
	var lightbox = document.getElementById('lightbox');
	var lightbox_content = document.getElementById('lightbox_content');
	var w = lightbox.offsetWidth;
	var h = lightbox.offsetHeight;
	var pagew = window.innerWidth;
	var pageh = window.innerHeight;
	
	if (!pagew || !pageh) {
		var pagew = document.body.clientWidth;
		var pageh = document.body.clientHeight;
	}
	
	lightbox.style.top  = ((parseInt(pageh) - parseInt(h)) / 2) + 'px';
	lightbox.style.left = ((parseInt(pagew) - parseInt(w)) / 2) + 'px';
}/**
 * Javascripts généraux.
 */

var main_page = 'http://localhost:8080/MesSerrures';

/*
 * Vérifie que le referrer est le même site que celui qui appele la page.
 */
function checkReferrer() {
	if (document.referrer == null 
		|| document.referrer == '' 
		|| !document.referrer.contains(main_page)) {
		window.location = main_page;
	}
}

/*
 * Construit une image à partir de l'url source
 * @param String url: url de l'image
 */
function buildImage(url) {
	var inc = document.createElement("img");
	inc.src = url;
	return inc;
}

/*
 * Construit une iframe à partir de l'url source
 * @param String url: url du fichier HTML qui sert de source à l'iFrame
 */
function buildIframe(url) {
	var inc = document.createElement("iframe");
	inc.src = url;
	inc.style.border = 0;
	inc.style.width = '500px';
	inc.style.height = '500px';
	return inc;
}

/*
 * Construit une vidéo à partir de l'url source
 * @param String url: url de la vidéo
 */
function buildPlayer(url) {
	var inc = document.createElement("span");
	inc.innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"http://hercule.prive.lba-sa.fr/imagesfmvideos/player_flv_multi.swf\" width=\"480\" height=\"360\"><param name=\"movie\" value=\"http://hercule.prive.lba-sa.fr/imagesfmvideos/player_flv_multi.swf\" /><param name=\"FlashVars\" value=\"flv=" + url + "&autoload=1&autoplay=1&loop=0&showstop=1&showvolume=1$amp;autonext=1&showplayer=always&showfullscreen=1&buffermessage=Chargement _n_&showopen=1\" /></object>";
	return inc;
}



/*
 * Fonction qui efface le texte par défaut de la zone de recherche.
 * @param Object obj: objet <input type="text" /> source
 * @param String text: texte par défaut
 */
function clearSearch(obj, text) {
	var search = obj.value;
	if (search == text) {
		obj.value = '';
	}
}

/*
 * Fonction qui affiche une image dans la zone de prévisualisation.
 * @param Object sobj: objet <img /> source
 * @param Object tobj: objet <img /> cible
 */
function switchImage(div, trg) {
	var sobj = div.childNodes[1];
	var source = sobj.src;
	if (source != null && source.length > 0) {
		trg.src = source;
		document.getElementById('viewer_title').innerHTML = sobj.title;
	}
}

/*
 * Fonction qui affiche une image dans le popup d'affichage des images.
 * @param Object obj: objet <a /> source
*/ 
function displayImage(obj) {
	var sobj = obj.childNodes[1];
	var source = sobj.src;
	if (source != null && source.length > 0) {
		lightboxAppendContent(sobj.title, buildImage(source));
		lightboxDisplay();
	}
}

/*
 * Fonction qui affiche un fichier HTML dans le popup d'affichage des images.
 * @param String title: titre du lightbox
 * @param String source: fichier HTML à inclure
*/ 
function displayHelp(title, url) {
	if (url != null && url.length > 0) {
		lightboxAppendContent(title, buildIframe(url));
		lightboxDisplay();
	}
}

/*
 * Fonction qui affiche une vidéo dans le popup d'affichage des images.
 * @param String title: titre du lightbox
 * @param String source: fichier FLV
*/ 
function displayMovie(title, url) {
	if (url != null && url.length > 0) {
		lightboxAppendContent(title, buildPlayer(url));
		lightboxDisplay();
	}
}

/*
 * Fonction qui soumet le formulaire d'appel à la page de promotion.
 */
function submitPromo() {
	document.promotion_form.submit();
}

/*
 * Fonction qui soumet le formulaire d'appel à la page de nouveauté.
 */
function submitNew() {
	document.nouveaute_form.submit();
}

/*
 * Fonction qui soumet le formulaire d'appel à la page de compte.
 */
function submitAccount() {
	document.compte_form.submit();
}

/*
 * Fonction qui soumet le formulaire d'appel au panier.
 */
function submitCart() {
	document.panier_form.submit();
}

/*
 * Fonction qui soumet le formulaire d'appel à la page staique.
 */
function submitStatic() {
	document.static_form.submit();
}

/*
 * Fonction qui soumet le formulaire de recherche.
 */
function submitSearch(type, brand, words, parent) {
	document.search_form.search_type.value  = type;
	document.search_form.search_brand.value = brand;
	document.search_form.search_words.value = words;
	document.search_form.search_parent.value= parent;
	document.search_form.submit();
}

/*
 * Soumet le formulaire de recherche dans la barre de recherche.
 */
//function startSearch() {
//	var query = document.getElementById('srch_words').value;
//	submitSearch(
//			document.getElementById('srch_type').value,
//			document.getElementById('srch_brand').value,
//			(query != 'recherche ...') ? query : '',
//			null)
//}

function startKbSearch(e) {
	if (!e) var e = window.event;
	if (e.keyCode == 13) startSearch();
}

function startSearch () {
	var type = document.getElementById('srch_type').value;
	var query = document.getElementById('srch_words').value;
	var brand = document.getElementById('srch_brand').value;
	if (type == 1 && (query != 'recherche ...' || query != '')) {
		submitSearch(2, null, query, null);
	}
	else if (type == 2 && brand != 'Choisissez une marque') {
		submitSearch(2, brand, null, null);
	}
	else if (type == 3 && (query != 'recherche ...' || query != '')) {
		submitSearch(0, null, query, null);
	}
	else if (type == 4 && (query != 'recherche ...' || query != '')) {
		submitSearch(1, null, query, null);
	}
}
function displaySearch () {
	var type = document.getElementById('srch_type').value;
	if (type != 2) {
		document.getElementById('srch_brand').style.display = 'none';
		document.getElementById('srch_words').style.display = 'inline';
	}
	else {
		document.getElementById('srch_brand').style.display = 'inline';
		document.getElementById('srch_words').style.display = 'none';
	}
}

/*
 * Peuple la select des fournisseurs dans la barre de recherche.
 */
function buildFournis() {
	try {
		var select = document.getElementById('srch_brand');
		var opt = document.createElement('OPTION');
		opt.Value = 0;
		opt.innerHTML = 'Choisissez une marque';
		select.appendChild(opt);
		var fournis = json_fournis[0];
		for (var i = 0; i < fournis.length; i++) {
			opt = document.createElement('OPTION');
			opt.value = fournis[i];
			opt.innerHTML = json_fournisnames[fournis[i]];
			select.appendChild(opt);
		}	
	}
	catch (err) {}
}

/*
 * Affiche la fenetre d'enregistrement du panier.
 */
function displaySavecart (bool) {
	document.getElementById('save_cart').style.display = (bool) ? 'table-row' : 'none';
}

/**
 * Affiche un message de confirmation d'ajout au panier.
 * @param String objid
 * @param String msg
 */
function alertAddCart (objid, msg) {
	obj = document.getElementById(objid);
	var coords = findPosition(obj);
	var popup = document.getElementById('add_cart');
	popup.innerHTML = msg;
	popup.style.top = (coords[0]) + 'px';
	popup.style.left = (coords[1]) + 'px';
	popup.style.display = 'block';
	setTimeout("hideAlertAddCart ()", 2000); 
}
function hideAlertAddCart () {
	var popup = document.getElementById('add_cart');
	popup.style.display = 'none';
}
// Ne pas supprimer
function ieResize () {}

function displayRefResults (eid, bool) {
	document.getElementById(eid).style.display = (bool) ? 'block' : 'none';
}

function sendMail(to) {
	if (to != null) {
		var dest = to.split("@")[0];
		if (dest != null) {
			location.href="mailto:" + dest + "@lbathivel.com";
		}
	}
}

function displayWaitCmd (bool) {
	try {
		var backbox  = document.getElementById('backbox');
		var wobj = document.getElementById('waitplease');
		backbox.style.display = bool ? 'block' : 'none';
		wobj.style.display = bool ? 'block' : 'none';
	
		var pagew = window.innerWidth;
		var pageh = window.innerHeight;
		
		if (!pagew || !pageh) {
			var pagew = document.body.clientWidth;
			var pageh = document.body.clientHeight;
		}
		
		wobj.style.top  = ((parseInt(pageh) - 135) / 2) + 'px';
		wobj.style.left = ((parseInt(pagew) - 300) / 2) + 'px';
	}
	catch (err) {}
}
/**
 * Javascripts de la barre de navigation.
 * @author David LIMA
 * @date 21/09/2008
 * @lastupdate 25/09/2008
 *
 * @copyright LBA Thivel
 */

/*
 * Retourne la position absolue de l'objet spécifié sur la page.
 * @param Object obj
 */
function findPosition(obj) {
	curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curtop,curleft];
}

/*
 * Retourne un nouveau lien de navigation.
 * @param String label
 * @param String id
 */
function getEntry(label, id) {
	li = document.createElement('li');
	li.className = 'noout';
	li.innerHTML = "<a href=\"#\" id=\"" + id + "\" name='subnav_link' class='subnav_link' onClick=\"submitNav(this)\" >> " + label + "</a>";
	return li;
}

/*
 * Génère le sous-menu pour l'objet courant.
 * @param Object obj
 */
function generateMenu(obj) {
	div = document.getElementById('nav_sub');
	div.innerHTML = '';
	ul = document.createElement('ul');
	ul.className = 'noout';
	oid = parseInt(obj.id);
	childs = json_childs[oid];
	for (i = 0; i < childs.length; i++) {
		id = childs[i];
		ul.appendChild(getEntry(json_names[id], id));
	}
	div.appendChild(ul);
}

/*
 * Affiche le sous-menu de l'objet courant.
 * @param Object obj
 */
function displayMenu(obj) {
	hideMenu();
	coords = findPosition(obj);
	div = document.getElementById('nav_sub');
	div.style.display = 'block';
	div.style.top = coords[0] + obj.offsetHeight + 'px';
	div.style.left = coords[1] + 'px';
	
	childs = document.getElementsByName('subnav_link');
	if (childs.length > 0) childs[0].focus();
}

/*
 * Masque le sous-menu affiché.
 */
function hideMenu() {
	div = document.getElementById('nav_sub');
	div.style.display = 'none';
}

/*
 * Enregistre l'evennement onMouseOut sur le div permettant
 * d'afficher les sous-menus.
 */
function nav_ol() {
	var onglet;
	try {
		document.getElementById('nav_sub').onmouseout = outMenu;
		onglet = parseInt(onglet = document.search_form.selOnglet.value);
	}
	catch (err) { onglet = 0; }
	
	try {
		document.getElementById('onglet' + onglet).className = 'ongletSEL';
	}
	catch (err) { return; }
}

/*
 * Masque automatiquement le sous-menu affiché lorsque celui-ci n'est plus survolé
 * par la souris.
 * Requiert que la fonction nav_ol() soit chargée dans la page.
 * @param Event e
 */
function outMenu(e) {
	// Event
	if (!e) { e = window.event; }

	// Source object of the event
	if (e.target) { src = e.target; }
	else if (e.srcElement) { src = e.srcElement; }

	// Target object of the event
	if (e.relatedTarget) { trg = e.relatedTarget; }
	else if (e.toElement) { trg = e.toElement; }

	if (src.id == 'nav_sub' && trg.className != 'noout' && trg.className != 'subnav_link') {
		src.style.display = 'none';
	}
}

/*
 * Met à jour et soumet le formulaire de navigation.
 * @param Object obj
 */
function submitNav(obj) {
	document.navigation_form.pid.value = obj.id;
	hideMenu();
	document.navigation_form.submit();
}

function submitHelp() {
	document.help_form.submit();
}

/*
 * Masque le popup de promo.
 */
function hidePromo () {
	document.getElementById('promoCont').style.display = 'none';
}

function displayCartOpts() {
	var hidden = document.getElementById('cartOpts_upload').style.display == 'none';
	document.getElementById('showCartOpts').innerHTML = (hidden) ? 'Masquer les options du panier' : 'Afficher les options du panier';
	document.getElementById('cartOpts_upload').style.display = (hidden) ? 'block' : 'none';
}

function submitWait (obj, form) {
	obj.className = "submit_wait";
	obj.value="Veuillez patienter...";
	obj.disabled = true;
	form.submit();
}

function goFourni () {
	document.brands_form.hidden.value = document.brands_form.srch_brand.value;
	document.brands_form.submit();
}

function highlightHelp (elementId) {
	try {
		var lis = document.getElementsByTagName("LI");
		for (i = 0; i < lis.length; i++) {
			if (lis[i].className == "highlight") {
				lis[i].className = "";
			}
		}
	
		var obj = document.getElementById(elementId);
		obj.className = "highlight";
	}
	catch (err) {}
}

function displayMore(bool) {
	document.getElementById('lbadesc').style.display = (bool) ? 'block' : 'none';
}

function displaySub (id, style, bool, event) {
	var obj = document.getElementById('sub_' + id);
	if (bool) {
		hideAllSubs ();
		obj.className = style;
		obj.style.display = 'block';
	}
	else {
		var x = event.clientX;
		var y = event.clientY;

		if (y < 90 || y > (90 + obj.offsetHeight)) {
			hideAllSubs ();
		}
	}
}
function hideAllSubs () {
	var subs = document.getElementsByName('submenus');
	for (i = 0; i < subs.length; i++) {
		subs[i].style.display = 'none';
	}
}

