var dedPopup = new Class({
     
	//  Options
	mouseover:		false,
	objClass:		"dedPopup",
	objUpClass:		"dedPopupUp",
	titleClass:		"header",
	bodyClass:		"body",
	titleTag:		"h3",
	bodyTag:		"div",
	titleDelimiter:	"::",
	showTransition:	"opacity",
	showTime:		500,
	hideTransition:	"opacity",
	hideTime:		500,
	hideDelay:		500,
	manualPosition:	false,
	top:			false,
	left:			false,
	bottom:			false,
	right:			false,
	offsetX:		0,
	offsetY:		0,
	selfAsTarget:	false,
	fitInWindow:	false,
	showing:		false,
	
	//  Class constructor   
	initialize: function(targetElements, options, popupElement){
		
		//  Set the popupElement to false if not set
		if (arguments.length < 3) {
			popupElement = false;
		}
		
		//  Make sure the target elements is an array
		if ($type(targetElements) != "array") {
			targetElements = [targetElements];
		}
		
		//  Set the variables
		this.targets = targetElements;
		this.obj = popupElement;
		this.setOptions(options);
		
		
		//  Loop through target elements and determine if the elements already exist
		var elementsReady = true;
		for (var i = 0, numTargets = this.targets.length; i < numTargets; ++i) {
			if ((($type(this.targets[i]) == "string") && !$(this.targets[i])) || !this.targets[i]) {
				elementsReady = false;
				break;		
			}
		}
		
		//  If all the target elements exist, proceed immediately; otherwise do it ondomready
		if (elementsReady) {
			this.run();
		} else {
			var thisObj = this;
			window.addEvent("domready", function() { thisObj.run(); });
		}
		
	},
	
	//  Set options
	setOptions: function(options) {
		for (i in options) {
			this[i] = options[i];
		}
	},
	
	//  Run
	run: function() {
	
		//  If the popup element was not passed, create a popup element and append it to the body
		if (!this.obj) {
		
			//  Get the content from the tag's longdesc or title
			var content = this.getContent();
			
			//  Create the popup object
			this.createObj(content);
			
			//  Append the popup to the body
			document.getElementsByTagName("body")[0].appendChild(this.obj);
			
		} else {
			this.obj = new Element(this.obj);
		}
		
		//  Save the popup's height for future reference and hide the element
		this.objHeight = this.obj.getSize().size.y;
		this.obj.setStyle("display", "none");
		
		//  Add the popup itself as a target element if option is set
		if (this.selfAsTarget) {
			this.targets.push(this.obj);
		}
		
		//  Loop through target elements and apply events
		for (var i = 0, numTargets = this.targets.length; i < numTargets; ++i) {
		
			//  Convert the element to a moo element
			this.targets[i] = new Element(this.targets[i]);
		
			//  Apply toggle event only if the element exists
			if (this.targets[i]) {
				if (this.mouseover) {
					this.applyMouseEvents(this.targets[i]);
				} else {
					this.applyClickToggle(this.targets[i]);
				}
			}
		
		}
		
		//  Position the popup if the manual position isn't set
		if (!this.manualPosition) {
			this.positionObj();
		}
		
		//  Create the transition effects
		this.createTransitionEffects();
		
		//  Save a list of all other popups for future reference
		this.allPopups = $$("." + this.objClass);
		
	},
	
	//  Get the content from the first target element
	getContent: function() {
		if (!this.targets[0].getProperty) {
			this.targets[0] = new Element(this.targets[0]);
		}
		if (this.targets[0].getProperty("longdesc")) {
			return this.targets[0].getProperty("longdesc");
		} else if (this.targets[0].getProperty("title")) {
			return this.targets[0].getProperty("title");
		} else {
			return "";
		}
	},
	
	//  Create the object
	createObj: function(content) {
	
		//  Create the object
		this.obj = new Element(document.createElement("div"));
		this.obj.addClass(this.objClass);
		
		//  If the title delimiter is present split the content up; otherwise dump it all in
		if (content.indexOf(this.titleDelimiter) > -1) {
			
			//  Create the header
			var headerObj = new Element(document.createElement(this.titleTag));
			headerObj.addClass(this.titleClass);
			headerObj.setHTML(content.substr(0, content.indexOf(this.titleDelimiter)));
			
			//  Create the body
			var bodyObj = new Element(document.createElement(this.bodyTag));
			bodyObj.addClass(this.bodyClass);
			bodyObj.setHTML(content.substr(content.indexOf(this.titleDelimiter) + this.titleDelimiter.length));
			
			//  Add the header and the body to the popup
			this.obj.appendChild(headerObj);
			this.obj.appendChild(bodyObj);
			
		} else {
			this.obj.setHTML(content);
		}
		
	},
	
	//  Apply click toggle
	applyClickToggle: function(obj) {
		var thisObj = this;
		obj.onclick = function() {
			if (thisObj.showing) {
				thisObj.hide();
			} else {
				thisObj.show();
			}
		};
	},
	
	//  Apply mouse events
	applyMouseEvents: function(obj) {
		var thisObj = this;
		obj.onmouseover = function() { thisObj.show(); };
		obj.onmouseout = function() { thisObj.hide(); };
	},
	
	//  Position the popup
	positionObj: function() {
	
		//  Make the popup absolutely positioned
		this.obj.setStyle("position", "absolute");
		
		//  If there are offsets, position the popup relative to the first target element
		if ((this.offsetX > 0) || (this.offsetY > 0)) {
		
			//  Find the x and y coordinates of the first target element
			this.targetCoords = this.targets[0].getCoordinates();
		
			//  Save coordinates for future use
			this.top = this.targetCoords.top + this.targetCoords.height + this.offsetY;
			this.left = this.targetCoords.left + this.offsetX;
			this.upTop = this.targetCoords.top - this.offsetY - this.objHeight;
			
			//  Set initial position
			this.obj.setStyle("top", this.top);
			this.obj.setStyle("left", this.left);
		
			//  Get additional properties of the window for future reference
			this.windowHeight = window.getHeight();
			
			//  Calculate the necessary height to display the popup below the target element
			this.requiredVerticalSpace = this.targetCoords.height + this.offsetY + this.objHeight;
		
		//  Otherwise, position the popup according to the coordinates passed in
		} else {
			
			var coords = ["top", "left", "bottom", "right"];
			for (var i = 0, numCoords = coords.length; i < numCoords; ++i) {
				if (this[coords[i]]) {
					this.obj.setStyle(coords[i], this[coords[i]]);
				}
			}
		
		}
		
	},
	
	//  Create transition effects
	createTransitionEffects: function() {
		var thisObj = this;
		this.showEffect = this.obj.effect(this.showTransition, {duration: this.showTime, onStart: function() {thisObj.obj.setStyle("display", "block");} });
		this.hideEffect = this.obj.effect(this.hideTransition, {duration: this.hideTime, onComplete: function() {thisObj.obj.setStyle("display", "none");} });
	},
	
	//  Show it
	show: function() {
		$clear(this.hideTimeout);
		for (var i = 0, numPopups = this.allPopups.length; i < numPopups; ++i) {
			if (this.allPopups[i] != this.obj) {
				this.allPopups[i].setStyle("display", "none");	
			}
		}
		this.hideEffect.stop();
		if (!this.showing) {
			if (this.fitInWindow) {
				var distanceToTop = this.targetCoords.top - window.getScrollTop();
				var distanceToBottom = this.windowHeight - distanceToTop - this.targetCoords.height;
				if (distanceToTop > distanceToBottom) {
					this.obj.addClass(this.objUpClass);
					this.obj.setStyle("top", this.upTop);
				} else {
					this.obj.removeClass(this.objUpClass);
					this.obj.setStyle("top", this.top);
				}
			}
			this.showEffect.start(0, 1);
			this.showing = true;
		}
	},
	
	hide: function() {
		var thisObj = this;
		this.hideTimeout = (function() {
			thisObj.hideEffect.start(1, 0);
			thisObj.showing = false;
		}).delay(this.hideDelay, this)
	}

});

var dedTip = dedPopup.extend({
	
	//  Options
	objClass:	"dedTip",
	objUpClass:	"dedTipUp",
	
	//  Class constructor   
	initialize: function(targetElements, options, tipElement){
		
		//  Make sure there are values for optional
		if (arguments.length < 3) {
			tipElement = false;
			if (arguments.length < 2) {
				options = {};	
			}
		}
		
		//  By default the tip shows onmouseover
		options.mouseover = true;
		if (options.click) {
			options.mouseover = false;
		}
		if (!options.objClass) {
			options.objClass = this.objClass;	
		}
		if (!options.objUpClass) {
			options.objUpClass = this.objUpClass;	
		}
		
		//  Call the parent class constructor
		this.parent(targetElements, options, tipElement);
	
	}

});

window.addEvent("domready", function() {
	var itineraries = $("itineraries");
	if (itineraries) {
		var titles = $("itineraries").getElements("dt");
		for (var i = 0, numTitles = titles.length; i < numTitles; ++i) {
			//var img = titles[i].getNext().getElement("img");	
			var tip = new dedTip([titles[i]], { 
				selfAsTarget: true,
				fitInWindow: true,
				offsetX: 350
			});
		}
	}
});