//Makes a popup inside the target (needs to be called in an onclick function)
//lots of css and class and content options
//will one day be expanded to replace confirm boxes
function PopUp(e,data,options) {
	try {
		if ($type(e) != 'event') throw('e');
		this.event = e;
		this.event.stop();
		this.target = e.getTarget();
		if (!$chk(options)) options = new Object();
		if ($type(options) != 'object') throw('options');
		if ($chk(options.useClass)) {
			this.cssClass = 'poppermcpoppy ' + options.useClass;
			this.css = null;
		}
		else {
			this.cssClass = 'poppermcpoppy';
			this.css = "position:absolute;width:200px;height:200px;bottom:20;background-color:#0069a3;color:#000;border:1px solid #000;z-index:500;padding:15px;font-weight:normal;font-size:12px;line-height:normal;text-decoration:none;text-align:left;";
		}
		if ($chk(options.addStyle)) {
			if (options.addStyle.contains('style')) throw('style');
			var styleTokens = new StringTokenizer(options.addStyle,';');
			while(styleTokens.hasNext()) {
				var token = styleTokens.nextToken().trim();
				var key = token.getToken(':');
				var value = token.getToken(':',1);
				if (this.css.contains(key)) this.css = this.css.substring(0,this.css.indexOf(key) + key.length + 1) + value + this.css.substr(this.css.indexOf(key) + this.css.slice(this.css.indexOf(key)).indexOf(';'));
				else this.css = this.css + token + ';';
			}
		}
		if ($type(data) != 'string' && $type(data) != 'element') throw('data');
		if ($type(data) == 'element') {
			this.html = null;
			this.grabee = data;
		}
		else if ($(data) != null) {
			this.html = null;
			this.grabee = $(data);
		}
		else {
			this.html = data;
			this.grabee = new Element('span');
		}
		this.unique = function() {
			if (this.target.getSibling('span') != null && this.target.getSibling('span').hasClass('poppermcpoppy')) {
				this.target.getSibling('span').dispose();
				return false;
			}
			else if (this.target.getParent('span') != null && this.target.getParent('span').hasClass('poppermcpoppy')) {
				this.target.getParent('span').dispose();
				return false;
			}			
			return true;
		};
		if (!this.unique()) return false;
		if (this.target.getParent('a') !== null) this.target.getParent('a').setStyle('text-decoration','none');
		new Element('span',{'style':this.css,'class':this.cssClass,'html':this.html}).inject(this.target,'after').adopt([
			this.grabee,
			new Element('p',{'style':'text-align:center'}).grab(new Element('a',{'href':'#','html':'(close)','style':'color:#fff;font-weight:normal;line-height:2em','events':{'click':function(e){
				e.getTarget().getParent('span').dispose();
				e.stop();
			}}}))
		]);				
		document.onclick = function() {
			document.onclick = null;
			this.target.getSibling('span').dispose();
		}.bind(this);			
	}
	catch(err) {
		if (err == 'e') console.error('class PopUp: constructor: first parameter must be an Event.');
		else if (err == 'data') console.error('class PopUp: constructor: second parameter must be an element or an id or a string.');
		else if (err == 'options') console.error('class PopUp: constructor: third parameter must be an object.');
		else if (err == 'style') console.error('class PopUp: constructor: Leave "style=" out of addStyle.');
		else console.error('class PopUp:' + err);
	}
}

//ignores leading and trailing delimiters
//3rd param is how many tokens to skip initially
//nextToken can return multiple tokens at once, including delimiters
//new StringTokenizer("1-2-3-4",'-',2).nextToken(2) == '3-4'
//native js
function StringTokenizer(str,del,start) {
	try {
		if (del === undefined || str === undefined) throw('constructor');
		this.str = str + '';
		this.del = del;
		this.i = 0;	
		if (this.str.indexOf(this.del) == 0) this.str = this.str.slice(this.del.length);	
		this.nextToken = function(tokens) {
			if (tokens === undefined) tokens = 1;
			var str = "";
			while(tokens--) {
				var j = this.str.indexOf(this.del,this.i);
				if (j == -1 && this.i >= this.str.length) return str;
				else if (j == -1) j = this.str.length;
				if (tokens == 0) str += this.str.substring(this.i,j);
				else str = str + this.str.substring(this.i,j) + this.del;
				this.i = j + this.del.length;	}
			return str;
		};
		this.hasNext = function() {
			var j = this.str.indexOf(this.del,this.i);
			if (j == -1 && this.i >= this.str.length) return false;
			return true;
		};
		if (start !== undefined) this.nextToken(start);
		if (!this.hasNext()) delete this;	
	}
	catch (err) {if (err == 'constructor') console.error('class StringTokenizer: constructor format is: string,delimiter,[number of skipped tokens]')}
}String.prototype.getToken = function(del,start,tokens) {return new StringTokenizer(this,del,start).nextToken(tokens)};

function defaultFor(p,d) {
	if (!$chk(p)) p = d;
	return p;
}

Element.prototype.getSibling = function(type,num) {
	num = defaultFor(num,0);
	var index = 0;
	var sibling = null;
	this.getSiblings().each(function(el){
		if (el.getTag() == type && num == index && sibling === null) sibling = el;
		else if (el.getTag() == type) index++;
	});
	return sibling;
};

Element.prototype.getSiblings = function() {return this.getParent().getChildren()}
Element.prototype.getTag = function() {return this.tagName.toLowerCase()};
Element.prototype.hasTag = function(tag) {return (this.tagName.toLowerCase() == tag.toLowerCase())};

Event.prototype.getTarget = function() {
	if (Browser.Engine.trident) return new Element(this.srcElement);
	return new Element(this.target);
}

