/* -------------------------------------------------------------
    AUTHOR: Stefan Nafra
    UPDATED: 07.07.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 () {

    if ($('a.d-set:eq(0)').length > 0) {

        var classL = $('a.d-set:eq(0)').attr('class').split(' ');

        if ($.url.param('cd') === '') {
            $('.contentlist li:gt(' + (parseInt(classL[2].match(/\d+/), 10) - 1) + '), div.dynamiclist li:gt(' + (parseInt(classL[2].match(/\d+/), 10) - 1) + ')').hide();
            $('a.d-setnr1').addClass('active');
        } else {
            $('.contentlist li, div.hitlist li, div.dynamiclist li').hide().slice(Math.ceil(parseInt($.url.param('cd'), 10) / parseInt(classL[2].match(/\d+/), 10)) * parseInt(classL[2].match(/\d+/), 10) - parseInt(classL[2].match(/\d+/), 10), Math.ceil(parseInt($.url.param('cd'), 10) / parseInt(classL[2].match(/\d+/), 10)) * parseInt(classL[2].match(/\d+/), 10)).show();
            $('a.d-setnr' + Math.ceil(parseInt($.url.param('cd'), 10) / parseInt(classL[2].match(/\d+/), 10))).addClass('active');
        }
    }
});

/* -------------------------------------------------------------
        02.2. Zeigt das ausgewählte Set an.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('a.d-set:not(a.d-set-next, a.d-set-pre)').live('click', function () {

    var classL = $(this).attr('class').split(' ');

    $('a.d-set').removeClass('active');
    $('a.d-setnr' + $(this).text()).addClass('active');
    $('.contentlist li, div.hitlist li, div.dynamiclist li').hide().slice(parseInt(classL[1].match(/\d+/), 10) * parseInt(classL[2].match(/\d+/), 10) - parseInt(classL[2].match(/\d+/), 10), parseInt(classL[1].match(/\d+/), 10) * parseInt(classL[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 () {
        var classPN = $(this).attr('class').split(' ');
        $(this).parent().children('ul').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'));

        $('#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. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    $('li[class*=swapimage]').live('mouseover', function () {

        var classArray = $.grep($(this).attr('class').split(' '), function (value) {
            return value.match('swapimage');
        });

        $('#' + classArray.toString().split('-')[1]).find('img').parent().html($(this).find('img').parent().html()).children().attr('src', $(this).find('img').attr('src').split('_')[0] + '_1.jpg').removeAttr('width').removeAttr('height').closest('div').next().find('h3').children().html($(this).children('h3').children().html()).parent().next().html($(this).children('p').html());
    });
});