
//
//
// MENU
//
//

var embeddedContentName = "embeded";

function addSubMenuEffects(id, mainContentURL, lowerContentURL) {
	getLink(id).onclick = function() {
		if (mainContentURL != undefined) loadMainContent(mainContentURL);
		if (lowerContentURL != undefined) loadLowerContent(lowerContentURL);
	}
	if(queryString(id) == 'blind') {
		Effect.toggle(id, 'blind', { duration: 0.5 } );
	}
	if(queryString(id) == 'true') {
		$(id).setStyle({display: 'block'});
	}
}

function addSubMenu() {
	addSubMenuEffects("MenuMediStory");
	addSubMenuEffects("MenuExpressVitale");
}

function showEmbeddedContent() {
	var embeddedContent = $(embeddedContentName);
	new Effect.BlindDown(embeddedContentName, {duration: 0.5, queue: {position:'end', scope: 'embedded'}});
}

function hideEmbeddedContent() {
	var embeddedContent = $(embeddedContentName);
	new Effect.BlindUp(embeddedContentName, {duration: 0.2, queue: {position:'end', scope: 'embedded'}});
	//new Effect.Puff(embeddedContentName, {duration: 0.5, queue: {position:'end', scope: 'embedded'}});

}

function loadMainContent(url) {
	new Ajax.Updater('maincontent', url, {method:'get', asynchronous:true, evalScripts:true});
}

function loadLowerContent(url) {
	new Ajax.Updater('lowercontent', url, {method:'get', asynchronous:true, evalScripts:true});
}

function loadEmbededContent(url) {
	new Ajax.Updater(embeddedContentName + '_content', url, {method:'get', asynchronous:true, evalScripts:true, onComplete: showEmbeddedContent, queue: {position:'end', scope: 'embedded'}});
}


function getMapLink(id) {
	return $("map_" + id);
}

function addShowHideDepEffects(id) {
	var element = getMapLink(id);
	if (element != undefined) {
		element.onclick = function() {
			showHideDep(id);
		}
	}
	if ($(id + '_content') != undefined) {
		Element.hide(id + '_content');
	}
}

var previousShown = null;

function showHideDep(id) {
	// to prevent redoing it on the same one
	if ((id + '_content') != previousShown) {
		if ($(id + '_content') != undefined) {
			if (previousShown != null) {
				//the we need to hide the prevous one
				new Effect.Appear(id + '_content', 'appear',  {duration: 0.5, queue: {position:'end', scope: 'reseau'}});
				new Effect.Fade(previousShown,  {duration: 0.5, queue: {position:'end', scope: 'reseau'}});
			} else {
				new Effect.Appear(id + '_content', 'appear',  {duration: 0.5, queue: {position:'end', scope: 'reseau'}});
			}
			previousShown = id + '_content';
		}
	}
}

var mapShown = false;

function addIDFZoom(id) {
	var element = $(id);
	if (element != undefined) {
		element.onclick = function() {
			if ($("dep-IDF_content") != undefined){ 
				if (mapShown) {
					new Effect.Shrink("dep-IDF_content",{direction: 'top-right', duration: 0.5, queue: {position:'end', scope: 'idfMap'}});
					mapShown = false;
				} else {
					new Effect.Appear("dep-IDF_content",{direction: 'top-left', duration: 0.5, queue: {position:'end', scope: 'idfMap'}});
					mapShown = true;
				}
				
			}
		}
	}
	if ($("dep-IDF_content") != undefined) {
		Element.hide("dep-IDF_content");
		mapShown = false;
	}
}


ELO.functionsToCallOnload.push(addSubMenu);


//
//
// SLIDES
//
//
var situationNames = ["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah"];
var startPosition = 0;

var PESituation = Class.create();

PESituation.prototype = {
    // vars
    valid: false,
    elementOfPage: undefined,

   initialize: function(name, left, top, width, height, hoverImageName, url) {
		this.elementOfPage = $(name);
        if (this.elementOfPage == undefined) {
            this.valid = false;
			alert(name + "is not valid");
        } else {
            this.valid = true;
            var styleHash = {};
            styleHash.left = left + "px";
            styleHash.top = top + "px";
            styleHash.width = width + "px";
            styleHash.height = height + "px";
            this.elementOfPage.setStyle(styleHash);
        }
		//this.elementOfPage.originalImageName = this.elementOfPage.getStyle("background-image");
		this.elementOfPage.url = url;
		//alert(this.elementOfPage.originalImageName);
		this.elementOfPage.onmouseover = function() {
			//alert("onmouseover");
			this.setStyle({backgroundImage: 'url(images/' + hoverImageName + ')'});
		};
		this.elementOfPage.onmouseout = function() {
			//alert("onmouseout");
			// disabled as this.elementOfPage.getStyle("background-image") doesn't work in Safari FDF
			//this.setStyle({backgroundImage: this.originalImageName});
			this.setStyle({backgroundImage: 'url(images/SlideBG.gif)'});
		};
 		this.elementOfPage.onclick = function() {
			//alert("onclick");
			//FDF disabled for lack of content
			//loadEmbededContent(this.url);
		};
   }
   
};	

var PESituationsSlider = Class.create();

PESituationsSlider.prototype = {
    // vars
    valid: false,
    numberOfSlides: undefined,
    icons: undefined,
    bracket: undefined,
    slider: undefined,
    currentPosition: undefined,
    bracketSize: 3,

   initialize: function(sliderName, iconsName, bracketName, numberOfSlides) {
		this.slider = $(sliderName);
		this.icons = $(iconsName);
		this.bracket = $(bracketName);
        if (this.slider == undefined || this.icons == undefined || this.bracket == undefined) {
            this.valid = false;
			alert(sliderName + "is not valid\n" + iconsName + "is not valid\n" + bracketName + "is not valid");
        } else {
            this.valid = true;
            this.numberOfSlides = numberOfSlides;
			// need to 
            var styleHash = {};
            styleHash.width = (this.numberOfSlides * 10) + "px";
            styleHash.height = "8px";
			//alert(iconsName + " width: " + (this.numberOfSlides * 10) + "px");
            this.icons.setStyle(styleHash);
			
        }
   },

   isEnd: function() {
      return this.currentPosition >= (this.numberOfSlides - this.bracketSize);
   },

   isStart: function() {
      return this. currentPosition == 0;
   },

   nextMotion: function() {
        if (! this.isEnd()) {
            return new Effect.MoveBy(this.bracket, 0, 10, { sync: true });
        }
   },

   previousMotion: function() {
        if (! this.isStart()) {
            return new Effect.MoveBy(this.bracket, 0, -10, { sync: true });
        }
   }
};	

var PESituations = Class.create();

//defining the rest of the class implmentation
PESituations.prototype = {
    valid: false,
    elementOfPage: undefined,
	situations: [],
    currentPosition: 0,
    situationWidth: undefined,
    situationHeight: undefined,
    bracketSize: 3,
    slider: undefined,
 
    initialize: function(name, situationNames, width, height, startPosition, hoverImageName, urls) {
		this.elementOfPage = $(name);
        if (this.elementOfPage == undefined) {
            this.valid = false;
			alert(name + "is not valid");
        } else {
            var numberOfSituations = situationNames.length;
            this.situationWidth = 180;
            this.situationHeight = height;
           for (var i = 0; i < situationNames.length; i += 1) {
	                 this.situations[i] = new PESituation(situationNames[i], this.situationWidth * i, 0, this.situationWidth, this.situationHeight, hoverImageName, urls[i]);
            }
			var styleHash = {};
            styleHash.width = (numberOfSituations * this.situationWidth) + "px";
            styleHash.height = this.situationHeight + "px";
            this.elementOfPage.setStyle(styleHash);
			
	
			// craete slider
			this.slider = new PESituationsSlider(name + "_slider", name + "_icons", name + "_bracket", numberOfSituations);
			// link up the arrows

        }
   },

   isEnd: function() {
      return this.currentPosition >= (this.situations.length - this.bracketSize);
   },

   isStart: function() {
      return this.currentPosition < 1;
   },

   next: function() {
	   	//alert("next() isEnd: " + this.isEnd());
	
        if (! this.isEnd()) {
	   		//alert("next()\nelementOfPage: " + this.elementOfPage + "\nsituationWidth: " + this.situationWidth);
			
			new Effect.Parallel(
				[
				 new Effect.MoveBy(this.elementOfPage, 0, -1 * this.situationWidth, { sync: true }) 
				 ,this.slider.nextMotion()
				],
				{ duration: .75, queue: {position:'end', scope: 'slideshow'}}
			);
			this.currentPosition++;
        }
   },

   previous: function() {
        if (! this.isStart()) {
			new Effect.Parallel(
				[
				 new Effect.MoveBy(this.elementOfPage, 0, this.situationWidth, { sync: true })
				 ,this.slider.previousMotion()
				],
				{ duration: .75, queue: {position:'end', scope: 'slideshow'}}
			);
			this.currentPosition--;
        }
      
   }
};	



function addSlideshow() {
	
}

ELO.functionsToCallOnload.push(addSlideshow);

//
//
// FORMS
//
//
/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/

var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

function checkFormKey(id, bad, good) {
	var valueForID = queryString(id);
	if(valueForID == 'E') {
		document.write(bad);
	} else {
		if (good != undefined) {document.write(good);}
		if ((valueForID != undefined) && (valueForID != "false")) {
			document.getElementById(id).value = Utf8.decode(valueForID);
		}
	}
}
