/* $Id: ajax.js 102 2012-01-22 17:15:11Z dorian $ */

$(document).ready(function () {
    "use strict";
    var History = window.History;
    var still_loading = false;
    
    $(document).delegate('a.ajax', 'click', function (e) {
        e.preventDefault();
        
        if (still_loading) {
            return false;
        }
        still_loading = true;
        
        var newURL = $(this).attr('href');
        var $navElement = $('ul#side-nav a[href="' + newURL + '"]');

        $.get(newURL, function (data) {
            still_loading = false;
            var $newContent = $(data);
            // We cannot use jQuery to get the title as the browser's innerHTML function it employs will strip the title tag
            var newTitle = /<title>(.*)<\/title>/.exec(data)[1];
            $('div#sub-column-left').empty().html($newContent.find('div#ajax-loader'));
            $('div#ajax-loader').css('opacity', 0);
            $('ul#side-nav li').removeClass('nav-active');
            $('ul#side-nav h2').removeClass('active');
            $navElement.parents('li.nav-single').toggleClass('nav-active', true);
            $navElement.parents('li.nav-sub').toggleClass('nav-closed', false);
            $navElement.parents('li.nav-sub').toggleClass('nav-open', true);
            $navElement.parents('li.nav-sub, li.nav-single').find('h2').toggleClass('active', true);
            $navElement.parents('li.nav-second').toggleClass('nav-active', true);
            if (! $navElement.parents('ul.side-nav-sublevel').is(':visible')) {
                $navElement.parents('ul.side-nav-sublevel').slideDown(250, function () {
                    accordion_mutex = false;
                });
            }
            var newHeight = Math.max($('div#ajax-loader').outerHeight(true), $('div#column-left').outerHeight(true));            
            $('div#sub-column-left').animate({height: newHeight}, 100, function () {
                $('div#sub-column-left').css('height', ''); // Remove custom height when we're done (because of textarea resize issues)
            });
            $('div#ajax-loader').animate({opacity: 1}, 200, function () {
                // Track page view in Analytics and set new URL in location bar if possible
                _gaq.push(['_trackPageview', '/' + newURL]);
                if (History.enabled && !$.browser.opera) { // Opera seems to have an issue with replaceState and <base> tags...
                    History.replaceState(null, newTitle, newURL);
                }
            });
        });
        
        hideOldContent();
        
    });
    
    $(document).delegate('form#form-contact', 'submit', function (e) {
        e.preventDefault();
        
        if (still_loading) {
            return false;
        }
        still_loading = true;
        
        var $f = $(this),
            url = $f.attr('action'),
            url_data = $f.serialize();
        
        // Interestingly, jQuery.serialize does not serialize the value of the 'submit' element itself
        var $submit = $f.find('input#form-submit');
        var param_obj = {};
        param_obj[$submit.attr('name')] = $submit.attr('value');
        url_data += ('&' + $.param(param_obj));
        $.post(url, url_data, function (response) {
            still_loading = false;
            var $sub_col = $('div#sub-column-left');
            var $newContent = $(response);
            $sub_col.empty().html($newContent.find('div#ajax-loader'));
            var $loader = $('div#ajax-loader');
            $loader.css('opacity', 0);
            var newHeight = Math.max($('div#ajax-loader').outerHeight(true), $('div#column-left').outerHeight(true));            
            $sub_col.animate({height: newHeight}, 100);                
            $loader.animate({opacity: 1}, 200);
            if ($loader.find("div#form-complete").length > 0) {
                // Form was successfully completed, track as event
                _gaq.push(['_trackEvent', 'Contact Form', 'Sent']);
            }
        });
            
        hideOldContent();
       
    });
    
    function hideOldContent() {
        $('div#ajax-loader').animate({opacity: 0}, 200, function () {
            if (still_loading) {
                $('div#sub-column-left').css('height', $('div#sub-column-left').height()); // Fix the height until we are done loading the new content                
                $('div#sub-column-left').empty().html('<div style="width:32px; margin:100px auto 0 auto;"><img src="/fileadmin/img/ajax-loader.gif" width="32" height="32" alt="Loading indicator"></div>');
            }
        });
    }
    
    
    var handler_in = function () {
        $(this).children('span.team-pic-overlay').animate({opacity: 1}, 200);
    };
    
    var handler_out = function () {
        $(this).children('span.team-pic-overlay').animate({opacity: 0}, 200);
    };
    
    $('div#main').delegate('.tx-teampic-pi1 div.team-pic-box', 'mouseenter', handler_in);
    $('div#main').delegate('.tx-teampic-pi1 div.team-pic-box', 'mouseleave', handler_out);
    

});
