$(document).ready(function(){
    var menu_ul_width = 0;
    $("#header_bottom_menu>ul>li").each(function(){
        menu_ul_width += $(this).width();
        menu_ul_width += parseInt($('#header_bottom_menu>ul>li').css('margin-left')) ;
    });
    menu_ul_width -= parseInt($('#header_bottom_menu>ul>li').css('margin-left')) ;
    $('#header_bottom_menu ul.sub2').css('width',menu_ul_width);
    $('#header_bottom_menu ul.sub2').each(function(){
        var curul = $(this);
        var all = $(this).find('li').length;
        var num = Math.ceil(all / 4);
        for (i=0; i<num; i++) {
            $(this).append('<li class="column'+i+'"></li>');
        }
        var j = -1;
        for (i=0; i<all; i++) {
            if (i%4 == 0) {
                j++;
            }
            $(this).find('li.column'+j).append($(this).find('li:eq('+i+')').html());
        }
        $(this).find('li:lt('+all+')').remove();
    });
    $('#header_bottom_menu>ul>li:eq(0)').find('ul.sub2').css('width','70');
    $('#header_bottom_menu>ul>li:eq(1)').find('ul.sub2').css('width','166');
    $('#header_bottom_menu>ul>li:eq(2)').find('ul.sub2').css('width','250');




     if ($('div.allinone .itemlist ul li a:eq(0)').length) {
        $('div#aioimage').html('<a></a>');
        $('div#aioimage a').attr('target','_blank');
        $('div#aiotitle').html('<a></a>');
        $('div#aiotitle a').attr('target','_blank')
        var first = $('div.allinone .itemlist ul li a:eq(0)');
        $('div#aioimage a').attr('href', first.attr('href')).attr('rel', first.attr('rel'));
        $('div#aiotitle a').attr('href', first.attr('href')).attr('rel', first.attr('rel'));
        first.find('.themesmall').clone().prependTo($('div#aioimage a'));
        first.find('.title').clone().prependTo($('div#aiotitle a'));
    }


    $('div.allinone .itemlist ul li a').click(function(){
        $('div#aioimage a').empty().attr('href', $(this).attr('href')).attr('rel', $(this).attr('rel'));
        $('div#aiotitle a').empty().attr('href', $(this).attr('href')).attr('rel', $(this).attr('rel'));
        $(this).find('.themesmall').clone().prependTo($('div#aioimage a'));
        $(this).find('.title').clone().prependTo($('div#aiotitle a'));
        var cc = $(this).attr('rel');
        if (!$(this).attr('rel').length) {
            cc = $(this).attr('rev');
        }
        var cc_array = cc.split("#");
        
        function vw_js_rotate_color (images_array) {
             if (images_array.length > 1) {
                if ($('div#aioblock div#aioimage #rotatealtimg').length) {
                    $('#rotatealtimg').show();
                } else {
                    $('div#aioblock div#aioimage').append('<div id="rotatealtimg"></div>');
                }
                var current_i = 1;
                $('#rotatealtimg').click(function(){
                    $('div#aioblock div#aioimage span.themesmall').css('backgroundImage','url('+images_array[current_i]+')');
                    current_i++;
                    if (current_i >= images_array.length) {
                        current_i = 0;
                    }
                });
            } else {
                $('#rotatealtimg').hide();
            }
        }

        if (cc_array[2]) {
            $.ajax({
                url: SD+'ee_ajax_transport.php',
                data: 'f=ajax_vw&id=' + cc_array[2] + '&o=altimage',
                type: 'POST',
                dataType : 'xml',
                success: function(data){
                    i=0;
                    images_array = undefined;
                    images_array = new Array();
                    $(data).find('image').each(function(){
                        images_array[i] = $(this).find('url').text();
                        i++;
                    });
                },
                cache: false,
                complete: function(){
                    vw_js_rotate_color (images_array);
                }
            });
        }
        return false;
    });

    $('#aioimage a').click(function(){return false});

});
