$(document).ready(function()
{
    $('a.popup-social').popupWindow(
    { 
        width           : 500,
        height          : 400, 
        centerBrowser   : 1 
    }); 
    
    // Ajax- und Drag-Funktion für Internet-Explorer 6 deaktivieren
    if(!($.browser.msie && $.browser.version < "7.0"))
    {
        var start = true;
        
        // Verwalten der History
        $.history.init(function(hash)
        {
            if(hash == "")
            {
                if(start == false) ajax_load("");
            }
            else
            {
                ajax_load(hash);
            }
            
            start = false;
        },
        { unescape: ",/" });
        
        // Links der Hauptnavigation abfangen
        $("#navigation a:not(a.blog), a#logo, ul.info-list a, a.ajax").live("click", function()
        {
            var href = $(this).attr('href');
            
            $.history.load($(this).attr('href'));   
                
            return false;
        });
        
        
        // Absenden von Formularen abfangen.
        $('form').live("submit", function()
        {
            var category = $(this).attr("action");
            var link     = category;
            var fields   = $(this).serialize();
            
            jQuery.ajax(
            {
                url         : link,
                type: 'POST',
                dataType    : 'json',
                data        : "ajax=content&"+fields+"&submit=submit",
                success     : function(data)
                              {
                                  obj = data;
                                  
                                  $('#wrapper').html(obj['content']);
                              },
                error       : function(data)
                              {
                                  //$('#'+location).animate({opacity: 1}, 500);
                              }
            });
           
            return false;
        });
        
        // Draggable live, damit nach Ajax-Abfragen die Drag-Funktion erhalten bleibt
        jQuery.fn.liveDraggable = function (opts) {
          this.live("mouseover", function() {
             if (!$(this).data("init")) {
                $(this).data("init", true).draggable(opts);
             }
          });
       };
    
        // Klasse der Infoliste entfernen, damit JavaScript das aufklappen übernimmt 
        $('ul.info-list').removeClass('no-script');
            
        // Macht die einzelnen Punkte der Liste verschiebbar
        $( "ul.info-list li" ).liveDraggable(
        {
            start       : function(event, ui)
                          {
                              $(event.target).children('.content').css('opacity', 0);
                              $(event.target).children('.content').hide();
                              $(event.target).addClass('dragging');
                          },
            stop        : function(event, ui)
                          {
                              $(event.target).removeClass('dragging');
                              $(event.target).trigger('mouseover');
                          },
            opacity     : 0.5,
            distance    : 10,
            containment : "#body"
        });
    }
    
    // Vibration der Plusse
    var vibrateIndex = 0;
    var vibrateDelay = 350;
    
    function vibrateInfoList()
    {
        $('ul.info-list li').each(function()
        {
            var element = $(this);
            
            setTimeout(function()
            {
                element.vibrate();
            }, 
            vibrateIndex*vibrateDelay);
            
            vibrateIndex++; 
        });
    }

    vibrateInfoList();
    
    // Ausklappen der Inhaltsbox für die Infoliste
    $('ul.info-list li:not(li.dragging)').live("mouseover", function()
    {
        if(!$(this).data('initHoverIntent'))
        {
            $(this).data('initHoverIntent', true);
            
            $('ul.info-list li:not(li.dragging)').hoverIntent(
            {
                timeout: 20,
                over: function () {
                        
                    // Setzt z-Index, damit die Inhalte nicht unter den "Plussen" liegt
                    $("ul.info-list li").css('zIndex', '150');
                    $(this).css('zIndex', '151');
                        
                    // Ermittelt die Position und Größe der Elemente
                    var content = $(this).children('.content');
                    var left    = parseInt($(this).css('left'), 10);
                    var top     = parseInt($(this).css('top'), 10);
                    var height  = content.height() +  17; 
                        
                    // Setz X-Position für Inhalt
                    if(left >= 492)
                        content.css('left', '-237px');    
                    else
                        content.css('left', '11px');
                        
                    // Setz Y-Position für Inhalt    
                    if(top >= 263)
                        content.css('top', '-'+height+'px');    
                    else
                        content.css('top', '11px');
                        
                    // Zeigt Inhaltselement an
                    content.css('opacity', 0);
                    content.show();
                    content.stop().animate({opacity: 1}, 300);
                },
                out: function(){
                    var content = $(this).children('.content');
                        
                    // Blendet Inhaltselement aus
                    content.stop().animate({opacity: 0}, 300, function()
                    {
                        content.hide();    
                    });
                    }
            });
                
            $(this).trigger("mouseover");
        }
    });
    
    // Bewegung des Pfeils in der Hauptnavigation
    $('#navigation').hoverIntent(
    {
        timeout : 20,
        over    : function(){ $('#arrow').animate({top: '50px'}, {duration: 450, easing: "easeOutBack"}); },
        out     : function(){ $('#arrow').animate({top: '0px'}, {duration: 850, easing: "easeOutElastic"}); }
    });
    
    // Durchlaufen des Pfeils in der Hauptnavigation wenn ein Punkt angeklickt wurde.
    $('#navigation li a').click(function()
    {
        $('#navigation li').removeClass('active');
        $(this).parent().addClass('active');
        
         $('#arrow').animate({top: '100px', 'opacity': 0}, 250, function()
         {
            $(this).css({top: '-80px'}).animate({top: '50px', 'opacity': 1}, {duration: 350, easing: 'easeOutBack'});
         });
    });
    
    // Ausführen der Ajax-Anweisung
    function ajax_load(address)
    {
        var obj;
        var link = address;
        
        if(address == "") link = "index.php";
        
        $('#body').css('backgroundImage', $('#content').css('backgroundImage'));
        
        $('#wrapper').animate({opacity: 0}, 250, function()
        {
            jQuery.ajax(
            {
                url         : link,
                dataType    : 'json',
                data        : "ajax=content",
                success     : function(data)
                              {
                                  obj = data;
                        
                                  document.title = obj['title'];
                            
                                  $('#wrapper').html(obj['content']);
                                  $('#wrapper').animate({opacity: 1}, 750);
                                  $('ul.info-list').removeClass('no-script'); 
                                  
                                  vibrateInfoList();
                              },
                error       : function(data)
                              {
                                  //$('#'+location).animate({opacity: 1}, 500);
                              }
            });
        });
    }
});
