/*
	Motoring's design uses a raised menu for dropdowns, that sits on top of regular menus.
	This file implements that effect.
*/

(function ($) {

    if ($.cookie('Rainbow_motoringau') != null) return;

    // don't attempt menus on ie6
    if ($.browser.msie && $.browser.version == "6.0") return;

    // Set to 1 to prevent the menu from autohiding,
    // useful when debugging or styling
    floating_menu_debug = 0;

    // Unbind the existing hover, as we are going to perform our own...
    $('li:has(ul)').unbind();

    $.fn.tinyMenu = function () {
        var menuOverSpecial = function () {
            var itm = $(this), mnu = itm.parent('ul:first')[0];
            clearTimeout(mnu.sfTimer);

            // Maintain one instance of the fancymenuheader.
            // Create it if necessary.
            fancyplaceholder = $('.fancyitemplaceholder');

            isLifestyleDropdown = 1; // todo: fix this...

            if (fancyplaceholder.length <= 0) {
                $("#r-menu #menu").append('<div class="fancyitemplaceholder"></div>');
                fancyplaceholder = $('.fancyitemplaceholder');
                fancyplaceholder.append(document.createTextNode('lifestyle'));
                parentposition = $('#menu2807555').position();
                fancyplaceholder.css('left', parentposition.left - 4);
                fancyplaceholder.css('top', parentposition.top - 2);
                fancyplaceholder.mousemove(function (e) {
                    // If they move the cursor in the transparent section of the fancymover
                    // then hide it...
                    var x = e.pageX - fancyplaceholder.offset().left;
                    var y = e.pageY - fancyplaceholder.offset().top;

                    if (x > 100 && y < 30 && !floating_menu_debug) {
                        $('#menu2807555 ul').hide();
                        $('.fancyitemplaceholder').hide();
                    }
                });
                fancyplaceholder.mouseenter(function () { clearTimeout(mnu.sfTimer) });
                fancyplaceholder.mouseleave(function () {
                    // This fixes a problem where moving outside the fancy placeholder
                    // cancels the menu timeout, so check it manually here.
                    if (
						!window.isHovering('#menu2807555 ul') &&
						!window.isHovering('#menu2807555 li') &&
						!floating_menu_debug) {
                        $('#menu2807555 ul').hide();
                        $('.fancyitemplaceholder').hide();
                    }
                });
            }

            if (isLifestyleDropdown)
                fancyplaceholder.show();

            itm.find('>ul:hidden').show();
            $('#menu2807555 ul').css('z-index', 10000);
            itm.siblings().find('ul:visible').hide();
        };
        var menuOutSpecial = function () {
            var itm = $(this), mnu = itm.parent('ul:first')[0];
            clearTimeout(mnu.sfTimer);
            mnu.sfTimer = setTimeout(function () {
                if (floating_menu_debug) return;
                itm.find('>ul').hide();
                $('.fancyitemplaceholder').hide();
            }, 500);
        };

        $('li:has(ul)', this).hover(menuOverSpecial, menuOutSpecial);

        // Functions that allow an item's hoverstate to be tracked.
        $('li:has(ul)').hover(function () {
            $(this).data('hover', 1); //store in that element that the mouse is over it    
        },
		function () {
		    $(this).data('hover', 0); //store in that element that the mouse is no longer over it    
		});
        window.isHovering = function (selector) {
            return $(selector).data('hover') == 1 ? true : false; //check element for hover property    
        }

    };
})(jQuery);


