$(document).ready(function() {

    // Dropdown Menu
    
    $('.sub-menu').wrap('<div class="submenu-container" />');
    $('.submenu-container').hide();
    $('.submenu-container').prepend('<div id="submenu-arrow"></div>');

    $("ul.menu li").filter(":has(ul)").hover(function() { //When trigger is hovered...

        // Possition the top orange arrow of submenu
        var destinationLeft = $(this).find("a").offset().left + $(this).find("a").width()/2-13;
        $(this).find("#submenu-arrow").css("left", destinationLeft);

        //Following events are applied to the menu itself (moving menu up and down)
        $(this).find(".submenu-container").slideDown('fast').show(); //Drop down the sub-menu on click

        // Possition the submenu links to apear below the parent link
        var elems = $(this).find(".sub-menu li"); // returns a nodeList
        var arr = jQuery.makeArray(elems);
        var arr_half_length = parseInt(arr.length/2);
        var menulinks_width = 0;
        
        $(this).find('.sub-menu li').each(function(index) {
            if (index < arr_half_length+1) {
                //alert(index + ': ' + $(this).text());
                if (index != arr_half_length) {
                    menulinks_width += $(this).find('a').width()+50+6;
                } else {
                    menulinks_width += $(this).find('a').width()/2+25+3;
                }
            }
        });
        destinationLeft = $(this).find("a").offset().left + $(this).find("a").width()/2 - menulinks_width;
        $(this).find(".sub-menu").css("left", destinationLeft);

        $(this).hover(function() {
            }, function(){
                $(this).find(".submenu-container").slideUp('fast'); //When the mouse hovers out of the sub-menu, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    // Contact Form

    $('.contact-wrapper').hide();
    $('.contact-email').mouseover(
        function () {
            $('.contact-wrapper').slideDown(500);
            $('.contact-email').addClass('hover');
            $('html,body').animate({
                scrollTop: $('.contact-wrapper').offset().top-88
            }, 500);
            return false;
        }
        );

    $("a[href='#footer']").click(function() {
        $('.contact-wrapper').slideDown(500);
        $('.contact-email').addClass('hover');
        var elementClicked = $(this).attr("href");
        var destination = $(elementClicked).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination-100
        }, 500 );
        return false;
    });

    var menulinks_width = 965;

    $('.menu li').each(function(index) {
        menulinks_width -= $(this).find('a').width();
    //alert(index + ': ' + $(this).text() + ' - ' + $(this).find('a').width());
    });

    $('.menu li').find('a').css('margin-left', menulinks_width/5);
    $('ul.menu li:last-child a').css('margin-left', 0);

    $('ul.sub-menu li a').css('margin-left', 25);
    $('ul.sub-menu li a').css('margin-right', 25);
    $('ul.sub-menu li a').css('float', 'left');

    $(".bottom-gallery-entry a").hover(function() { //When trigger is hovered...
        $(this).find("img").css('opacity', 0.8);
        $(this).find("h2").css('opacity', 0.8);
    }, function(){	//On Hover Out
        $(this).find("img").css('opacity', 1.0); //On hover out
        $(this).find("h2").css('opacity', 1.0);
    });

    $(".wp-pagenavi a").each(function(index) {
        var title = $(this).attr("href")+'#bottom-wrapper';
        $(this).attr("href", title);
    });

    $('.widget_wp_sidebarlogin input[type="submit"]').attr("value", '');

    $('div.bottom-gallery-entry:eq(3)').css('margin-right', 0);
    $('div.bottom-gallery-entry:eq(7)').css('margin-right', 0);
    $('div.bottom-gallery-entry:eq(11)').css('margin-right', 0);
    $('div.bottom-gallery-entry:eq(15)').css('margin-right', 0);

    $('div.bottom-gallery-entry:eq(3)').css('margin-left', 1);
    $('div.bottom-gallery-entry:eq(7)').css('margin-left', 1);
    $('div.bottom-gallery-entry:eq(11)').css('margin-left', 1);
    $('div.bottom-gallery-entry:eq(15)').css('margin-left', 1);

}); //end

