/* declaring some arrays for the navigation */
var menu_active = new Array;
var menu_show_timer = new Array;
var menu_delete_timer = new Array;

$(document).ready(function($)
{
    
    $('a.redactietip_ajax').live('click', function()
    {
        
        var url = $(this).attr('href');
        var direction = url.split('/').pop();
        var elm = $(this).parents('div.redactietips');
        var active = elm.find('.redactietip_active');
        var css = parseInt (elm.css('left'));
        var next = direction.match('next') ? true : false;
        var length = next ? active.next().length : active.prev().length;
        var redactietip_width = parseInt (elm.parent().css('width'));
        
        $('.redactietip_active').removeClass('redactietip_active');
        
        if (length == 0)
        {
        
            $.get(url, '', function(data)
            {
                
                if (next)
                {
                    elm.append(data);
                    active = elm.find('.redactietip:last');
                    css -= redactietip_width;
                } 
                else
                {
                    elm.prepend(data);
                    elm.css('left', (css-redactietip_width) + 'px');
                    active = elm.find('.redactietip:first');
                }
                
                elm.animate({ left: css + 'px' }, 1000);
                active.addClass('redactietip_active');
            
            });
            
        }
        else
        {
            if (next)
            {
                active = active.next();
                css -= redactietip_width;
            }
            else
            {
                active = active.prev();
                css += redactietip_width;
            }
            elm.animate({ left: css + 'px' }, 1000);
            active.addClass('redactietip_active');
        }
        
        return false;
        
    });
    
    /*
        give every menu item an unique identifier if it doesn't have one already
        also check for present submenu items for style changing :D
    */
    counter = 0;
    $('#mainMenuItems li')
    .each(function()
    {
        
        var a = $(this).children('dt').children('a');
        
        if (a.attr('id') == '')
        {
            a.attr('id', 'navigation_' + counter);
            counter++;
        }
        
        id = a.attr('id');
        
        menu_active[id] = false;
        menu_delete_timer[id] = '';
        menu_show_timer[id] = '';
        
        if ($(this).children('dd').length > 0)
        {
            a.addClass ('menulabel-hassubmenu');
        }
        
    });
    delete counter;
    delete id;
    
    /*
        the behaviour for the menu items;
        when you hover it
            check for submenu and show it
            if no submenu is present just show border
        when you mouse it of it
            check for submenu and hide it
            if no submenu is present delete border
        when click on it
            if it has submenu determine wether to show or hide it
    */
    $('#mainMenuItems li a')
    .mouseover(function()
    {
        if ($(this).hasClass('menulabel-hassubmenu'))
        {
            $(this).addClass('menulabel-hassubmenu-selected');
            
            var id = $(this).attr('id');
            clearTimeout (menu_delete_timer[id]);
            clearTimeout (menu_show_timer[id]);
            
            menu_show_timer[id] = setTimeout (function() { showMenu(id) }, 300);
        
        }
        else
        {
            $(this).addClass('menulabel-selected');
        }
        
    })
    .mouseout(function()
    {
        if ($(this).hasClass('menulabel-hassubmenu'))
        {
            var id = $(this).attr('id');
            menu_active[id] = false;
            clearTimeout (menu_delete_timer[id]);
            clearTimeout (menu_show_timer[id]);
            
            if ($(this).hasClass('menulabel-hassubmenu-selected'))
            {
                if ($(this).siblings('div:visible').length == 0)
                {
                    $(this).removeClass('menulabel-hassubmenu-selected');
                }
                menu_delete_timer[id] = setTimeout (function() { clearMenu(id) }, 300);
            }
            
        }
        else
        {
            $(this).removeClass('menulabel-selected');
        }
        
    }).click(function()
    {
        var returnt = true;
        
        if ($(this).hasClass('menulabel-hassubmenu'))
        {
            $(this).addClass('menulabel-hassubmenu-selected');
            
            var id = $(this).attr('id');
            if ($(this).parent().siblings('dd:visible').length == 0)
            {
                showMenu (id);
            }
            else
            {
                clearMenu (id);
            }
            
            returnt = false;
            
        }
        
        $(this).blur();
        return returnt;
       
    });
    
    /* 
        the behaviour for the submenu list;
        when you hover it don't close,
        when you move away let it close
    */
    $('#mainMenuItems li dd')
    .mouseover(function()
    {
        var id = $(this).siblings('dt').children('a').attr('id');
        menu_active[id] = true;
        clearTimeout (menu_delete_timer[id]);
    })
    .mouseout(function()
    {
        var id = $(this).siblings('dt').children('a').attr('id');
        menu_active[id] = false;
        clearTimeout (menu_delete_timer[id]);
        menu_delete_timer[id] = setTimeout (function() { clearMenu(id) }, 300);
    });
    
    $('.adsBlock').each(function()
    {
        var img = $(this).find("img");
        if (img.length > 0 && img.attr('src') !== undefined)
        {
            if(img.attr('src').indexOf('817-grey.gif') > -1)
            {
                $(this).hide();
            }
        }
    });
    
    $('a[href^="http://"]').click( function() {
        pageTracker._trackPageview('/outgoing/' + $(this).attr('href'));
        
        if (! $(this).attr('href').match(document.domain))
        {
            $(this).attr('target', '_new');
        }
        
        return true;
    });
    
    $('.lbOn').colorbox();
    $('#cboxLoadedContent').find('input, textarea').live('click', $.colorbox.resize);
    $('#closePoll').live('click', $.colorbox.close);

});