$(document).ready(function() {
    // Find out which page we are on
    $page = jQuery.url.attr("file");
    // If the page is not set (i.e. we have browsed to coachamal.com) set the page to index.html
    if(!$page) {
        $page = 'index.html';
    }


    //
    if( jQuery.browser.msie ) {
        ie6DOMManipulator();
    }

    jQuery('.banner').prepend("<div id='navLine'></div>");
    
    

    // For every <a> element within .navigation list, register the fadingLinks function
    jQuery('.navigation a').dwFadingLinks({
        duration: 200
    });

    // Hide all sub menu navigation menus
    jQuery('.navigation li ul').hide();

    // Highlight current page
    jQuery('.navigation li a').each(function(){
        // get the last part of the link
        var $elem = jQuery(this)
        var $href = $elem.attr('href').split('/').pop();
        // if the page we are on is the same as this link
        if ( ($href == $page) || ($href == '') ) {
            // Current Page!
            // Set the class to 'on'. This will alter the color to green
            $elem.addClass('on');
            // if we are a <a> tag in a submenu, show the submenus containing <ul>
            $elem.parents('ul').show();
            // Set the parent li class to 'on', coloring it green
            $elem.parent('li').parent('ul').parent('li').children('a').addClass('on');

            // Get any parent <li> tags that have a ul child
            lelem = $elem.parents('li').filter(':has(ul)');
            // If we have got some, then we must be in a submenu
            if (lelem.size() > 0) {
                moveNav(lelem);
                jQuery('#navLine').fadeIn(0);
            }
        } else {
            // we are not on this page, remove the 'on' class
            $elem.removeClass('on');
        }
    });

    // We have mouseentered a link, is it a submenu heading?
    jQuery('.navigation li').mouseenter(function(){

        // create a reference to the active element (this)
        // so we don't have to keep creating a jQuery object
        $heading = jQuery(this);
        $heading.addClass('on');
        $heading.siblings().removeClass('on');
        
        // create a reference to visible sibling elements
        // so we don't have to keep creating a jQuery object
        $expandedSiblings = $heading.siblings().find('ul:visible');
        // if there are expanded siblings, fade them out, then fade in our element
        if ($expandedSiblings.size() > 0) {
            $expandedSiblings.fadeOut(0, function(){
                $heading.find('ul').fadeIn(600);
            });
        } else {
            // just fade in our element
            $heading.find('ul').fadeIn(600);
        }
    });

    // Select only the list items that have a further ul submenu
    jQuery('.navigation li').filter(':has(ul)').mouseenter(function() {
        moveNav(jQuery(this));
        jQuery('#navLine').fadeIn(600);

    });

    jQuery('.navigation li').filter(':has(ul)').mouseleave(function() {
        jQuery('#navLine').fadeOut(0);
    });
  
});

function ie6DOMManipulator() {

  // For each image in the banner
  jQuery('.banner img').remove();
  // For each image in the banner
  jQuery('.banner').prepend('<div id="ieFix" style="position:absolute;float:left;margin-left:9%;z-index:-3;"><img src="/assets/images/nav/header.jpg"></div>');

}

function moveNav(lelem) {
    foffset = parseInt(lelem.css('font-size')) / 2;
    ltop = Math.floor(lelem.offset().top);
    lleft =  lelem.offset().left;
    jQuery('#navLine').offset({top: ltop + foffset, left: lleft - 30});
}

$(window).load(
    function(){

        // Find out which page we are on
        $page = jQuery.url.attr("file");
        // If the page is not set (i.e. we have browsed to coachamal.com) set the page to index.html
        if(!$page) {
            $page = 'index.html';
        }
        if ($page == 'index.html') {
            jQuery("#logo").animate({top:'100px'}, 0).animate({opacity: '1.0', top:'36px'}, 500);
            jQuery(".navigation").delay(350).animate({opacity: '1.0'},660);
            jQuery("#rose_1").delay(1500).animate({opacity: '1.0'}, 500);
            jQuery("#rose_2").delay(1750).animate({opacity: '1.0'}, 500);
            jQuery("#rose_3").delay(2000).animate({opacity: '1.0'}, 500);
            jQuery("#petal").delay(1000).animate({opacity: '1.0'}, 500);
            jQuery(".strapline").delay(2500).animate({opacity: '0.0', top:'232px'}, 0).animate({opacity: '1.0', top:'208px'}, 500);
            jQuery("#flowerEmblem").delay(3000).fadeIn(700).fadeOut(500);
			
        }
    });
