/*=======================================================================*
*	         Copyright 2002-09 by Virtual Gallerie, LLC.             *
*     This material is the confidential trade secret and proprietary     *
*     information of Virtual Gallerie, LLC. It may not be reproduced,    *
*     used, sold or transferred to any third party without the prior     *
*     written consent of Virtual Gallerie, LLC. All rights reserved.	 *
*=======================================================================*/
// requires layers.js (at least)

if (Com == null) {
	var Com = new Object();
}
if (Com.Jantje == null) {
	Com.Jantje = new Object();
}


function getURL_PRIVATE(url, file_pp) {

	if (url != "") {
		if (file_pp != "") {
			if (url.indexOf("http:") == 0) return url;
			else if (url.indexOf("https:") == 0) return url;
			else if (url.indexOf("file:") == 0) return url;
			else {
				if ((url.charAt(0) == '/') || (url.charAt(0) == '\\')) {
					return file_pp + url.substring(1);
				}
				return (file_pp + url);

			}
		} else {
			return url;
		}

	} else {
		return "";
	}
}

function getFileDocURLBaseIf(b_include_last_slash, b_pull_file) {
	var url = window.document.URL;
	var qi;
	url = replaceToDirSep(url);
	
	if ((qi = url.indexOf("?")) > -1) {
		url = url.substring(0, qi);
	}
	var start_i = 0;
		
	if (b_pull_file) {
		var pi = url.indexOf(":" + DIR_SEP + DIR_SEP + DIR_SEP);
		if (pi > -1) start_i = pi + (!gb_mac ? 4 : 4);
		else if ((pi = url.indexOf(":" + DIR_SEP + DIR_SEP)) > -1) {
			start_i = pi + (!gb_mac ? 3 : 3);
		}
	}	var end_i = url.lastIndexOf(DIR_SEP);
	
	if (b_include_last_slash) end_i++;
	
	return url.substring(start_i, end_i);
}

function getLastIndexOfChar(s, c) {

	for (i = s.length; i >= 0; i--) {
		if (s.charAt(i) == c) return i;
	}
	return -1;
}

function isDefined(v) {
	return (typeof(v) != "undefined");
}

function getAnyWindowSize(win) {
  var w = 0, h = 0;
  if( typeof( win.innerWidth ) == 'number' ) {
    //Non-IE
    w = win.innerWidth;
    h = win.innerHeight;
  } else if( win.document.documentElement && ( win.document.documentElement.clientWidth || win.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    w = win.document.documentElement.clientWidth;
    h = win.document.documentElement.clientHeight;
  } else if( win.document.body && ( win.document.body.clientWidth || win.document.body.clientHeight ) ) {
    //IE 4 compatible
    w = win.document.body.clientWidth;
    h = win.document.body.clientHeight;
  }
  
  var o = new Object();
  o.w = w;
  o.h = h;
  return o;

}

Com.Jantje.WindowInfoObj = function(url, name, fld_prefix, h, w, b_scroll_bars, extra_props, tw) {
	this._NAME = name;
	this._FLD_PREFIX = fld_prefix; // so we can find related fields for it in a form... need method for this
					//todo: FLD_PREFIX is unused... should p/h remove it, eh?
	this._H = h;
	this._W = w;
	this._TW = (!isDefined(tw) && isDefined(w)) ? this._W - 20 : 0;
	this._OPEN_PROPS = ""; //"scrollbars=1"; // ", resizable=1";

	function addOpenProp(o, a, v) {
		var s = a + "=" + v;
		if (o._OPEN_PROPS != "") o._OPEN_PROPS += ", " + s;
		else o._OPEN_PROPS = s;
	}
	if (extra_props != "blank") {
		if (b_scroll_bars) {
			addOpenProp(this, "scrollbars", "yes");
			this._TW -= 25;
		} else {
			addOpenProp(this, "scrollbars", "no");		
		}
	} else {
		extra_props = null;
	}
	this._OPEN_LIKE_OPENER = false;
	
	if (isDefined(this._H) && (this._H > 0)) addOpenProp(this, "height", this._H);
	if (isDefined(this._W) && (this._W > 0)) addOpenProp(this, "width", this._W);

	if (extra_props) {
		this._OPEN_PROPS += (this._OPEN_PROPS != "") ? (", " + extra_props) : extra_props;
	}
	this._WIN = null; // can hold a pointer to this window....

	this._FILE_URL_PREPEND = getFileDocURLBaseIf(true, false);

	this._URL = getURL_PRIVATE(url, this._FILE_URL_PREPEND); //this.setURL_PRIVATE(url); // can be ""

	//alert(this._URL);

	//this.openYourWindow = openYourWindow;
	//this.setYourURL = setYourURL;
	//this.openOrReload = openOrReload;

	//this.openMyWin_PRIVATE = openMyWin_PRIVATE;

};

Com.Jantje.WindowInfoObj.prototype.setToOpenLikeOpener = function(tf) {
	this._OPEN_LIKE_OPENER = tf;
}

Com.Jantje.WindowInfoObj.prototype.fillScreen = function(o_reloc) {
	try {
		if (o_reloc) this._WIN.moveTo(o_reloc.x, o_reloc.y);
		else this._WIN.moveTo(0,0);
	}
	
	catch (er) {
		return 1;
	}
	
	try {
		this._WIN.resizeTo(screen.width,screen.height);
	} 
	catch (er) {
		return 2;
	}
}

Com.Jantje.WindowInfoObj.prototype.setQueryString = function(new_s, b_preload) {
	var qi = this._URL.indexOf("?");
	var b_add_qm = new_s.charAt(0) != '?';
	
	if (qi > -1) {
		this._URL = this._URL.substring(0, qi);		
	}
	this._URL += (b_add_qm) ? "?" + new_s : new_s;
	
	if (b_preload) {
		return; // we're not actually settin the window.location, since it's not opened yet!	
	} else if (this.isWindowOpen()) {
		this._WIN.location = this._URL;
		this._WIN.focus();
	} else {
		this.openMyWin_PRIVATE();
	}
};

Com.Jantje.WindowInfoObj.prototype.setURL_PRIVATE = function (url) {

	if (url != "") {
		if (this._FILE_URL_PREPEND != "") {
			if (url.indexOf("http:") == 0) this._URL = url;
			else if (url.indexOf("file:") == 0) this._URL = url;
			else {
				this._URL = this._FILE_URL_PREPEND + url;

			}
		} else {
			this._URL = url;
		}

	} else {
		this._URL = "";
	}
};

Com.Jantje.WindowInfoObj.prototype.presetURL = function (url) {
	this.setURL_PRIVATE(url);
}

Com.Jantje.WindowInfoObj.prototype.openYourWindow = function(url) {
	this.setURL_PRIVATE(url);
	return this.openMyWin_PRIVATE();

};

//returns er obj if any err, else null
Com.Jantje.WindowInfoObj.prototype.openMyWin_PRIVATE = function() {
	
	if (this._URL != "") {
		try {
			var op = this._OPEN_LIKE_OPENER ? "" : this._OPEN_PROPS;
			this._WIN = window.open(this._URL, this._NAME, op);
		}
		catch(er) {
			return er;
		}
	} else {
		alert("window with no url :" + this._NAME);
	}
	return null;
};


// only call if we know it's open!
Com.Jantje.WindowInfoObj.prototype.setURLAndReload = function (url) {
	this.setURL_PRIVATE(url);
	//if (this.URL != url) {
		//this._URL = url
		this._WIN.location = this._URL;
	//} else {
	//	this._WIN.reload();
	//}
	this._WIN.focus(); // bring to front!
};

Com.Jantje.WindowInfoObj.prototype.openOrReload = function () {
	if (!this._WIN || this._WIN.closed) {
		return this.openMyWin_PRIVATE();
	} else {
		this._WIN.location.reload();
		this._WIN.focus();
	}
	return null;
};

Com.Jantje.WindowInfoObj.prototype.gotoTheWindow = function () {
	if (!this._WIN || this._WIN.closed) {
		this.openMyWin_PRIVATE();

	} else {
		this._WIN.focus();
	}
};

Com.Jantje.WindowInfoObj.prototype.isWindowOpen = function () {
	return (this._WIN && !this._WIN.closed);
};

Com.Jantje.WindowInfoObj.prototype.hasWindowBeenClosed = function () {
	return (this._WIN && this._WIN.closed);
};


Com.Jantje.WindowInfoObj.prototype.closeWin = function () {
	if (this.isWindowOpen()) {

		this._WIN.close();
	}
};

Com.Jantje.WindowInfoObj.prototype.goAndShow = function (url) {
	// if url the same as current and it's open? what then? nothing? just bring to front?

	if (!this._WIN || this._WIN.closed) {
		this.openYourWindow(url);
	} else {

		this.setURLAndReload(url);
	}
};

Com.Jantje.WindowInfoObj.prototype.setPosition = function(x, y) {
	if (this.isWindowOpen()) {
		this._WIN.moveTo(x, y);
	} else {
		// set the top/left to x, y, and then when it opens next time, need to use them!
	}
};

Com.Jantje.WindowInfoObj.prototype.setSize = function(x, y) {
	if (this.isWindowOpen()) {
		this._WIN.resizeTo(x, y);
	} else {
		// set the top/left to x, y, and then when it opens next time, need to use them!
	}
};

Com.Jantje.WindowInfoObj.prototype.getActualSize = function() {
	if (this.isWindowOpen()) {
		try {
			return getAnyWindowSize(this._WIN);
		} catch (er) {
		
		}
	}
	return null;
};



Com.Jantje.WindowInfoObj.prototype.doEval = function(eval_str) {
	if (this.isWindowOpen()) {
		try {
			this._WIN.eval(eval_str);
		}
		catch (er) {
			return false;
		}
		
		return true;
	} else {
		
	}
	return false;
};

Com.Jantje.WindowInfoObj.prototype.doDelayedEval = function(eval_str, dur) {
	if (!dur) {
		this.doEval(eval_str);
		return;
	}
	if (this.isWindowOpen()) {
		try {
			this._WIN.eval('setTimeout("' + eval_str + '",' + dur + ");");
		}
		catch (er) {
			return false;
		}
		
		return true;
	} else {
		
	}
	return false;
};

Com.Jantje.WindowInfoObj.prototype.getWinIfOpen = function() {
	if (this.isWindowOpen()) {
		return this._WIN;
	} else {
		return null;	
	}
};

Com.Jantje.WindowInfoObj.prototype.getCurrentLocationString = function() {
	if (this.isWindowOpen()) {
		try {
			return this._WIN.location.toString();
		}
		catch(er) {
			//
		}
	}
	return null;	
};



// could also set this dynamically based on the length of the text, but would be
// wierd if it was still open when artist changed and size would have changed!

// can we put scroll bars on the text if necessary?