
document.observe('dom:loaded' ,function(){

    $$('ul.top_menu>li').last().addClassName('last');

    //takes the categories from magento and inserts it under Products
    var top_menu = $$('ul.top_menu').first().identify(),
    categories = $('mage_categories'),
    cat_holder = $$('.catalog_holder');
    //cat_holder.invoke('next')[0].remove();
    cat_holder.invoke('insert', {
        after: categories.remove()
    });


    new ProtoFish(top_menu, '600', 'active', false, false, false );

	// fix uls that may be shown off the page
    adjustMenu.delay(.5, 'mage_categories', 'top_menu_nav');

    
    if($('helpfulHints')) {
    	
    	$('helpfulHints').observe('click', function(ev){
    		ev.stop();
    		var elem = ev.element();
    		window.open(elem.readAttribute('href'), '_blank', "toolbar=0,location=0,status=0,menubar=0,scrollbars=1,width=500,height=400");
    	});
    	
    }

});

/*
	Adjusts menu ul's that may appear outside of the wrapper.
	Basically, turns the left: CSS rule into a negitive.
*/
function adjustMenu(menu, constraint){

    var all_uls = $$('div#top_menu_nav li>ul');

	// display them so we can read width and viewport
    all_uls.invoke('setStyle', {
        display: 'block',
        visibility: 'hidden'
    });

	// get the constraint viewport px
    constraint = getRight(constraint);

	// grab all the uls to copmare to the the constraint
    menu = $(menu);
    var uls = $$('#' + menu.identify() + ' li>ul');

	// create a has for the uls that are over the constraint
    var adjust = $H();
    uls.each(function(ul){
        var r = getRight(ul);
        // adjust
        var lft = false;
        if(r > constraint){
			lft = '-' + (r - constraint) + 'px';
			adjust.set(ul.identify(),lft);
        }
    });

	// because IE is stupid
    all_uls.invoke('writeAttribute', 'style', '');

    // set the left: CSS rule
    adjust.each(function(pair){
    	$(pair.key).setStyle({
    		left: pair.value
    	});
    });

}

/*
	Calculates an elements right viewport
*/
function getRight(elm){
    elm = $(elm);
    return elm.viewportOffset().left + elm.getWidth();
}

/*
 * Under Construction
 */
function under_construction(mess){

	var w = $('wrapper');

	if(!w){
		w = $$('div.wrapper')[0];
	}

	var div = new Element('div', {
		id: 'uc'
	});
	var message = new Element('p');
	message.update(mess);

	div.update(message);

	w.insert(div);
}

