/*
** Constructeur
** plugId > un identifiant unique dans la page
** classeout > la class ccs qui s'applique quand la soursi sort d'une image
** classeover > la class ccs qui s'applique au passage sur une image
** classeselected > la class css qui s'applique quand l'image à été cliquée
** nom > ?
** nomiframe > identifiant de l'iframe qui reçoit les pages de destination
** taillemax > taille du bandeau
** boucle > boolean qui définit si le bandeau doit tourner en boucle
*/
function dmc_Plugin0(plugId,classeout,classeover,classeselected,nom,nomiframe,taillemax,obligatoire,boucle, inVerticale, inOrientation) {
	this.numPlugin = 0; // utilité ?
	this.id = plugId;
	this.nom = nom;
	this.plug = Dmc_Layer.is_object("aoplugin"+this.id); // on vérifit que le conteneur existe bien
	this.element = Dmc_Layer.is_object("aopluginconteneur"+this.id); //de même pour le conteneur des images
	this.cadre = this.plug//.getElementsByTagName("dd")[0]; // le 'cadre' qui contient le 'conteneur'
	this.classe = {"out":classeout,"over":classeover,"selected":classeselected}; // hashmap de trois éléments pour gérer l'apparence
	this.verticale = inVerticale;
	this.orientation = inOrientation;
	
	/* Le boutons */
	this.boutons = new Array(2);
	this.boutons[0] = document.getElementById("btn1_" + this.nom);
	this.boutons[1] = document.getElementById("btn2_" + this.nom);
	
	/* Vitesse de défilement automatique et forcée */
	this.speed=50;
	this.autospeed=50;
	this.qt=10;
	this.autoqt=2;
	/* FIN vitesse */
	
	this.tailleWidth = taillemax;
	this.obligatoire = obligatoire;
	this.timer = null; // ?
	this.iframe = nomiframe; // l'id de l'iframe
	this.elementSelected = ""; // ?
	this.boucle = (boucle!=undefined)? boucle : false; // définit si le bandeau boucle ou non
	
	/* PLUS UTILISE EN LIGNE SUR ADDONLINE */
	this.fermer =function(){
		var iframe = Dmc_Layer.is_object(this.iframe);
		if(iframe != false){
			iframe.style.display ="none";
			var temp = this.elementSelected;
			if(temp){
			var aimage = temp.getElementsByTagName("img");
				if(aimage[0]){
					aimage[0].className=this.classe.out;
				}
			}
			var temp = null;
		}
		/*
		FONCTION POUR FERMER L'IFRAME (mettre dans iframe)
		function fermer(){
			var dmcPlugin = document.location.hash; 
			var exp_reg = /#/gi; 
			dmcPlugin = dmcPlugin.replace(exp_reg, "")
			if(dmcPlugin!="" && window.top[dmcPlugin]){
			var momo = window.top[dmcPlugin]
				momo.fermer()
			}else{
				window.close()
			}
		}
		*/
	}
	
	/* Initialisation de la position des images */
	this.init = function() {
		var alllien = this.element.getElementsByTagName("a");
		var imgBase;
		var posx = 0;
		var isboucle=false;
		var positionTop = 0 ;
		if(this.verticale) {
			this.element.style.height = this.tailleWidth + "px";
			this.element.style.width = this.obligatoire + "px";
		} else {
			this.element.style.width = this.tailleWidth + "px";
			this.element.style.height = this.obligatoire + "px";
			this.plug.style.width = this.tailleWidth + "px";
		}
		for(var i = 0 ; i < alllien.length; i++){
			if(this.boucle == true){
				alllien[i].style.display = "block";
				alllien[i].style.position = "absolute";
				if(this.verticale) {
					alllien[i].style.top = positionTop + "px";
					positionTop += alllien[i].clientHeight;
				} else {
					alllien[i].style.left = posx + "px";
					alllien[i].style.top = "0px";
				}
				posx += parseInt(alllien[i].offsetWidth);
				if(posx > this.tailleWidth) {
					isboucle=true;
				}
			}
			this.initImage(alllien[i]);
		}
		
		this.initBoutons(isboucle);
		
		if(!isboucle) {
			this.boucle = false;
		}
	}
	
	this.initBoutons = function() {
		var btn1 = this.boutons[0];
		var btn2 = this.boutons[1];
		if(!arguments[0]) {
			btn1.className += " aoplugin_visible";
			btn2.className += " aoplugin_visible";
		}
		var nom = this.nom;
		var orientations = this.getOrientations(this.orientation);
		btn1.onmouseover = function() {
			window[nom].stopScroll();
			window[nom].getScroll(orientations[0], false);
		}
		btn1.onmouseout = function() {
			window[nom].stopScroll();
			window[nom].getScroll(orientations[0]);
		}
		btn2.onmouseover = function() {
			window[nom].stopScroll();
			window[nom].getScroll(orientations[1], false);
		}
		btn2.onmouseout = function() {
			window[nom].stopScroll();
			window[nom].getScroll(orientations[1]);
		}
	}
	
	this.getOrientations = function(inOrientation) {
		var tmp = new Array(2);
		switch(inOrientation) {
			case "r" :
				tmp[0] = "l";
				tmp[1] = "r";
				break;
			case "l" :
				tmp[0] = "l";
				tmp[1] = "r";
				break;
			case "t" :
				tmp[0] = "t";
				tmp[1] = "b";
				break;
			case "b" :
				tmp[0] = "t";
				tmp[1] = "b";
				break;
		}
		return tmp
	}
	
	this.initImage = function(node){
			node.classe = this.classe;
			node.plugin = this;
			node.onclick = function(){
				
				var iframe = Dmc_Layer.is_object(this.plugin.iframe);
				if(iframe != false){
					this.plugin.stopScroll();
					iframe.src= this.plugin.setUrl(this.href)
					iframe.style.display ="block";
					var allimage = this.getElementsByTagName("img") ;
					if(this.plugin.elementSelected==""){
						this.plugin.elementSelected = this;
					}else{
						var temp = this.plugin.elementSelected;
						var aimage = temp.getElementsByTagName("img");
						if(aimage[0]){
							aimage[0].className=this.classe.out;
						}
					}
					if(allimage[0]){
						allimage[0].className=this.classe.selected;
					}
					this.plugin.elementSelected = this;
				} else {
					this.target = this.plugin.iframe;
					return true;
				}
				return false;
				
			};
			node.onmouseout = function(){
				if(this.plugin.elementSelected != this){
					var allimage = this.getElementsByTagName("img") ;
					if(allimage[0]){
						allimage[0].className=this.classe.out; 
					}
				}else{
					var allimage = this.getElementsByTagName("img") ;
					if(allimage[0]){
						allimage[0].className=this.classe.selected; 
					}
				}
			};
			node.onmouseover = function(){
				var allimage = this.getElementsByTagName("img") ;
				if(allimage[0]){
					allimage[0].className=this.classe.over;
				}
			};
	}
	this.init();
	
	this.getParametre = function(){
		var hash = document.location.toString();
		var hashTable = hash.split("\?"); 
		if(hashTable[1]){
			var parametre = hashTable[1].split("&");
			var is_thisplug = false;
			var is_thisplughref= "";
			
			for(var i=0;i<parametre.length;i++){
				var tableparametre = parametre[i].split("=");
				if(tableparametre[1]==this.nom && tableparametre[0]=="aoplugin"){
					is_thisplug = true;
				}else if(tableparametre[0]=="aoplugin"+this.numPlugin+"href"){
					is_thisplughref = tableparametre[1];
				}
			}
			if(is_thisplug==true && is_thisplughref!=""){
				var alllien = this.element.getElementsByTagName("a");
				for(var h=0;h<alllien.length;h++){
					if(((alllien[h].href).toString()).indexOf(is_thisplughref)!=-1){
						var iframe = Dmc_Layer.is_object(this.iframe);
						if(iframe != false){
							iframe.src = this.setUrl(is_thisplughref);
							iframe.style.display ="block";
						}
						var allimage = alllien[h].getElementsByTagName("img") ;
						if(allimage[0]){
							allimage[0].className=this.classe.selected;
						}
						this.elementSelected = alllien[h];
						break;
					}				
				}
			}
		}
		/*
		FONCTION POUR REDIRIGER L'IFRAME SUR LA HOME  ou AUTRE (mettre dans iframe)
		if(window.top.location==document.location){
			document.location="index.html?aoplugin=dmc_Plugin0&aoplugin0href="+document.location;
		}
		*/
	};
	
	this.setUrl = function(href) {
		var urlPage = href + "#" + this.nom;
		return urlPage;
	}
	
	this.getParametre();
	
	/* Modifi les positions des images et gère la rotation si le bandeau est en mode 'boucle' */
	this.moveElement = function(orientation, quant) {
		if(this.boucle == true) {
			/* Modification de la position de toutes les images */
			var alllien = this.element.getElementsByTagName("a");
			for(var i = 0 ; i < alllien.length; i++){
				if(orientation == "l") {
					var temp =  parseInt(alllien[i].style.left) - quant;
					alllien[i].style.left = (temp) + "px";
				} else if(orientation == "r") {
					var temp =  parseInt(alllien[i].style.left) + quant;
					alllien[i].style.left =(temp) + "px";
				} else if(orientation == "t") {
					var temp =  parseInt(alllien[i].style.top) - quant;
					alllien[i].style.top = (temp) + "px";
				} else if(orientation == "b") {
					var temp =  parseInt(alllien[i].style.top) + quant;
					alllien[i].style.top = (temp) + "px";
				}
			}
			
			/* Rotation */
			if(orientation=="r") {
				if(parseInt(alllien[0].style.left) >= -parseInt(alllien[0].offsetWidth)) {
					var clone = alllien[alllien.length-1].cloneNode(true);
					var supprimer = this.element.removeChild(alllien[alllien.length-1]);
					this.element.insertBefore(clone,alllien[0]);
					clone.style.left= (parseInt(alllien[1].style.left) - parseInt(clone.offsetWidth)) + "px";
					this.initImage(clone)
				}
			} else if(orientation=="l") {
				if(parseInt(alllien[alllien.length-1].style.left) <= this.tailleWidth ) {
					var clone = alllien[0].cloneNode(true);
					var supprimer = this.element.removeChild(alllien[0]);
					this.element.appendChild(clone);
					clone.style.left = (parseInt(alllien[alllien.length-2].style.left) + parseInt(alllien[alllien.length-2].offsetWidth)) + "px";
					this.initImage(clone)
				}
			} else if(orientation == "t") {
				if(parseInt(alllien[alllien.length-1].style.top) <= this.tailleWidth ) {
					var clone = alllien[0].cloneNode(true);
					var supprimer = this.element.removeChild(alllien[0]);
					this.element.appendChild(clone);
					clone.style.top = (parseInt(alllien[alllien.length-2].style.top) + parseInt(alllien[alllien.length-2].offsetHeight)) + "px";
					this.initImage(clone)
				}
			} else if(orientation == "b") {
				if(parseInt(alllien[alllien.length-1].style.top) >= this.tailleWidth ) {
					var clone = alllien[alllien.length-1].cloneNode(true);
					var supprimer = this.element.removeChild(alllien[alllien.length-1]);
					this.element.insertBefore(clone,alllien[0]);
					clone.style.top = (parseInt(alllien[1].style.top) - parseInt(clone.offsetHeight)) + "px";
					this.initImage(clone)
				}
			}
		} 
	}
	
	/* Fait défiler le bandeau 
	** orientation > vers le haut, bas, droite, gauche
	** auto > boolean qui définit si le défilement s'effectue à vitesse automatique ou non
	*/
	this.getScroll = function(orientation,auto) {
		//var posx = (this.element.style.left == "")? 0 : parseInt(this.element.style.left);
		//var posy = (this.element.style.top == "")? 0 : parseInt(this.element.style.top);
		var quant = this.qt;
		
		if(auto == true) {
			quant = this.autoqt;
		}
		
		if(orientation == "l") {
			this.moveElement(orientation, quant);
		} else if(orientation == "r") {
			this.moveElement(orientation, quant);
		} else if(orientation == "t") {
			this.moveElement(orientation, quant);
		} else if(orientation == "b") {
			this.moveElement(orientation, quant);
		}

		if(auto == false) {
			this.timer =  setTimeout("window['" + this.nom + "'].getScroll('"+orientation+"',false)", this.speed);
		} else {
			this.timer =  setTimeout("window['" + this.nom + "'].getScroll('"+orientation+"',true)", this.autospeed);
		}
	}
	
	/* Arrête le défilement */
	this.stopScroll = function () {
		clearTimeout(this.timer);
	}
	
	/* Débute le défilement à vitesse automatique */
	this.startAutoScroll = function () {
		this.stopScroll();
		this.getScroll(this.orientation,true);
	}
}

