﻿// AgencyWindow.js


//return the browser is IE or not
function isIE()
{
	var nav=navigator.userAgent.toLowerCase();
	alert(nav);
	if(nav.indexOf("msie")!=-1)
	{
		return true;
	}
	return false;
}

//Window object
function Window(w,h,l,t){
	this.windowWidth=500;
	this.windowHeight=500;
	this.windowLeft=0;
	this.windowTop=0;
	this.windowX=0;
	this.windowY=0;
	
	//you can set this property true ot flase
	this.dropShadow=true;
	
	if(!isNaN(parseInt(w)))
	{
		windowWidth=w;	
	}
	if(!isNaN(parseInt(h)))
	{
		windowHeight=h;	
	}
	if(!isNaN(parseInt(l)))
	{
		windowLeft=l;
	}
	if(!isNaN(parseInt(t)))
	{
		windowTop=t;
	}
	
	if(typeof this.windowElement=="undefined")
	{
		this.windowElement=document.createElement("div");
		this.windowElement.id="window_element";
	}
	
	if(typeof this.windowHeader=="undefined")
	{
		this.windowHeader=document.createElement("div");
		this.windowHeader.id="window_header";
	}
	
	if(typeof this.windowTitle=="undefined")
	{
		this.windowTitle=document.createElement("div");
		this.windowTitle.id="window_title";
	}
	
	if(typeof this.windowToolbar=="undefined")
	{
		this.windowToolbar=document.createElement("div");
		this.windowToolbar.id="window_toolbar";
	}
	
	if(typeof this.windowButton=="undefined")
	{
		this.windowButton=document.createElement("span");
		this.windowButton.id="window_button";
	}
	
	if(typeof this.windowBody=="undefined")
	{
		this.windowBody=document.createElement("iframe");
		this.windowBody.id="window_body";
		this.windowBody.frameBorder=0;
	}
	
	this.windowElement.appendChild(this.windowHeader);
	//define a mouseIsDown property
	this.windowHeader.mouseIsDown=false;
	
	this.windowElement.appendChild(this.windowBody);
		
	this.windowHeader.appendChild(this.windowTitle);
	this.windowHeader.appendChild(this.windowToolbar);
	this.windowToolbar.appendChild(this.windowButton);
	
	this.windowElement.style.width=windowWidth;
	this.windowElement.style.height=windowHeight;
	this.windowElement.style.left=windowLeft;
	this.windowElement.style.top=windowTop;
	this.windowElement.style.zindex=100;
	this.windowElement.style.position="absolute";
	
	this.windowElement.style.fontSize="12px";
	this.windowElement.style.backgroundColor="#F8F8F8";
	this.windowElement.style.border="1px solid #DDDDDD";
	
	if(this.dropShadow)
	{
		this.windowElement.style.filter="progid:DXImageTransform.Microsoft.DropShadow(OffX=8, OffY=8,Color='black', Positive='false')";
	}
	
	this.windowHeader.style.cursor="move";
	this.windowHeader.style.padding="3px";
	
	
	this.windowTitle.style.styleFloat="left";
	this.windowTitle.style.fontWeight="bold";
	
	this.windowToolbar.style.textAlign="right";
	
	this.windowButton.style.cursor="pointer";
	
	this.windowBody.style.width="100%";
	this.windowBody.style.height="100%";
	//this.windowBody.style.border="1px solid #DDDDDD";
	
	this.windowTitle.innerHTML="BroGame";
	this.windowButton.innerHTML="[关闭]";
	this.windowButton.title="close window";
	
	//set the content of the window title
	//the parameter "str" can contain some html code
	this.setTitle=function(str)
	{
		this.windowTitle.innerHTML=str;
	}
	
	//set the content of the close button
	//the parameter "str" can contain some html code
	this.setButton=function(str)
	{
		this.windowButton.innerHTML=str;
	}
	
	//handle the MouseEnter event on windowHeader element
	this.windowHeader.onmouseenter=function()
	{
		this.style.backgroundColor="#F2F2F2";	
	}
	
	//handle the MouseLeave event on windowHeader element
	this.windowHeader.onmouseleave=function()
	{
		this.style.backgroundColor="#F8F8F8";	
	}
	
	//handle the MouseDown event on windowHeader element
	this.windowHeader.onmousedown=function()
	{
		this.setCapture();
		windowX=event.offsetX;
		windowY=event.offsetY;
		this.mouseIsDown=true;
	}
	
	//handle the MouseUp event on windowHeader element
	this.windowHeader.onmouseup=function()
	{
		this.mouseIsDown=false;
		this.releaseCapture();
	}
	
	//handle the MouseMove event on windowHeader element
	this.windowHeader.onmousemove=function()
	{
		if(this.mouseIsDown)
		{
			var obj=document.getElementById("window_element");
			obj.style.left=document.body.scrollLeft+event.x-windowX;
			obj.style.top=document.body.scrollTop+event.y-windowY;
		}
	}
	
	//close window
	this.windowButton.onclick=function(){
		if(document.getElementById("window_element"))
		{
			document.body.removeChild(document.getElementById("window_element"));
		}
	}
	
	//set the window width and height properties
	this.setSize=function(w,h)
	{
		this.windowElement.style.width=w;
		this.windowElement.style.height=h;
	}
	
	//return the window if opened or not
	this.isOpened=function()
	{
		if(document.getElementById("window_element"))
		{
			return true;
		}
		return false;
	}
	
	//open a special url
	this.openURL=function(url)
	{
		if(!this.isOpened()){
			document.body.appendChild(this.windowElement);
		}
		this.windowBody.src=url;
	}
}
