/**
 * @author alexander.farkas
 */

jQuery.noConflict();
(function($){

    function callOnDomReady() {
        $('html').addClass('js-on'); // html class

        createStyleSwitcher();

        //Zebra-Tables
        $('div.text tbody tr:nth-child(odd)').addClass('odd');

        addPrintLink();

        //wait for the swfobject DOM-Ready, so we can return a proper reference to flash object
        swfobject.addDomLoadEvent(function(){
            var s = $('div.flash')
                .embedSWF();
        });


        //initialize flowplayer
        window.contextPath = window.contextPath ||
            '';

        $("div.video a").flowplayer(
            contextPath + "/.resources/templating-kit/swf/flowplayer.swf", {
                // splash image
                clip:{
                    autoPlay: false,
                    autoBuffering: true
                }
            }
        );

        //different tabs
        createTabs();
        faqToc();

        //Teaser-Switchers
        $('div.teaser-wrapper:not(.tw-paging,.tw-slider)').scroller();

        pagingTeaserSwitcher();
        slidingTeaserSwitcher();

        $('body')
                .showbox({
                    close: 'Close',
                    prev: '',
                    next: '',
                    posBox: '#showbox',
                    dialogPosStyle: 'absolute',
                    constrainToView: false,
                    top: 10
                });

        slidingTabs();

        $.socialbookmark.init('li.social-b a');


    }



    function slidingTabs(){
        $.fn.mySlide = function(fn){
            fn = fn || function(){};
            return this.animate({
                height: 'toggle',
                opacity: 'toggle'
            }, {
                duration: 500,
                complete: fn,
                deque: true
            });
        };

        $('ol.superpromos-toc')
            .each(function(){
                $(this)
                    .bind('tabtreecollapse', function(e, ui){
                        ui.panel
                            .stop(true, true)
                            .animate({
                                height: 'hide',
                                opacity: 0
                            }, {duration: 500});
                    })
                    .bind('tabtreeexpand', function(e, ui){
                        ui.panel
                            .stop(true, true)
                            .animate({
                                height: 'show',
                                opacity: 1
                            }, {duration: 500});
                    })
                    .tabtree(
                        {activeButtonClass: 'on',
                        selectEvents: 'mouseenter focus',
                        handleDisplay: 'initial'
                    })
                    .find('a')
                    .bind('click', function(e){

                        if(e.pageX !== 0 && e.pageY !== 0){
                            var url = $($(this).attr('href'))
                                .find('a:first')
                                .attr('href');
                            if(url){
                                location = url;
                            }
                        }
                    });
            });


    }

    function pagingTeaserSwitcher(){
        function myPag(status){
                if(status == 'inactive'){
                    $('a',this).animate({opacity: 0.5},{duration: 500});
                } else {
                    $('a',this).animate({opacity: 1},{duration: 500});
                }
            }
            function myLink(status){
                if(status == 'show'){
                    this.animate({opacity: 1},{duration: 500});
                } else {
                    this.animate({opacity: 0},{duration: 500});
                }
            }
            $('div.teaser-wrapper.tw-paging').scroller({
                pagination: 'div.pagination',
                paginationFn: myPag,
                linkFn: myLink,
                paginationTitleFrom: 'h2',
                paginationAtoms: '<li class="pa-$number" title="$title"><a href="#">$number</a></li>'
            });
    }

    function slidingTeaserSwitcher(){

        $('div.teaser-wrapper.tw-slider')
            .each(function(){
                var jElm = $(this),
                    slider = $('div.slider', this)
                        .css({display: 'block'})
                        .wrap('<div class="slider-bar" />');

                $('div.slider-bar', this)
                    .append('<a href="#" class="prev">vor</a> <a href="#" class="next">zurück</a>');

                function slideSliderCall(e, o, ui){
                    ui = ui ||
                        o;
                    var val = (ui && isFinite(ui.value))? ui.value : (o) ? o.value : false;
                    jElm
                        .scroller('moveTo', val + '%', false);
                }

                slider
                    .slider({maxValue: 100})
                    .bind('slide', slideSliderCall);

                jElm
                    .scroller({
                        prevLink: 'div.slider-bar a.prev',
                        nextLink: 'div.slider-bar a.next'
                    })
                    .bind('uiscrollerslide', function(e, d){
                        slider
                            .slider('value', d.percentPos);
                    });

            });
    }


    function addPrintLink(){

        function print(){
            window.print();
            return false;
        }

        $('<li class="print"><a href="#">Print</a></li>')
            .prependTo('ul#text-features')
            .find('a')
            .click(print);
    }

    function faqToc() {
        var hash = location.hash;
            tabs = $('#faq-wrapper').find('h3 a');

        if(hash){
            tabs
                .filter('[href='+hash+']')
                .addClass('on');
        }

        $('#faq-wrapper')
            .bind('tabtreecollapse', function(e, ui){
                ui.panel.stop(true, true).slideUp(400);
            })
            .bind('tabtreeexpand', function(e, ui){
                ui.panel.stop(true, true).slideDown(400);
            })
            .tabtree(
                {
                    buttonSel: 'h3 a',
                    activeButtonClass: 'on',
                    toggleButton: true,
                    multiSelectable: true,
                    handleDisplay: 'initial'
                }
            );
    }

    $.fn.tabChangeAddon = function(){
        this
            .bind('tabtreecollapse', function(e, ui){
                ui.button.parent().removeClass('on');
            })
            .bind('tabtreeexpand', function(e, ui){
                ui.button.parent().addClass('on');
            });

        $('a.on', this)
            .parent()
            .addClass('on');
        return this;
    };

    function createTabs(){

        $('ol.text-box-toc, ol.toc-box-toc')
            .each(function(){
                $(this)
                    .tabtree(
                        {activeButtonClass: 'on'}
                    )
                    .tabChangeAddon();
            });
    }

    function createStyleSwitcher(){
        if(location.pathname.indexOf('forms.html') != -1){
            var box = $('<div class="box" />');
            box
                .append('<h3>Styleswitcher</h3>');
            box
                .appendTo('#extras-1');

            $.CssSwitcher.createVisualSwitcher()
                .appendTo(box[0]);

            $('input', box[0]).checkBox();
        }
    }

    $('html').addClass('js-on'); // html class

    $.extend($.ui.scroller.defaults, {
        //Elements Classes
        atoms: 'div.rack-teaser',
        prevLink: 'div.prev a',
        nextLink: 'div.next a'
    });


    $(callOnDomReady);
})(jQuery);
