////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2001-2002 CreativeCow.net - All Rights Reserved
//
// window.js
//
// open and manage javascript windows
////////////////////////////////////////////////////////////////////////////////

// for the windows open
var winArray = new Array();

var WINDOW_BASIC = 0;
var WINDOW_BASIC2 = 1;
var WINDOW_RESIZE = 2;
var WINDOW_PLAIN = 3;
var WINDOW_RELATIVE = 4;
var WINDOW_NORMAL_RELATIVE = 5;

// for the relative window
var SHOW_TOOLBAR_YES = 1;
var SHOW_TOOLBAR_NO = 0;

// open a basic window with no fixin's
function openBasicWin (url, w, h) {
		
    if ((top.basicWin) && (! top.basicWin.closed)) {
	// window is already open - close and restart
	top.basicWin.close();
    }

    top.basicWin = window.open(url,'bWin','top='
    	    + this.getDialogUpperThirdTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
	    + ',toolbar=no,location=no,directories=no,'
	    + 'status=no,menubar=no,scrollbars=no,resizable=no,width='
	    + w + ',height=' + h);

    if (! top.basicWin.opener) {
	top.basicWin.opener = window;
    }
}

// open another basic window with no fixin's
function openBasicWin2 (url, w, h) {
		
    if ((top.basicWin2) && (! top.basicWin2.closed)) {
	// window is already open - close and restart
	top.basicWin2.close();
    }
    
    top.basicWin2 = window.open(url,'bWin2','top='
    	    + this.getDialogUpperThirdTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
	    + ',toolbar=no,location=no,directories=no,'
	    + 'status=no,menubar=no,scrollbars=no,resizable=no,width='
	    + w + ',height=' + h);

    if (! top.basicWin2.opener) {
	top.basicWin2.opener = window;
    }
}


// open plain window with basic functions
function openPlainWin (url, w, h) {
    if ((top.plainWin) && (! top.plainWin.closed)) {
	// window is already open - close and restart
	top.plainWin.close();
    }

    top.plainWin = window.open(url,'pWin','top='
    	    + this.getDialogUpperThirdTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
	    + ',toolbar=no,location=no,directories=no,'
	    + 'status=no,menubar=no,scrollbars=yes,resizable=no,width='
	    + w + ',height=' + h);

    if (! top.plainWin.opener) {
	top.plainWin.opener = window;
    }
}

// open a resize-able window with basic functions
function openResizeWin (url, w, h, showToolbar) {
    if ((top.resizeWin) && (! top.resizeWin.closed)) {
	// window is already open - close and restart
	top.resizeWin.close();
    }

    top.resizeWin = window.open(url,'sWin','top='
    	    + this.getDialogUpperThirdTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
    	    + ',toolbar='
	    + showToolbar
	    + ',location=no,directories=no,'
	    + 'status=no,menubar=no,scrollbars=yes,resizable=yes,width='
	    + w + ',height=' + h);

    if (! top.resizeWin.opener) {
	top.resizeWin.opener = window;
    }
}

// open a window with size relative to screen real estate
function openRelativeWin (url, w, h) {
    if ((top.relativeWin) && (! top.relativeWin.closed)) {
	// window is already open - close and restart
	top.relativeWin.close();
    }
    
    top.relativeWin = window.open(url,'rWin','top='
    	    + this.getDialogCenterTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
	    + ',toolbar=no,location=no,directories=no,'
	    + 'status=no,menubar=no,scrollbars=yes,resizable=yes,width='
	    + w + ',height=' + h);

    if (! top.relativeWin.opener) {
	top.relativeWin.opener = window;
    }

    // if running 640 x 480, resize window down to fit
    if (screen.availWidth <= 640) {
	top.relativeWin.resizeTo(screen.availWidth,screen.availHeight);
    }
}

// open a window with size relative to screen real estate
function openNormalRelativeWin (url, w, h) {
    if ((top.normalRelativeWin) && (! top.normalRelativeWin.closed)) {
	// window is already open - close and restart
	// top.normalRelativeWin.close();
	top.normalRelativeWin.focus();
    }
        
    top.normalRelativeWin = window.open(url,'nRWin','top='
    	    + this.getDialogCenterTop(h)
	    + ',left='
	    + this.getDialogCenterLeft(w)
	    + ',toolbar=yes,location=yes,directories=no,'
	    + 'status=yes,menubar=no,scrollbars=yes,resizable=yes,width='
	    + w + ',height=' + h);

    if (! top.normalRelativeWin.opener) {
	top.normalRelativeWin.opener = window;
    }

    // if running 640 x 480, resize window down to fit
    if (screen.availWidth <= 640) {
	top.normalRelativeWin.resizeTo(screen.availWidth,screen.availHeight);
    }
}

// pages can call this on unload to close all windows
function closeAllWins () {
    if ((top.basicWin) && (! top.basicWin.closed)) {
	top.basicWin.close();
    }

    if ((top.basicWin2) && (! top.basicWin2.closed)) {
	top.basicWin2.close();
    }

    if ((top.resizeWin) && (! top.resizeWin.closed)) {
	top.resizeWin.close();
    }

    if ((top.plainWin) && (! top.plainWin.closed)) {
	top.plainWin.close();
    }

    if ((top.relativeWin) && (! top.relativeWin.closed)) {
	top.relativeWin.close();
    }

    if ((top.normalRelativeWin) && (! top.normalRelativeWin.closed)) {
	top.normalRelativeWin.close();
    }
}

// check the open status of the passed window from an open window - 
// this can be used to determine if another window of the same type
// can be opened at the current time
function windowIsOpen (window) {
    if (window == WINDOW_BASIC) {
	if ((opener.top.basicWin) && (! opener.top.basicWin.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    } else if (window == WINDOW_BASIC2) {
	if ((opener.top.basicWin2) && (! opener.top.basicWin2.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    } else if (window == WINDOW_PLAIN) {
	if ((opener.top.plainWin) && (! opener.top.plainWin.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    } else if (window == WINDOW_RESIZE) {
	if ((opener.top.resizeWin) && (! opener.top.resizeWin.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    } else if (window == WINDOW_RELATIVE) {
	if ((opener.top.relativeWin) && (! opener.top.relativeWin.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    } else if (window == WINDOW_NORMAL_RELATIVE) {
	if ((opener.top.normalRelativeWin) && (! opener.top.normalRelativeWin.closed)) {
	    // MAY EXIT THIS BLOCK
	    return true;
	}
    }

    // the default
    return false;
}

// returns the center left position related to the passed dialog width
function getDialogCenterLeft (w) {
    return ((screen.availWidth / 2) - (w / 2));
}

// returns the top third position related to the passed dialog width
function getDialogUpperThirdTop (h) {
    var rhett = ((screen.availHeight / 3) - (h / 2));
    rhett = (rhett < 0) ? -rhett : rhett;

    return rhett;
}

// returns the center top position related to the passed dialog width
function getDialogCenterTop (h) {
    var rhett = ((screen.availHeight / 2) - (h / 2));
    rhett = (rhett < 0) ? -rhett : rhett;

    return rhett;
}


