/* Based on work from: http://www.paulsrichards.com/tag/jquery/ */
(function($){
	var pm_elements = new Array();
	var pm_elements_button = new Array();
	var pm_elements_count = 0;
	isIE6 = ($.browser.msie && jQuery.browser.version.substr(0,1)=="6");

	$.fn.popupmenu = function(p_options, menuEle) {
		var defaults = {
			addStyle: "in_popupOpen",
			time: 500,
			speed: "fast",
			autooff: true
		};
		var options = $.extend(defaults, p_options);
		var global_menu_div = false;
		var global_menu_top = false;
		var global_t;

		function doMenu(e) {
			e.preventDefault();

			var target	= e.data.target;
			var button	= $(this);

			button.parent().blur();
			clearTimeout(global_t);

			if (target.is(":visible")) {
				if (button.hasClass("in_click")) {
					if(options.addStyle != false){
						button.removeClass(options.addStyle);
					}
					target.hide();
				}
				return;
			}

			// check to see if any elements need turning off first.
			if(options.autooff){
				for(i=0; i < pm_elements.length; i++){
					pm_elements[i].hide();
					if(options.addStyle != false){
						pm_elements_button[i].removeClass(options.addStyle);
					}
				}
			}
			
			if(options.addStyle != false){
				button.addClass(options.addStyle);
			}
	
			var pos = button.offset();
			var thisCss = {position:"absolute"};
			if (target.hasClass("in_popupBR300")) {
				thisCss.left	= (pos.left + button.outerWidth() - target.outerWidth()) + "px";
				thisCss.top 	= (pos.top - 3 - target.outerHeight()) + "px";
			} else if (target.hasClass("in_popupBL300")) {
				thisCss.left	= pos.left + "px";
				thisCss.top 	= (pos.top - 3 - target.outerHeight()) + "px";
			} else if (target.hasClass("in_popupMMtoTR")) {
				thisCss.left	= (pos.left + 0.5*button.outerWidth() - target.outerWidth()) + "px";
				thisCss.top 	= (pos.top + 0.5*button.outerHeight()) + "px";
			} else if (target.hasClass("in_popupTRtoTL")) {
				thisCss.left	= (pos.left + button.outerWidth()) + "px";
				thisCss.top		= pos.top + "px";
			} else {
				thisCss.left	= pos.left + "px";
				thisCss.top		= (pos.top + button.outerHeight()) + "px";
			}

			target.css(thisCss).show();
		}

		return this.each(function() {
			var button = $(this);

			// If a button only exists to popup a js menu, it will be hidden initially
			// so unhide it now we know we are doing JS
			if (button.hasClass("in_menuTrigger")) {
				button.addClass("in_menuButton").removeClass("in_menuTrigger");
			}

			if (menuEle) {
				target = menuEle;
			} else {
				var thisId = button.attr("id");
				var targetId = thisId + "_Menu";
				var thisRel = button.attr("rel");
				if (thisRel) {
					targetId = thisRel;
				}
				var target = $("#" + targetId);
				if (target.size() == 0) {
					return true;
				}
			}

			target.appendTo("body");
			if (isIE6 && $.fn.bgiframe ) {
				target.bgiframe();
			}

			// Register the element target
			pm_elements[pm_elements_count] = target;
			pm_elements_button[pm_elements_count] = button;
			pm_elements_count++;

			button.bind(button.hasClass("in_click")?"click":"mouseover", {target: target}, doMenu);

			button.mouseout( function() {
				if(!global_menu_div){
					global_t = setTimeout(
					function() {
						if(options.addStyle != false){
							button.removeClass(options.addStyle);
						}
						target.hide();
					}
					,options.time);
				}
			});

			target.mouseover( function() {
				global_menu_div = true;
				clearTimeout(global_t);
			});

			target.mouseout( function() {
				global_menu_div = false;
				global_t = setTimeout(
				function() {
					if(options.addStyle != false){
						button.removeClass(options.addStyle);
					}
					target.hide();
				}
				,options.time);
			});

			return true;
		});
	};

	$.fn.tooltip = function() {
		var ttDoneList = {};
		function doToolTip(btn, thisId, show) {
			var ttEle = $("#" + thisId);
			if (show) {
				var border_right = $(window).width() + $(window).scrollLeft();
				if (ttEle.length == 0) {
					ttEle = $("#" + thisId);
				}
				var thisCss = {};
				if (border_right > btn.offset().left + ttEle.outerWidth()) { 
					thisCss.left	= btn.offset().left + "px";
				} else {
					thisCss.left	= (border_right - 10 - ttEle.outerWidth()) + "px";
				}
				thisCss.top 	= (btn.offset().top + btn.outerHeight()) + "px";
				ttEle.css(thisCss).show();
			} else {
				ttEle.hide();
			}
		};

		return this.each(function() {
			if ($(this).attr("title") != "" && $(this).attr("title") != "undefined" ) {
				var thisId = "in_autoTT_" + $(this).attr("title").replace(/[^a-z0-9_]/ig, "");
				if (!ttDoneList[thisId]) {
					var thisTitle = $(this).attr("title");
					$("body").append("<div class='in_tooltip' id='" + thisId + "'>" + thisTitle + "</div>");
					ttDoneList[thisId] = 1;
				}
				$(this).removeAttr("title").mouseover(function(){
					doToolTip($(this), thisId, true);
				}).mouseout(function(){
					doToolTip($(this), thisId, false);
				});
			}
		});
	};
})(jQuery);