/*------------------------- --------*\
|		P M Notifier v1.5
|	(C) Simie 2006 - Present
| Do not repost without permission
\*--------------------------------- */

var PM_Notifier = {
	gotIDs: Array(), // Will cache document.getElementById
	td: document.getElementsByTagName("td"), // Used in the code, might as well have it as a global var
	newPMs: 0, // Will hold the number of new PMs
	animProg: false, // True: Animation in progress; False: Idle
	boxWidth: 350, // Width of the box - Not a good idea to change below this - MUST be a multiple of 4
	enableOverlay: false, // Enable overlay or not
	overlayOpacity: 70, // Opacity of the page overlay
	
	$: function(id){
		if(!this.gotIDs.isKey(id)){
			// Check if it is cached, if not, cache it
			this.gotIDs[id] = document.getElementById(id);
			this.gotIDs[id].prototype = [];
			this.gotIDs[id].show = function(){
				// Show "Object"
				
				this.style.display = "";
			}
			this.gotIDs[id].hide = function(){
				// Hide "Object"
				
				this.style.display = "none";
			}
		}
		return this.gotIDs[id]; // Always get from cache
	},
	
	goNotify: function(){
		
		// Some functions used later in the code...
		

		Array.prototype.isKey = function(input){
			// Find if something is in an array.
			
			
			for(key in this){
				
				// Loop through array.
				
				if(key === input)

					// Use "===" to make sure it is EXACTLY the same.
					
					return true; // Yep, 'tis here.
					
			}
			
			return false; // Nothing here...
		}
		
		// End functions
		
		if(Overlay == true)
			this.enableOverlay = true;
		
		this.newPMs = (this.td[2].innerHTML.match(/(\d+)\s(are|is)\s/i))? RegExp.$1 : false; // Get how many new PMs there are
		if(this.newPMs != false && this.newPMs > 0){
			// If there are PMs, show the flashing image
			this.$('pmHolder').show();
		  	setTimeout('PM_Notifier.doFlash(0)', 1000); // Flash it
		}
	},
	doFlash: function(mode){
		// Check which mode to do...
		switch(mode){
			case 0: // Hides the icon, sets timeout for showing it
				this.animProg = true; // No more animations while this is running...
				this.$('pmHolder').hide();
				setTimeout('PM_Notifier.doFlash(1)', 500);
				break;
			case 1: // Shows it again
				this.$('pmHolder').show();
				this.animProg = false; // Allow more animations
			  	if(!document.cookie.match(/PMshown\=1/) && !location.href.match(/tion=pm/i)){
			  		// If the cookie says yes, show the full box
			  		setTimeout('PM_Notifier.doExpand(false)', 500);
				}
				break;
		}
	},
	doExpand: function(userOpen, timeOuting){
		if(this.animProg == false || timeOuting){ // Check if another animation is in progress
			this.animProg = true; // If it's not, set is as true so no other animation can disturb this one
			var moreNeeded = true; // Set the variable that defines whether we need more time to expand the box
		  	var holder = this.$('pmHolder'); // Get the div we are going to expand
		  	var holderParent = holder.parentNode; // This will centre the box in the middle of the screen
		  	var width = holder.style.width; // Get its current width
		  	var height = holder.style.height; // Get its current height
		  	var top = holderParent.style.top; // Get its distance from the top (%)
		  	var right = holderParent.style.right; // Get its distance from the right (%)
		  	var relTop = holder.style.top; // Get its relative distance from the top
		  	var relRight = holder.style.right; // Get its relative distance from left
		  	
		  	if(this.enableOverlay) {
				var pageOverlay = this.$('pageOverlay'); // Get the page overlay...
				if(!timeOuting){
					var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
					pageOverlay.style.height = _docHeight + 'px';
				}
				var curOpacity = parseFloat(pageOverlay.style.opacity);
				var curMsieOpacity = curOpacity * 100;
				var nxtDecOpacity = curOpacity + 0.05;
				if(!timeOuting)
					pageOverlay.show();
				if(curOpacity < (this.overlayOpacity / 100)){
					pageOverlay.style.opacity = curOpacity + 0.05;
					pageOverlay.style.MozOpacity = curOpacity + 0.05;
					pageOverlay.style.filter = "alpha(opacity="+(curMsieOpacity+5)+")";
				}
		  	}
		  	if(parseInt(top) < 50){
				holderParent.style.top = parseInt(top) + 1 + '%';
				holderParent.style.right = parseInt(right) + 1 + '%';
				moreNeeded = true;
			} else {
				moreNeeded = false;
			}
			
			if(parseInt(relTop) > -50){
				holder.style.top = parseInt(relTop) - 2 + 'px';
				moreNeeded = true;
			} else {
				moreNeeded = false;
			}
			if(parseInt(relRight) > -(this.boxWidth / 2)){
				holder.style.right = parseInt(relRight) - 2 + 'px';
				moreNeeded = true;				
			} else {
				moreNeeded = false;
			}
		  	if(parseInt(width) < this.boxWidth){
		  		holder.style.width = parseInt(width)+4+'px';
				moreNeeded = true;
			} else
				moreNeeded = false
				
			if(moreNeeded)
				setTimeout('PM_Notifier.doExpand(false, true)', 1);
			else {
				this.addInfo();
				this.$('theA').onclick = function(){ PM_Notifier.doCollapse(1); };
				this.animProg = false;
			}
		}
	},
	addInfo: function(){
		if(this.newPMs != false && this.newPMs > 1){
			var theText = "You have "+this.newPMs+" new messages!";
		} else {
		  	var theText = "You have "+this.newPMs+" new message!";
		}
		this.$('pmNot_newPms').innerHTML = theText;
		this.$("pmNot_theInfo").show(1);
	},
	doCollapse: function(doRemove, noShow, timeOuting){
		if(this.animProg == false || timeOuting){
			if(doRemove)
				this.$('pmNot_theInfo').hide();
			if(noShow)
				document.cookie = 'PMshown=1';
			var moreNeeded = true;
		  	var holder = this.$('pmHolder');
		  	var holderParent = holder.parentNode; // This will centre the box in the middle of the screen
		  	var width = holder.style.width; // Get its current width
		  	var height = holder.style.height; // Get its current height
		  	var top = holderParent.style.top; // Get its distance from the top (%)
		  	var right = holderParent.style.right; // Get its distance from the right (%)
		  	var relTop = holder.style.top; // Get its relative distance from the top
		  	var relRight = holder.style.right; // Get its relative distance from left
		  	
		  	if(this.enableOverlay) {
				var pageOverlay = this.$('pageOverlay'); // Get the page overlay...

				var curOpacity = parseFloat(pageOverlay.style.opacity);
				var curMsieOpacity = curOpacity * 100;
				var nxtDecOpacity = curOpacity - 0.05;
				if(!timeOuting)
					pageOverlay.show();
				if(curOpacity > 0){
					pageOverlay.style.opacity = curOpacity - 0.05;
					pageOverlay.style.MozOpacity = curOpacity - 0.05;
					pageOverlay.style.filter = "alpha(opacity="+(curMsieOpacity-5)+")";
				}
		  	}
		  	
		  	if(parseInt(top) > 0){
				holderParent.style.top = parseInt(top) - 1 + '%';
				holderParent.style.right = parseInt(right) - 1 + '%';
				moreNeeded = true;
			} else {
				moreNeeded = false;
			}
			
			if(parseInt(relTop) < 0){
				holder.style.top = parseInt(relTop) + 2 + 'px';
				moreNeeded = true;
			} else {
				moreNeeded = false;
			}
			if(parseInt(relRight) < 0){
				holder.style.right = parseInt(relRight) + 2 + 'px';
				moreNeeded = true;				
			} else {
				moreNeeded = false;
			}
		  	if(parseInt(width) > 100){
		  		holder.style.width = parseInt(width)-4+'px';
				moreNeeded = true;
			} else
				moreNeeded = false
				
			if(moreNeeded)
				setTimeout('PM_Notifier.doCollapse(false, true)', 1);
			else {
				this.$('theA').onclick = function(){ PM_Notifier.doExpand(1); };
				this.animProg = false;
				pageOverlay.hide();
			}
		}
	},
	flash: function(){
		document.getElementById('pmHolder').style.display="none";
		setTimeout('flash2()', 500);
	},
	flash2: function(){
	  	document.getElementById('pmHolder').style.display="";
	  	if(!document.cookie.match(/PMshown\=1/)){
	  		setTimeout('expand(1)', 500);
		}
	}
}

if(window.addEventListener){
	window.addEventListener('load', function(){
		PM_Notifier.goNotify()
	}, false);
} else if(window.attachEvent)
	window.attachEvent('onload', function(){
		PM_Notifier.goNotify()
	});
else
	window.onload = function(){
		PM_Notifier.goNotify();
	}