/* -------------------------------------------------------------
    AUTHOR: Stefan Nafra
    UPDATED: 06.05.2010
    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 initList() {
        $('a.d-setnr1').each(function () {
            var $this = $(this);

            if ($.url.param('cd') === '') {
                $this.click();
            } else {
                $this.parent().children('a.d-setnr' + parseInt($.url.param('cd'), 10)).addClass('active').click();
                $this.parent().children('a.d-setnr' + Math.ceil(parseInt($.url.param('cd'), 10) / (parseInt($this.attr('class').split(' ')[2].match(/\d+/), 10) - 1))).addClass('active').click();
            }
        });
    }
    initList();

    $('h3[id^=teaserbox]').live('click', function () {
        initList();
    });

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

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

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

    $this.addClass('active').siblings().removeClass('active');
    $('.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);

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

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

$(function () {

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

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

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

    var classPN = $(this).attr('class').split(' '), ulID = $.trim($(this).parent().children('ul').attr('class')), liLast = $('.' + ulID + ' li:visible:last');

    if (liLast.next().length > 0) {
        $('.' + ulID + ' li').hide().slice($('.' + ulID + ' li').index(liLast) + 1, parseInt($('.' + ulID + ' li').index(liLast), 10) + parseInt(classPN[2].match(/\d+/), 10) + 1).show();
    } else {
        $('.' + ulID + ' li').hide().slice(0, parseInt(classPN[2].match(/\d+/), 10)).show();
    }

    if ($('.' + ulID + ' li:visible').length < classPN[2].match(/\d+/)) {
        $('.' + ulID + ' li').hide().slice($('.' + ulID + ' li').index($('.' + ulID + ' li:last')) - parseInt(classPN[2].match(/\d+/), 10) + 1, $('.' + ulID + ' li').index($('.' + ulID + ' li:last')) + 1).show();
    }
});

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

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

    var classPN = $(this).attr('class').split(' '), ulID = $.trim($(this).parent().children('ul').attr('class')), liFirst = $('.' + ulID + ' li:visible:first');

    if (liFirst.prev().length > 0) {
        $('.' + ulID + ' li').hide().slice($('.' + ulID + ' li').index(liFirst) - parseInt(classPN[2].match(/\d+/), 10), parseInt($('.' + ulID + ' li').index(liFirst), 10)).show();
    } else {
        $('.' + ulID + ' li').hide().slice($('.' + ulID + ' li').index($('.' + ulID + ' li:last')) - parseInt(classPN[2].match(/\d+/), 10) + 1, $('.' + ulID + ' li').index($('.' + ulID + ' li:last')) + 1).show();
    }

    if ($('.' + ulID + ' li:visible').length === 0) {
        $('.' + ulID + ' li:lt(' + parseInt(classPN[2].match(/\d+/), 10) + ')').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'));

        $('#hover').css({
            'left' : $(this).position().left - ($(this).width() / 2),
            'top' : $(this).position().top - ($(this).height() / 2) + 16
        }).html($(this).html()).show().children().removeClass('imgwidth').show();
    });

    $('#hover').mouseleave(function () {
        $('#hover').hide();
    });
});
