var over = false;


function check(selectObject) {
	if(selectObject.options[selectObject.selectedIndex].value != "")
		document.location.href=(selectObject.options[selectObject.selectedIndex].value);
}

function getPopUp() {
    return document.getElementById("popUpContent");
}

function showPopUp(event) {
    var popUp = getPopUp();
    popUp.style.visibility = "visible";
}

function hidePopUp(event, obj) {
    getPopUp().style.visibility = "hidden";
}

/*function showPopUp(event, text) {
    var popUp = getPopUp();
    var content = "";
    if (text != null && text.length > 0) {
        document.getElementById("popUpContent").innerHTML = text;
    }
    popUp.style.visibility = "visible";
}

function hidePopUp(event, obj) {
    getPopUp().style.visibility = "hidden";
}*/


var Fuhre = {
	methods: {domready: new Array(), load: new Array(), unload: new Array()},
	
	initCore: function() {
		if (typeof DOMAssistant != "undefined") {
			DOMAssistant.DOMReady(Fuhre.onDOMReadyCore);
			var window = $(window);
			window.addEvent("load", Fuhre.onLoadCore);
			window.addEvent("unload", Fuhre.onUnloadCore);
			
			this.addPlugin(this);
		}
	},
	
	onDOMReadyCore: function() {
		Fuhre.runPluginMethods("domready");
	},
	
	onLoadCore: function() {
		Fuhre.runPluginMethods("load");
	},

	onUnloadCore: function() {
		Fuhre.runPluginMethods("unload");
	},

	addPlugin: function(plugin) {
		if (typeof DOMAssistant != "undefined") {
			if (plugin.init) {
				plugin.init();
			}
			this.addMethod(plugin, "domready", plugin.onDOMReady);
			this.addMethod(plugin, "load", plugin.onLoad);
			this.addMethod(plugin, "unload", plugin.onUnload);
		}
	},
	
	addMethod: function(plugin, methodName, method) {
		if (method) {
			this.methods[methodName].push({el: plugin, fn: method});
		}
	},
	
	runPluginMethods: function(methodName) {
		var methodsToRun = this.methods[methodName];
		if (methodsToRun) {
			for (var i = 0; i < methodsToRun.length; i++) {
				methodsToRun[i].fn.call(methodsToRun[i].el);
			}
		}
	},
	
	onDOMReady: function() {},
	
	onLoad: function() {},

	onUnload: function() {},
	
	getClassNameValue: function(el, prefix) {
		var ret = new RegExp(".*" + prefix + "-(.*?)(?:\\s|$).*").exec(el.className);
		if (ret) {
			return ret[1];
		}
		return null;
	},
	
	getAnchoredElement: function(anchor) {
		return $$(anchor.href.replace(/.*\#(.*)/, "$1"));
	},

	getActualHeight: function(elem) {
		var elem = $(elem);
		var contentHeight = Fuhre.stripPx(elem.getStyle("height"));
		var padding = Fuhre.stripPx(elem.getStyle("padding-top")) + Fuhre.stripPx(elem.getStyle("padding-bottom"));
		var margin = Fuhre.stripPx(elem.getStyle("margin-bottom")) + Fuhre.stripPx(elem.getStyle("margin-top"));	
		return contentHeight + padding + margin;
	},
	
	getActualWidth: function(elem) {
		var elem = $(elem);
		var contentWidth = Fuhre.stripPx(elem.getStyle("width"));
		var padding = Fuhre.stripPx(elem.getStyle("padding-left")) + Fuhre.stripPx(elem.getStyle("padding-right"));
		var margin = Fuhre.stripPx(elem.getStyle("margin-left")) + Fuhre.stripPx(elem.getStyle("margin-bottom"));
		return contentWidth + padding + margin;
	},
	
	stripPx: function(str) {
		return parseInt(str.substring(0, str.length - 2));
	}
}
Fuhre.initCore();


/**
 * Event delegation
 */
Fuhre.eventDelegator = {
	events: new Array(),
	
	init: function() {
		$(document).addEvent("click", function(e) {
				if ((e.which && e.which != 1) || (e.button && e.button != 0)) {
					return true;
				}
				var target = e.eventTarget;
				if (target.onclick) {
					return false;
				}
				for (var i = Fuhre.eventDelegator.events.length - 1; i >= 0; i--) {
					var match = Fuhre.eventDelegator.getMatch(target, Fuhre.eventDelegator.events[i].selector);
					if (match != null) {
						var callbackValue = Fuhre.eventDelegator.events[i].fn.call(match, e);
						if (typeof callbackValue === "undefined") {
							callbackValue = true;
						}
						if (callbackValue) {
							DOMAssistant.preventDefault(e);
							return false;
						}
					}
				}
				return true;
			}
		);	
	},
	
	registerClick: function(selector, fn) {
		this.events.push({selector: this.compileSelector(selector), fn: fn});
	},
	
	getMatch: function(el, selector) {
		var matchingEl = null;
		var noOfElms = selector.length - 1;
		var matchedSelector = false;
		for (var j = noOfElms; j >= 0; j--) {
			matchedSelector = false;
			while (!matchedSelector && el.parentNode) {
				var match = true;
				if (selector[j].tag && selector[j].tag != el.tagName) {
					match = false;
				} else if (selector[j].id && selector[j].id != el.id) {
					match = false;
				} else if (selector[j].className && el.className.indexOf(selector[j].className) < 0) {
					match = false;
				}
				if (matchingEl == null && match) {
					matchingEl = el;
				}
				matchedSelector = match;
				el = el.parentNode;
			}
		}
		if (!matchedSelector) {
			matchingEl = null;
		}
		return matchingEl;
	},
	
	compileSelector: function(selector) {
		var compiled = new Array();
		var part = new Array();
		var key = "tag";
		var value = "";
		var ch;
		for (var i = 0; i < selector.length; i++) {
			ch = selector.charAt(i);
			if (i == selector.length - 1) {
				value += ch;
				ch = " ";
			}
			if (ch == " ") {
				part[key] = value;
				if (part.tag) {
					part.tag = part.tag.toUpperCase();
				}
				compiled.push(part)
				part = new Array();
				key = "tag";
				value = "";
			} else if (ch == "#") {
				part[key] = value;
				key = "id";
				value = "";
			} else if (ch == ".") {
				part[key] = value;
				key = "className";
				value = "";
			} else {
				value += ch;
			}
		}
		return compiled;
	}
}
Fuhre.addPlugin(Fuhre.eventDelegator);


/**
 * Slideshow
 */
Fuhre.slideshow = {
	onDOMReady: function() {
		this.hasSlideshow = $$("slideshow") != null;
		if (this.hasSlideshow) {
			Fuhre.eventDelegator.registerClick("#slideshow A", function(e) {
					if ($(this.parentNode).hasClass("do-show-all")) {
						$$("slideshow").addClass("show-all");
						Fuhre.slideshow.showSiblings(this.parentNode);
					} else {
						if (!$(this.parentNode).hasClass("selected")) {
							var holder = $$("slideshow-contents");
							var selected = $("#slideshow SPAN");
							for (var i = 0; i < selected.length; i++) {
								var li = $(selected[i]);
								li.className= "";
							}
							this.parentNode.addClass("selected");
							if ($(this).hasClass("map")) {
								var newEl = $$(Fuhre.map.mapId);
								newEl.style.display = "block";
								height = Fuhre.stripPx(newEl.getStyle("height"));
								Fuhre.slideshow.show(holder, newEl, 350, 1, true);
							} else {
								var duration = 500;
								if (this.previousWasMap) {
									duration = 1;
								}
								var image = holder.create("img", {id: "slideshow-current"});
								image.addEvent("load", function() {
										Fuhre.slideshow.show(holder, image, this.height, duration, false);
									}
								);
								image.src = this.href;
							}
						}
					}
				}
			);
			var slideshow = $$("slideshow");
			var el = null;
			for (var i = 0; i < slideshow.childNodes.length; i++) {
				if (slideshow.childNodes[i].nodeType == 1) {
					el = slideshow.childNodes[i];
					break;
				}
			}
			var height = null;
			if (el != null) {				
				if (el.tagName == "IMG") {
					el.id = "slideshow-current";
					height = el.height;
				} else if (el.tagName == "DIV") {
					height = Fuhre.getActualHeight(el);
					if (el.id != Fuhre.map.mapId) {
						el.id = "slideshow-current";			
					} else {
						this.previousWasMap = true;
					}
				} else  {
					el = null;
				}
			}
			var holder = slideshow.create("div", {id: "slideshow-contents"}, false, el);
			if (height != null) {
				holder.style.height = height + "px";
			}
			slideshow.insertBefore(holder, slideshow.firstChild);
		}
	},
	
	show: function(holder, el, height, duration, isMap) {
		var image = el;
		el = holder.create("div", {className: "next"}, true, image);
		holder.animate({height: height}, {duration: 500});
		el.show({duration: duration, callback: function() {
					var prev = el.prev();
					if (prev) {
						prev = $(prev);
						prev.remove();												
					}
					holder.insertBefore(image, el);
					el.remove();
				}
			}
		);
	},
	
	showSiblings: function(nextEl) {
		if (nextEl != null) {
			$(nextEl).show({duration: 10, callback: function() {
						Fuhre.slideshow.showSiblings(nextEl.next());
					}
				}
			)
		}
	}
}
Fuhre.addPlugin(Fuhre.slideshow);
