/*
Copyright ?2010 Daniel Wiesen?cker

    FrameCenteredInfo is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    FrameCenteredInfo is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FrameCenteredInfo.  If not, see <http://www.gnu.org/licenses/>.
*/

	function checkClip(width, height){
		var delim = new Array();
		if(width!=(document.all||true?parseInt(document.body.clientWidth):parseInt(window.innerWidth))||height!=(document.all||true?parseInt(document.body.clientHeight):parseInt(window.innerHeight))){
			
			delim = new Array();
			delim[0]="?";
			var val=tokenize(document.location.href, delim, true);
			var href="";
			for(var v=0; v < val.length; v++){
				if(val[v]=="binded"){
					v+=2;
				}else href+=val[v];
			}
			document.location.href=href;
		}
	}

	function Dimension(width, height){
		this.width=width;	
		this.height=height;		
	}

	function Rectangle(point, dim){
		this.topLeft=point;
		this.size=dim;
	}

	function Point(x,y){
		this.x=x;
		this.y=y;
	}

	function FrameInfo(){
		this.size=null;
		this.embedSize=null;
		this.innerFrame=null;
		this.overlap = null;
		this.middle=null;
		this.winSize = null;
		this.t=null;
		this.frameObj=null;
		this.layerId="";

		this.init = function(launchStart){
			document.writeln("<DIV id=\"" + "\" style=\"position: absolute; left: 0px; top: 0px; width: " + (this.embedSize!=null?(this.embedSize.width + "px") :"100%") +  "; height: " + (this.embedSize!=null?(this.embedSize.height + "px") :"100%") + ";  z-index: 0; \"><TABLE style=\"width: " + (this.embedSize!=null?(this.embedSize.width + "px") :"100%") +  "; height: " + (this.embedSize!=null?(this.embedSize.height + "px") :"100%") + "; \"><TR><TD style=\"width: " + (this.embedSize!=null?(this.embedSize.width + "px") :"100%") +  "; height: " + (this.embedSize!=null?(this.embedSize.height + "px") :"100%") + "; \">&nbsp;</TD></TR></TABLE></DIV>");

			this.size=new Dimension(parseInt(document.body.scrollWidth), parseInt(document.body.scrollHeight));
			this.winSize=new Dimension(document.all||true?parseInt(document.body.clientWidth):parseInt(window.innerWidth), document.all||true?parseInt(document.body.clientHeight):parseInt(window.innerHeight));
			delim = new Array();
			delim[0]="?";
			var val=tokenize(document.location.href, delim, false);
			if(urlGet('binded')==-1) document.onload=(document.location.href=document.location.href + (val.length>1?"&binded=YES":"?binded=YES"));

			this.middle = new Point(Math.floor(this.size.width/2), Math.floor(this.size.height/2));

			if(this.embedSize==null) this.setInnerFrame(this.winSize.width, this.winSize.height);

			this.setFrame(this.winSize.width, this.winSize.height);

			this.setOverlap(0,0);
			if(urlGet("cent")!="YES"&&launchStart){
				this.t=setInterval("checkClip(" + this.winSize.width + ", " + this.winSize.height + ");", 1000);
 //by dw94g
			}
		}

		this.setFrame = function(width, height){
			this.size  = new Dimension(width, height);
			this.winSize=new Dimension(document.all||true?parseInt(document.body.clientWidth):parseInt(window.innerWidth), document.all||true?parseInt(document.body.clientHeight):parseInt(window.innerHeight));
			this.middle = new Point(Math.floor(this.winSize.width/2), Math.floor(this.winSize.height/2));
		}


		this.setInnerFrame = function(width, height){
			this.embedSize = new Dimension(width, height);
			this.innerFrame = new Rectangle(new Point(Math.floor(this.middle.x-width/2), Math.floor(this.middle.y-height/2)), this.embedSize);
		}

		this.setOverlap  = function(width, height){
			var size= new Dimension(width?width:1, height?height:1);
			var limit=0;
			var stepW=0;
			var stepH=0;
			var savNewEmbedWidth=this.embedSize.width;
			var savNewEmbedHeight= this.embedSize.height;
			
			if(size.width<size.height){
				limit=size.width;
				stepW=1;
				stepH=size.height/limit;
				
			}else{
				limit = size.height;
				stepH=1;
				stepW=size.width/limit;
			}
			var count=0;
			while((this.embedSize.width<this.size.width&&this.embedSize.height<this.size.height)&&count<limit){
				savNewEmbedWidth=savNewEmbedWidth+stepW;
				savNewEmbedHeight=savNewEmbedHeight+stepH;
				this.embedSize = new Dimension(Math.floor(savNewEmbedWidth), Math.floor(savNewEmbedHeight));
				count++;
			}
			if(size.width<size.height){
				this.overlap = new Dimension(count, Math.floor(count*stepH));
			}else{
				this.overlap = new Dimension(Math.floor(count*stepW), count);
			}
			this.innerFrame.topLeft=new Point(Math.floor(this.innerFrame.topLeft.x-this.overlap.width/2), Math.floor(this.innerFrame.topLeft.y-this.overlap.height/2));
			this.innerFrame.size.width+=this.overlap.width;
			this.innerFrame.size.height+=this.overlap.height;

		}


		this.setMinimumSize = function(width, height){
			this.setInnerFrame(width, height);
		}

		this.setPreferedSize = function(width, height){
			this.setOverlap(width-this.size.width, height-this.size.height);
		}

		this.setSize = function(width, height){
			this.setFrame(width, height);
		}


		this.getMiddle = function(){
			return this.middle;	
		}

		this.toString = function(){
			return "left=" + this.innerFrame.topLeft.x + "&top=" + 
			this.innerFrame.topLeft.y + "&width=" + 
			this.embedSize.width + "&height=" + 
			this.embedSize.height  + "&ovX=" + 
			this.overlap.width + "&ovY=" + 
			this.overlap.height;
		}

		this.getLayerId = function(){
			return this.layerId;
		}

		this.makeVisible = function(b){
			b=b?b:false;
			try{
				if(b){
					this.frameObj.visibility="visible";
				}else this.frameObj.visibility="hidden";
			}catch(e){
				//whatis ie;
			}
		}

		this.getWindow = function(){
			return window.frames[0];
		}

		this.writeIFrame = function(isUrl, scrolling, zindex, relX, relY){
			relX=relX?relX:0;
			relY=relY?relY:0;
			
zindex=zindex?zindex:1;
			isUrl+="?cent=YES&" + this.toString();
			var id="centered_" + Math.floor(Math.random()*100000);
			document.writeln("<DIV id=\"" + id + "\" style=\"position: absolute; left: " + (this.innerFrame.topLeft.x+relX)  + "px; top: " +   (this.innerFrame.topLeft.y+relY)  + "px; width: " + this.innerFrame.size.width + "; height: " + this.innerFrame.size.height + ";  z-index: " + zindex + "; visibility: hidden;\"><IFRAME width=\"" + this.innerFrame.size.width +  "\" height=\"" + this.innerFrame.size.height + "\" src=\"" + isUrl + "\" frameborder=\"0\"" + (!scrolling?" scrolling=\"no\"":"") + " allowtransparency=\"true\" style=\"background-color: transparent; \"></IFRAME></DIV>");

			try{ 
				this.frameObj=document.getElementById(id).style;
			}catch(e){
				;//whatis ie
			}
			this.layerId=id;
		}

	}

