myWindow = Class.create();
myWindow.prototype = {
		
    initialize: function(id,options) {
	
		this.options =  {
						classname: 'profilegroup',
						overlay:true,
						overlayColor:'#FFCCFF',
						windowContent: '',
						windowType: 'alert',
						windowHeight:100,
						windowWidth:'250',
						center  : true,
						offsetX : 0,
						offsetY : 0,
						btnOk : 'ok',
						btnCancel : 'cancel',
						btnOkCallback: null,
						btnCancelCallback: null,
						btnCustom: 'continue',
						btnCustomCallback: null,
						appearEffect:null,
						appearOptions:null,
						disappearEffect:null,
						disappearOptions:null,
						uploadIdentifierId:'UPLOAD_IDENTIFIER',
						language: 'en',
						uploadControllerUrl: '/admin/php/uploadProgress.php?progress_key='
						};
							
		Object.extend(this.options, options || {});
		
		// ecoute si scroll bar a bouger afin de recentrer la fenetre
		this.eventResize = this._recenter.bindAsEventListener(this);
		Event.observe(window, "scroll", this.eventResize);
		

		this.iefix = false;
		this.mywindow = id;
		this.progressBar = null;
		this.uploadController = null;
		this.destroyWindow();
		this.getBrowserSize();
		this.uploadController = null;
		this.show();	
	},
	
	show: function(){
		this.showOverlay();	
		this.showWindow();
	},
	
	/*=======================================================*/
	/* construction de l'overlay
	/*=======================================================*/
	showOverlay: function(){
		
		height = this.height + this.top
		this.overlayDefaultStyle = { width: '100%',
									 height: (height+'px'),
									 position: 'absolute',
									 top: '0px',
									 left: '0px',
									 zIndex: '500',
									 filter: 'alpha(opacity=0)',
									 opacity: '0.6',
									 display: 'none'
								   };
		//if(this.options.overlayColor)
			//this.overlayDefaultStyle.backgroundColor = this.options.overlayColor;
			
			
		//console.debug(this.overlayDefaultStyle);
								   
								   
		new Insertion.Top(document.body, '<div id="myWindow_overlay_'+ this.mywindow +'"  style="display:none"></div>');
		
		$('myWindow_overlay_'+ this.mywindow).setStyle(this.overlayDefaultStyle);
		
		if(this.options.appearEffect){
			Effect.Appear('myWindow_overlay_'+ this.mywindow +'', { duration: 1.0, from: 0.0, to: 0.6  });
		}
		else{
			$('myWindow_overlay_'+ this.mywindow +'').setStyle({'display':'block'});
		}
		
	},
	
	/*=======================================================*/
	/* construction de la fenetre
	/*=======================================================*/
	showWindow: function(){
		
		this.windowDefaultStyle =  { width: (this.options.windowWidth+'px'),
									 position :'absolute',
									 top:'0px',
									 left:'0px',
									 zIndex:'510',
									 display:'none'
								   };
								   
								   
		
		var swindow = '\
					<div id="'+ this.mywindow +'_window" >\
						<table width="100%" border="0" cellspacing="0" cellpadding="0">\
						  <tr >\
							<td class="'+ this.options.classname +'_TL">&nbsp;</td>\
							<td class="'+ this.options.classname +'_T" >&nbsp;</td>\
							<td class="'+ this.options.classname +'_TR">&nbsp;</td>\
						  </tr>\
						  <tr>\
							<td class="'+ this.options.classname +'_L">&nbsp;</td>\
							<td class="'+ this.options.classname +'_content" id="'+ this.mywindow +'_content" style="height:'+ this.options.windowHeight +'px;" >'+ this.options.windowContent +'</td>\
							<td class="'+ this.options.classname +'_R">&nbsp;</td>\
						  </tr>\
						  <tr id="'+ this.mywindow +'_row_btn" style="height:30px;" >\
							<td class="'+ this.options.classname +'_L" id="'+ this.options.classname +'_L">&nbsp;</td>\
							<td class="'+ this.options.classname +'_content" align="center" id="'+ this.mywindow +'_dialog_btn" ></td>\
							<td class="'+ this.options.classname +'_R" id="'+ this.options.classname +'_R">&nbsp;</td>\
						  </tr>\
						  <tr>\
							<td class="'+ this.options.classname +'_BL">&nbsp;</td>\
							<td class="'+ this.options.classname +'_B">&nbsp;</td>\
							<td class="'+ this.options.classname +'_BR">&nbsp;</td>\
						  </tr>\
						</table>\
					</div>';
					
		new Insertion.Top(document.body,swindow);
		$(this.mywindow +'_window').setStyle(this.windowDefaultStyle);
		
		// center window
		if(this.options.center)
			this.centerwindow();
		else
			this.setWindowPosition();
			
		switch(this.options.windowType){
			case 'alert'  :
					this.setAlertButton();	
					break;
			case 'dialog' :
					this.setDialogButton();	
					break;
			case 'window' :
					this.setWindowButton();	
					break;
			case 'window_message' :
					this.destroyButton();
					//this.setWindowButton();	
					break;			
			case 'upload'  :
					this.setUploadBar();
					this.setUploalController();
					//this.setAlertButton();			
					break;
		}
		//iefix
		this.element = $(this.mywindow+'_window');
		this._checkIEOverlapping();
		Effect.Appear(this.mywindow +'_window', { duration: 1.0});
	},
	
	/*=======================================================*/
	/* cache les fenetre
	/*=======================================================*/
	hideWindow: function(){
		Effect.Fade('myWindow_overlay_'+ this.mywindow +'',{ duration: 1.0});
		Effect.Fade(this.mywindow +'_window',{ duration: 1.0});
		//iefix
		if(this.iefix){ 
			_iefix = this.hideIeFix.bind(this);
      		setTimeout(_iefix, 1000);
		}
	},
	
	hideIeFix: function(){
		this.iefix.hide();	
	},
	
	/*=======================================================*/
	/* ajoute les bouton de type alert
	/*=======================================================*/
	setAlertButton: function(){

		var btn = "";
		btn = '<input class="'+ this.options.classname +'_button" id="'+ this.mywindow +'_window_alertbtn"  type="button" onclick="'+ this.options.btnOkCallback +';"  value="'+ this.options.btnOk +'" />'	
		//new Insertion.Top($(this.mywindow +'_dialog_btn'),btn);	
		$(this.mywindow +'_dialog_btn').update(btn);
		Event.observe($(this.mywindow +'_window_alertbtn'), 'click', this.hideWindow.bindAsEventListener(this));
		$(this.mywindow +'_row_btn').setStyle({'display':''});
		this.resizeContentWindowHeight();
		//new Draggable($(this.mywindow +'_window'),{revert:true});
		
	},
	
	/*=======================================================*/
	/* ajoute les bouton de type dialog
	/*=======================================================*/
	setDialogButton: function(){
		var btn = "";
		btn = btn + '<input class="'+ this.options.classname +'_button" id="'+ this.mywindow +'_window_dialogok"  type="button" value="'+ this.options.btnOk +'" />'
		btn = btn + '<input class="'+ this.options.classname +'_button" id="'+ this.mywindow +'_window_dialogcancel"   type="button"  value="'+ this.options.btnCancel +'" />'
		new Insertion.Top($(this.mywindow +'_dialog_btn'),btn);	
		Event.observe($(this.mywindow +'_window_dialogok'), 'click', this.options.btnOkCallback.bindAsEventListener(this));
		Event.observe($(this.mywindow +'_window_dialogcancel'), 'click', this.hideWindow.bindAsEventListener(this));
		$(this.mywindow +'_row_btn').setStyle({'display':''});
		this.resizeContentWindowHeight();
		//new Draggable($(this.mywindow +'_window'),{revert:true});	
	},
	/*=======================================================*/
	/* ajoute les bouton de type window
	/*=======================================================*/
	setWindowButton: function(){
		
		var btn = "";
		
		if(this.options.btnOk != false || this.options.btnCancel != false){
			
			if(this.options.btnOk != false)
				btn = btn + '<input class="'+ this.options.classname +'_button"  type="button" onclick="'+ this.options.btnOkCallback +';" value="'+ this.options.btnOk +'" />'
			
			if(this.options.btnCancel != false)
				btn = btn + '<input class="'+ this.options.classname +'_button"    type="button" onclick="'+ this.options.btnCancelCallback +';" value="'+ this.options.btnCancel +'" />'
			
			
			new Insertion.Top($(this.mywindow +'_dialog_btn'),btn);	
			this.resizeContentWindowHeight();
		}
		
	},
	
	/*=======================================================*/
	/* ajoute les bouton de type customs
	/*=======================================================*/
	setCustomsButton: function(){

		var btn = "";
		btn = '<input class="'+ this.options.classname +'_button" id="'+ this.mywindow +'_window_custom_btn"  type="button"  value="'+ this.options.btnCustom +'" />'	
		new Insertion.Top($(this.mywindow +'_dialog_btn'),btn);	
		Event.observe($(this.mywindow +'_window_custom_btn'), 'click', this.options.btnCustomCallback.bindAsEventListener(this));
		$(this.mywindow +'_row_btn').setStyle({'display':''});
		this.resizeContentWindowHeight();		
	},
	
	/*========================================================*/
	/* ajoute une bar de progression (upload) 
	/*=======================================================*/
	setUploadBar: function(){
		
		
		switch(this.options.language){
			case 'fr':
				uploadingFileStr = 'Upload du fichier';
				uploadingConnStr = 'Connexion au serveur';
				uploadingWaitStr = 'Veuillez patienter...';
				break;
			case 'nl':
				uploadingFileStr = 'Uploading file';
				uploadingConnStr = 'Connecting to the remote server';
				uploadingWaitStr = 'Please wait...'
				break;
			case 'en':
				uploadingFileStr = 'Uploading file';
				uploadingConnStr = 'Connecting to the remote server';
				uploadingWaitStr = 'Please wait...'
				break;	
		}
		
		var htm = '\
					<div id="progressbar_filename" style="margin-top:5px;margin-bottom:5px;text-align:left;""><strong>'+ uploadingFileStr +'</strong> </div>\
    				<div id="test"></div>\
        			<div id="progressbar_status" style="margin-top:5px;text-align:left;">'+ uploadingConnStr +'<br>'+ uploadingWaitStr +'</div>';
		
		$(this.mywindow +'_content').update(htm);
		$(this.mywindow +'_row_btn').hide();
		this.progressBar = new myProgressBar('test');
	},	
	
	/*========================================================*/
	/* ajoute le controler de l'upload
	/*========================================================*/
	setUploalController: function(){
		this.uploadController = new myUpload(this.options.uploadIdentifierId,'myupload_iframe_controller',{idfilename: 'progressbar_filename',idstatus: 'progressbar_status',progressBar: this.progressBar, url: this.options.uploadControllerUrl }); 	
	},
	
	/*=======================================================*/
	/* cnntre la fenetre
	/*=======================================================*/
	centerwindow: function(){
		windowWidth  = $(this.mywindow +'_window').getWidth();
		windowHeight = $(this.mywindow +'_window').getHeight();
		var left = (this.width - windowWidth)/2
		var top  = ((this.height - windowHeight)/2)+this.top;
		$(this.mywindow +'_window').setStyle({'left': left+'px','top': top +'px'});	
	},
	
	/*=======================================================*/
	/* repositionnement de la fenetre
	/*=======================================================*/
	setWindowPosition: function(){
		$(this.mywindow +'_window').setStyle({'left': this.options.offsetY+'px','top': this.options.offsetX +'px'});
	},
	
	
	/*=======================================================*/
	/* Fonction des destructions des boutons 
	/*=======================================================*/	
	destroyButton: function(){
		$(this.mywindow +'_row_btn').setStyle({'display':'none'});
		$(this.mywindow +'_dialog_btn').update('');
		$(this.mywindow +'_content').style.height = this.options.windowHeight +'px';
	},
	/*=======================================================*/
	/* fonction de desctruction de la fenetre
	/*=======================================================*/
	destroyWindow: function(){
		if($(this.mywindow +'_window') != null){
			$('myWindow_overlay_'+ this.mywindow +'').remove();
			$(this.mywindow +'_window').remove();
			//iefix
			if(this.iefix) 
     		 	Element.remove(this.iefix);
		}
	},
	
	/*=======================================================*/
	/* window set content remplace le contenu de la fenetre
	/*=======================================================*/
	setWindowContent: function(content){
		$(this.mywindow +'_content').update(content)
	},
	
	
	/*=======================================================*/
	/* resize le contenu lorsque ajout de boutons
	/*=======================================================*/	
	resizeContentWindowHeight: function(){
		heigth = parseInt(this.options.windowHeight) - 30;
		$(this.mywindow +'_content').style.height = heigth +'px';
	},
	
	
	/*=======================================================*/
	/* fix bug ie
	/*=======================================================*/	
	
	_checkIEOverlapping: function() {
		if(!this.iefix && (navigator.appVersion.indexOf('MSIE')>0) && (navigator.userAgent.indexOf('Opera')<0) && (this.element.getStyle('position')=='absolute')) {
			new Insertion.After(this.mywindow+'_window', '<iframe id="' + this.mywindow + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
			this.iefix = $(this.mywindow+'_iefix');
		}
	
		if(this.iefix) 
			setTimeout(this._fixIEOverlapping.bind(this), 50);
  	},

  	_fixIEOverlapping: function() {

		//o = this.element.cumulativeOffset();
		//d = this.element.getDimensions();
		
		this.getBrowserSize();
		
		iefixeStyle = { width:  this.width +'px',
						height: this.height +'px',
						position: 'absolute',
						zIndex: this.element.style.zIndex - 1,
						filter: 'alpha(opacity=0)',
						opacity: '0.0'
					};
		
		this.iefix.setStyle(iefixeStyle);
      	this.iefix.show();
  	},
	
	
	/*=======================================================*/
	/* rcupere la taille du browser et des scroll
	/*=======================================================*/
	getBrowserSize: function() {
		var scrOfX = 0, scrOfY = 0;//, myWidth = 0, myHeight = 0;
		// on rcupre le scroll
		if (typeof(window.pageYOffset) == 'number' ) {
		   //Netscape compliant
		   scrOfY = window.pageYOffset;
		   scrOfX = window.pageXOffset;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		   //DOM compliant
		   scrOfY = document.body.scrollTop;
		   scrOfX = document.body.scrollLeft;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		   //IE6 standards compliant mode
		   scrOfY = document.documentElement.scrollTop;
		   scrOfX = document.documentElement.scrollLeft;
		}
		this.top= scrOfY;
		this.left= scrOfX;
		// on rcupre la taille de la fentre
		 if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		  }
		  
		this.top= scrOfY;
		this.left= scrOfX;
		this.width= myWidth;
		this.height= myHeight;
	},
	
	/*=======================================================*/
	/* recentre la fenetre et l'overlay 
	/*=======================================================*/
	_recenter: function(){
		this.getBrowserSize();
		this.centerwindow();
		if(this.options.overlay){
			height = this.height + this.top
			$('myWindow_overlay_'+ this.mywindow +'').setStyle({'height':height+'px'});
		}	
	}
	
	
}