/*
	Copyright © ProVision 2006
	http://templates.proboards1.com
	All rights reserved

	Coding can not be used without permission from the creator
*/

var host_path = "http://eternityonline.org.uk/ProVision/Forum/Coding/PHP/Points/";

var GLOBALS = {

	username: pb_username,

	displayname: pb_displayname,

	logged_in: (this.username != "Guest")? true : false,

	ff: (document.attachEvent)? false : true,

	ff2: (typeof globalStorage != "undefined")? true : false,

	host: location.host,

	temp: {},

	patterns: {
		main_page: /\.com(\/|.+cgi(\?|#.+|\?\w+=home)?)?$/i,
		profile: /=viewprofile(&user=(\w+))?#?$/i,
		thread: /=(gotopost|display)/i,
		board: /\?board=\w+/i
	},

	id: function(id){
		if(id){
			return document.getElementById(id);
		}

		return false;
	},

	addEvent: function(obj, type, fn){
		if(obj.attachEvent){
			obj["e" + type + fn] = fn;
			obj[type + fn] = function(){
				obj["e" + type + fn](window.event);
			}

			obj.attachEvent("on" + type, obj[type + fn]);
		} else {
			obj.addEventListener(type, fn, false);
		}
	},

	element_position: function(eid){
		var off_left = 0; 
		var off_top = 0;
		var off_trail = this.id(eid);

		while(off_trail){
			off_left += off_trail.offsetLeft;
			off_top += off_trail.offsetTop;
			off_trail = off_trail.offsetParent;
		}

		return {
			left: off_left,
			top: off_top
		};
	},

	element: function(elem, attr, frag, br_s, br_e){
		var fragment;
		var elem = (typeof elem == "string")? document.createElement(elem) : elem;
	
		if(frag){
			fragment = document.createDocumentFragment();
		}

		if(br_s){
			fragment = (frag)? fragment : elem;
			fragment.appendChild(GLOBALS.element("br"));
		}

		for(key in attr){
			if(key == "style"){
				for(a in attr[key]){
					elem.style[a] = attr[key][a];
				}
			} else {
				for(a in attr[key]){
					elem.setAttribute(a, attr[key][a]);
				}
			}
		}

		if(frag){
			fragment.appendChild(elem);
		}

		if(br_e){
			fragment = (frag)? fragment : elem;
			fragment.appendChild(GLOBALS.element("br"));
		}

		return (fragment)? fragment : elem;
	},

	get_cookie: function(cook){
		if(cook){
			if(document.cookie.match(new RegExp("\\b" + cook + "=([\\S]+)(;|\\b)"))){
				return decodeURIComponent(RegExp.$1);
			}
		}
	},

	set_cookie: function(name, value, days){
		var date = new Date();

		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		date.toGMTString();

		document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + date;
	},

	save_value: function(db, value){
		if(db && value){
			if(this.ff && this.ff2){
				var pb_storage = globalStorage[GLOBALS.host];

				pb_storage[db] = value;
			} else if(!this.ff){
				this.id(db).setAttribute("pb_pst", value);
				this.id(db).save(db);
			}

			return true;
		}

		return false;
	},

	load_value: function(db){
		var value;

		if(db){
			if(this.ff && this.ff2){
				var pb_storage = globalStorage[GLOBALS.host];

				value = pb_storage[db];
			} else if(!this.ff){
				this.id(db).load(db);
				value = this.id(db).getAttribute("pb_pst");
			}
		}

		return value;
	},

	include: function(file){
		if(file){
			document.write('<script type="text/javascript" src="' + host_path + file + '"><\/script>');
		}
	},

	write_storage_objs: function(){
		var storage_objs = "";
	}
}

function DOMBuffer(element){
	this.elt = (typeof element == "String")? document.getElementById(element) : element;
	this.data = "";
	this.buffer = [];
}

DOMBuffer.prototype.clear = function(){
	this.elt.innerHTML = "";
}

DOMBuffer.prototype.write = function(){
	this.buffer.push.apply(this.buffer, arguments);
}



DOMBuffer.prototype.flush = function(){
	this.elt.innerHTML += this.data = this.buffer.join("");
	this.buffer.length = 0;
}

if(location.href.match(GLOBALS.patterns.main_page) && pb_username != "Guest" && (!GLOBALS.get_cookie("pb_db_qr") || GLOBALS.get_cookie("pb_db_qr") == 1)){
	GLOBALS.include("index.php?action=component&component=points&show=qualify&user=" + pb_username);
}

if(location.href.match(GLOBALS.patterns.main_page)){
	GLOBALS.include("index.php?action=component&component=points&show=top");
}

if(location.href.match(GLOBALS.patterns.profile)){
	var user = GLOBALS.username;

	if(RegExp.$2){
		user = RegExp.$2
	}

	GLOBALS.include("index.php?action=component&component=points&show=profile&user=" + user);
}

