/**
 * siteNav - plugin
 * @version 1.5	(28 JUL 2011) Adding Tier 3 functionality
 * @requires jQuery 1.3.2
 * @author v-spsund
 */
(function ($) {
    $.fn.siteNav = function (options) {
        var opts = $.extend({}, $.fn.siteNav.defaults, options);
        return this.each(function () {
            var methods = {
                toggle: function (Li, direction) {
                    /*
                    * toggle: navigation mouseenter/mouseleave events, sent from navIn and navOut
                    * el        : the nav li being moused over
                    * direction : in or out
                    */
                    if (opts.ready == false) { return; }
                    var a = Li.find('> a'),
						ul = Li.find('> ' + opts.T2wrapper),
						ulPad = parseInt(ul.find('a:first').css('paddingLeft'));

                    if ((direction == 'out') && Li.hasClass(opts.visibleWatcher)) {
                        Li.removeClass(opts.visibleWatcher);
						if (opts.pageDirection == 'rtl'){
                        	ul.css({ 'right': '-999999em' });
						} else {
							ul.css({ 'left': '-999999em' });
						}
                    } else if ((direction == 'in') && (Li.hasClass(opts.visibleWatcher) == false)) {
                        Li.addClass(opts.visibleWatcher);
                        methods.doPositioning(a, ul, ulPad);
                    }
                },
                navIn: function () {
                    methods.toggle($(this), 'in');
                },
                navOut: function () {
                    methods.toggle($(this), 'out');
                },
                doPositioning: function (jParent, jChild, padOffset) {
                    /*
                    * jParent   = nav item li > a
                    * jChild    = div.subNavWrapper aka the submenu
                    * padOffset = Left padding of the first <a> inside the subnav/jChild
                    *
                    * pos = relative position of the nav item <a>
                    *       relative to its offsetParent aka sitenav div
                    */
                    var pos = jParent.position(),
	                    posLeft = pos.left,
	                    /* pos = null, */
	                    posTop = jChild.prev('a').outerHeight(true);
						
					if (jChild.data('depth') ==2){
						posLeft = jParent.outerWidth() + parseInt(jParent.css('padding-left'));
						posTop = pos.top - ( 
							( parseInt(jChild.css('padding-top')) + parseInt(jParent.css('padding-top')) ) *2
						);
					}
					pos=null;
					
					if (opts.pageDirection == 'rtl'){
						var posRight =  jParent.parents('#sitenav').width() - ( jParent.width() + posLeft );
						if (padOffset){ posRight -= padOffset; }						
						jChild.css({ 'right': posRight, 'top': posTop }).show();
						posRight = null;
					} else {
						if (padOffset) { posLeft -= padOffset; }
						jChild.css({ 'left': posLeft, 'top': posTop }).show();
					}
					
                    jChild = jParent = padOffset = posLeft = null;
                },
                unload: function () {
                    $T1li.removeClass(opts.visibleWatcher);
                }
            };

            var $This = $(this).find('> div > ul'),
				$T1li = $This.find('> li'),
				subNavs = $This.find(opts.T2wrapper);
			
			opts.pageDirection = ($('body').hasClass('page-direction-rtl')) ? 'rtl' : 'ltr';
            /* Ensure we don't have "ghost hovering" if the back button is used. */
            $(window).unload(function(){ methods.unload(); });

            /* Get the parent LI of the subnav and attach hover events */
            subNavs.each(function () {
                var t = $(this),
					li = t.find('li');
					
				if (!li.length){
					/* An empty list: nuke it */
					t.remove();
				} else {
					/* Bind mouseover events */
					t.parent('li').hover(methods.navIn, methods.navOut);
					
					/* Log depth data: */
					var X = t.parents('li');
					if (X){
						t.data('depth', X.length);
					}
					X=null;
				}
				
				
				t = null;
            });
            opts.ready = true;


        });  /* END return this.each */

    };
    $.fn.siteNav.defaults = {
        ready: false,
        T2wrapper: 'div.subNavWrapper',
        subArrowClass: 'sub',
        visibleWatcher: 'visible' /* class name used by JS to track subnav visibility */
    };
})(jQuery);

$(document).ready(function(){
	$('#sitenav').siteNav();
});


