/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|
|		Copyright (c) 2008 CSA
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


/* Events */
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/* ______________________[ 01 | Interactivité du menu principal (menu horizontal) ]________________________ */

function SmartMenu(who) {
	if (document.getElementById&&document.getElementsByTagName&&document.getElementById(who)) {
		navRoot=document.getElementById(who);
		for (i=0;i<navRoot.childNodes.length;i++) {
			node=navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.firstChild.onclick=function() {
					var bt=this.parentNode.id;
					switch (bt) {
						case "rub1":
							CL = new Array ("rub2","rub3","rub5","rub4");
							//CL = new Array ("rub2","rub3","rub4");
							break;
						case "rub2":
							CL = new Array ("rub1","rub3","rub5","rub4");
							//CL = new Array ("rub1","rub3","rub4");
							break;
						case "rub3":
							CL = new Array ("rub1","rub2","rub5","rub4");
							//CL = new Array ("rub1","rub2","rub4");
							break;
						case "rub4":
							CL = new Array ("rub1","rub2","rub5","rub3");
							//CL = new Array ("rub1","rub2","rub3");
							break;
						case "rub5":
							CL = new Array ("rub1","rub2","rub3","rub4");
							break;
					}
					for (d=0;d<CL.length;d++) {
						c=document.getElementById(CL[d]);
						c.className="y";
					}
					this.parentNode.className=this.parentNode.className=="on"?"y":"on";
				};
			}
		}
	}
};

function rubrique(obj,rub){
	var aReturn = obj.getElementsByTagName("a");
	aReturn[0].href += '?rub=' + rub;
}

/* ______________________[ 02 | Lancement d’une impression pour les navigateurs compatibles ]________________________ */

function DirectPrint() {
	if (window.print) self.print();
};


/* ______________________[ 03 | Miscellaneous ]________________________ */

function AddSearchBehaviour(sElementID) {
	if(document.getElementById && document.getElementById(sElementID)) {
		var oSearchInput = document.getElementById(sElementID);
		var sOldValue = oSearchInput.value;
		var aFormSearchElements = document.getElementById("FormRecherche").childNodes;
		for(var i = 0 ; i < aFormSearchElements.length ; i++) {
			if(aFormSearchElements[i].nodeType != 3 ) {
				aFormSearchElements[i].style.position = "relative";
			}
		}
		oSearchInput.onclick = function() {
			if(this.value === "Recherche") {
				this.value = "";
			}
		};
		oSearchInput.onblur = function() {
			if(this.value === "") {
				this.value = sOldValue;
			}
		};
	}
};

function verifRecherche(oObj){
	if (oObj.mot.value == 'Recherche' || oObj.mot.value == '') {
		alert('Veuillez saisir un mot-clé pour lancer une recherche');
		return false;
	} else return true;
}

function showInformation(sElementID, sClass) {
	if(document.getElementById && document.getElementsByTagName && document.createElement && document.getElementById(sElementID)) {
		var oContainer = document.getElementById(sElementID);
		var aChannels = oContainer.getElementsByTagName("li");
		var oLink = null;
		// hiding all info blocks
		var aInfoBlocks = getElementsByClassName(oContainer,"div",sClass);

		for(var i = 0 ; i < aInfoBlocks.length ; i++) {
			aInfoBlocks[i].style.display = "none";
		}

		for(var i = 0 ; i < aChannels.length ; i++) {
			oLink = aChannels[i].getElementsByTagName("a")[0];
			oLink.onmouseover = function() {
				var oInfoBlock = getElementsByClassName(this.parentNode,"div",sClass)[0];
				if(oInfoBlock) {
					oInfoBlock.style.display = "block";
				}
			};
			oLink.onmouseout = function() {
				var oInfoBlock = getElementsByClassName(this.parentNode,"div",sClass)[0];
				if(oInfoBlock) {
					oInfoBlock.style.display = "none";
				}
			};
		}
	}
};

function moreInformation(oRoot,sEvent) {
	if(oRoot) {
		var oList = getElementsByClassName(oRoot,"ul","section")[0];
		if(oList) {
			var i = 0;
			for(i = 0; i < oList.childNodes.length; i++) {
				if(!(DOMUtils.isIgnorableNode(oList.childNodes[i]))) {
					var oPTitle = DOMUtils.getFirstChild(oList.childNodes[i]);
					var oLink = DOMUtils.getFirstChild(oPTitle);
					var oDetailBlock = (getElementsByClassName(oList.childNodes[i],"div","detail").length > 0 ? getElementsByClassName(oList.childNodes[i],"div","detail")[0] : null);
					if(oDetailBlock) {
						addEvent( oLink, sEvent, function() {
							var oLi = this.parentNode.parentNode;
							var oUL = oLi.parentNode;
							var oRegExpHorsChamp = new RegExp("(^|\\s)horschamp(\\s|$)");
							var oTarget = null;

							// close other list elements
							/*
							for(var i= 0;i<oUL.childNodes.length;i++) {
								if(!(DOMUtils.isIgnorableNode(oUL.childNodes[i])) && oUL.childNodes[i] != oLi) {
									if(getElementsByClassName(oUL.childNodes[i],"div","detail").length > 0) {
										oUL.childNodes[i].className = "";
										var oTarget = getElementsByClassName(oUL.childNodes[i],"div","detail")[0];
										if(!oRegExpHorsChamp.test(oTarget.className)) {
											oTarget.className += " horschamp";
										}
									}
								}
							}
							*/

							//alert(oLi.className);

							if(getElementsByClassName(oLi,"div","detail").length > 0 && ( oLi.className == "" || oLi.className == "last" ) ) {
								var oTarget = getElementsByClassName(oLi,"div","detail")[0];
								oLi.className = "on";
								oTarget.className = oTarget.className.replace(" horschamp","");
							} else if ( oLi.className == "on" ) {
								var oTarget = getElementsByClassName(oLi,"div","detail")[0];
								oLi.className = "";
								oTarget.className += " horschamp";
							}
						});
						moreInformation(oDetailBlock,"click");	//recursive power !
					}	// if oDetailBlock
				} // if !validNode
			} // for
		} // if oList
	} // if oRoot
};	// moreInformation()


function mapClick(sID) {
	if(document.getElementById && document.createElement && document.getElementById(sID)) {
		var aAreas = document.getElementById(sID).getElementsByTagName("area");
		var aDeptChaines = getElementsByClassName( document.getElementById(sID), "div", "listeChaines");

		// masquer les blocs de détail
		for (var i = 0; i < aDeptChaines.length ; i++) {
			aDeptChaines[i].innerHTML += '<p><a href="javascript:;">Fermer</a></p>';
			aDeptChaines[i].className += " horschamp";
		}

		for(var i = 0 ; i < aAreas.length ; i++) {
			aAreas[i].onclick = function() {
				for (var i = 0; i < aDeptChaines.length ; i++) {
					if(aDeptChaines[i].className.indexOf("horschamp") == -1) {
						aDeptChaines[i].className += " horschamp";
					}
				}
				var sTargetDetail = "chainesLocDept"+this.id.substr(4,2);
				if(document.getElementById(sTargetDetail)) {
					var oTargetDetail = document.getElementById(sTargetDetail);
					oTargetDetail.className = oTargetDetail.className.replace(/horschamp/,"");
					oTargetDetail.getElementsByTagName("p")[0].getElementsByTagName("a")[0].onclick = function() {
						oTargetDetail.className += " horschamp";
					};
				}
			};
		}
	}
};

function popupGallerie() {
	var aPopupLinks = document.getElementById("ColB").getElementsByTagName("a");

	for(i = 0 ; i < aPopupLinks.length ; i++) {
		if(aPopupLinks[i].rel && aPopupLinks[i].rel.indexOf("popup") > -1) {
			aPopupLinks[i].onclick = function() {
				window.open(this.href,'Album','status=no,location=no,toolbar=no,directories=no,resizable=no,width=286,height=392,top='+((screen.height-400)/2)+',left='+((screen.width-350)/2));
				return false;
			}
		}
	}
};

function carteSurvol () {
  $("#carteTNT area").each( function () {
    $(this).parents("#carteTNT").prepend("<div class='AreaSurvol' id='piece"+$("#carteTNT area").index(this)+"'><a href='"+$(this).attr("href")+"' style='color:black;font-weight:normal'>Zone<br />"+$(this).attr("alt")+"</a></div>");
  });
  var aid = "#piece";
  $("#carteTNT area").bind("mouseover focus", function(){
    $(aid+$("#carteTNT area").index(this)).addClass('over');
  });
  $("#carteTNT area").bind("mouseout blur", function(){
    $(aid+$("#carteTNT area").index(this)).removeClass('over');
  });
};

/* ______________________[ 08 | Lancement des scripts ]________________________ */

window.onload=function() {
	SmartMenu("NavigationPrincipale");
	AddSearchBehaviour("searchtext");
	//showInformation("chainesNatGratuites","infoChaine");
	mapClick("carte");
	popupGallerie();
	var aDetailBlocks=getElementsByClassName(document,"div","detail");
	if(aDetailBlocks.length>0) {
		for( var i=0;i< aDetailBlocks.length;i++) {
			aDetailBlocks[i].className+=" horschamp";
		}
		moreInformation(getElementsByClassName(document,"div","foldable")[0],"click");
	}

	/*  ___ [ Carte survolée ] __*/
	if( $("#carteTNT").length > 0 ) {
		carteSurvol();
	}

	if ( document.getElementById('map') || document.getElementById('map_canvas') ){
        load();
    }

	/* IE 5-6 background flicker fix */
	try {
		document.execCommand('BackgroundImageCache', false, true);
	}
	catch(e) { }
};


/* ______________________[ 09 | Ajouts CSA ]________________________ */

function popup(link,name,size){
	newwin = window.open(link,name,size);
}

function OpenStatut(){
	window.open('/outils/legales/mentionslegales.php','','status=0,location=0,toolbar=0,directories=0,resizable=1,width=500,height=520,top=60,left=100');
}

function popuptab(url, w, h, sc) {
    window.open(url, '', 'resizable=yes,scrollbars=' + sc + ',menubar=no,width=' + w + ',height=' + h);
}


/*
 * Tooltip script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
this.tooltip = function(){
	/* CONFIG */
		xOffset = 10;
		yOffset = 20;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$("img.tooltip").hover(function(e){
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("width","250px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("img.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
// starting the script on page load
$(document).ready(function(){
	tooltip();
});
/* End Tooltip script */