// JavaScript Document
if(typeof(SuperClass)=='function'){OpenLink.prototype = new SuperClass; /* héritage des méthodes */}

function OpenLink(classCSS){
	if(typeof(SuperClass)!='function'){alert("fichier SuperClass.js manquant!");}
	else if(typeof(JsPop)!='function'){alert("fichier JsPop.js manquant!");}
	else{
		this.jsPop=new JsPop();

		/* héritage */
		this.classMere = SuperClass;  // classe parente
		this.classMere(); // appel du super constructeur
		delete this.classMere; // inutile de garder la classe parente
		
		this.classCSS=classCSS;
		this.nodeTab=new Array();		
		
		this.prct=95;
		this.loading;
		this.loadingId="loading__pop__";
		this.setTextLoading("Chargement...");
		this.imgExtension = new Array('jpg', 'jpeg', 'jpe', 'gif', 'png'); /// liste des extensions images
		this.pageExtension = new Array('html', 'htm', 'php', 'php3', 'asp', 'jsp'); /// liste des extensions pages
		
		
	}
}


OpenLink.prototype.onLoad = function(){
var localThis=this;
	this.addEvent(window, "load",
		function(){
			localThis.init();
		}				  
	); 
}

OpenLink.prototype.init = function(){
this.nodeTab = this.getElementsByClassName(this.classCSS);
this.initEvent();
}

OpenLink.prototype.initEvent = function(){
var nb = this.nodeTab.length;
var elem;
var localThis=this;
	for(var i=0; i<nb; i++){
		elem=localThis.nodeTab[i];
		
		var oldclick=elem.onclick;
		elem.onclick=function(){
			if(oldclick) oldclick();
			var href=this.href;
			localThis.open(href);	
			return false;
		}		
	}	
}

OpenLink.prototype.setTextLoading = function(text){
this.loading=document.createElement("span");
var text=document.createTextNode(text);
this.loading.id=this.loadingId;
this.loading.appendChild(text);
}

OpenLink.prototype.setImgLoading = function(url){
this.loading=document.createElement("img");
this.loading.id=this.loadingId;
this.loading.src=url;
}

OpenLink.prototype.setTextFermer = function(text){
this.jsPop.setTextFermer(text);
}

OpenLink.prototype.setImgFermer = function(url){
this.jsPop.setImgFermer(url);
}

OpenLink.prototype.getExtension = function(chaine){
	var tempTab=new Array();
	tempTab=this.explode("#", chaine);
	var extension=tempTab[0];
	
	tempTab=this.explode("?", extension);
	extension=tempTab[0];
	
	tempTab=this.explode(".", extension);
	extension=tempTab[Math.max(0,tempTab.length-1)].toLowerCase();
	
	return extension;
}

OpenLink.prototype.open = function(chaine){	
	var localThis=this;
	this.addEvent(window, "resize", this.tempResizeContent=function(){localThis.resizeContent();});
	this.jsPop.closeFunc=function(){localThis.removeEvent(window, "resize", localThis.tempResizeContent);}

	
	var extension=this.getExtension(chaine);
	this.jsPop.openPop("auto","auto",this.loading.cloneNode(true)); //ouvre pop avec marqué chargement
	
	
	if(this.in_array(extension,this.imgExtension)==true){	
		var newImg=document.createElement("img");
		newImg.src=chaine;
		this.verifChargementImg(newImg, function(){localThis.jsPop.addBoutonFermer();localThis.openImg(newImg);});
	}	
	
	if(this.in_array(extension,this.pageExtension)==true){	
		var frame=document.createElement("iframe");		
		frame.src=chaine;
		frame.style.width=this.getViewSize()[0]+"px";
		frame.style.height=this.getViewSize()[1]+"px";
		frame.style.position="absolute";
		frame.style.visibility="hidden";
		
		frame.frameBorder=0;
		frame.style.top=0;
		frame.style.left=0;
		document.body.appendChild(frame);
		this.checkFrame(frame, 
			function(){
				var tempDiv=document.createElement("div");
				tempDiv.innerHTML=localThis.getFrameDocument(frame).body.innerHTML;
				tempDiv.style.visibility="hidden";
				tempDiv.style.position="absolute";
				document.body.removeChild(frame);
				document.body.appendChild(tempDiv);
				var w=tempDiv.offsetWidth+20;
				var h=tempDiv.offsetHeight+20;
				document.body.removeChild(tempDiv);
				
				localThis.jsPop.addBoutonFermer();
				localThis.jsPop.openFramePop(w, h, chaine);
			}
		);
	}
}

OpenLink.prototype.openImg = function(newImg){	
	if(newImg.realWidth==undefined){
		newImg.realWidth=newImg.width;
		newImg.realHeight=newImg.height;
	}
	var imgWidth=newImg.realWidth;
	var imgHeight=newImg.realHeight;	
	
	var ratio=imgWidth/imgHeight;
	if(ratio>1){newImg.style.height='100%';}
	else{newImg.style.width='100%';}
	
	this.jsPop.contenuDiv.style.top='-3000px';
	this.jsPop.contenuDiv.style.left='-3000px';
	this.jsPop.setContent(newImg);
	
	if(this.jsPop.contenuDiv.realWidth==undefined){
		this.jsPop.contenuDiv.realWidth=this.jsPop.contenuDiv.offsetWidth;
		this.jsPop.contenuDiv.realHeight=this.jsPop.contenuDiv.offsetHeight;
	}

	
	this.resizeContent();
}

OpenLink.prototype.resizeContent = function(){
	var w,h;
	w=this.jsPop.contenuDiv.realWidth;
	h=this.jsPop.contenuDiv.realHeight;
	
	var tailleAffich=this.getViewSize();
	//alert(w+'>='+tailleAffich[0]+' || '+h+'>='+tailleAffich[1]);
	if(w>=tailleAffich[0] || h>=tailleAffich[1]){	
		var coeff=Math.max(w/(tailleAffich[0]*this.prct/100), h/(tailleAffich[1]*this.prct/100));
		w=Math.round(w/coeff);
		h=Math.round(h/coeff);	
		this.jsPop.setSizeContenu(w,h);
	}	
	else{this.jsPop.setSizeContenu(w,h);}
	this.jsPop.setPositions();
	//alert(this.jsPop.contenuDiv.getElementsByTagName("img")[0].realHeight+' '+this.jsPop.contenuDiv.getElementsByTagName("img")[0].height);
}
