//popup open function...
$.fn.edicraAdminPopup = function() {
    var href = $(this).attr('href');
    if (typeof(edcLargePanel) != 'undefined') edcLargePanel.close();
    if (typeof(edcMediumPanel) != 'undefined') edcMediumPanel.close();
    wdth = 852;
    hght = 590;
    lft  = screen.width/2 - wdth/2;
    tp   = screen.height/2 - hght/2;
    edcLargePanel = window.open(href, 'edcLargePanel','width='+wdth+', height='+hght+', left='+lft+', top='+tp+'');
    edcLargePanel.focus();
}


$.fn.edicraMenu = function() {
    var menu = $('#edicraMenu > ul', this);
    var clicked = false;
    
    //adding arrows...
    $('li:has(ul)', menu).addClass('expanding');
    $(menu).addClass('level1');
    $('> li > ul', menu).addClass('level2 panel');
    $('> li > ul > li > ul', menu).addClass('level3 panel');
    
    //wraping table around each menu...
    var table   = '<table class="shadowBox" border="0"><tr class="middle"><td class="center"></td></tr></table>';
    var left    = '<td class="left shadow"></td>';
    var right   = '<td class="right shadow"></td>';
    var top     = '<tr class="top"><td class="left shadow"></td><td class="right shadow" colspan="2"></td></tr>';
    var bottom  = '<tr class="bottom"><td class="left shadow" colspan="2"></td><td class="right shadow"></td></tr>';
    $('ul', menu).wrap(table).parent().before(left).after(right).parent().before(top).after(bottom);
    $('a', menu).wrapInner('<nobr/>');
    $('a', menu).wrapInner('<span class="inner" />');
    $('a', menu).wrapInner('<span class="icon" />');
    $('a', menu).wrapInner('<span class="outer" />');
    
    $('.shadowBox', menu).each(function(){
        var w = $('td.center > ul', this).width();
        $(this).width(w+10);
    });
     
    $('ul.level2 .shadowBox', menu).each(function() {
        $(this).css({'left': $(this).parents('.shadowBox').width()-10});
    });
    
    //this have to be separated due to ie7 bug...
    $('ul.level2 .shadowBox', menu).hide();
    $('.shadowBox', menu).hide();
    
    //hovering lvl 1...
    $('> li', menu).mouseenter(function() {
        $(this).parent().find('>li>.shadowBox').hide();
        $('.selected', menu).removeClass('selected');
        if (clicked) {
            if ($(this).hasClass('expanding')) {
                $(this).addClass('selected');
                $('> .shadowBox', this).show();
            } else {
                $(this).addClass('hover');
            }
        } else {
            $(this).addClass('hover');
        }
    }).mouseleave(function() {
        $(this).removeClass('hover');
    }).mousedown(function(){
        clicked = true;
        $(this).addClass('selected');
        $('> .shadowBox', this).show();
        return false;
    }).mouseup(function(){
        if (!$(this).hasClass('expanding')) {
            $(this).removeClass('hover selected');
            clicked = false;
        }
    });
    
    $('> li > a', menu).mouseenter(function() {
        $('.hover', menu).removeClass('hover');
        $('ul.level2 .shadowBox', menu).hide();
    });
    
    //hovering panel...
    $('ul.level2 > li', menu).mouseenter(function() {
        if (!$(this).hasClass('hover')) {
            $(this).parent().find('>.hover').removeClass('hover');
            $(this).addClass('hover');
            $('.panel .shadowBox', menu).hide();
            $('.shadowBox', this).show();
        }
    });
    
    $('ul.level2 .shadowBox li', menu).mouseenter(function() {
        $(this).addClass('hover');
    }).mouseleave(function() {
        $(this).removeClass('hover');
    });
    
    $('a', menu).click(function() {
        if ($(this).attr('href') != '#') {
            $('.shadowBox', menu).hide();
            $('> li', menu).removeClass('selected');
            clicked = false;
            if (!$(this).hasClass('noPopup')) {
                $(this).edicraAdminPopup();
                return false;
            }
        } else {
            return false;
        }
    });
   
    //clicking somwhere...
    $(this).mousedown(function(){
        //this have to be separated due to ie7 bug...
        $('ul.level2 .shadowBox', menu).hide();
        $('.shadowBox', menu).hide();
        $('.selected', menu).removeClass('selected');
        $('.hover', menu).removeClass('hover');
        clicked = false;
    });
    
    //clicking somwhere...
    $('li a', menu).mousedown(function(){
        if (clicked && $(this).parent().hasClass('selected')) {
            $('ul.level2 .shadowBox', menu).hide();
            $('.shadowBox', menu).hide();
            $('.selected', menu).removeClass('selected');
            $(this).parent().addClass('hover');
            clicked = false;
            return false;
        }
        
    });
    $('#edicraMenu').css({visibility: 'visible'});
}

$(function() {
    $('html').edicraMenu();
    $('#edicraMenuWrapper:not(.raised)').each(function(){
        $('#edicraApplicationWrapper').addClass('lowered');
    });
    $('#edicraMenuLogoutLink').click(function(){
        $('#edicraMenu').animate({top: '-31px'}, 'slow');
        $('#edicraMenuWrapper').animate({height: 0}, 'slow');
        $('#edicraApplicationWrapper').animate({top: 0}, 'slow', function(){
            location = $('#edicraMenuLogoutLink').attr('href');
        });
        return false;
    });
    $('#edicraMenuWrapper.raised').each(function(){
        $(this).animate({height: '31px'}, 'slow');
        $('#edicraMenu').animate({top: 0}, 'slow');
        $('#edicraApplicationWrapper').animate({top: '30px'}, 'slow');
    });
    $('#edicraMenu .edicra a').click(function(){
        $(this).edicraAdminPopup();
        return false;
    });
});

$(window).load(function() {

});




