

	(function( pb, $)
	{
		pb.popupWindow = function()
		{
			this.popupObject = null;
			this.isOpen = false;
			this.currentObject = null;
			this.tickerBarObject = null;
		}
		
		pb.popupWindow.prototype = 
		{
				register : function( popupObject )
				{
					if( typeof( popupObject ) != "object")
					{
						alert("PopupObject is not an object");
						return false;
					}
					
					this.popupObject = popupObject;
					
					delete popupObject;
					
					return
				}
				
			,	setEvents : function()
				{
					var	thisObject = this;
					
						$("div#timeline").find("li a").bind("mousedown", function(){ return false; } );
						$("div#timeline").find("li a").bind("click", function()
						{
							thisObject.currentObject = $(this).parent();
							thisObject.openPopup( $(this), thisObject );
						
							return false;
						});
						
						thisObject.popupObject.find("a#close").bind("mouseover", function(){ $(this).addClass("close-over"); return false; } );
						thisObject.popupObject.find("a#close").bind("mouseout", function(){ $(this).removeClass("close-over"); return false; } );
						thisObject.popupObject.find("a#close").bind("mousedown", function(){ $(this).removeClass("close-over").addClass("close-down"); thisObject.closeButton(); return false; } );
						thisObject.popupObject.find("a#close").bind("click", function()
						{
							$(this).removeClass("close-down");
							thisObject.hidePopup();		
							
							return false;
						});
					
						thisObject.popupObject.find("a.navigate-left").bind("mousedown", function(){ $(this).addClass("navigate-left-down"); thisObject.closeButton(); return false; } );
						thisObject.popupObject.find("a.navigate-left").bind("click", function()
						{
							var	itemId	= ((thisObject.currentObject.attr("id").split("_")[1]*1)+1);
							var thisEventObject = $("li#item_"+itemId+" a");
							
							if(thisEventObject.get(0))
							{
								if(thisObject.openPopup( thisEventObject, thisObject ))
									thisObject.currentObject = $("li#item_"+itemId);
							}
							
							delete itemId, thisEventObject;
							
							return false;
						});
					
						thisObject.popupObject.find("a.navigate-right").bind("mousedown", function(){ $(this).addClass("navigate-right-down"); thisObject.closeButton(); return false; } );
						thisObject.popupObject.find("a.navigate-right").bind("click", function()
						{
							var	itemId	= ((thisObject.currentObject.attr("id").split("_")[1]*1)-1);
							var thisEventObject = $("li#item_"+itemId+" a");
							
							if(thisEventObject.get(0))	
							{
								if(thisObject.openPopup( thisEventObject, thisObject ))
									thisObject.currentObject = $("li#item_"+itemId);
							}
							
							delete itemId, thisEventObject;
							
							return false;
						});
						
					delete thisObject;
						
					return;
				}
				
			,	setTickerBar : function( tickerBarObject )
				{
					if( typeof( tickerBarObject ) != "object")
					{
						alert("TickerBarObject is not an object");
						return false;
					}
					
					this.tickerBarObject = tickerBarObject;
					
					delete tickerBarObject;
					
					return
				}
			
			,	openPopup : function( thisEventObject, thisObject )
				{
					if(thisEventObject.hasClass("ticker"))
					{
						$(".top-middle-slurf").removeClass("top-middle-slurf");
						$(".bottom-middle-slurf").removeClass("bottom-middle-slurf");
						
						thisObject.popupObject.find("#navigate").hide();
						thisObject.popupObject.find(".content").html(thisEventObject.parent().find(".project-info").html());
						thisObject.isOpen = true;
						thisObject.popupObject.css( { left : 75, top:85 } ).show().find(".top-middle").addClass("top-middle-slurf");
						
						return false;
					}
					
					var itemPos = thisObject.getPosition(thisEventObject.get(0));
					var projectPos = thisObject.getPosition($("div#projects").get(0));	
					var	itemScroll = { x:$("div#projects").get(0).scrollLeft, y:$("div#projects").get(0).scrollTop };
					var	popupLeftPos = (itemPos.x-projectPos.x-itemScroll.x-35);

						if(popupLeftPos < 0 || popupLeftPos > $("div#projects").get(0).offsetWidth)
							return false;
					
					var slurfClass = (itemPos.y < 175) ? "top-middle" : "bottom-middle"; 
					
						$(".top-middle-slurf").removeClass("top-middle-slurf");
						$(".bottom-middle-slurf").removeClass("bottom-middle-slurf");
						
						thisObject.popupObject.find(".content").html(thisEventObject.parent().find(".project-info").html());
						
					var popupPos = (itemPos.y < 175) ? (thisEventObject.get(0).offsetHeight+5) : -(85+thisObject.popupObject.show().find(".content").get(0).offsetHeight);
					
						thisObject.popupObject.hide();
						thisObject.popupObject.find("#navigate").show();
						
						if(!thisObject.isOpen)
						{
							thisObject.isOpen = true;
							thisObject.popupObject.css( { left : popupLeftPos, top:(itemPos.y+popupPos) } ).show().find("."+slurfClass).addClass(slurfClass+"-slurf");
						}
						else
						{
							thisObject.popupObject.animate( { left : popupLeftPos, top:(itemPos.y+popupPos) } ).show().find("."+slurfClass).addClass(slurfClass+"-slurf");
						}
						
					delete itemPos, projectPos, itemScroll, slurfClass, popupPos, popupLeftPos;
					
					return true;
				}
				
			,	closeButton : function()
				{	
					var	thisObject = this;
					
					$(document).bind("mouseup", function()
					{
						if(thisObject.tickerBarObject && thisObject.currentObject.find("a").hasClass("ticker"))
							thisObject.tickerBarObject.stopScroll( false );
				
						$(this).find(".navigate-left-down").removeClass("navigate-left-down");
						$(this).find(".navigate-right-down").removeClass("navigate-right-down");
						$(this).find(".close-down").removeClass("close-down");
						$(this).unbind("mouseup");
					});
					
					delete thisObject;
					
					return;
				}
				
			,	hidePopup : function()
				{	
					if(!this.isOpen)
						return;
					
					this.isOpen = false;
					this.popupObject.hide();	
					
					return;
				}
		
			,	getPosition : function ( obj )
				{
					var 	x=0, y=0;
					var 	elm = obj;
					
					while(elm)
					{
						x += elm.offsetLeft + ( !$.browser.opera ? ( parseInt($.css(elm, "borderLeftWidth") ) || 0) : 0);
						y += elm.offsetTop + ( !$.browser.opera ? ( parseInt($.css(elm, "borderTopWidth") ) || 0) : 0);
				
						elm = elm.offsetParent;			
					}
			
					return { x:x, y:y }
				}
		};
		
	})( Pluxbox, jQuery);