/*
*********************************************************
 Copyright (c) 2007 CiderHouse. All Rights Reserved.
*********************************************************
*/

var $OBSERVERS_LIST = new Array();
	
Event = 
{
	
	Remove:function(obj,name,func,capture) 
	{
		if(func == null) { return true; }
		if (obj.removeEventListener) 
		{ 
			obj.removeEventListener(name,func,capture); 
			return true; 
		}
		if (obj.detachEvent) 
		{
			return (obj.detachEvent("on" + name,func));
		}
		return false;
	},
	// returns the correct event
	GetEvent:function(e) 
	{
		return e || window.event;
	},
	// stop the event propagation from event e
	Stop:function(e) 
	{
		if(!e) { return false; }
		var e = Event.GetEvent(e);
		e.cancelBubble = true;
		if (e.preventDefault) {e.preventDefault();}
		if (e.stopPropagation) {e.stopPropagation();}
	},
	// returns the element that fired the event e
	GetElement:function(e,name) 
	{
		if(!e) { return null; }
		var e = Event.GetEvent(e);
		var obj = !e.target ? e.srcElement : e.target;
		
		if (typeof(name) != "undefined") 
		{
			while(obj.parentNode && obj.nodeName.toLowerCase() != "label") 
			{
				obj = obj.parentNode;
			}
		}
		return obj;
	},
	// attaches an event listener to an element obj
	Observe:function(obj,name,func,capture) 
	{
		var success = false;
		if (obj.addEventListener) 
		{
			obj.addEventListener(name,func,capture); 
			success = true;
		}
		if (obj.attachEvent) 
		{
			obj.attachEvent("on" + name,func); 
			success = true;
		}
		if (success) 
		{
			$OBSERVERS_LIST.push({Object:obj,Name:name,Function:func,Capture:capture});
			return true;
		}
		return false;
	},
	Mouse:
	{
		// get position
		Position:function(e) 
		{
			var e = Event.GetEvent(e);
			var pos = {x:0,y:0};
			
				if (e.pageX || e.pageY)	
				{
					pos.x = e.pageX;
					pos.y = e.pageY;
				}
				else if (e.clientX || e.clientY) 	
				{
					pos.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
					pos.y = e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
				}
			return pos;
		}
	}
}

	
	
	
	//Dragging part of the drag/drop functionality of the popup
	StartDrag = function(e) 
	{
		
		Event.Stop(e);
		if (this.DragHandler) 
		{
			Event.Remove(document,"mousemove",this.DragHandler,false);
		}
		if (this.StopDragHandler) 
		{
			Event.Remove(document,"mouseup",this.StopDragHandler,false);
		}
		
		this.StopDragHandler = this.StopDrag.bind(this);
		this.DragHandler = this.Drag.bind(this);
		
		var popup = document.getElementById("PopupStory");//$class("popupHolder",$id("popupContainer_" + this.Id),"div")[0];
		popup.style.left = GetElementPosition(popup).x;
		popup.style.top = GetElementPosition(popup).y
		popup.style.marginLeft = 0;
		
		
		DragOffset.x = Event.Mouse.Position(e).x - GetElementPosition(popup).x;
		DragOffset.y = Event.Mouse.Position(e).y - GetElementPosition(popup).y;
		
		Event.Observe(document,"mouseup",StopDragHandler,false);
		Event.Observe(document,"mousemove",DragHandler,false);
	}

//Dragging part of the drag/drop functionality of the popup
	Drag = function(e) 
	{
		Event.Stop(e);
		
		var popup = document.getElementById("PopupStory");//$class("popupHolder",$id("popupContainer_" + this.Id),"div")[0];
		
		if (popup) 
		{
			var popupLeft = Event.Mouse.Position(e).x - (this.DragOffset.x);
			var popupTop = Event.Mouse.Position(e).y - (this.DragOffset.y);
			
			var popupWidth = popup.offsetWidth;
			var popupHeight = popup.offsetHeight;
			
			var popupRight = popupLeft + popupWidth;
			var popupBottom = popupTop + popupHeight;
			
			var pageWidth = document.documentElement.clientWidth;
			var pageHeight = document.documentElement.clientHeight;
			
			var pageScrollTop = document.documentElement.scrollTop;
			var pageScrollLeft = document.documentElement.scrollLeft;
			
			var inUpperSide = (popupTop + Math.round(popupHeight/2)) <= (pageScrollTop + Math.round(pageHeight/2));
			var inLeftSide = (popupLeft + Math.round(popupWidth/2)) <= (pageScrollLeft + Math.round(pageWidth/2));
			
			if (popupLeft >= pageScrollLeft && popupRight <= pageScrollLeft + pageWidth) 
			{
				popup.style.left = popupLeft + "px";
			}
			else 
			{
				popup.style.left = (inLeftSide ? pageScrollLeft : pageScrollLeft + pageWidth - popupWidth) + "px";
			}		
			if (popupTop >= pageScrollTop && popupBottom <= pageScrollTop + pageHeight)
			{
				popup.style.top = popupTop + "px";
			}
			else 
			{
				popup.style.top = (inUpperSide ? pageScrollTop : pageScrollTop + pageHeight - popupHeight) + "px";
			}
		}
		
		return false;
	}

//Dropping part of the drag/drop functionality of the popup
StopDrag = function(e) 
{
	Event.Stop(e);
	Event.Remove(document,"mousemove",this.DragHandler,false);
	Event.Remove(document,"mouseup",this.StopDragHandler,false);
}
	
	var DragOffset = function() {};
	DragOffset.x = 0; 
	DragOffset.y = 0;
	
	
	function GetElementPosition(obj) 
	{
			var pos = {x:0,y:0};
			if (obj.offsetParent) 
			{
				pos.x = obj.offsetLeft;
				pos.y = obj.offsetTop;
				
				while(obj = obj.offsetParent) 
				{
					pos.x += obj.offsetLeft;
					pos.y += obj.offsetTop;
				}
				
			}
			return pos;
	}
	
	//Bind the THIS scope to a function
	Function.prototype.bind = function(obj) 
	{
		var method = this;
		temp = function() 
		{
			return method.apply(obj, arguments);
		};
		return temp;
	}

this.StartDragHandler = this.StartDrag.bind(this);
		
		
