jQuery(function($) {
/** Tutorial, Webseite mit jQuery animieren
 * @requires jQuery v1.3
 * http://olaf-bosch.de/tutorial-webseite-mit-jquery-animieren-teil-1/
 * Copyright (c) 2008-2009 Olaf Bosch (http://olaf-bosch.de/)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0
 */
 
// Animation der oberen Navigation, http://olaf-bosch.de/wp-content/obosch/templates/juery/tutorial-1/navigation.html
    $('#nav ul li').ahover({toggleEffect: 'height', moveSpeed: 300, toggleSpeed: 150});

// Klapp Menue http://olaf-bosch.de/wp-content/obosch/templates/juery/tutorial-1/klapp-menu.html
    
	// get the value for the itemID query parameter   
	
	
	
	
	//$(var_seite).css('list-style-image', 'url("../images/navigation/pfote_klein_rot.gif")');
	
	
	/*
	$(var_seite).append(' &raquo;');
	$('#sidebar li.categories li').each(function(i){
				
				if($(this).children(':has(.cat-item)')){
				i = i+1;	
				}
				
			});
	*/
	var_seite = jQuery.url.param("seite") // returns 2   
	var_seite = '#' + var_seite;
	var_unter = jQuery.url.param("unter") // returns 2   
	var_unter = '#' + var_unter;
	
	
	//jAlert(var_seite, 'Alert Dialog');
	
	//$(var_seite).css('list-style-image', 'url("../images/navigation/pfote_klein_rot.gif")');
	//$(var_seite).css('font-weight','bold');
	
	$(var_seite).addClass('cat-item current-cat');
	
	
	var $klapp = $('#sidebar li.categories li');
	
    $klapp.filter(':not(.current-cat)').children('ul').css("display","none");
    $klapp.filter(':has(ul)').children(':first-child').append(' &raquo;').click(function(){
      $(this).parent().siblings().children('ul:visible').slideUp('normal');
      $(this).next().slideToggle('normal');
      return false;
    });


	$(var_seite).addClass("aktive");
	$(var_unter).addClass("aktive");


/** laden der Seiten per Ajax, http://olaf-bosch.de/wp-content/obosch/templates/juery/tutorial-1/ajax.html
 * ich verwende hier 2 Funktionen aus:
 * History/Remote - jQuery plugin for enabling history support and bookmarking
 * @requires jQuery v1.0.3
 * http://stilbuero.de/jquery/history/
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 0.2.3
 */
$.ajaxHistory = new function() {
    var RESET_EVENT = 'historyReset';
    var _currentHash = location.hash;
    var _intervalId = null;
    var _observeHistory; // define outside if/else required by Opera
    this.update = function() { }; // empty function body for graceful degradation
    // create custom event for state reset
    var _defaultReset = function() {
        $('.remote-output').empty();
    };
    $(document).bind(RESET_EVENT, _defaultReset);
    
    // TODO fix for Safari 3
    // if ($.browser.msie)
    // else if hash != _currentHash
    // else check history length
    if ($.browser.msie) {
        var _historyIframe, initialized = false; // for IE
        // add hidden iframe
        $(function() {
            _historyIframe = $('<iframe style="display: none;"></iframe>').appendTo(document.body).get(0);
            var iframe = _historyIframe.contentWindow.document;
            // create initial history entry
            iframe.open();
            iframe.close();
            if (_currentHash && _currentHash != '#') {
                iframe.location.hash = _currentHash.replace('#', '');
            }
        });
        this.update = function(hash) {
            _currentHash = hash;
            var iframe = _historyIframe.contentWindow.document;
            iframe.open();
            iframe.close();
            iframe.location.hash = hash.replace('#', '');
        };
        _observeHistory = function() {
            var iframe = _historyIframe.contentWindow.document;
            var iframeHash = iframe.location.hash;
            if (iframeHash != _currentHash) {
                _currentHash = iframeHash;
                if (iframeHash && iframeHash != '#') {
                    // order does matter, set location.hash after triggering the click...
                    $('a[href$="' + iframeHash + '"]').click();
                    location.hash = iframeHash;
                } else if (initialized) {
                    location.hash = '';
                    $(document).trigger(RESET_EVENT);
                }
            }
            initialized = true;
        };
    } else if ($.browser.mozilla || $.browser.opera) {
        this.update = function(hash) {
            _currentHash = hash;
        };
        _observeHistory = function() {
            if (location.hash) {
                if (_currentHash != location.hash) {
                    _currentHash = location.hash;
                    $('a[href$="' + _currentHash + '"]').click();
                }
            } else if (_currentHash) {
                _currentHash = '';
                $(document).trigger(RESET_EVENT);
            }
        };
    } else if ($.browser.safari) {
        var _backStack, _forwardStack, _addHistory; // for Safari
        // etablish back/forward stacks
        $(function() {
            _backStack = [];
            _backStack.length = history.length;
            _forwardStack = [];
        });
        var isFirst = false, initialized = false;
        _addHistory = function(hash) {
            _backStack.push(hash);
            _forwardStack.length = 0; // clear forwardStack (true click occured)
            isFirst = false;
        };
        this.update = function(hash) {
            _currentHash = hash;
            _addHistory(_currentHash);
        };
        _observeHistory = function() {
            var historyDelta = history.length - _backStack.length;
            if (historyDelta) { // back or forward button has been pushed
                isFirst = false;
                if (historyDelta < 0) { // back button has been pushed
                    // move items to forward stack
                    for (var i = 0; i < Math.abs(historyDelta); i++) _forwardStack.unshift(_backStack.pop());
                } else { // forward button has been pushed
                    // move items to back stack
                    for (var i = 0; i < historyDelta; i++) _backStack.push(_forwardStack.shift());
                }
                var cachedHash = _backStack[_backStack.length - 1];
                $('a[href$="' + cachedHash + '"]').click();
                _currentHash = location.hash;
            } else if (_backStack[_backStack.length - 1] == undefined && !isFirst) {
                // back button has been pushed to beginning and URL already pointed to hash (e.g. a bookmark)
                // document.URL doesn't change in Safari
                if (document.URL.indexOf('#') >= 0) {
                    $('a[href$="' + '#' + document.URL.split('#')[1] + '"]').click();
                } else if (initialized) {
                    $(document).trigger(RESET_EVENT);
                }
                isFirst = true;
            }
            initialized = true;
        };
    }
    this.initialize = function(callback) {
        // custom callback to reset app state (no hash in url)
        if (typeof callback == 'function') {
            $(document).unbind(RESET_EVENT, _defaultReset).bind(RESET_EVENT, callback);
        }
        // start observer
        if (_observeHistory && _intervalId == null) {
            _intervalId = setInterval(_observeHistory, 200); // Safari needs at least 200 ms
        }
    };
};

// olaf content dazu
$.fn.remote = function(output, content, settings, callback) {
    callback = callback || function() {};
    if (typeof settings == 'function') { // shift arguments
        callback = settings;
    }
    settings = $.extend({
        hashPrefix: 'remote-'
    }, settings || {});
    var target = $(output).size() && $(output) || $('<div></div>').appendTo('body');
    target.addClass('remote-output');
    return this.each(function(i) {
        var href = this.href, hash = '#' + (this.title && this.title.replace(/\s/g, '_') || settings.hashPrefix + (i + 1)),
            a = this;
        this.href = hash;
        $(this).click(function(e) {
            // lock target to prevent double loading in Firefox
            if (!target['locked']) {
                // add to history only if true click occured, not a triggered click
                if (e.clientX) {
                    $.ajaxHistory.update(hash);
                }
           // olaf ab hier von mir angepasst
           $.get(href, function(data){
             var div = $(data).find(content);
             target.fadeOut("slow",function(){
             if ($.browser.msie){
               this.style.removeAttribute('filter');
             };
             target.html(div).slideDown(1500);
             target['locked'] = null;
             callback.apply(a);
             document.title = $.trim(data.replace( /([\s\S]+)<title>/, '' ).replace(/<\/title>([\s\S]+)/, '' ));
           });
           // olaf bis hier
                });
            }
        });
    });
};

// Ajax starten
  $.ajaxHistory.initialize();
  $("#nav a").remote('#content', '.entry', function() {
    $(this).parent().parent().children().removeClass("current_page_item");
    $(this).parent().addClass("current_page_item");
    code_viewer();
  });
// aus ajaxHistory
  if (location.hash && typeof _addHistory == 'undefined') {
    $('a[href$="' + location.hash + '"]').trigger('click');
  }
// Ende Ajax starten

/*
// gleiche Funktion ohne Back-Forward-Funktion
  $("#nav a").click(function(){
    $.get($(this)[0].href, function(data){
      var div = $(data).find(".entry");
      $("#content").fadeOut("slow",function(){
        $(this).html(div).slideDown(2000);
        if ($.browser.msie){
          this.style.removeAttribute('filter');
        };
        code_viewer();
      });
    });
    $(this).parent().parent().children().removeClass("current_page_item");
    $(this).parent().addClass("current_page_item");
    return false;
  });
  */
});

