/*!
 * Simple Tabs
 * http://www.flux-it.de
 *
 * Copyright 2010 Elena Kuprienko
 * Licensed under GPL version 3
 *
 * http://www.gnu.org/licenses/gpl.txt
 */


(function( $ ){
    














    function pagerFactory(idx, slide) {
        return '<a href="#">'+(idx+1)+'</a>';
    }


    

    /*LIST VIEW btn-info*/
    $('.list-item').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover') {
            teaser_mouseover(this);
        } else if (event.type == 'mouseout') {
            teaser_mouseout(this);
        }
    });
    $('.list-item').live('click', function(event) {
        open_url(this,'.btn-info');
    });


    function teaser_mouseover(e){
        $(e).css({
            "cursor": "pointer"
        });

    }
    function teaser_mouseout(e){
        $(e).css({
            "cursor": ""
        });
    }
    function open_url(e,classname){
        var url;
        var ret = true;
        if(classname){
            url = $(e).find(classname).attr('href');
        }else{
            url = $(e).find('a').attr('href');
        }
        if(url){
            window.location.href = url;
            ret = false;
        }
        return ret;
    }




    /*
     *  AJAX LOAD Zeichen
     *
     **/
    $("html").bind("ajaxSend", function(){
        var height = $("#content div.left div.tab:visible div.news-list-container").height()-100;
        $("#content #tabs").parent('div').prepend('<div class="loader">&nbsp;</div>');
        $('.loader').css({
            'height': height
        });
        $(".loader").fadeIn();
    }).bind("ajaxComplete", function(){
        $(".loader").fadeOut("fast");
        $(".loader").remove();
    });












    $(document).ready(function() {
        /*
     *  TOP Events
     *
     **/
        $('#topevents')
        .cycle({
            timeout: 0,
            activePagerClass: 'act',
            pager:  '#topeventsnavi',
            prev:   '#topeventsprev',
            next:   '#topeventsnext',
            pagerAnchorBuilder: pagerFactory
        });

        /*
     *  HOME SLIDESHOW
     **/
        $('#detail').cycle({
            timeout: 5000,
            pause:   1,
            pager: '#fc',
            //pagerEvent: 'mouseover',
            pauseOnPagerHover: true,
            //pagerEvent: 'mouseover',
            pagerAnchorBuilder: function(idx, slide) {
                // return selector string for existing anchor
                return '#fc .csc-default:eq(' + idx + ') a';
            },
            // override Cycle's default updateActivePagerLink function
            updateActivePagerLink: function(pager, activeIndex) {
                $(pager).find('.csc-default:eq('+activeIndex+')').addClass('activeSlide').siblings().removeClass('activeSlide');
                $(pager).find('.csc-default').each(function(index){

                    var classNr = $(this).attr('class').match(/^.*_(\d+).*/)[1];
                    
                      var classNrNew = parseInt(classNr) - 1;
                    
                    if (classNr == 0){
                        var cloneEl = $(this).clone().attr('id','clone').removeClass();
                        if($('#clone').length == 0){
                            cloneEl.prependTo($('#preview'));
                            $('#clone').wrap('<div class="clone" />');
                        }else{
                            $('#clone').css('top','+=23').replaceWith(cloneEl);
                        }
                        $('#clone').animate({
                            top: '-=103'
                        },1000,function(){
                            $('#clone').animate({
                                top: '-=20'
                            },1000,function(){});
                        });

                        classNrNew = parseInt($(pager).find('.csc-default').length) - 1;
                        $(this)
                        .switchClass('slide_' + classNr, 'slide_a', 1000)
                        .addClass('slide_' + classNrNew);
                    }else if(classNr == (parseInt($(pager).find('.csc-default').length) - 1)){
                        $(this)
                        .switchClass('slide_a', 'slide_b', 1000);
                        $(this)
                        .removeClass('slide_' + classNr)
                        .addClass('slide_' + classNrNew);
                    }else if(classNr == (parseInt($(pager).find('.csc-default').length) - 2)){
                        $(this)
                        .switchClass('slide_b', 'slide_c', 1000);
                        $(this)
                        .removeClass('slide_' + classNr)
                        .addClass('slide_' + classNrNew);
                    }else if(classNr == (parseInt($(pager).find('.csc-default').length) - 3)){
                        $(this)
                        .removeClass('slide_' + classNr)
                        .addClass('slide_' + classNrNew);
                        $(this)
                        .switchClass('slide_c', 'slide_d', 1000);

                    }else if(classNr == (parseInt($(pager).find('.csc-default').length) - 4)){
                        $(this)
                        .removeClass('slide_' + classNr)
                        .addClass('slide_' + classNrNew);
                        $(this)
                        .switchClass('slide_d', 'slide_right', 1000);

                    }else if(classNr == (parseInt($(pager).find('.csc-default').length) - 5)){
                        $(this)
                        .removeClass('slide_right');
                        $(this)
                        .removeClass('slide_' + classNr)
                        .addClass('slide_' + classNrNew);
                    }else{
                        $(this).switchClass('slide_' + classNr, 'slide_' + classNrNew, 1000);
                    }


                });

            }
        });
/*        $('#fc .csc-default').live('click',function() {
            $(this).find('a').trigger('click');
            return false;
        });
*/
$('#fc .csc-default').live('mouseover',function() {
    $('#fc .csc-default').css({'cursor': 'default'});
});
        /*$('#fc .csc-default').live('mouseover', function(){
            $('#detail').cycle('pause');
            $(this).find('a').trigger('click');
            return false;
        });*/


        $('#detail .csc-default .fc-image').live('click',function() {
            //alert($(this).children('h1 a'));
            //$(this).find('a').trigger('click');
            var url = $(this).next('div').find('h1');
            open_url(url,'');

            return false;
        });




















        $('marquee').marquee('pointer');

        /* TOPEVENT ROLLOVER */
        $("#topevents .event .inner span").stop().animate({
            bottom:"-5.5em"
        },{
            queue:false,
            duration:200
        });
        $('#topevents .event').live('click', function(event){
            var url = $(this).find('a').attr('href');
            window.location.href = url;
            return false;
        });
        $("#topevents .event").hover(function(){
            $(this).css({
                "cursor": "pointer"
            });
            $("span", this).stop().animate({
                top:"0"
            },{
                queue:false,
                duration:200
            });
        }, function() {
            $(this).css({
                "cursor": ""
            });
            $("span", this).stop().animate({
                top:"-5.5em"
            },{
                queue:false,
                duration:200
            });
        });




        /* EVENT State-TOOLTIP */
        function set_info(e){
            e.find('span').show('fast');
            e.mousemove(function(el){
                e.css({
                    cursor: 'help'
                });
                e.find('span').css({
                    position: 'absolute',
                    left: '0',
                    bottom: '2em'
                //position: 'fixed',
                //top: el.pageY+25 ,
                //left: el.pageX-10
                });
            });
        }
        function remove_info(e){

            e.find('span').hide();
        }
        $(".btn-ausverkauft span, .btn-abgesagt span").each(function(){
            $(this).hide();
        });
        $(".btn-ausverkauft, .btn-abgesagt").live({
            mouseover: function() {
                var i = $(this);
                if(i.find('span').text().length > 0){
                    set_info(i);
                }
            },
            mouseout: function() {
                var i = $(this);
                remove_info(i);
            }
        });




        /* RIGHT SCROLL */
        if($('#right.scroll').length > 0){
            var offset = $("#right.scroll").offset();
            var topPadding = 4;
            $(window).scroll(function() {

                if ($(window).scrollTop() > offset.top) {
                    $("#right").stop().animate({
                        marginTop: $(window).scrollTop() - offset.top + topPadding
                    });
                } else {
                    $("#right").stop().animate({
                        marginTop: 0
                    });
                }
            });
        }





    });






})( jQuery );
