/*
 * dhMenu.js
 * damion hickman design 2009
 * steve@damionhickman.com
 */

gDropDownMaxLines = 10;

function addHighlight(imgsrc) {
   if (imgsrc.indexOf("-over.gif") == -1) {
        return imgsrc.replace(".gif","-over.gif");
   }
   return imgsrc;
}

function closeAllMenuDropDowns() {
    gHoverTimer = -1;
    $j(".dropDown").each(function (i) {
        $j(this).trigger("mouseenter");
        $j(this).trigger("mouseleave");
    });
}

$j.fn.outerHTML = function() {
    var doc = this[0] ? this[0].ownerDocument : document;
    return $('<div>', doc).append(this.eq(0).clone()).html();
};

var gHoverTimer = -1;

$j(document).ready(function($){
    if (jQuery.browser.msie && (jQuery.browser.version < 8)) {
        ISIE = true;
    } else {
        ISIE = false;   
    }

    $('.menuKill').mouseover(
        function() { 
            closeAllMenuDropDowns();
        }
    );

	$('#region_selector').click(function() {
		var position = $(this).position();
		var popup = $("#region_list");
		var top = position.top - popup.height() - 75;
		var left = position.left;
		popup.css({'left':left,'top':top})
			.slideDown("fast");
	});

	$('.dropmenuClose').click(
		function () {
			if (ISIE) {
				$(this).closest(".dropmenu").hide();
			} else {
				$(this).closest(".dropmenu").fadeOut("fast")
			}
		});
        
    $('.tab').mouseover(
        function() {            
            var position = $(this).position();
            var pnt = $(this).parent();
            var outerpos = pnt.position();
            //var menu = ".for-" + $(this).attr('id');
			var menu = $(this).next('.dropmenu');
            
            //stop all dropmenu animations
            $(".dropmenu").stop(true, true);
            
            //does this work if they are not the same class?
            $(".dropmenu").not(menu).each(function (i) {
                $(this).hide();
            });
            
            $('.tab').removeClass('on');
            
            if ($(menu).length == 0) return;
            
            $(this).addClass('on');
            
            var theDropDown = $(menu);
            
            //detect if it only has one column
            var numCols = $('.linkcolumn', theDropDown).length;

			theDropDown.css({left: -10000}).show();

            var pl;
            if (numCols == 0) {
                pl = position.left;
            } else {
                pl = Math.round(($(window).width() - theDropDown.width()) / 2);
            }

			theDropDown.hide();
            /* colin says - "please center it"
            if (pl + theDropDown.width() > $(window).width()) {
                pl = $(window).width() - theDropDown.width() - 100;
            }
            */
            
            theDropDown.css('left', pl);    
            theDropDown.css('top', outerpos.top + pnt.height() + 7);
            
            theDropDown.slideDown("fast").bind("mouseenter", function (e) {
               $(this).bind("mouseleave", function (e) {  
                    if (ISIE) {
                        $(this).hide();
                    } else {
                        $(this).fadeOut("fast")
                    }
                    $(this).unbind("mouseenter mouseleave");
                    $('.tab').removeClass('on');
               }); 
               
            });
        }
    );
 });
