﻿/// <reference path="jquery.intellisense.js"/>

jQuery.fn.QuickMenu = function() {

    $('ul.quickmenu-data li')
        .addClass('quickmenuitem');

    $('ul.quickmenu-data li:has(ul)').addClass('quickmenuitem-haschildren');

    // assign a unique id to all panes X
    // add a pane class to all panes X
    $('ul.quickmenu-data, ul.quickmenu-data ul')
            .each(assigneachPaneId);

    // add link to parent(s) to each child pane
    // locate all parent objects and add link to child as div
    $('ul.quickmenupane').each(addNavigationLinks);


    // add link to child pane
    // move attach pane to holder element

    $('.quickmenuitem a').wrapInner('<div class="item"></div>')

    $('.quickmenupane').appendTo('#menuholder');

    //   $('#menuholder').click(function() { $('#quickmenupane0').show(); });


    $('#quickmenupane0').show();


}

function addNavigationLinks() {
    
    // with all li's that have child ul's
    // get the descendant ul
    var parentPageId = $(this).attr('id');
    // get the parent li and get it's text name


    $(this).children('li:has(ul)').each(function() {

        var parentCallback = 'unshowPane(\'#' + parentPageId + '\', \'#' + $(this).children('ul:first').attr('id') + '\')';

        var parentName = $(this).children('a:first').text();

        var linkId = parentPageId + '_link';
        
        $(this).children('ul').prepend('<div onclick="' + parentCallback + '" class="parentLink">' + parentName + '</div>');
    });

}

var n = 0;
var paneId = 0;

// First go through all li items and set id...
// .. if the element has children attached then add the arrow and a link
// the new div

function assigneachItem() {
    this.id = 'quickmenuitem' + n++;
 }

 function assigneachPaneId() {
     var newId = 'quickmenupane' + paneId++;
    this.id = newId;

    $(this).addClass('quickmenupane');
    $(this).css('display', 'none');
    
    var callback = 'showPane(\'#' + newId + '\');';

    // Add display event handler to parent item
    // $(this).parent().append('<div class="showchild" onclick="' + callback + '">&nbsp;</div>');
    var linkId = newId + '_link';
    $(this).parent().append('<div class="showchild" id="' + linkId + '" >&nbsp;</div>');
    $('#' + linkId).click(function() { eval(callback); });

    // setup hoverintent
    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 150, // number = milliseconds for onMouseOver polling interval    
        over: function() { eval(callback); }, // function = onMouseOver callback (REQUIRED)    
        timeout: 0, // number = milliseconds delay before onMouseOut    
        out: function() {  } // function = onMouseOut callback (REQUIRED)    
    };

$('#' + linkId).hoverIntent(config);
  
}

function debugeach() {
    alert(this.innerText + " id = " + this.id);
}

function showPane(paneId) {

    $(paneId).menuLeft(200, '', '');

}

jQuery.fn.menuLeft = function(speed, easing, callback) {
    $(this).css("width", "0px");
    $(this).css("margin-left", "220px");
    $(this).show();
    return $(this).animate({ width: "220px", marginLeft: "0px" }, speed);
}

jQuery.fn.menuRight = function(speed, easing, callback) {
    $(this).css("width", "0px");
    $(this).css("margin-right", "0px");
    return $(this).animate({ width: "220px", marginRight: "220px" }, speed, null, callback);
}



function unshowPane(paneId, unshowpaneId) {

    // Fly old menu out to right
    
    $(unshowpaneId).fadeOut(200);
    $(paneId).fadeIn(200);
            
    }
