
// force CSS screen for old FIrefox versions (force for < 1.9.1.0 (1.9.0.19 requires it))
var v_i = $.browser.version.substr(0,3);	// 1.9
var v_ii = $.browser.version.substr(4,5);	// (1.9.)2.3
if ($.browser.mozilla && (v_i < 1.9 || (v_i == "1.9" && v_ii < 1)))
{
	// go find the /root folder, so it works with /corporate, /entreprise, /biznes, etc
	var href = document.getElementsByTagName("head")[0].getElementsByTagName("link") [0].href;
	var root = href.split ("/") [3];

	var fileref = document.createElement('link');
	fileref.setAttribute ("rel","stylesheet");
	fileref.setAttribute ("type", "text/css");
	fileref.setAttribute ("media", "screen");
	fileref.setAttribute ("href", "/" + root + "/images/main.css");
	if (typeof fileref != "undefined")
		document.getElementsByTagName("head")[0].appendChild (fileref);

	var filerefb = document.createElement('link');
	filerefb.setAttribute ("rel","stylesheet");
	filerefb.setAttribute ("type", "text/css");
	filerefb.setAttribute ("media", "screen");
	filerefb.setAttribute ("href", "/" + root + "/images/basic.css");
	if (typeof filerefb != "undefined")
		document.getElementsByTagName("head")[0].appendChild (filerefb);
	
	var filerefp = document.createElement('link');
	filerefp.setAttribute ("rel","stylesheet");
	filerefp.setAttribute ("type", "text/css");
	filerefp.setAttribute ("media", "print");
	filerefp.setAttribute ("href", "/" + root + "/images/print.css");
	if (typeof filerefp != "undefined")
		document.getElementsByTagName("head")[0].appendChild (filerefp);
}

$(document).ready(function() {
	
	// on forms hide the default text on textarea
	$('textarea').each(function()
	{
		if ($(this).attr('defaultvalue') != null)
		{
			this.value = $(this).attr('defaultvalue');
			
			$(this).focus(function()
			{
				if(this.value == $(this).attr('defaultvalue')) this.value = '';
			});
		 
			$(this).blur(function()
			{
				if(this.value == '') this.value = $(this).attr('defaultvalue');
			});
		}
	});
		
	// do on non mobile devices
	if (!isMobile ())
	{
		/* Popin */
		if (typeof $().colorbox != "undefined")
		{
			$(".ma_tools_addcomment a").colorbox({width:"540px", height:"485px", iframe:true});
			$(".ma_tools_mail a").colorbox({width:"540px", height:"495px", iframe:true});
		}
	
		/* ---------------------------------------------
			NAVIGATION PRINCIPALE
		--------------------------------------------- */
		$("#ma_menuprinc ul").hide();
		
		$("#ma_menuprinc li > a").hover(
			function() {
				$(this).next().show();
				//console.log($(this).next());
			},	
			function() {
				$(this).next().hide();
				//console.log($(this).next());
			}	
		);
	
		$("#ma_menuprinc li > ul").hover(
			function() {
				// temporary make the tab active while going into its childs
				// we create a temp active class so the real active tab won't be removed while out!
				$(this).prev().addClass('ma_active_temp');
				$(this).show();
				//console.log($(this).next());
			},	
			function() {
				// remove la class ma_active pr pas que l'onglet reste actif
				// ms ne doit pas retirer un onglet actif!!
				$(this).prev().removeClass('ma_active_temp');
				$(this).hide();
				//console.log($(this).next());
			}	
		);
	}
});

/**
 *	isMobile ()
 * 	Detect if we are runnign on a mobiel device or not
 *	by simply looking at which stylesheet is loaded
 */
function isMobile ()
{
	// just look at the margin-bottom value of #ma_header > h1.
	// on mobiles it's 10px, otherwise it's 0px
	var controlValue = $ ("#ma_header > h1").css ("margin-bottom");
	if (controlValue == "10px") return true;
	else return false;
}

// Collapsable paragraphs
$(function(){
	// do on non mobile devices
	if (!isMobile ())
	{
		$("#ma_questions-answers dd").hide();
		$("#ma_questions-answers dd.ma_activated").show();
		$("#ma_questions-answers dt.ma_activated > span > a").addClass('ma_on');
	}
});

$(function(){
	// do on non mobile devices
	if (!isMobile ())
	{
		$("#ma_questions-answers > dt > span > a").click(function(){
																  
			$(this).toggleClass('ma_on');
			$(this).parent().parent().next().toggle(500);
			
			// reload the google maps if any
			if (typeof (initialize) != "undefined")
				initialize();
				
			return false;
		});
	}
});

/**
 *	dropDown_submit ()
 *	Submit a onchange dropdown event
 */
function dropDown_submit (select_node, form_id) {
	// get the selected value
	var value = select_node.options [select_node.selectedIndex].value; 
	
	// check that we're not on the first element
	if (select_node.selectedIndex != 0) 
	{	
		document.getElementById (form_id).action = value;
		document.getElementById (form_id).submit();
		// open the link in a new window
		//window.open(value, '_blank');
	}
}

/**
 *	changeFormAction ()
 *	When a select is changed, the action will be changed by the email address of the selected option
 */
function changeFormAction (formId, select) {
		// if the field's value is a mailto, we'll take it and change the form's action with it!
		var value = select.options [select.selectedIndex].value; 
		if (value.indexOf ("mailto") > -1) {		
			document.getElementById (formId).action = value;
		}
}

/**
 *  Class forms
 *  Manage forms queries
 */
var forms = {
	FilterOrigin: null,

    /**
     *  load ()
     *  Load the forms by doing things like:
     *      - if a text/textarea field has a default text, clear it on focus
     *      - etc
     */
    load: function () {
		
		$ ("form").each (function () {
			var currentForm = $ (this);
			
			// omit the search form
			if ($ (this).attr ("id") != "ma_search-form") {
			//	alert ("form found");
				
				// ok, let's look for linked items
				// get the select nodes
				var selects = $ (this).find ("select").each (function () {
				
					$ (this).change (function () {
			//				alert ("select found");
						
						// get this option node
						$ (this).find ("option:selected").each (function () {
							var value = $ (this).attr ("value");
							var rel = $ (this).attr ("rel");
							var found = [];	// store the values found for removing the other ones
							
							// get the rels
							if (typeof rel != "undefined") {
								// form: rel='link:Receiver>Service Conseil Clients##Receiver>Pôle Voyage / Loisirs / Mobilité##'
								
								// split the shit
								var options = rel.split ("link:");
								var cleared = false;
								if (options.length > 0) {
									
									// let's get the links, seperated by a ##
									var links = options [1].split ("##");
									
									for (var i = 0; i < links.length - 1; i++) {
										// get the select name and the values it can take
										var values = links [i].split (">");

										var selectID = "ctl00_mainContent_" + values [0];
										var selectValue = values [1];
										
										// ok, now that we have that we need to get the values the select has and filter them to keep only the ones we've found.
										// @note We store that once in forms.FilterOrigin so we have the complete list of values
										
										// get the values
										var valuesList;
										if (forms.FilterOrigin != null) 
											valuesList = forms.FilterOrigin;
										else {
											// grab from the select 'selectID'
											valuesList = $ ("#" + selectID);
											forms.FilterOrigin = valuesList.clone ();			// store it
										}
										
										// now that we are our values, we'll loop through them, and only keep the one we have
										// in fact, we'll clear this select and add to it the values found
										if (!cleared) {
										//	$ ("#" + selectID).html ('');
											cleared = true;
										}
										
										valuesList.find ("option").each (function () {
								//			alert ("look for option match for " + selectValue);
										
											if ($ (this).text () == selectValue) {
								//				alert ("Found for replace: " + selectValue + ", add to " + "#" + selectID);
												
												// only add the item if not already in there!
												var notThereAlready =false;
												$ ("#" + selectID).find ("option").each (function () {
													if ($ (this).text () == selectValue)
														notThereAlready = true;
												});
												
												if (!notThereAlready) {
													$ ("#" + selectID).append ("<option value='" + $ (this).attr ("value") + "'>" + $ (this).text () + "</option>");
												}
												
												found.push (selectValue);
											}
										});
										
										// remove the other ones
										$ ("#" + selectID).find ("option").each (function () {
											if (!in_array (found, $ (this).text ())) {
								//				alert ("removing item " + $ (this).text ());
												$ (this).remove ();
											}
										});
									}
								}
							}
						});
					});
				});
			}
			
		});
    }
};

function in_array (array, value) {
	for (var i = 0; i < array.length; i++) {
		if (array [i] == value) return true;
	}
	return false;
}

/* --------------------------------------------
	AGRANDISSEMENT ET REDUCTION
	DE LA TAILLE DU TEXTE
--------------------------------------------- */
var currentSize = 10;
var minSize = 6;
var maxSize = 16;
function changeTaille(taille) {
	currentSize += taille;
	currentSize = (currentSize<minSize) ? minSize : ((currentSize>maxSize) ? maxSize : currentSize);

//	if (document.all)
//		document.all.centre.style.setAttribute('font-size', (currentSize/10)+'em');
//	else
	if (document.getElementById('centre'))
		document.getElementById('centre').style.fontSize = (currentSize/10)+'em';
	if (document.getElementById('article'))
		document.getElementById('article').style.fontSize = (currentSize/10)+'em';
        if (document.getElementById('art'))
                document.getElementById('art').style.fontSize = (currentSize/10)+'em';
	if (document.getElementById('blocnote'))
		document.getElementById('blocnote').style.fontSize = (currentSize/10)+'em';
}

/**********************Zoom Avant/Arrière************************/

$(function(){

	Cpt = 3;
	var Ech = new Array("20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "160%", "170%", "180%");
	
	$("#ma_dec-font").click(function(){
		if(Cpt > -2) {
			Cpt--;
			document.getElementById("ma_mainContent").style.fontSize = Ech[Cpt+4];

		}
		return(false);		
	});
	
	$("#ma_inc-font").click(function(){
		if(Cpt < (Ech.length-1)) {
			Cpt++;
			document.getElementById("ma_mainContent").style.fontSize = Ech[Cpt+5];
		}
		return(false);
	});
	
});
/**********************Zoom Avant/Arrière************************/

$(function(){

	Cpt = 3;
	var Ech = new Array("20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "160%", "170%", "180%");
	
	$("#ma_dec-font2").click(function(){
		if(Cpt > -2) {
			Cpt--;
			document.getElementById("ma_bloc-all").style.fontSize = Ech[Cpt+4];

		}
		return(false);		
	});
	
	$("#ma_inc-font2").click(function(){
		if(Cpt < (Ech.length-1)) {
			Cpt++;
			document.getElementById("ma_bloc-all").style.fontSize = Ech[Cpt+5];
		}
		return(false);
	});
	
});
