//-------------------------------------------------------------------------------------
//WINDOW OBJECT
//version:2.0
//author :duncan ion
//company:interactive1.com
//date   :10/01/03
//required:browserlibrary

//USE
//create new window, return a reference to it.

function i1_Window() {
	var args           = arguments;
	this.isPopWindow = 0;
	var i1_win = this;
	this.url           = args[0] || "about:blank";
	this.winName       = args[1] || "blank";
	this.width         = args[2] || null;
	this.height        = args[3] || null;
	this.center        = args[4] || 0;
	this.windowXpos    = args[5] || 0;
	this.windowYpos    = args[6] || 0;
	
	this.defaultStatus = args[7] || 0;
	this.location      = args[8] || this.defaultStatus;
	this.menubar       = args[9] || this.defaultStatus;
	this.resizeable    = args[10]|| this.defaultStatus;
	this.scrollbars    = args[11]|| this.defaultStatus;
	this.status        = args[12]|| this.defaultStatus;
	this.titlebar      = args[13]|| this.defaultStatus;
	this.toolbar       = args[14]|| this.defaultStatus;
	this.hotkeys       = args[15]|| this.defaultStatus;
	this.callMethod	   = args[16]|| null;
	this.ErrorMessage  = args[17]|| 'The onload function to open a window has been '+
									'suppressed.<br /> For this site to function as '+
									'designed\n you need to allow pop up windows.';
	//for this to be on objectreferencelibrary.js needs to be referenced;
	this.buildErrorWindow = args[18]||false;	
									
	this.createdWindow  = null;
}

i1_Window.prototype.create = function(){
	if(this.center){
	    this.xposition = (screen.width - this.width) / 2;
        this.yposition = (screen.height - this.height) / 2;
	}else{
        this.xposition = (screen.width - this.windowXpos)/2;
        this.yposition = (screen.height - this.windowYpos)/2;	
	}

    this.windowArguments = "" 
    + "width="      + this.width + ","
    + "height="     + this.height     + ","
    + "location="   + this.location   + ","
    + "menubar="    + this.menubar    + ","
    + "resizable="  + this.resizeable + ","
    + "scrollbars=" + this.scrollbars + ","
    + "status="     + this.status     + ","
    + "titlebar="   + this.titlebar   + ","
    + "toolbar="    + this.toolbar    + ","
    + "hotkeys="    + this.hotkeys    + ","
    + "screenx="    + this.xposition  + ","     //NN Only
    + "screeny="    + this.yposition  + ","     //NN Only
    + "left="       + this.xposition  + ","     //IE Only
    + "top="        + this.yposition;           //IE Only
    
   	this.createdWindow = window.open( this.url,this.winName,this.windowArguments);
   	
   	if(this.createdWindow){
   		if(this.content){
   			this.writeTo();
   		}else{
	   		this.createdWindow.focus();
	   	}
	}else{
		//IT MAY HAVE BEEN SUPPRESSED BY POPUPS
		if(!document.createElement){
			alert(this.ErrorMessage);
			return;
		}

		if(this.buildErrorWindow){		
			/*
			//THIS SHOULD BE UPDATED TO BE A GENERIC FUNCTION
			*/
			var infoDiv = document.createElement("DIV");
				infoDiv.id 					 = "suppressedPopUpDiv";
				infoDivStyle 				 = infoDiv.style;
				infoDivStyle.position 		 = "absolute";
				infoDivStyle.top 			 = "10";
				infoDivStyle.left 			 = "100";
				infoDivStyle.width 			 = "300";
				infoDivStyle.backgroundColor = "#ffffff";
				infoDivStyle.fontFamily 	 = "arial,helvetica";
				infoDivStyle.border 		 = "solid red 3px";
				infoDivStyle.padding 		 = "3px";
				
				document.body.appendChild(infoDiv);
			
			var infodiv = new i1_ObjectReference("suppressedPopUpDiv");
				infodiv.innerHTML(this.ErrorMessage+
								  '<br />Please do this then '+
								  '\n<a href="javascript:document.location.reload()">click here</a>');
		}else{
			alert(this.ErrorMessage);
		}
	}
}

i1_Window.prototype.close = function(){
	if(this.createdWindow)
		this.createdWindow.close();
}

i1_Window.prototype.content = "";

i1_Window.prototype.writeTo = function(){
	
	var args = arguments;
	this.content = (args[0]||this.content)
	
	if(this.createdWindow){
		this.createdWindow.document.open("text/html");
		this.createdWindow.document.write("<html>"+this.content+"</html>");
		this.createdWindow.document.close();
	}
}

i1_Window.prototype.moveTo = function(left,top){

	if(this.createdWindow){
		this.createdWindow.moveTo(left,top);
		this.xposition = left;
		this.yposition = top;
	}
}

i1_Window.prototype.moveBy = function(left,top){
	
	if(this.createdWindow){
		this.createdWindow.moveBy(left,top);
		this.xposition = this.xposition+left;
		this.yposition = this.yposition+top;
	}
}

i1_Window.prototype.resizeTo = function(width,height){

	if(this.createdWindow){
		this.createdWindow.resizeTo(width,height);
		this.width  = width;
		this.height = height;
	}
}

i1_Window.prototype.animateResizeTo = function(finalWidth,finalHeight,step){

	//NOT IMPLEMENTED;
}

i1_Window.prototype.createPopUp = function(){
	this.isPopWindow = 1;
	this.createdWindow = window.createPopup();
}

i1_Window.prototype.show = function(x,y,w,h,rPosition,content){
	
	this.ref = this.createdWindow.document.body;
	
	if(typeof(content)=="string"||typeof(this.content)=="string"){
		this.ref.innerHTML  = content||this.content;
	}else{
		this.ref.insertAdjacentHTML("afterBegin",content||this.content);
	}
	this.createdWindow.show(x, y, w, h, rPosition||document.body);
	this.isOpen = this.createdWindow.isOpen;
}
i1_Window.prototype.isOpen = 0;

i1_Window.prototype.hide = function(){
	this.createdWindow.hide();
}

//-------------------------------------------------------------------------------------------------
// ACCESS FRAME SRC

function i1_LoadFrame()
{
	var args 	    = arguments;
	var	frameNames  = args[0];  // string containing the root to the correct frame;
	var src         = args[1]||'about:blank';  // IF SRC IS EMPTY LOAD BLANK PAGE
	var checkOpener = args[2]||0;
	var reload	    = args[3]||0;
	var topWindow   = null;
	var FrameObject = null;
	
	if(checkOpener){
		topWindow = (top.window.opener!=null&&!top.window.opener.closed)?"top.opener.top":0 //CHECK TO SEE IF OPENER EXISTS
	}else{
		topWindow   = "top"; // SET 'top' AS DEFAULT	
	}
	
	if(topWindow){
		FrameObject = eval(topWindow+"."+frameNames);

		if(FrameObject){ 								   // IF FRAME EXISTS LOAD SRC INTO IT;
			FrameObject.location.href = src;
			FrameObject.focus();
		}else{
			if(reload){
				//if window doesnt exist reload source in new window;
				newWindow = new i1_Window(src,"",(screen.width-100),(screen.height-300),1,null,null,1);
				newWindow.create();
			}
		}
	}
}

//------------------------------------------------------------------------------------------------
