function ready()
{
	initLightbox();
	baimg = $("#Banner > img");
	blend();

	if ($('[editurl]').size() < 1)
	{
        enableAjax();
        initHashchange();
	}
}

function initHashchange()
{
    $.initHashChange();
    $.hash(function() {
        
        // Bei URLs die bereits /thema enthalten und nun noch #thema bekommen sollen: UMLEITEN
        if (document.location.href.match(/([\w]+)#/))
//        if (document.location.href.match(/((\/)[\w]+)#/))
        {
            href = document.location.href.replace(/(.*)#(.+)/, '/#$2');
//            href = document.location.href.replace(/((\/)[\w]+)#/, '$2#');
            document.location.href = href;
            return;
        }
        
        href = document.location.href.replace(/#/, '') + '/mode/ajax';

        $.get(href, function(data) {
            $('#Content').html(data.body);
            $('title').html(data.title);
            enableAjax();
            initLightbox();
        });
    });
    if ($.hash().length > 0)
    {
        $(document).trigger('hashChange', $.hash());
    }
    
//    if (document.location.pathname.match(/\/.+/))
//    {
//        var href = 'http://' + document.location.hostname + '/#' + document.location.pathname;
//        document.location.href = href;
//    }
}

function initLightbox()
{
    $("[light]").each(function() {
        $('a', this).lightBox();
    });
}

function blend()
{
	$(baimg[baimgi])
		.animate({ opacity: '0.6'}, 3000)
			.delay(timeout-4000)
				.animate({ opacity: '0.0'}, 3000);
	
	baimgi++;
	if (baimgi >= baimg.length)baimgi = 0;
	setTimeout(blend, timeout);
}

function enableAjax()
{
//    var links = $('a[href^="/"]');
    var links = $('a[target="_self"]');
    links.unbind();
    links.click(function(e) {
        var me   = $(this);
        var href = me.attr('href');
        if (href.match(/\//))
        {
            e.preventDefault();
            href = href.replace(/^\//, '');
            $.hash(href);
        }
    });
}

var baimg;
var baimgi = 0;
var timeout = 10000;

$("document").ready(ready);

/**
 * Hash-Change Listener
 */
$.initHashChange = function() {
    var last  = $.hash();
    var sleep = 64;
    var check = function() {
        if ($.hash() != last)
        {
            last = $.hash();
            $(document).trigger('hashChange', last);
        }
    };
    var iid = setInterval(check, sleep);
};

/**
 * Hash-Wert holen und setzen
 */
$.hash = function (value) {
    if (typeof(value) == 'string')
    {
        document.location.hash = value;
    }
    else if (typeof(value) == 'function')
    {
        $(document).bind('hashChange', value);
    }
    else
    {
        var hash = document.location.hash;
        hash = hash.replace(/^#/, '');
        if (hash.length < 1)return false;
        else                return hash;
    }
};

