var globalz=1;
var REPLACE_ICON=true;
var ie4 = (document.all)? true:false;

//function scrollChanged(){
//    if(WindowSystem.getCurrentWindow()){
//        if(WindowSystem.getCurrentWindow().isOpenedModale()){
//            var mask=$(WindowSystem.getCurrentWindow().id+'mask');
//            if(mask){
//
//                if($(WindowSystem.getCurrentWindow().id)){
//                    yWithScroll= $(WindowSystem.getCurrentWindow().id).getHeight();
//                    console.log('Editor  '+yWithScroll);
//                }
//                if(yWithScroll){
//                    mask.style.height=yWithScroll+"px";
//                }
//            }else{
//                console.log('le mask na pas été trouvé');
//            }
//        }else{
//            console.log('la modale getCurrentWindow '+WindowSystem.getCurrentWindow().id+' nest pas ouverte');
//        }
//    }else{
//        console.log('getCurrentWindow nexiste pas');
//    }
//}

function CWindow(id_, el_event_,title_){

	if(!document.getElementById(id_)){
		
		globalz++;					 
		this.titleWin=title_;
		//Array [ id_div=>lien], liste des divs contenu ds la fenetre qui peuvent charger du contenu
		this.dyndiv=new Array();		
		//identifiant de la fenetre
		this.id=id_;
		//largeur de la fenetre					
		this.width=percentX(100);
		//hauteur de la fenetre		
		this.height=(document.body.clientHeight-50);
		//id element qui a apell?	l'event d'ouverture de la fenetre
		this.idEvent=el_event_;
		//la fenetre est ouverte (visible ou masqu?)
		this.opened=false;
		//le div contener principal qui forme la window
		this.divContener = document.createElement("div");	
		//le div contenant
		this.divContent=null;		
		this.modaleURL=new Array();
		this.lkdiv = new Array();
		
		this.openedModale=false;
		this.shortcuts=new Array();		

		 //gen div contener
		 this._genWindowDiv();			
	}
}

CWindow.prototype.getPopupDiv=function(){	
	return document.getElementById(this.id+'editor_content');
}

CWindow.prototype._genWindowDiv=function(){		
		
	this.divContener.setAttribute("id", this.id);
	this.divContener.className ="WinContener";
	this.divContener.style.zIndex=globalz;		
	this.divContener.style.display='none';	
	
	//le contenu du div (masque, popup, etc)
	var htmlPop='';
	var id_=this.id;
	
	var closeHref="#";
	if(ie4) closeHref='javascript:WindowSystem.getWindow(\''+id_+'\').closeModal();';
	
	htmlPop+='<div class="WindowTop"><a href="javascript:;" onClick="WindowSystem.getCurrentWindow().close();" class="WinClose" style="position:static;float:right;"></a><span>&raquo; '+this.titleWin+'</span></div>'+
					'<div id="'+id_+'mask" class="WinMask" style="display:none"></div>'+
						'<div class="WinEditor"  id="'+id_+'editor" style="display:none;">' +
							'<div  class="blockContenerContent">'+
								'<div class="BlockBorderTop" style="height:20px;">'+
									'<a href="'+closeHref+'" class="WinClose" onClick="WindowSystem.getWindow(\''+id_+'\').closeModal();"></a>'+
									'<div id="'+id_+'WinBar"></div>' +
								'</div>'+
								'<div id="'+id_+'editor_content" class="WinEditorContent" style="padding:0;"></div>' +
						'</div></div>'+
						'<div id="'+id_+'_content" class="WinContent"></div>' +
						'<br class="clear"/>';
	
	this.divContener.innerHTML=htmlPop;	

	// ajout des nouveaux div au body de la page	
	var bodyHtml=document.getElementById('contenercws');
	bodyHtml.appendChild(this.divContener);		
	this.divContent = document.getElementById(id_+"_content");	
}

CWindow.prototype.loadContent=function(div_,html_){
	inner(html_,div_);
}

CWindow.prototype.loadLink=function(div_, link_, _reload, el_to_mask){
	
	if(_reload || (this.dyndiv[div_]!= link_)){
		this.dyndiv[div_] = link_;
		loadContent(link_,div_);
                if(el_to_mask!=null){
			this.lkdiv[div_] = el_to_mask;
			el_to_mask.style.display='none';
		}
	}
}

CWindow.prototype.reset=function(div_){
	inner('', div_);
	this.dyndiv[div_]=null;
	if(this.lkdiv[div_]!=null){
		this.lkdiv[div_].style.display='';
		this.lkdiv[div_] = null;
	}
}

CWindow.prototype.loadGeneralLink=function(link_,_reload){
	this.loadLink(this.divContent.id,link_);
}

CWindow.prototype.reload=function(div_,param_){
	if(this.dyndiv[div_]!=null){
		link_=this.dyndiv[div_];
		if(param_)
		this.loadLink(div_,link_+param_,true);
		else
		this.loadLink(div_,link_,true);		
	}
}

CWindow.prototype.reloadModal=function(){
	this.reload(this.id+"editor_content");
}

CWindow.prototype.show=function(){	
	this.opened=true;
	WindowSystem.isOpened(this);
	
	if(this.id=='cw_chat'){
		this.divContener.style.height=$("modBox").getDimensions()["height"]-80+"px";
		this.divContener.style.marginLeft='0';
		this.divContener.style.marginTop='0';
				
		if(this.divContener.style.display!='') this.divContener.style.display='';
		
		this.divContener.style.visibility='visible';		
		
		if(this.divContener.style.position!="absolute")
			this.divContener.style.position="absolute";
			
	}else{
		this.divContener.style.display='';
	}
	globalz++;this.divContener.style.zIndex=globalz;
}

CWindow.prototype.showExplode=function(from_){
	this.idEvent=from_;

	this.idEvent.className="menuButtonSel";
	if(REPLACE_ICON){
		var img=this.idEvent.getElementsByTagName('img')[0];
		var imgSrc=img.src;
		var imgSel=imgSrc.replace("iconbig.gif","iconbigSel.gif");	
		img.src=imgSel;
	}
	
	this.opened=true;
	WindowSystem.isOpened(this);
	
	if(this.id=='cw_chat'){
		this.divContener.style.marginLeft='0';		
		this.divContener.style.marginTop='0';			
		this.divContener.style.visibility='visible';
		this.divContener.style.right="0";
		this.divContener.style.left="0";	
		
		if(this.divContener.style.display!='') this.divContener.style.display='';		
		
		this.divContener.style.height=$("modBox").getDimensions()["height"]-80+"px";
			
		if(this.divContener.style.position!="absolute"){
			this.divContener.style.position="absolute";
			this.divContener.style.right="0";
			this.divContener.style.left="0";			
		}
	}else{
		this.divContener.style.display='';
	}
	globalz++;this.divContener.style.zIndex=globalz;
}

CWindow.prototype.hide=function(){
	this.opened=false;

	if(this.idEvent!=null && this.idEvent!=''){			
	
		this.idEvent.className="menuButtonHiding ";
		if(REPLACE_ICON){
			var img=this.idEvent.getElementsByTagName('img')[0];
			var imgSrc=img.src;	
			var imgSel=imgSrc.replace("iconbigSel.gif","iconbig.gif");
			img.src=imgSel;
		}		
	}
	
	if(this.id=='cw_chat'){		
		this.divContener.style.visibility='hidden';
		this.divContener.style.height='1px';
		
	}else{
		this.divContener.style.display='none';
	}
}

CWindow.prototype.close=function(e){
	
	try{
		this.closeModal();
		this.divContener.style.display='none';
		this.idEvent.className="menuButton "+this.idEvent.classname2;
		this.loadContent(this.divContent.id, '');
		this.dyndiv[this.divContent.id]='';
		
		if(REPLACE_ICON){
			var img=this.idEvent.getElementsByTagName('img')[0];
			var imgSrc=img.src;
			var imgSel=imgSrc.replace("iconbigSel.gif","iconbig.gif");		
			img.src=imgSel;
		}
		this.idEvent=null;
	}catch(e){}
}

CWindow.prototype.openModal=function(link_,title_,w_, h_,img_,save_){

	if(!this.opened){
		openCWindowMod(this.id.replace("cw_",""));
	}
	
	this.openedModale=true;
	
	if(!w_) w_="80%";
	if(!save_) save_= 0;	
	
	if(save_==1){
		if(in_array(link_,this.modaleURL))
		this.modaleURL=array_remove(link_,this.modaleURL);
		this.modaleURL.push(link_);
	}
	document.getElementById(this.id+"editor").style.right="20px";
	document.getElementById(this.id+"editor").style.left="20px";
	document.getElementById(this.id+"editor").style.top="40px";
	this.loadLink(this.id+"editor_content",link_,true);
	
	var winW=0;
	if (ie4) winW=document.body.clientWidth;
	else 	winW=window.innerWidth;
	
	document.getElementById(this.id+"mask").style.display='';	
	document.getElementById(this.id+"editor").style.display='';	
	
	var winH=windowH()-posY(document.getElementById(this.id+"mask"));

	if(heightY(this.divContener) > winH ) winH=heightY(this.divContener);
	document.getElementById(this.id+"mask").style.height=winH+"px";
	
	
	var elSelect=this.divContener.getElementsByTagName("APPLET");
	for(var i=0; i<elSelect.length; i++){
		elSelect[i].style.visibility="hidden";
	}
	
	if (ie4) {
            document.getElementById(this.id+'_content').style.display="none";
//		
	}

        //rendre draggable une modale
        /*new Draggable(
                        $(this.id+"editor") ,
                        { handle: $(this.id+"editor").firstChild.firstChild}
                    );*/
         
            
}

CWindow.prototype.showModal=function(content_,title_,w_, h_,img_){
	this.closeModal();
	document.getElementById(this.id+"editor_content").innerHTML=content_;

	var contentTitre="";
	if(title_) contentTitre=title_;
	if(img_) contentTitre="<img src=\""+img_+"\" align=\"middle\" style=\"float:left;margin:1px;\">"+contentTitre;

	document.getElementById(this.id+"WinBar").innerHTML=contentTitre;

	var windowW=0;
	if (ie4) windowW=document.body.clientWidth;
	else 	windowW=window.innerWidth;

	document.getElementById(this.id+"editor").style.left=((windowW-w_)/2-22)+"px";
	document.getElementById(this.id+"editor").style.right=((windowW-w_)/2-22)+"px";

	document.getElementById(this.id+"mask").style.display='';
	document.getElementById(this.id+"editor").style.display='';

}

CWindow.prototype.openModalPercent=function(link_,title_,w_, h_){
	this.openModal(link_,title_,percentX(w_),percentY( h_));
}

CWindow.prototype.closeModal=function(){

	this.openedModale=false;
	
	//si popup deja ouverte:
	if(this.modaleURL.length>1){		
		//this.modaleURL.reverse();
		this.modaleURL.pop();
		var lastURL=this.modaleURL[this.modaleURL.length-1];
		this.openModal(lastURL,null,null,null,null,2);
	}else{
		document.getElementById(this.id+'mask').style.display='none';
		document.getElementById(this.id+'editor').style.display='none';
		this.modaleURL=new Array();
		inner(this.id+"editor_content",'');
	}

	if (ie4) {
		document.getElementById(this.id+'_content').style.display="block";
	}
	
	var elSelect=this.divContener.getElementsByTagName("APPLET");
	for(var i=0; i<elSelect.length; i++){
		elSelect[i].style.visibility="visible";
	}	
}

CWindow.prototype.isOpenedModale=function(){
	return this.openedModale;
}

CWindow.prototype.setShortCut=function(shortcut, callback){
	this.shortcuts[shortcut]=callback;
}

CWindow.prototype.getShortCut=function(shortcut){
	return this.shortcuts[shortcut];
}

