/********************************/
/* Eliteology Limited 2006-2007 */
/* www.eliteology.com			*/
/********************************/

var windows = {

	/* original windows */
	/*open : window.open,*/
	showModalDialog : window.showModalDialog,
	/*alert : window.alert,*/
	confirm : confirm,

	el : null, body : null, mask : null, window : null, title : null, close : null, omax : null, frame : null,
	regW : new RegExp("\\bwindows-w-([0-9]*)\\b"), 
	regH : new RegExp("\\bwindows-h-([0-9]*)\\b"), 
	regX : new RegExp("\\bwindows-x-([0-9a-zA-Z]*)\\b"), 
	regY : new RegExp("\\bwindows-y-([0-9a-zA-Z]*)\\b"),
	regMax : new RegExp("\\bwindows-max-(true)\\b"),
	_loading : null, interval : null,

	/* initialize links */
	init : function()
	{
		windows.bind();
		events.add(window, "unload", windows.dispose);
	},
	
	bind : function()
	{
		var links = document.getElementsByTagName("A");

		for (var i=0; i<links.length; i++)
		{
			var el = links[i];
			if (!el.bound && el.rel=="external")
			{
				events.add(links[i], "click", windows.show);
				//el.onclick = function(){return false;};

				/* cache width and height*/
				if (windows.regW.test(el.className)) el.w = windows.regW.exec(el.className)[1];
				if (windows.regH.test(el.className)) el.h = windows.regH.exec(el.className)[1];
				if (windows.regX.test(el.className)) el.x = windows.regX.exec(el.className)[1];
				if (windows.regY.test(el.className)) el.y = windows.regY.exec(el.className)[1];
				el.max = (windows.regMax.test(el.className));
				
				//alert(el.innerHTML + "\n" + el.max);
				
				/* mark as bound*/
				el.bound=true;
			}
		}
	},

	/* insert window container */
	insert : function()
	{
		/* build window els */
		windows.body = document.body || document.documentel;
		windows.mask = support.createElement(windows.body, "div", {'class': "mask"});
		windows.window = support.createElement(windows.body, "div", {'class': "window"});
		windows.title = support.createElement(windows.window, "h1");
		windows.omax = support.createElement(windows.window, "a", {'class': 'max'});
		windows.close = support.createElement(windows.window, "a", {'class': 'close'});
		windows.frame = support.createElement(windows.window, "iframe", {'frameBorder':'0'});
		windows._loading = support.createElement(windows.window, "div", {'class': "loading"},{'display':'none'});
		support.createElement(windows._loading, "h2", "","","Loading... Please Wait");
		
		/* register close event */
		events.add(windows.close, "click", windows.hide);
		events.add(windows.omax, "click", windows.max);
		events.add(window, "resize", windows.resize);
	},
	
	/* open window to set dimensions */
	open : function(url, name, features, replace)
	{			
		var h=-1, w=-1, t=-1, l=-1;
		var drag=true, status=false, dialog=false, resize=true;
		var title = (document.title!="") ? document.title + " - " : "";
		var bLeftCenter=false, bTopCenter= false;		
		var aFeatures = new String(features).split(",");
		
		/* if (sName=="_media"|sName=="_parent"|sName=="_search"|sName=="_self"|sName=="_top") { oOriginalWinOpener(sURL, sName, sFeatures, bReplace); return true; } */
		
		for(var i=0;i<aFeatures.length;i++)
		{
			/* rewrite to use regex.test & replace */
			if (aFeatures[i].indexOf("height")>-1)	h = aFeatures[i].replace(/[^0-9]/gi,"");
			if (aFeatures[i].indexOf("width")>-1)	w = aFeatures[i].replace(/[^0-9]/gi,"");
			if (aFeatures[i].indexOf("top")>-1)		t = aFeatures[i].replace(/[^0-9]/gi,"");
			if (aFeatures[i].indexOf("left")>-1)	l = aFeatures[i].replace(/[^0-9]/gi,"");
			if (aFeatures[i].indexOf("status")>-1)	status = ((aFeatures[i].indexOf("yes")>-1|aFeatures[i].indexOf("1")>-1) ? true:false);
			if (aFeatures[i].indexOf("drag")>-1)	drag = ((aFeatures[i].indexOf("no")>-1|aFeatures[i].indexOf("0")>-1) ? false:true);
			if (aFeatures[i].indexOf("dialog")>-1)	dialog = ((aFeatures[i].indexOf("yes")>-1|aFeatures[i].indexOf("1")>-1) ? true:false);
			if (aFeatures[i].indexOf("resize")>-1)	resize = ((aFeatures[i].indexOf("no")>-1|aFeatures[i].indexOf("0")>-1) ? false:true);
			if (aFeatures[i].indexOf("title")>-1)	title = title + aFeatures[i].replace(/title=/gi,"");				
			if (aFeatures[i].indexOf("top")>-1)		bTopCenter	= ((aFeatures[i].indexOf("center")>-1) ? true:false);
			if (aFeatures[i].indexOf("left")>-1)	bLeftCenter	= ((aFeatures[i].indexOf("center")>-1) ? true:false);
		};
		
		/* insert window markup */
		if (!windows.window) windows.insert();
		
        windows.load(title, url || "about:blank", w, h, resize);
	},

	/* display window */
	show : function(e)
	{
		var el	= e.target;
			el = (el.nodeName!="A") ? el.parentNode : el;
		
		if (el.href!="")
		{
		    events.cancel(e);
		    
	        /* insert window markup */
	        if (!windows.window) windows.insert();
        	
            windows.el = el;
            
            windows.load(el.title, el.href, el.w, el.h, el.max);
		}
		
		return false;
	},
	
	load : function(title, url, w, h, max)
	{
	    // calc dimensions
        w = (w) ? (w) : 600;
        h = (h) ? (h) : 450;
	
	    /* set width and height */
	    windows.frame.style["width"] = w - (support.getPos(windows.window).w - support.getPos(windows.frame).w) + "px";
	    windows.frame.style["height"] = h - (support.getPos(windows.window).h - support.getPos(windows.frame).h) + "px";
    	
	    /* css center the window */
	    windows.window.style["margin"]	= "-" + (h/2) + "px 0px 0px -" + (w/2) + "px";

	    /* need to attach state change to allow loading image */
	    windows.title.appendChild(document.createTextNode(title || ""));
	    windows.mask.style.display = "block";
	    windows.window.style.display = "block";
	    windows.frame.style.visibility = "hidden";
        windows._loading.style.display = "block";
        windows.omax.style.display = (max) ? "block" : "none";

        // register event
        events.add(windows.frame,"load", windows.loaded);
        
        // ie loading hack
        windows.interval = window.setInterval(function()
        {
            if ((windows.frame) && (windows.frame.document) && (windows.frame.document.readyState=="complete"))
            {
                window.clearInterval(windows.interval);
                window.setTimeout(windows.loaded,1000);
            }
        },250);

	    windows.frame.src = url;
	    windows.body.className = "noscroll";
	    windows.body.scroll="no";
    	
	    windows.window.focus();
    	
    	// monitor escape key
	    events.add(document, "keydown", windows.keydown);
	},
	
	loaded : function()
	{
        events.remove(windows.frame,"load", windows.loaded);
        windows._loading.style.display = "none";
	    windows.frame.style.visibility = "visible";
	},

	/* hide window */
	hide : function()
	{
		windows.el					= null;
		windows.mask.style.display		= "none";
		windows.window.style.display	= "none";
		windows.frame.src				= "";
		windows.body.className			= "";

		if (windows.title.firstChild) windows.title.removeChild(windows.title.firstChild);
		events.remove(document, "keydown", windows.keydown);
	},
	
	max : function()
	{
	    var w = support.viewport().width;
	    var h = support.viewport().height;
	
	    // update max flag
	    windows.frame.maxed=true;
	
	    // store current values to allow restore
	    windows.frame.w = support.getPos(windows.frame).w;
	    windows.frame.h = support.getPos(windows.frame).h;
	
	    /* set width and height */
	    windows.frame.style["width"] = w - (support.getPos(windows.window).w - support.getPos(windows.frame).w) + "px";
	    windows.frame.style["height"] = h - (support.getPos(windows.window).h - support.getPos(windows.frame).h) + "px";
    	
	    /* css max window */
	    windows.window.style["margin"]	= "-" + (h/2) + "px 0px 0px -" + (w/2) + "px";
	
		events.remove(windows.omax, "click", windows.max);
        events.add(windows.omax, "click", windows.restore);
	    windows.omax.className += " restore";
	},
	
	restore : function()
	{
        events.remove(windows.omax, "click", windows.restore);
		events.add(windows.omax, "click", windows.max);
	    windows.omax.className.replace(" restore","");
	    
	    var w = windows.frame.w;
	    var h = windows.frame.h;
	    
	    // update max flag
	    windows.frame.maxed=false;
	
	    /* set width and height */
	    windows.frame.style["width"] = w + "px";
	    windows.frame.style["height"] = h + "px";
    	
	    /* css center the window */
	    windows.window.style["margin"]	= "-" + (h/2) + "px 0px 0px -" + (w/2) + "px";
	},
	
	resize : function()
	{
	    if (windows.frame.maxed)
	    {
	        var w = support.viewport().width;
	        var h = support.viewport().height;
    	
	        /* set width and height */
	        windows.frame.style["width"] = w - (support.getPos(windows.window).w - support.getPos(windows.frame).w) + "px";
	        windows.frame.style["height"] = h - (support.getPos(windows.window).h - support.getPos(windows.frame).h) + "px";
        	
	        /* css max window */
	        windows.window.style["margin"]	= "-" + (h/2) + "px 0px 0px -" + (w/2) + "px";
	    }
	},
	
	/* keyboard support */
	keydown : function(e)
	{
	    if (e.key==27) 
	    {
	        windows.hide();
		    return false;
        }
        return true;
	},

	/* release memory */
	dispose : function()
	{
		if (windows.window)
		{
			events.remove(windows.mask, "click", windows.hide);
			events.remove(windows.title, "click", windows.hide);

			windows.body.removeChild(windows.mask);
			windows.body.removeChild(windows.window);
			windows.mask	= null;	windows.window	= null;
			windows.title	= null; windows.frame	= null;
			windows.body	= null; windows.el = null;
			windows.close	= null; windows.max		= null;
		}
	}
}

if(window.DomLoaded)
{DomLoaded.load(windows.init)}
else
{events.add(window,"load",windows.init);}
