// var tonyPopup = new TonyPopup();

//this.tonyPopup.loadURL( url, isModal, showTitle, area, useIFrame );


function TonyPopup(){
	
	this.bID=0;
	
	this.wipeOpacity=.9;
	
	this.editBarHeight=0;
	
	this.rules=[];
	
	this.init = function(){ 
		if(this.initiated) return; 
		//this.rootURL=$('tonyPopup_rootURL').val();
		this.editBarHeight
		this.addPopupHTML();   
		
		//this.pageWipeInit(1);
		
		if( typeof(window.popupObjs)=='undefined' ){ 
			//$(window).resize( function(){ for(var i=0; i < this.popupObjs.length ;i++) this.popupObjs[i].pageWipeInit(1);  });
			$(window).addEvent( 'resize', function(){ tonyPopup.pageWipeInit(1);  } );
		} 
		
		//window.popupObjs.push(this); 
		
		this.initiated=1;
	}
	
	this.addPopupHTML = function(){
		
		this.popupWrap= $('TonyPopup_wrap'); 
		this.pw= this.popupWrap.getElements('.TonyPopup_pageWipe'); 
		this.popupThrobber = this.popupWrap.getElements('.TonyPopup_throbber'); 
		this.pageWipeBox = this.popupWrap.getElements('.TonyPopup_box');  
		
		this.pw.tonyPopup=this;
		this.pw.addEvent( 'click', function(){ tonyPopup.modalClose(); } );
	}
	
	
	this.pageWipeInit = function( noCenter ){
		if(!this.pw || this.hidden) return;
		try{ 
		//var pwb=$('pageWipeBottom');
		//alert( $(window).getHeight() ); 
		//alert( $(window).scrollTop() );
		wsh=$(document).getScrollHeight();  

		/* 
		pwb=tonyPopup.pageWipeBox;
		if( pwb){
			pwbY = pwb.getPosition().y + pwb.getHeight(); 
			//alert( pwbY + ' ' + pwb.getPosition().y + ' ' + pwb.getHeight() );
			if( pwbY>=wsh ) this.popupWrap.setStyle('height',pwbY+10);
			else this.popupWrap.setStyle('height',wsh);
		}else */this.popupWrap.setStyle('height',wsh); 
		
		this.popupWrap.setStyle( 'width', $(document).getWidth() ); 
		if( typeof(noCenter)=='undefined' || !noCenter ) this.centerBox(); 
		}catch(e){ alert(e.message+' pageWipeInit') }
	}	
	
	this.loadURL = function ( url, isModal, width ){ 
		if(!url) return;
		
		if(!this.origWidth) 
			this.origWidth=parseInt(this.pageWipeBox.getStyle('width')+''.replace('px',''));
			
		if(width) this.pageWipeBox.setStyle('width',parseInt(width)+'px');
		else if(this.origWidth) this.pageWipeBox.setStyle('width',parseInt(this.origWidth)+'px');
		
		this.modalCloseOn=isModal; 
		this.toggleSelects(0);
		this.showThrobber();
		/* 
		if(!noScroll) popupHelper.scrollUp();
		var a=new Ajax(url,{method:'get',update:this.pageWipeBox,evalScripts:true}).request(); //,evalScripts:true
		a.callback=callback;
		a.addEvent('onComplete',function(){
			if( typeof(this.callback)=='function' ) this.callback();
			popupHelper.contentReady();	 
			$('pageWipeBox').forceEvalScripts();
		})
		*/ 
		this.pageWipeBox.setStyle('display','none');
		this.pageWipeBox.getElements('.TonyPopup_boxBody').innerHTML=''; 
		var tonyPopup=this; 
		var r = new Request.JSON({ 
			method:'get',					  
			data:'',
			url: url,
			onFailure: function(){ alert('Error: the page you are requesting could not be loaded.'); },
			onException: function(headerName, value){ alert( headerName+' '+value ); },
			onSuccess: function(response,resposeHTML){  
				/*
				eval('var jObj='+resposeXML);
				if( jObj && jObj.error ){
					tonyPopup.hide();
					alert(jObj.error);
				}else{   
					alert(jObj.html);
					tonyPopup.pageWipeBox.getElement('.TonyPopup_boxBody').innerHTML= jObj.html ; 
					if(!jObj.iframeMode){
						tonyPopup.addHeaderCSS(jObj.cssFiles);
						tonyPopup.addHeaderJS(jObj.jsFiles); 
					}
					tonyPopup.contentReady();  
				}
				*/  
				tonyPopup.pageWipeBox.getElement('.TonyPopup_boxBody').set('html',resposeHTML); 
				tonyPopup.contentReady();  
			}
		});
		r.send(); 
	} 
	
	this.addHeaderCSS = function(headerFiles){ 
		if(typeof(headerFiles)=='undefined' || !headerFiles.length) return;
		var linkTags=$('link');
		var tonyPopup=this;
		for(var i=0;i<headerFiles.length;i++){ 
			var file=headerFiles[i];  
			var linkExists=0;
			linkTags.each(function(i,el){ if(el.href==file || el.href==(tonyPopup.rootURL+file) ) linkExists=1; });
			if(linkExists) continue;
			var l=document.createElement('link');
			//l.id='book_styles_design'+parseInt(BookDesign.designId);
			l.type='text/css';
			l.rel='stylesheet';  
			$('head').append(l);
			l.href=file; 
		}
	}

	this.addHeaderJS = function(headerFiles){ 
		if(typeof(headerFiles)=='undefined' || !headerFiles.length) return;
		var scriptTags=$('script');
		var tonyPopup=this;
		for(var i=0;i<headerFiles.length;i++){
			var file=headerFiles[i];   
			var scriptExists=0;
			scriptTags.each(function(i,el){ if( typeof(el.src)!='undefined' && el.src==file || el.src==(tonyPopup.rootURL+file) ) scriptExists=1; });
			if(scriptExists) continue; 
			/*
			var s=document.createElement('script');
			s.type='text/javascript';
			//$(s).html('alert("got here")');
			$('head').append(s);
			$(s).attr('src',file); 
			alert(file);
			*/ 
			$.ajaxSetup({async: false});
			$.getScript(file);
			$.ajaxSetup({async: true});
		}
	}
	
	this.showThrobber = function(){
		this.showPageWipe(); 
		//var t = Math.floor( ( $(window).getHeight() / 2 ) - ( parseInt(this.popupThrobber.setStyle('height').replace('px',''))/2+20) ) +'px';
		var t = $(window).getScroll().y + Math.floor( ($(window).getHeight() / 2) - 50 );
		this.popupThrobber.setStyle('top', t ) ;
		this.popupThrobber.setStyle('display','block');
		this.throbberShown=1;
	}
	
	this.hideThrobber = function(){
		this.popupThrobber.setStyle('display','none');
		this.throbberShown=0;
	}
	
	this.showPageWipe = function(){
		this.hidden=0;
		this.pageWipeInit();
		//if(this.popupWrap.setStyle('display')=='block') return;
		
		//fade in main wrap
		if(this.popupWrap.getStyle('display')!='block' || this.popupWrap.getStyle('opacity')!=1){
			if(this.popupWrap.getStyle('display')!='block'){
				this.popupWrap.setStyle('opacity',0); 
				this.popupWrap.setStyle('display','block');
			} 
			this.pw.setStyle('opacity',this.wipeOpacity);
			//this.popupWrap.animate( {'opacity': 1}, 500 );
			if( this.popupWrap.fx && typeof(this.popupWrap.fx.stop)=='function') this.popupWrap.fx.stop();
			this.popupWrap.fx = new Fx.Morph( this.popupWrap, {duration:1000, transition: Fx.Transitions.Cubic.easeInOut});	
			this.popupWrap.fx.start({ 'opacity': 1 });			
		}   
		//this.popupWrap.fx.addEvent('onComplete',function(el){ popupHelper.pageWipeInit(); }); 
	}
	
	this.contentReady = function (){  
		this.hideThrobber();
		this.showBox();		
		//this.activateLinks(1);
		this.pageWipeInit(); 
		this.boxResizeWatcher();
		window.fireEvent('popupReady'); 
	}	
	
	this.lastBoxHeight=0;
	this.boxResizeWatcher = function(){ 
		if(this.hidden){
			this.lastBoxHeight=0;
			return false;
		}
		var h = this.pageWipeBox.getHeight(); 
		if( this.lastBoxHeight!=0 && this.lastBoxHeight!=h ){ 
			this.pageWipeInit(1); 
		} 
		this.lastBoxHeight=h; 
		setTimeout( 'tonyPopup.boxResizeWatcher()',1000 );
	}
	
	this.showBox=function(){ 
		this.popupWrap.setStyle('display','block'); 
		this.pageWipeBox.setStyle('display','block');
		/*
		if( this.pageWipeBox.setStyle('opacity')==1 && this.pageWipeBox.setStyle('display')=='block') return;
		if( this.pageWipeBox.fx && typeof(this.pageWipeBox.fx.stop)=='function') this.pageWipeBox.fx.stop();
		if(this.pageWipeBox.style.display!='block'){
			this.pageWipeBox.setStyle('opacity',0);
			this.pageWipeBox.style.display='block';
		}
		this.pageWipeBox.fx = new Fx.Style( this.pageWipeBox, 'opacity',{duration:1000,fps:20,transition:Fx.Transitions.Cubic.easeInOut}).start(1);
		*/
	}
	
	this.centerBox=function(){   
		var pwh=this.pageWipeBox.getHeight(), wh=$(window).getHeight(), t=30; 
		if(!pwh) return;   
		
		if(pwh<wh){
			//var pwb=$E('#pageWipeBottom',this.pageWipeBox), diff=wh-pwh; 
			//if(pwb) diff=diff+pwb.getHeight();
			//t=Math.floor(diff/2);
		} 
		t=t+$(window).getScroll().y+this.editBarHeight; 
		
		if( (this.pageWipeBox.getStyle('top')+'').replace('px','')!=t){
			this.pageWipeBox.setStyle('top',t); 
		}    
		
		//make sure that pageWipe doesn't need to be longer, if box now extends past bottom
		var top = parseInt(this.pageWipeBox.getStyle('top')+''.replace('px',''));
		var boxBottom = parseInt(top) + parseInt(this.pageWipeBox.getHeight());  
		if( boxBottom > 10 && boxBottom >= parseInt($(document).getScrollHeight()+''.replace('px','')) ){   
			this.popupWrap.setStyle('height',(boxBottom+40)+'px');
		}
	},	
	
	this.hidden=1;
	this.hide = function(){ 
		if( this.popupWrap.fx && typeof(this.popupWrap.fx.stop)=='function') this.popupWrap.fx.stop();
		this.popupWrap.fx = new Fx.Morph( this.popupWrap, {duration:1000, transition: Fx.Transitions.Cubic.easeInOut});
		this.popupWrap.fx.addEvent('onComplete',function(el){ $(el).setStyle('display','none'); $(el).getElement('.TonyPopup_boxBody').innerHTML=''; });	
		this.popupWrap.fx.start({ 'opacity': 0 }); 
		this.hidden=1;
		window.fireEvent('popupClose'); 
		this.toggleSelects(1);
	}
	
	//for IE z-index bug
	this.toggleSelects = function(display){
	  for (j=0; j<document.forms.length; j++)
		  for (i=0; i<document.forms[j].elements.length; i++) {
			 n=document.forms[j].elements[i];
			 if (n.tagName == "SELECT"){
				if(display==1) n.style.display='inline';
				else n.style.display='none';
			 }
		  }
	}	
	
	this.modalCloseOn=0;
	this.modalClose=function(){ if(this.modalCloseOn && !this.throbberShown) this.hide();  }
}


function wipeBoxSubmit(f,submitBtn,callback){
	
	try{ 
		
		postStr=formSubmitStr(f);
		if(submitBtn!=null) submitOnce(submitBtn);
		if(typeof(callback)!='function') 
			callback=function(){wipePopupLoad.fire()}
		//ahah(URL,'pageWipeBox',postStr);
		//ajaxQue(form.action,'pageWipeBox',postStr,callback);  
		new Request({ url: f.action, async:false, method:'post', onComplete:function(responseText){ 
				$('TonyPopup_wrap').getElement('.TonyPopup_boxBody').innerHTML=	responseText; 							  
			 }
		}).send(postStr);  
		
		scroll(0, parseInt(tonyPopup.pageWipeBox.getStyle('top')+"".replace('px',''))+20);
	}catch(e){
		alert(e.message)	
	}
	return false;
}





