(function($) {
    $(document).ready(function () {
        tyliai_init_control_boxes()
        // width is dinamic, because text can be in different languages and because text changes as buttons state. we set static width on start
        $('.tyliai_cr .style_small_compact_text').each(function(){
            control_box = $(this)
            btn_play = $('.btn_play span', this)
            text = btn_play.html()
            width_play = btn_play.html($('.btn_play_text',control_box).html()).width()
            width_pause = btn_play.html($('.btn_pause_text',control_box).html()).width()
            width_max = (width_play > width_pause)? width_play:width_pause
            btn_play.html(text).width(width_max) // set old text and max-stable width
        })
        // horizontal centering of control blocks
        $('.tyliai_cr .style_small_compact.h_pos_center, .tyliai_cr .style_small_compact_text.h_pos_center, .tyliai_cr .slides_pager.h_pos_center').each(function(){
            $(this).css('margin-left', '-'+ parseInt($(this).width()/2)+'px');// we do this three times because -margin allows to element
            $(this).css('margin-left', '-'+ parseInt($(this).width()/2)+'px');// be bigger, and we have to center it again
            $(this).css('margin-left', '-'+ parseInt($(this).width()/2)+'px');
        })
        // vertical centering of control blocks
        $('.tyliai_cr .style_small_compact.v_pos_middle, .tyliai_cr .style_small_compact_text.v_pos_middle, .tyliai_cr .slides_pager.v_pos_middle').each(function(){
            $(this).css('margin-top', '-' + parseInt($(this).height() / 2) + 'px');
        })

         // pager setup
        $('.tyliai_cr .slides_pager').each(function(){
            $i = 0
            slider = $(this).parents('.tyliai_cr')
            $('.slides_pager span', slider).each(function(){
                $(this).click(tyliai_cycle_to(slider[0].id, $i, this))
                $i++
            })
        })
    })
    function tyliai_init_control_boxes(){
        $('.slider_controls').css('display', 'block')
        $(".tyliai_cr a.btn_c").click(function(){
            var projector = $(this).parents('.tyliai_cr')
            var rel_main = $(".btn_play", projector).attr('rel');
            $("a.btn_c", projector).attr('rel', 'off').removeClass('active');
            $(this).attr('rel', 'on');
            if($(this).hasClass('btn_prev')){
                $('.tyliai_cr_visual', projector).cycle('go_backwards').cycle('prev')
                $('.info', projector).cycle('go_backwards').cycle('prev')
            }
            if($(this).hasClass('btn_next')){
                $('.tyliai_cr_visual', projector).cycle('go_forwards').cycle('next')
                $('.info', projector).cycle('go_forwards').cycle('next')
            }
            if (rel_main == 'play') $(this).addClass('active');

            return false;
        });

        // autoplay button handling
        $(".tyliai_cr .btn_play").click(function(){
            var rel_ = $(this).attr('rel');
            var projector = $(this).parents('.tyliai_cr')
            $('.pics', projector).cycle('toggle');
            $('.info', projector).cycle('toggle');
            if (rel_ == 'play') {
                text = $('.btn_play_text',projector).html()
                if(text)
                    $('span', this).html(text)
                $(this).attr('rel', 'pause').removeClass('active').addClass('pause').removeClass('play');
                //$("a.btn_c", projector).removeClass('active');
            }

            if (rel_ == 'pause') {
                text = $('.btn_pause_text',projector).html()
                if(text)
                    $('span', this).html(text)
                $(this).attr('rel', 'play').addClass('active').removeClass('pause').addClass('play');
                $("a.btn_c", projector).each(function() {
                    if ($(this).attr('rel') == 'on') $(this).addClass('active');
                });
            }

            return false;
        });
    }
})(jQuery);
function tyliai_cycle_to(slider_id, slide_nr, button){
    return function(){
        jQuery('#'+slider_id+' .slides_pager span').removeClass('active')
        jQuery(button).addClass('active')
        jQuery('#'+slider_id+' .pics').cycle(slide_nr)
        jQuery('#'+slider_id+' .info').cycle(slide_nr)
    }
}

function hide_if_no_hover_class(control, hide){
    if(jQuery(control).hasClass('hover') == false){
         jQuery(hide).stop(true, true).fadeOut('slow')
    }
}
function tyliai_updateActivePagerLink($slider_selector, $slide_nr){
    jQuery($slider_selector+' span').removeClass('active')
    jQuery($slider_selector + ' .slide'+$slide_nr).addClass('active')
}
