// show-hide

function showStuff(id) {
	document.getElementById(id).style.display = 'block';
}

function hideStuff(id) {
	document.getElementById(id).style.display = 'none';
}

jQuery(document).ready(function() {
    var $j = jQuery.noConflict();
    var wrapper = $j("#block-system-main-menu");
    var menu = wrapper.find('ul.menu');
    var that = this;
    var dot = $j('#menu-loc');
    var active = menu.find('a.active:first');
    var defaultDuration = 400;
    var dotIsHidden = true;
    
    var moveDotTo = function(anchor, duration) {
        var x =  (anchor.position().left) + (anchor.outerWidth(true) / 2) - (dot.outerWidth(true) / 2);
        options = {};
        if (dotIsHidden) {
            // make the dot transparent and animate it in.
            options.opacity = 1.0;
            if (dot.css('display') != 'none' && dot.css('opacity') != 0) {
                // not completely hidden, so animate the move and fade together
                options.left = x;
            }
            else {
                // completely hidden, so move it before animating the fade
                dot.css('left', x);
                dot.css('opacity', 0);
            }
            dot.show();
        }
        else {
            options.left = x;
        }
        dot.stop().animate(options, duration, function() { dotIsHidden = true; });
    }
    
    var resetDot = function(duration) {
        if (active.length == 0) {
            // current page has no active item
            // set dotIsHidden as fadeOut starts
            dotIsHidden = true;
            dot.stop().fadeOut(duration);
        }
        else {
//            dot.css('visibility', 'visible');
            moveDotTo(active, duration); 
        }
    }
    
    if (dot.length == 1 && menu.length == 1) {
        // reset the dot after a delay (the positioning isn't known yet)
        setTimeout(function() {
                        resetDot(0);
                     }, 1000);
                     
        // set up a hover function to move the dot
        menu.find('li').each(function() {
            var i = $j(this).find('a:first');
            i.hover(function() {
                        moveDotTo($j(this), defaultDuration);
                    },
                    function() {
                        resetDot(defaultDuration);
                    });
        });
    }
    
    // now check to see if we should show the contact window
    if (window.location.hash === "#contact") {
        if (jQuery('#overlay').children('#contact')) {
            // wait one second to show
            setTimeout(function() {
                            showStuff('overlay');
                        }, 1000);
        }
    }
        
});

// cookie functions
function UMI_setCookie(cookiename, value) {
    var expiration = new Date();
    expiration.setFullYear(expiration.getFullYear() + 5);
    var ckie = cookiename + "=" + escape(value) + "; expires=" + expiration.toUTCString() + ";";
    ckie += "path=/;";
    
    document.cookie = ckie;
}

function UMI_deleteCookie(cookiename) {
    var expiration = new Date();
    document.cookie = cookiename + "=;path=/;expires=" + expiration.toUTCString() + ";" + ";";
}

function UMI_getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

;
jQuery(document).ready(function() {
    // set up navigation menus
//    var menuItems = jQuery(".sidebar").find(".block-menu").find("ul.menu").children("li.expanded");
    var menuItems = jQuery(".sidebar").find(".block-menu").find("ul.menu").children("li");
    menuItems.each(function() {
        var item;
        if (jQuery(this).has("ul").length) {
            jQuery(this)
            .click(function(e) {
                jQuery(this).children('ul').toggle(250);
                if (jQuery(this).hasClass("expanded")) {
                    jQuery(this).removeClass("expanded").addClass("collapsed");
                }
                else {
                    jQuery(this).removeClass("collapsed").addClass("expanded");
                }
                return false;
            })
            .removeClass('expanded')
            .addClass('collapsed')
            .children('ul').hide();
        }
        else {
            jQuery(this)
            .click(function(e) {
                console.log('foo');
                e.stopPropagation();
                return true;
            })
        }

        /*        
        item = jQuery("<span class='plus'>+ </span>").click(function(e){
          jQuery(this)
            .text( jQuery(this).text() === "+ " ? "- " : "+ " )
            .parent().next().toggle();
          return false;
        });

        if (/current-cat/.test(this.className)) {
          item.text('-');
        } else {
          jQuery(this).find("li").hide();  
        }

      } else {
        item = jQuery("<span class='plus'>&nbsp;</span>");
      }

      jQuery(this).children("a").prepend( item );
*/
    });
});
;

