(function($){
    $.aweb = function(){
        function rebuildlinks(){
            src = $('a');
            src.each(function(){
                src = $(this).attr('href');
                if (!$(this).hasClass('ext')) {
                    if (src == '/' || src == '/de/' || src == '/en/'){
                        reb = '#' + src;
                    } else {
                        reb = '#/' + src;
                    }
                    $(this).attr('href', reb);
                }
            });
            
            
        }
        
        function closeall(){
            change_meta('');
            closebox(0);
        }
        
        function closebox(box){
            obox = $('#navigation .box.opened');
            if (obox.length == 0){
                openbox(box);
            }
            oboxTitle = obox.find('.box-title');
            oboxContentWrapper = obox.find('.box-content-wrapper');
            oboxContent = oboxContentWrapper.find('.box-content');
            
            oboxContent.animate({
                'opacity' : '0.0'
            }, 200, function(){
                oboxContentWrapper.slideUp(300, function(){
                    oboxTitle.fadeIn(600);
                    if (box != 0){ openbox(box); } else {
                        el.animate({ paddingLeft : '140px' }, 600);
                    }
                    obox.animate({
                        width : '110px'
                    }, 599, function(){
                        obox.removeClass('opened');
                    });
                });
            });
            
            
        }
        
        function change_meta(title, url){
            if (title != '') {
                title = 'creative cubes - mobile web solutions | ' + title;
                url = '#' + url;
            } else {
                title = 'creative cubes - mobile web solutions';
                url = '#/';
            }
            
            meta_title = $('title');
            meta_title.text(title);
            window.location.href=url;
        }
        
        function openbox(box){
            nav = $('#navigation');
            boxTitle = box.find('.box-title');
            boxContentWrapper = box.find('.box-content-wrapper');
            boxContent = boxContentWrapper.find('.box-content');
            url = box.attr('alt');
            nav.animate({ paddingLeft : '0px' }, 600);
            boxTitle.fadeOut(600);
            box.animate({
                width : '460px'
            }, 600, function(){
                boxContentWrapper.slideDown(300, function(){
                    boxContent.animate({
                        'opacity' : 1.0
                    },function(){
                        box.addClass('opened');
                        box.addClass('contentloaded');
                    });
                });
            });
            title = box.attr('title');
            change_meta(title, url);
        }
        
        function buildcontent(box){
            if (!box.hasClass('contentloaded')) {
                id = box.attr('rel');
                $.ajax({
                    url     : '/getjscontent/' + id + '/',
                    type    : 'get',
                    success : function(data){box.find('.box-content').append(data + '<div class="clear"></div>');}
                });
            }
            closebox(box);
        }
        
        function buildpage(){
            var current_url = document.URL.substring(document.URL.indexOf('#')+1 || document.URL.length);
            el = $('#navigation');
            
            if (current_url == '' || current_url == '/') {
                /*
                wt = $('#page #welcometext').show();
                console.log("before");
                wt.delay(1000).fadeOut( 200,function(){ 
                    $(this).remove(); 
                    console.log("after");
                });
                */
            }
            
            if (el.hasClass('opened')){ el.removeClass('opened'); }
            aList = el.find('a');
            aList.each(function(i){
                var last, openbox;
                id = $(this).attr('id');
                slug = $(this).attr('href');
                title = $(this).text();
                color = $(this).attr('class');
                color = color.replace('linkbutton', '').replace(' ', '');
                classes = color;
                if (parseInt(aList.length) - parseInt(1) == i) { classes = classes + ' last'; }
                if (current_url == '' || current_url == '/') { classes = classes + ' hidden'; }
                box = '<div class="box ' + classes + '" rel="' + id + '" alt="' + slug +  '" title="' + title + '">'
                        + '<div class="box-title">' + title + '</div>'
                        + '<div class="box-content-wrapper">'
                        + '<div class="box-content">'
                        + '<div class="box-content-outer"><span></span>' + title + '<span class="close"></span></div>'
                        + '</div>'
                        + '</div>';
                el.append(box);
                
                if (current_url == slug){
                    box = $('.box[rel=' + id + ']');
                    buildcontent(box);
                }
                
            });
            aList.remove();
            box = $('#navigation .box');
            if(box.hasClass('hidden')){
                box.each(function(i){
                    if (i == 0){ $(this).delay(2*800).fadeIn(800);
                    } else { i = i + 2; $(this).delay(i*800).fadeIn(800); }
                });
            }
            
            box.click(function(){
                box = $(this);
                if(!box.hasClass('opened') && !$(':animated').length){
                    buildcontent(box);
                }
            });
            
            boxClose = $('#navigation .box').find('span.close');
            boxClose.click(function(){
                closeall();
            });
            
            homeButton = $('#logo a.homebutton');
            
            logo = $('#logo a.homebutton.logo');
            logo.hover(function(){
                logo.find('span').stop().animate({ 'opacity' : 1.0 }, 200);
            }, function(){
                logo.find('span').stop().animate({ 'opacity' : 0.0 });
            });
            
            homeButton.click(function(){ closeall(); });
            
            langSwitch = $('#languageswitch span');
            langSwitch.click(function(){
                langSwitch.addClass('active');
                $(this).parent().find('ul').fadeIn(400);
                $(this).parent().find('ul').hover(function(){
                }, function(){
                    langSwitch.removeClass('active');
                    $(this).parent().find('ul').hide();
                });
            })
            
            $(document).keydown(function(e) {
                if (e.keyCode == 27){
                    e.preventDefault();
                    closeall();
                }
            });
        }
        
        function footer(){
            a = $('#footer-techs a');
            a.hover(function(){
                a = $(this);
                aL = a.offset().left;
                b = $('#footer-techs .tech-bubble');
                bc = b.find('.tech-bubble-content');
                ba = b.find('.tech-bubble-arrow')
                bc.html('Besuchen Sie: ' + a.attr('href').replace('http://', '').replace('/', ''));
                np = parseInt(aL) - parseInt(163);
                b.css({ marginLeft : np + 'px' }).show();
                b.stop().animate({ opacity : 0.6 }, 1200);
            }, function(){
                b = $('#footer-techs .tech-bubble');
                b.stop().animate({ opacity : 0.0 }, 200, function(){
                    b.hide();
                });
            });
        }
        
        buildpage();
        rebuildlinks();
        footer();
    }
})(jQuery);

$(document).ready(function(){
    $.aweb();
});

