// Set up main menu that appears on every page
YAHOO.util.Event.onDOMReady(function () {

    var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;

    //	Instantiate a MenuBar, passing in the id of the HTML element 
    //	representing the MenuBar.
    var oMenuBar = new YAHOO.widget.MenuBar(
        "menu_wrapper",
        { 
            autosubmenudisplay: true, 
            hidedelay: 1500, 
            lazyload: true
        }
    );
    
    // Render the MenuBar instance
    oMenuBar.render();

    // Prepare data
    var amulets = [
       {src:"/img/amulets/dreamstimeextrasmall_7405120.jpg", height:81, alt:"A golden amulet with jade"},
       {src:"/img/amulets/dreamstimeextrasmall_6618677.jpg", height:113, alt:"Vintage mystery amulet from old metal isolated on white background"},
       {src:"/img/amulets/dreamstimeextrasmall_9211265.jpg", height:82, alt:"Evil eye (amulet) on white peebles"},
       {src:"/img/amulets/dreamstimeextrasmall_7328055.jpg", height:81, alt:"Old amulet of voodoo magic"}, 
       {src:"/img/amulets/dreamstimeextrasmall_9063663.jpg", height:181, alt:"Hamsa hand amulet, used to ward off the evil eye"},
       {src:"/img/amulets/dreamstimeextrasmall_4172080.jpg", height:244, alt:"Amulet gold bells feng shui"},
       {src:"/img/amulets/dreamstimeextrasmall_2718528.jpg", height:81, alt:"From Africa voodoo amulet from gorilla&rsquo;s teeth."},
       {src:"/img/amulets/dreamstimeextrasmall_7270330.jpg", height:81, alt:"Amulet against evil ghosts"},
       {src:"/img/amulets/dreamstimeextrasmall_1928527.jpg", height:173, alt:"Big amulet. Isolated on white."},
       {src:"/img/amulets/dreamstimeextrasmall_9144460.jpg", height:81, alt:"Hanging amulet on an abstract background. Vectorial illustration is translated in a raster."},
       {src:"/img/amulets/dreamstimeextrasmall_7597263.jpg", height:97, alt:"Platted leather amulet wristband bracelet isolated on white"},
       {src:"/img/amulets/dreamstimeextrasmall_4086044.jpg", height:173, alt:"Red Japanese amulet in the darkness"},
       {src:"/img/amulets/dreamstimeextrasmall_5108961.jpg", height:81, alt:"Chinese Good Luck charm on White Background"},
       {src:"/img/amulets/dreamstimeextrasmall_3583255.jpg", height:181, alt:"A Greek good luck charm on a white wall"},
       {src:"/img/amulets/dreamstimeextrasmall_9139651.jpg", height:187, alt:"Authentic charm isolated in studio"},
       {src:"/img/amulets/dreamstimeextrasmall_8835239.jpg", height:91, alt:"Claw Bear on lace. The Amulet of the worldly-wise huntsman."}, 
       {src:"/img/amulets/dreamstimeextrasmall_5004257.jpg", height:113, alt:"Big green amulet over white"},
       {src:"/img/amulets/dreamstimeextrasmall_887573.jpg", height:91, alt:"Amulet as heart on a black cord"}
    ];
    amulets.sort(function(a,b) {return Math.random()-0.5;});
    var tool_data = {
        yui: {src: '/img/tools/Yuilib.jpg', width: 85, height: 51},
        perl: {src: '/img/tools/circle_power_perl.gif', width: 72, height: 72},
        apache: {src: '/img/tools/feather.png', width: 85, height: 24},
        w3c: {src: '/img/tools/w3c_home.png', width: 72, height: 47},
        debian: {src: '/img/tools/debian.png', width: 75, height: 92}
    };
    var tool_setup = false;

    var cover = new YAHOO.util.Element('left-cover');
    Event.onAvailable("doc", function(e) {
        // Resize the left-hand column to match the content
        var bottom = Dom.getXY('find_bottom')[1];
        var top = Dom.getXY('yui-main')[1];
        var height = bottom-top>600 ? 30+bottom-top : 600;
        var el = new YAHOO.util.Element('left-column');
        el.setStyle('height',  height+'px');
        cover.setStyle('height',  height+'px');
        cover.setStyle('left', 0);
 
        var dyn_height = 40;
        for(var i = 0; i < amulets.length; i++) {
            dyn_height += amulets[i].height+4;
            if (dyn_height > height) 
                break;
            var img = document.createElement('img');
            img.height = amulets[i].height;
            img.width = 120;
            img.src= amulets[i].src;
            img.alt = amulets[i].alt;
            cover.appendChild(img);
        }
        cover.removeClass('hide');
        addthis.init()

    });

    var set_up_tools = function() {
        if (tool_setup)
            return;
        var tool_anchors = Dom.getElementsByClassName('tool', 'a');
        for(var i = 0; i < tool_anchors.length; i++) {
            var id = tool_anchors[i].id;
            if (tool_data[id]) {
                var alt = tool_anchors[i].innerHTML;
                var img = '<img id="tool-img-'+id+'" alt="'+alt+'" src="'+tool_data[id].src+'" width="'+tool_data[id].width+'" height="'+tool_data[id].height+'"/>';
                tool_anchors[i].innerHTML = img;
            }
        }
        tool_setup = true;
    }

    Event.on("left-cover", "mouseenter", function (e) {
        set_up_tools();
        cover.setStyle('opacity', 0);
        cover.addClass('hide');
    });
    Event.on("left-column", "mouseleave", function (e) {
        cover.removeClass('hide');
        var myAnim = new YAHOO.util.Anim(
            'left-cover',
            {
                opacity: {
                    to: 1 
                } 
            },
            5,
            YAHOO.util.Easing.bounceBoth
        );
        myAnim.animate();
    });

    if (Dom.get("carousel")) {
        var carousel = new YAHOO.widget.Carousel("carousel", {
            isCircular: true,
            numVisible: 1,
            autoPlayInterval: 10000
        });
        for(var i = 2; i <= 4; i++) {
            var html = '<img alt="Green duende costume with large teeth from Columbia" src="/img/arcana/duende'+i+'.jpg" height="150" width="200" title="Duende photos courtesy of megavas on flickr (Guillermo Vasquez)">';
            carousel.addItem(html);
        }
        carousel.on("mouseenter", function(obj) {
            carousel.scrollForward();
        });
        carousel.render();
        carousel.show();
        carousel.startAutoPlay();
    }


});




