/* -------------------------------------------------------------
    AUTHOR: Stefan Nafra
    UPDATED: 20.01.2011
    CONTENT: Diverse Sortier, Gruppier und Blätterfunktionen.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* -------------------------------------------------------------
    01. Sortiert eine Liste.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function sortElements(criterion, order, date, thisLink) {

    var sortableList = ($(thisLink).parents(':eq(1)').is('li')) ? $(thisLink).closest('ul') : $('ul.sortable'), listItems = sortableList.children('li:not(.poi-header)').get(), element1Text, element2Text, dateRegex = /^(\d{2})[\.](\d{2})[\.](\d{4})/;

    if ($(thisLink).parents(':eq(1)').is('li')) {
        if ($(thisLink).parent().html().match(/'asc'/)) {
            $(thisLink).parent().html($(thisLink).parent().html().replace(/'asc'/, '\'desc\''));
        } else {
            $(thisLink).parent().html($(thisLink).parent().html().replace(/'desc'/, '\'asc\''));
        }
    } else {
        $('a.sort-' + criterion).toggle();
    }

    listItems.sort(function (element1, element2) {

        if (date !== true) {
            element1Text = $(element1).find('.sortby-' + criterion).text().toUpperCase();
            element2Text = $(element2).find('.sortby-' + criterion).text().toUpperCase();
        } else {
            element1Text = $(element1).find('.sortby-' + criterion).text().replace(dateRegex, '$3$2$1');
            element2Text = $(element2).find('.sortby-' + criterion).text().replace(dateRegex, '$3$2$1');
        }

        if (order !== 'desc') {

            // element1Text = (element1Text === 'XXX') ? 99999999 : element1Text;
            // element2Text = (element2Text === 'XXX') ? 99999999 : element2Text;
            return (element1Text < element2Text) ? -1 : (element1Text > element2Text) ? 1 : 0;
        } else {
            element1Text = (element1Text === 'XXX') ? 0 : element1Text;
            element2Text = (element2Text === 'XXX') ? 0 : element2Text;
            return (element1Text > element2Text) ? -1 : (element1Text < element2Text) ? 1 : 0;
        }
    });

    $.each(listItems, function (i, val) {
        sortableList.append(val);
    });

    $('a.d-setnr1').click();
}

/* -------------------------------------------------------------
    02. Set-Generator
        02.1. Erstellt beim Seitenaufbau die Sets.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    function initList() {
        $('a.d-setnr1').each(function () {

            var $this = $(this);

            if ($this.parent().children('a.active').text() < 2) {
                if ($.url.param('cd') === '') {
                    $this.addClass('active').parent().prev().children('li:gt(' + (parseInt($this.attr('class').split(' ')[2].match(/\d+/), 10) - 1) + ')').hide();
                    // dirty
                    $('.contentlist li:gt(' + (parseInt($this.attr('class').split(' ')[2].match(/\d+/), 10) - 1) + '), .dynamiclist li:gt(' + (parseInt($this.attr('class').split(' ')[2].match(/\d+/), 10) - 1) + ')').hide();
                } else {
                    $this.parent().children('a.d-setnr' + parseInt($.url.param('cd'), 10)).addClass('active').click();
                }
            }
        });
        $('a.d-setnr1').click();
    }
    
    jbox.ready(function(){
        if('teaser' in jbox)
        {
            jbox['teaser'].ready(function(){
                initList();
                console.log("initList");
            },true);
        }
        else
        {
            initList();
        }
    });

    $('h3[id^=teaserbox]').live('click', function () {
        $('a.d-setnr1').parent().prev().children().show();
        initList();
        console.log("ldkh");
    });
});

/* -------------------------------------------------------------
        02.2. Zeigt das ausgewählte Set an.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('a.d-set:not(a.d-set-next, a.d-set-pre)').live('click', function () {

    var $this = $(this), attr = $this.attr('class').split(' ');

    $this.parent().children('a.active').removeClass('active').parent().children('a.d-setnr' + $(this).text()).addClass('active');
    $this.parent().prev().children().hide().slice(parseInt(attr[1].match(/\d+/), 10) * parseInt(attr[2].match(/\d+/), 10) - parseInt(attr[2].match(/\d+/), 10), parseInt(attr[1].match(/\d+/), 10) * parseInt(attr[2].match(/\d+/), 10)).show();
    // dirty
    $('.contentlist li, .dynamiclist li').hide().slice(parseInt(attr[1].match(/\d+/), 10) * parseInt(attr[2].match(/\d+/), 10) - parseInt(attr[2].match(/\d+/), 10), parseInt(attr[1].match(/\d+/), 10) * parseInt(attr[2].match(/\d+/), 10)).show();
    window.scrollTo(0, 0);

    $('div.einschraenkung input').each(function () {
        $(this).removeAttr('checked');
    });
});

/* -------------------------------------------------------------
    03. Slider
        03.1. Versteckt beim Laden die anderen Bereiche.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    $('a.d-set-pre').each(function () {
        $(this).parent().children('ul').children('li:gt(' + (parseInt($(this).attr('class').split(' ')[2].match(/\d+/), 10) - 1) + ')').hide();
    });
});

/* -------------------------------------------------------------
        03.2. Rückt das Set um eins weiter.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('a.d-set-next').live('click', function () {

    var attr = $(this).attr('class').split(' '), $ul = $(this).parent().children('ul'), $li = $ul.children(), $liLastVisible = $ul.children('li:visible:last');

    if ($liLastVisible.next().length > 0) {
        $li.hide().slice($li.index($liLastVisible) + 1, $li.index($liLastVisible) + parseInt(attr[2].match(/\d+/), 10) + 1).show();
        if ($ul.children('li:visible').length < attr[2].match(/\d+/)) {
            $li.hide().slice($li.index($ul.children('li:last')) - parseInt(attr[2].match(/\d+/), 10) + 1, $li.index($ul.children('li:last')) + 1).show();
        }
    } else {
        $li.hide().slice(0, parseInt(attr[2].match(/\d+/), 10)).show();
    }
});

/* -------------------------------------------------------------
        03.3. Rückt das Set um eins zurück.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('a.d-set-pre').live('click', function () {

    var attr = $(this).attr('class').split(' '), $ul = $(this).parent().children('ul'), $li = $ul.children(), $liFirstVisible = $ul.children('li:visible:first');

    if ($liFirstVisible.prev().length > 0) {
        $li.hide().slice($li.index($liFirstVisible) - parseInt(attr[2].match(/\d+/), 10), $li.index($liFirstVisible)).show();
        if ($ul.children('li:visible').length === 0) {
            $ul.children('li:lt(' + parseInt(attr[2].match(/\d+/), 10) + ')').show();
        }
    } else {
        $li.hide().slice($li.index($ul.children('li:last')) - parseInt(attr[2].match(/\d+/), 10) + 1, $li.index($ul.children('li:last')) + 1).show();
    }
});

/* -------------------------------------------------------------
        03.3. Vergrößert den ausgewählten LI beim darüberfahren.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

// Eine Funktion um einen ausgewählten Li in die Mitte zu rücken.
//function slide(middleID, liID, ulID) {
//    if (liID > middleID && $('.' + ulID + ' li:visible:last').next().length > 0) {
//        $('.' + ulID + ' li:visible:last').next().show();
//        $('.' + ulID + ' li:visible:first').hide();
//        liID -= 1;
//        slide(middleID, liID, ulID);
//    } else if (liID < middleID && $('.' + ulID + ' li:visible:first').prev().length > 0) {
//        $('.' + ulID + ' li:visible:first').prev().show();
//        $('.' + ulID + ' li:visible:last').hide();
//        liID += 1;
//        slide(middleID, liID, ulID);
//    }
//}

$(function () {

    $('.slidepanel li').mouseenter(function () {

        //nur bei click
        //var middle = Math.ceil((parseInt($('.' + $(this).parent().attr('class') + ' li:visible:first').attr('class').match(/\d+/), 10) + parseInt($('.' + $(this).parent().attr('class') + ' li:visible:last').attr('class').match(/\d+/), 10)) / 2);
        //slide(middle, parseInt($(this).attr('class').match(/\d+/), 10), $(this).parent().attr('class'));

        $('#imagehover').stop(true).css({
            'left' : $(this).position().left - 70,
            'top' : $(this).position().top
        }).html($(this).html()).show().animate({
            top: $(this).position().top - 15
        }, 300, 'linear').find('img').css('opacity', '.1').animate({
            width: "142px",
            opacity: 1,
            height: "204px"
        }, 300, 'linear', function () {
            $('#imagehover').children(':eq(1)').show();
        });
    });
});

$('#imagehover a').live('mouseout', function () {
    $('#imagehover').stop().removeAttr('style').hide().children(':eq(1)').hide();
});

/* -------------------------------------------------------------
        03.4. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

var autoSlide;

function slide() {
    if ($('div.channelbox ul.slide li.hovered').next().length > 0) {
        $('div.channelbox ul.slide li.hovered').next().mouseover();
    } else {
        $('div.channelbox ul.slide li:eq(0)').mouseover();
    }
}

function startSlide() {
    autoSlide = window.setInterval('slide()', 3000);
}

function stopSlide() {
    window.clearInterval(autoSlide);
    autoSlide = '';
}

$('li[class*=swapimage]').live('mouseover', function (event) {

    $('li[class*=swapimage]').removeClass('hovered');

    var $this = $(this), replaceWith = $this.addClass('hovered').find('img'), liClass = $.grep($this.attr('class').split(' '), function (value) {
        return value.match('swapimage');
    });

    if ($this.is(':hidden')) {
        $this.closest('ul').next().click();
    }

    $('#' + liClass.toString().split('-')[1]).find('img').attr('src', replaceWith.attr('src').split('_')[0] + '_1.jpg').parent().attr('href', replaceWith.parent().attr('href')).parent('div').next().find('h3').html($this.children('h3').html()).next().text($this.children('p').text()).next().children().attr('href', replaceWith.parent().attr('href'));

    if (event.originalEvent !== undefined) {
        stopSlide();
    }
}).live('mouseout', function (event) {
    if (event.originalEvent !== undefined) {
        startSlide();
    }
});

$(function () {
    startSlide();
    $('div.channelbox li[class*=swapimage]:eq(0)').mouseover();
});
