/*
	Template Fission - ProVision

	No part of this file can be reused or redistributed without permission from ProVision.
	Any forum found using this who isn't a paid customer will be reported to ProBoards.

	Copyright © ProVision 2006
	http://templates.proboards1.com
	All rights reserved
*/

// Globlal object to hold globals properties and methods

var GLOBALS = {

	base: "http://teammerk.250free.com/MessageBoard/Fission/",

	split_height: 20,

	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,

	pm_total: 0,
	
	pm_new: 0,

	date: null,

	temp: {},

	patterns: {
		main_page: /\.com(\/|.+cgi(\?|#.+|\?\w+=home)?)?$/i,
		profile: /=viewprofile(&user=(\w+))?$/i,
		thread: /=(gotopost|display)/i,
		board: /\?board=\w+/i
	},

	tag: function(tag, node, num, num2){
		if(tag){
			if(node){
				return document.getElementsByTagName(node).item(num).getElementsByTagName(tag).item(num2);
			}

			return document.getElementsByTagName(tag);
		}
	
		return false;
	},

	id: function(id){
		if(id){
			return document.getElementById(id);
		}

		return false;
	},

	name: function(name){
		if(name){
			return document.getElementsByName(name);
		}

		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: 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;
	},

	node_text: function(obj){
		if(typeof obj.textContent != "undefined"){
			return obj.textContent;
		} else if(typeof obj.innerText != "undefined"){
			return obj.innerText;
		} else {
			alert("Error nodeText");
		}
	},

	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){
			var script = document.createElement("script");

			script.type = "text/javascript";
			script.src = this.base + "scripts/" + file + ".js";

			document.getElementsByTagName("head").item(0).appendChild(script);
		}
	},

	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;
	},

	write_storage_objs: function(){
		var storage_objs = "";

		storage_objs += '<input type="hidden" id="pb_db_avurl" class="userData" />';
		storage_objs += '<input type="hidden" id="pb_db_posts" class="userData" />';
		storage_objs += '<input type="hidden" id="pb_db_lv" class="userData" />';
		storage_objs += '<input type="hidden" id="pb_db_lp" class="userData" />';
	
		document.write(storage_objs);
	},

	set_welcome_table_info: function(){
		var avatar = this.load_value("pb_db_avurl");

		var url = null;
		var width = height = "";

		if(avatar){
			avatar = eval("(" + avatar + ")");

			if(avatar.url){
				url = avatar.url;
			}

			if(avatar.width && avatar.height){
				width = avatar.width;
				height = avatar.height;
			}
		}

		if(url && !url.match(/\/blank\./i)){
			var avatar_img = this.element("img", {
				attr: {
					src: url,
					width: width,
					height: height
				}
			});

			this.id("avatar").appendChild(avatar_img);
		} else {
			this.id("avatar").appendChild(document.createTextNode("No Avatar"));
		}

		this.id("pm_total").appendChild(document.createTextNode(this.pm_total));
		this.id("pm_new").appendChild(document.createTextNode(this.pm_new));
		
		if(this.date){
			this.id("current_date").appendChild(document.createTextNode(this.date));
		}

		this.id("welcome_name").appendChild(document.createTextNode(this.displayname));

		var posts = this.load_value("pb_db_posts");

		if(posts){
			this.id("posts_total").appendChild(document.createTextNode(posts));
		} else {
			this.id("posts_total").appendChild(document.createTextNode("0"));
		}

		var last_viewed = this.load_value("pb_db_lv");
		var last_posted = this.load_value("pb_db_lp");

		if(last_viewed){
			var viewed = eval("(" + last_viewed + ")");
			var posted = eval("(" + last_posted + ")");

			if(viewed && viewed.thread_link && viewed.thread_title && posts && parseInt(posts) > 0){
				GLOBALS.id("welcome_body").innerHTML = "The last topic you viewed was in the board <a href='" + viewed.board_link + "'>" + viewed.board_name + "</a>, titled <a href='" + viewed.thread_link + "'>" + viewed.thread_title + "</a>.<br /><br />";

				if(posted && posted.thread_link && posted.thread_title){
					GLOBALS.id("welcome_body").innerHTML += "The last topic you posted in was in the board <a href='" + posted.board_link + "'>" + posted.board_title + "</a>, titled <a href='" + posted.thread_link + "'>" + posted.thread_title + "</a>.";
				} else if(parseInt(posts) == 0){
					GLOBALS.id("welcome_body").innerHTML += "It appears that you have not made any posts on our forum.  Why not take a bit of time to create a new topic to introduce yourself to other members of our community?";
				} else {
					GLOBALS.id("welcome_body").innerHTML += "It appears that you have made posts on our forum, but there is no information to show at this time.";
				}
			} else {
				GLOBALS.id("welcome_body").innerHTML = "It appears that you have not viewed any topics or made any posts on our forum.<br /><br />Why not take a bit of time to create a new topic to introduce yourself to other members of our community?";
			}
		} else {
				GLOBALS.id("welcome_body").innerHTML = "It appears that you have not viewed any topics or made any posts on our forum.<br /><br />Why not take a bit of time to create a new topic to introduce yourself to other members of our community?";
		}
	}
};

GLOBALS.write_storage_objs();

({

	prep_header: function(){
		var welcome_table = GLOBALS.welcome_table = GLOBALS.tag("table").item(0);

		welcome_table.bgColor = welcome_table.className = "";
	}

}).prep_header();

// Lets build the header

var build_header = {

	pm_info_img: false,

	pm_img: "nonewpm",

	pm_left: "left",

	pm_right: "right",

	has_pm_data: false,

	build_it: function(){
		var welcome_table = GLOBALS.welcome_table;
		var header_table = GLOBALS.tag("table").item(3);
		var header_cell = header_table.insertRow(0).insertCell(0);
		var gap_cell = header_table.insertRow(2).insertCell(0);
		var pm_cell = header_table.insertRow(3).insertCell(0);

		this.pm_notify_check();

		var left = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + this.pm_left + ".jpg"
			}
		});

		var middle = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + "banner.gif"
			}
		});

		var right = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + this.pm_right + ".jpg"
			}
		});

		var pm_info_i;

		if(this.pm_info_img){
			pm_info_i = "inbox";

			var pm_info_l = GLOBALS.element("img", {
				att: {
					src: GLOBALS.base + "pminfoleft.jpg"
				}
			});

			
		} else {
			pm_info_i = "pminfo";
		}

		var pm_info = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + pm_info_i + ".jpg"
			}
		});

		if(this.pm_info_img){
			GLOBALS.addEvent(pm_info, "click", function(){
				location.href = "/index.cgi?action=pm";
			});

			GLOBALS.addEvent(pm_info, "mouseover", function(){
				this.style.cursor = "pointer";
				this.title = "Inbox";
			});
		}

		var pm = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + this.pm_img + ".jpg"
			}
		});

		var gap = GLOBALS.element("img", {
			att: {
				src: GLOBALS.base + "gap.jpg"
			}
		});

		header_cell.appendChild(left);
		header_cell.appendChild(middle);
		header_cell.appendChild(right);

		header_cell.height = "108";
		header_cell.align = gap_cell.align = "center";

		gap_cell.appendChild(gap);

		if(this.pm_info_img){
			pm_cell.appendChild(pm_info_l);
		}

		pm_cell.appendChild(pm_info);

		if(this.pm_info_img){
			pm_info_i = "inbox";

			var pm_info_o = GLOBALS.element("img", {
				att: {
					src: GLOBALS.base + "outbox.jpg"
				}
			});

			var pm_info_p = GLOBALS.element("img", {
				att: {
					src: GLOBALS.base + "preferences.jpg"
				}
			});

			var pm_info_r = GLOBALS.element("img", {
				att: {
					src: GLOBALS.base + "pminforight.jpg"
				}
			});

			GLOBALS.addEvent(pm_info_o, "click", function(){
				location.href = "/index.cgi?action=pm&view=2";
			});

			GLOBALS.addEvent(pm_info_p, "click", function(){
				location.href = "/index.cgi?action=pmprefs";
			});

			GLOBALS.addEvent(pm_info_p, "mouseover", function(){
				this.style.cursor = "pointer";
				this.title = "Preferences";
			});
	
			GLOBALS.addEvent(pm_info_o, "mouseover", function(){
				this.style.cursor = "pointer";
				this.title = "Outbox";
			});

			pm_cell.appendChild(pm_info_o);
			pm_cell.appendChild(pm_info_p);
			pm_cell.appendChild(pm_info_r);
		}

		var title, link;

		if(this.has_pm_data){
			link = "index.cgi?action=pm";
			title = "Check Your Messages";
		} else {
			link = "index.cgi?action=login";
			title = "Please Login To Check Your Messages";
		}

		GLOBALS.addEvent(pm, "click", function(){
			location.href = link;
		});

		GLOBALS.addEvent(pm, "mouseover", function(){
			this.style.cursor = "pointer";
			this.title = title;
		});

		pm_cell.appendChild(pm);
		
		pm_cell.align = "center";
		pm_cell.height = "41";

		header_table.cellPadding = "0";

		this.do_blur();
	},

	do_blur: function(){
		var links = GLOBALS["welcome_table"].getElementsByTagName("a");

		for(l = 0; l < links.length; l ++){
			links.item(l).onfocus = function(){
				this.blur();
			}
		}
	},

	pm_notify_check: function(){
		if(GLOBALS.username != "Guest" && GLOBALS.displayname != "Guest"){
			var pm_cell = GLOBALS.tag("td").item(2);

			if(GLOBALS.node_text(pm_cell).match(/(\d+) messages?, (\d+) (is|are) new\./i)){
				this.has_pm_data = true;
				GLOBALS.pm_total = RegExp.$1;
				GLOBALS.pm_new = RegExp.$2;
				GLOBALS.date = GLOBALS.node_text(pm_cell).split(" new.")[1];
				if(RegExp.$2 > 0){
					this.pm_img = "newpm";
					this.pm_left = "left_new";
					this.pm_right = "right_new";
				} else {
					this.pm_img = "nonewpm";
				}
			} else {
				this.pm_img = "nonewpm";
			}

			this.pm_info_img = true;
		} else {
			this.pm_img = "pm";
		}
	}
};

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;
}