/***********************************************************
* - |ctkPopFrame| IFramed Popup - Boxed Content!
 [ THIS IS A COPYRIGHTED WORKS - MIT STYLE LICENCE ]
 
 TOCRA.org
 (C) Copyright, Sbastien Brmond, 2003 | www.tocra.org
 
 Experimental home work for WEB-MEDIAS.
 WEB-MEDIAS.com
 (C) Copyright, Sbastien Brmond, 2007 | www.web-medias.com
***********************************************************/

/***********************************************************
* Use/Need: QuirksMode ppkPageSize.js
***********************************************************/

	var ctkPopFrame = {
		
		version:1.4,
		
		oFrmPop:null,
		oFrameWindow:null,
		oIterTimer:null,
		oOverlay:null,
		title:'',
		url:'',
		html:'',
		contenttype:'url',
		callback:null,
		
		Config:{
			headbarHeight: 20,
			headbarBGColor: '#E0E0E0',
			headbarBGUrl: 'http://www.tocra.org/img/skin-samples/degrad-headbar.gif',
			headbarCloseBtnUrl: 'http://www.tocra.org/img/skin-samples/btn-close.gif',
			popFrmBorderColor: '#98C0F4',
			popFrmBorderSize: 1,
			popFrmBGColor: '#FFFFFF',
			titleFontFamily: 'Verdana, Arial, Helvetica, sans-serif',
			titleColor: '#475A72',
			titleFontSize: '10px',
			titleFontWeight: 'bold',
			titleFontStyle: 'normal',
			allowNativeClose: true,
			persistentClosable:false,
			useHead:true
		},
		
		
		configure: function(Options){
			for(opt in Options){
				this.Config[opt] = Options[opt];
			}
		},
		
		
		setoverlay: function(Element,strBGColor){
			oCTKPopFrameOverlay = (typeof Element == 'object')? Element : this.getObj(Element);
			if(oCTKPopFrameOverlay == null || oCTKPopFrameOverlay == 'undefined')return;
			
			if(!oCTKPopFrameOverlay.style) oCTKPopFrameOverlay.style = oCTKPopFrameOverlay;

			oCTKPopFrameOverlay.style.backgroundColor = strBGColor;
		},
		
		
		open: function(Element,Params){
			oCTKPopFrame = (typeof Element == 'object')? Element : $(Element);
			if(oCTKPopFrame == null || oCTKPopFrame == 'undefined')return;
			
			if(!oCTKPopFrame.style) oCTKPopFrame.style = oCTKPopFrame;
			this.oOverlay = oCTKPopFrame.id+'Overlay';
			
			this.title = Params.title;
			if( Params.url && Params.url!='' ){
				this.url = Params.url;
				this.html= null;
				this.contenttype = 'url';
			}else if( Params.content && Params.content!='' ){
				this.url = null;
				this.html= Params.content;
				this.contenttype = 'html';
			}else{
				this.url = null;
				this.html= "<strong>Erreur lors de la dfinission du media !</strong><br /><br />\n<em>Aucun media ou type de mdia n'a t dfini...</em>";
				this.contenttype = 'html';
			}
			this.callback=(Params.callback)?Params.callback:null;

			document.body.style.overflow='hidden';
			
			
			this.Config.headbarHeight = (!this.Config.useHead)? 0:this.Config.headbarHeight;
			

			$(ctkPopFrame.oOverlay).setStyle({
				display:'block',
				visibility:'visible',
				zIndex:'20000',
				width:(getPageSize()[0])+'px',
				height:(getPageSize()[1])+'px',
				margin:'0px',
				padding:'0px'
			});
			
			
			oCTKPopFrame.setStyle({
				display:'block',
				visibility:'visible',
				zIndex:'20001',
				border:ctkPopFrame.Config.popFrmBorderColor+' solid '+ctkPopFrame.Config.popFrmBorderSize+'px',
				backgroundColor:ctkPopFrame.Config.popFrmBGColor,
				position:'absolute',
				left:Math.round((getPageSize()[2] - 1)/2)+'px',
				top:Math.round((getPageSize()[3] - 1)/2)+'px',
				width:1+'px',
				height:1+'px',
				overflow:'hidden',
				margin:'0px',
				padding:'0px',
				textAlign:'right',
				verticalAlign:'bottom'
			});
			
			this.oFrmPop=oCTKPopFrame;
			
			$(oCTKPopFrame).setOpacity(0.1);
			
			

			oIFrame = oCTKPopFrame.getElementsByTagName('iframe')[0];
			oIFrame.src = Params.url;
			
			$(oIFrame).setStyle({
				width:String(Params.width-(2*this.Config.popFrmBorderSize))+'px',
				height:String(Params.height-this.Config.headbarHeight-(2*this.Config.popFrmBorderSize))+'px',
				border:0,
				margin:0,
				padding:0,
				display:'block'
			});
						
			this.oFrameWindow=oIFrame;
			
			
			
			oPTitle = oCTKPopFrame.getElementsByTagName('p')[0];
			
		//	if(!oPTitle.style) oPTitle.style = oPTitle;
			oPTitle = $(oPTitle);
			if(!this.Config.useHead){ oPTitle.setStyle( {display:'none'} ); /*oPTitle.parentNode.removeChild(oPTitle);/**/ }
			else{
				oPTitle.setStyle({
					display:'block',
					border:'0',
					borderBottom:this.Config.popFrmBorderColor+' solid '+this.Config.popFrmBorderSize+'px',
					width:String(Params.width-this.Config.popFrmBorderSize)+'px',
					height:this.Config.headbarHeight+'px',
					margin:'0',
					padding:'0',
					overflow:'hidden',
					backgroundColor:this.Config.headbarBGColor,
					backgroundImage:'url('+this.Config.headbarBGUrl+')',
					backgroundPosition:'top left',
					backgroundRepeat:'repeat-x',
					fontFamily:this.Config.titleFontFamily,
					fontSize:this.Config.titleFontSize,
					fontWeight:this.Config.titleFontWeight,
					fontStyle:this.Config.titleFontStyle,
					color:this.Config.titleColor,
					textAlign:'left',
					lineHeight:(this.Config.headbarHeight-1)+'px',
					paddingLeft:'3px'
				});
				oPTitle.update( '<img src="'+this.Config.headbarCloseBtnUrl+'" onclick="ctkPopFrame.close()" style="float:right;margin-top:0px;margin-right:4px;cursor:pointer;">'+ Params.title );
			}
			
			
			if(this.Config.allowNativeClose){ this.oIterTimer = setInterval( "ctkPopFrame.tryFrmAccess();",250); }

			window.onresize = ctkPopFrame.adjust;
			window.onscroll = ctkPopFrame.adjust;


			new Effect.Opacity($(oCTKPopFrame), {
				duration:0.3, 
				from:0.1, 
				to:1,
				beforeStart:function(){
				//	$(oCTKPopFrame).setOpacity(0.1);
					new Effect.Opacity($(ctkPopFrame.oOverlay), {
						duration:0.5, 
						from:0.1, 
						to:0.8
					});

				},
				afterFinish:function(){
					ctkPopFrame.scaleTo(Params.width,Params.height);
				}
			});


			return this;
			
		},

		
		close:function(){
			oCTKPopFrame = (typeof this.oFrmPop == 'object')? this.oFrmPop : $(this.oFrmPop);
			if(oCTKPopFrame == null || oCTKPopFrame == 'undefined')return;
			
			/**/
			originalSizes = $(oCTKPopFrame).getDimensions();
			var ratioSizing = { x:(100*1)/originalSizes.width , y:(100*1)/originalSizes.height };
		
		
			new Effect.Opacity(oIFrame, {
				duration:0.3, 
				from:1.0, 
				to:0.0, 
				afterFinish:function(effect){

					new Effect.Scale(oCTKPopFrame,ratioSizing.y,{
						duration: 0.2,
						scaleX: false,
						scaleContent: false,
						scaleFromCenter: true,
						beforeStart:function(){
							oIFrame = oCTKPopFrame.getElementsByTagName('iframe')[0];
							oIFrame.src = 'About:blank';
							$(oIFrame).setStyle({display:'none'});
						},
						afterFinish:function(effect){
							
							new Effect.Scale(oCTKPopFrame,ratioSizing.x,{
								duration: 0.3,
								scaleY: false,
								scaleContent: false,
								scaleFromCenter: true,
								afterUpdate:function(effect){
									oPTitle = oCTKPopFrame.getElementsByTagName('p')[0];
									w = $(oCTKPopFrame).getWidth() - ctkPopFrame.Config.popFrmBorderSize;
									if(w<1)w=0;
									$(oPTitle).setStyle({ width:String(w)+'px' });
								},
								afterFinish:function(effect){
									
									oCTKPopFrame.style.visibility = 'hidden';
									oCTKPopFrame.style.display = 'none';
									
									new Effect.Opacity($(ctkPopFrame.oOverlay), {
										duration:0.5, 
										from:0.8, 
										to:0.0,
										afterFinish:function(){
										
											document.body.style.overflow='';
	
											ctkPopFrame.Config.allowNativeClose=true;
											ctkPopFrame.Config.persistentClosable=false;
											ctkPopFrame.Config.useHead = true;
										
										
											$(ctkPopFrame.oOverlay).setStyle({
												height:'0px',
												display:'none'
											});
											
											if(ctkPopFrame.callback!=null){ctkPopFrame.callback();}
											
	
										}
									});
									
								}
							});
							
						}
					});
					
				}
			});
			
		},
		
		resizeTo:function(w,h){},
		scaleTo:function(w,h){
			
			oCTKPopFrame = (typeof this.oFrmPop == 'object')? this.oFrmPop : this.getObj(this.oFrmPop);
			if(oCTKPopFrame == null || oCTKPopFrame == 'undefined')return;
			
			if(!oCTKPopFrame.style) oCTKPopFrame.style = oCTKPopFrame;
		
			originalSizes = $(oCTKPopFrame).getDimensions();
			var ratioSizing = { x:(100*w)/originalSizes.width , y:(100*h)/originalSizes.height };
			
			
			new Effect.Opacity(oIFrame, {
				duration:0.3, 
				from:1, 
				to:0, 
				afterFinish:function(effect){
					
					new Effect.Scale(oCTKPopFrame,ratioSizing.x,{
						duration: 0.3,
						scaleY: false,
						scaleContent: false,
						scaleFromCenter: true,
						
						afterUpdate:function(effect){
							oPTitle = oCTKPopFrame.getElementsByTagName('p')[0];
							$(oPTitle).setStyle({
								width:String($(oCTKPopFrame).getWidth() - ctkPopFrame.Config.popFrmBorderSize)+'px'
							});
							
							$(oIFrame).setStyle({
								width:String(w-(2*ctkPopFrame.Config.popFrmBorderSize))+'px',
								height:String(h-ctkPopFrame.Config.headbarHeight-(2*ctkPopFrame.Config.popFrmBorderSize)-0)+'px',
								border:0,
								margin:0,
								padding:0
							});
							
						},
						/**/
						afterFinish:function(effect){
							new Effect.Scale(oCTKPopFrame,ratioSizing.y,{
								duration: 0.5,
								scaleX: false,
								scaleContent: false,
								scaleFromCenter: true,
								afterFinish:function(effect){
									new Effect.Opacity(oIFrame, {
										duration:0.3, 
										from:0, 
										to:1,
										afterFinish:function(effect){
											
											if(!window.innerHeight){
											//	oCTKPopFrame.style.display = 'none';
											//	setTimeout("document.body.scrollTop=0; oCTKPopFrame.style.display='block';",10);
											}
											
										}
									});
								}
							});
						}
					});
					
				}
			});
		
			this.adjust();

		},
		
		adjust:function(){
			
			ctkPopFrame.oFrmPop.style.left = Math.round((getPageSize()[2] - (ctkPopFrame.oFrmPop.style.width).replace(/px/g,"") )/2)+'px';
			ctkPopFrame.oFrmPop.style.top = Math.round((getPageSize()[3] - (ctkPopFrame.oFrmPop.style.height).replace(/px/g,"") )/2)+'px';

		},
		
		tryFrmAccess:function(){
			if( String(this.oFrameWindow.contentWindow.close).match("native") ){
				this.oFrameWindow.contentWindow.close = function(){parent.ctkPopFrame.close();}
				this.oFrameWindow.contentWindow.resizeTo = function(w,h){parent.ctkPopFrame.scaleTo(w,h);}
			}else{ if(!this.Config.persistentClosable){clearInterval(this.oIterTimer);} }
		},
		
		
		setContent:function(content){
			this.contenttype = 'html';
			this.html = content;
			
		},

		setUrl:function(url){
			this.contenttype = 'url';
			this.url = url;
			
		},
		
		
		getObj:function(objRef){
			var nav = Object;
			var d = document;
			
			nav.ie 	= d.all ? 1 : 0
			nav.ns4 = d.layers ? 1 : 0
			nav.dom = d.getElementById ? 1 : 0
			
			return nav.dom ? d.getElementById(objRef) : nav.ie ? d.all[objRef] : d.layers[objRef];
		}
		
	};
