﻿var StartWithTab = 1;

var sliderCarousel = $.klass({

    startScroll: false,
    listId: '',

    carouselInitCallback: function (carousel) {
        if ($(carousel.options.obj.listId + ' > li > img').length > 0) {
            var img_width = $(carousel.options.obj.listId + ' > li > img').width();
            var img_height = $(carousel.options.obj.listId + ' > li > img').height();
        }
        else {
            var img_width = $(carousel.options.obj.listId + ' > li > div').width();
            var img_height = $(carousel.options.obj.listId + ' > li > div').height();
        }

        if ($(carousel.options.obj.listId + ' > li .auto_content').length > 0) {
            img_height = $($(carousel.options.obj.listId + ' > li .auto_content')[0]).innerHeight();
        }

        $(carousel.options.obj.listId + ' .jcarousel-item').css({ width: img_width, height: img_height });
        $(carousel.options.obj.listId).parents('.jcarousel-clip-horizontal').css({ width: img_width, height: img_height });
        $(carousel.options.obj.listId).parents('.jcarousel-container-horizontal').css({ width: img_width, height: img_height });
        $(carousel.options.obj.listId).parents('.tabbox').css({ width: img_width });
        $(carousel.options.obj.listId).css({ width: img_width * $(carousel.options.obj.listId + ' .jcarousel-item').length });

        var pos = 1;
        var klassname = carousel.options.obj.listId.replace('#', '');

        $("." + klassname + "_tabitem").each(function () {

            if ($(this).find('span').html() != '') {

                $(this).bind('mousedown', function () {
                    if ($(this).parent().children('.tabselector').length > 0) {
                        carousel.options.obj.scrollCursor = $(this).index();
                        carousel.options.obj.pic_pos = $(this).index();
                    }
                    else {
                        carousel.options.obj.scrollCursor = $(this).index() + 1;
                        carousel.options.obj.pic_pos = $(this).index() + 1;
                    }

                    carousel.scroll(carousel.options.obj.scrollCursor, true);
                });
            }
        });

        $("." + klassname + "_tabitem").bind('mousemove', function () {
            carousel.options.obj.locked = false;
            if ($(this).find('.tabtext span').html() == '') {
                $(this).css({ cursor: 'default' });
            }
        });

        $(carousel.options.obj.listId + "_current_slide").bind('mouseout', function () {
            carousel.options.obj.scrollCursor = 0;
        });

        $(carousel.options.obj.listId).bind('mouseout', function () {
            carousel.options.obj.scrollCursor = 0;
        });

        if (jQuery.browser.webkit && document.readyState != "complete") {
            setTimeout(function () {
                var newpos = $($("." + klassname + "_tabitem span")[0]).position().left + ($($("." + klassname + "_tabitem span")[0]).width() / 2 - ($(carousel.options.obj.listId + '_current_slide_cur').width() / 2));
                $(carousel.options.obj.listId + "_current_slide").css({ left: newpos + 'px' });
            }, 100);
        }
        else {
            var newpos = $($("." + klassname + "_tabitem span")[0]).position().left + ($($("." + klassname + "_tabitem span")[0]).width() / 2 - ($(carousel.options.obj.listId + '_current_slide_cur').width() / 2));
            $(carousel.options.obj.listId + "_current_slide").css({ left: newpos + 'px' });
        }
    },

    pic_pos: 1,

    scrollCursor: 0,

    scroll: function (carousel) {
        if (carousel.options.obj.startScroll == false || carousel.options.obj.locked == true) {
            return;
        }

        if ($(carousel.options.obj.listId + ' > li .auto_content').length > 0) {
            img_height = $($(carousel.options.obj.listId + ' > li .auto_content')[carousel.options.obj.pic_pos - 1]).innerHeight();

            $(carousel.options.obj.listId + ' .jcarousel-item').animate({ height: img_height });
            $(carousel.options.obj.listId).parents('.jcarousel-clip-horizontal').animate({ height: img_height });
            $(carousel.options.obj.listId).parents('.jcarousel-container-horizontal').animate({ height: img_height });
        }

        var klassname = carousel.options.obj.listId.replace('#', '');
        $("." + klassname + "_tabitem").removeClass('selected_tabitem');
        $($("." + klassname + "_tabitem")[carousel.options.obj.pic_pos - 1]).addClass('selected_tabitem');

        if (carousel.options.obj.scrollCursor > 0) {
            //var newpos = ($(carousel.options.obj.listId + "_current_slide").width() * carousel.options.obj.scrollCursor) - $(carousel.options.obj.listId + "_current_slide").width();
            var newpos = $($("." + klassname + "_tabitem span")[carousel.options.obj.pic_pos - 1]).position().left + ($($("." + klassname + "_tabitem span")[carousel.options.obj.pic_pos - 1]).width() / 2 - ($(carousel.options.obj.listId + '_current_slide_cur').width() / 2));

            if (this.scrollCursor == 1) {
                newpos = 0;
                carousel.options.obj.scrollCursor = 0
            }

            //this.pic_pos = this.scrollCursor-1;

            $(carousel.options.obj.listId + '_current_slide').animate({ left: newpos + 'px' }, 400);
            carousel.options.obj.scrollCursor = 0;
        }
        else {

            if (carousel.options.obj.pic_pos >= 3) {
                $(carousel.options.obj.listId + '_current_slide').animate({ left: '0px' }, 400);
                carousel.options.obj.pic_pos = 0;
            }
            else {
                var newpos = $($("." + klassname + "_tabitem span")[carousel.options.obj.pic_pos - 1]).position().left + ($($("." + klassname + "_tabitem span")[carousel.options.obj.pic_pos - 1]).width() / 2 - ($(carousel.options.obj.listId + '_current_slide_cur').width() / 2));

                $(carousel.options.obj.listId + '_current_slide').animate({ left: newpos + 'px' }, 400);
            }
            carousel.options.obj.pic_pos++;
        }
    },

    startScrollData: function (carousel) {
        carousel.options.obj.startScroll = true;
    },

    locked: false,

    init: function (obj, Id) {
        obj.listId = Id;

        $(window).bind('resize', function () {
            obj.locked = true;
        });

        if (StartWithTab == 'undefined') {
            StartWithTab = 1;
        }

        jQuery(this.listId).jcarousel({
            auto: 0,
            scroll: 1,
            wrap: 'last',
            start: StartWithTab,
            obj: obj,
            initCallback: obj.carouselInitCallback,
            itemFirstInCallback: obj.startScrollData,
            itemLoadCallback: {
                onBeforeAnimation: obj.scroll
            }
        });

        if (StartWithTab > 1) {
            jQuery(window).load(function () { $('#tabTD3').trigger('mousedown'); });
        }


        $(obj.listId).css({ visibility: 'visible' });
    }
});

jQuery(document).ready(function ()
{
    /*
    headlines and background image inside a tab module should also
    trigger the defined action respectively the button inside the module
    */
    function bind_headlines_and_images_within_tabmobule()
    {
        jQuery.each($(document).find('.tabbox li'), function (i, element)
        {
            if ($(this).find('.button a, .slide_buy > a').length == 1)
            {
                var href = $(this).find('.button a:first, .slide_buy > a:first').attr('href');
                var ln = $(this).find('.button a:first, .slide_buy > a:first');

                var items_to_be_clicked = $(this).children('img, .headlines');

                /*
                if there is a link inside the module-text, the text itself shouldn't be clickable
                */
                var headlines_got_extra_link = $(this).find('.headlines a');

                if (headlines_got_extra_link.length)
                {
                    items_to_be_clicked = $(this).children('img');
                }

                items_to_be_clicked.css('cursor', 'pointer');

                items_to_be_clicked.click(function (event)
                {
                    // does the item have an onclick attribute?
                    var check_onclick = ln.is('[onclick]');

                    // if so, we need to fire the 'event' defined in the onclick attribute
                    if (check_onclick)
                    {
                        var onclick_function = ln.attr('onclick');
                        log('onclick_function');
                    }
                    else
                    {
                        ln.click();
                    }


                    // window.location = baseurl + href;
                });

                // $(this).parent().find('a:first').unbind('click');
            }
        });
    }
    // currently disabled, doesn't work anymore, probably because of tracking bind to the read more links
    // bind_headlines_and_images_within_tabmobule();


    var id = 0;
    jQuery.each($(document).find('.tabbox'), function (i, element)
    {
        id++;
        $(element).children('ul').attr('id', 'msslider' + id);
        $(this).find('.tabboxes .tabselector').attr('id', 'msslider' + id + '_current_slide');
        $(this).find('.tabboxes .tabselectorarrow').attr('id', 'msslider' + id + '_current_slide_cur');
        $(this).find('.tabboxes .tabitem').addClass('msslider' + id + '_tabitem');

        // hide empty tab links (tab titles)
        $(this).find('.tabboxes .tabtext span:empty').parents('.carousel_tabitem:first').css({ display: 'none' });

        // if tab count is 1, hide the tabbar

        if ($(this).find('.tabboxes td.tabtext > span:not(:empty)').length == 1)
        {
            $(this).find('.tabboxes').css('visibility', 'hidden');
        }

        var sl = new sliderCarousel();
        sl.init(sl, '#msslider' + id);
        $(this).css({ visibility: 'visible' });
    });


    jQuery("#ten_reasons").jcarousel({
        scroll: 3
    });

    jQuery.each($(document).find('.ten_reasons'), function (i, element)
    {
        if ($(this).find('.left-right-titles').length > 0)
        {
            $(this).find('.jcarousel-prev').attr('title', $(this).find('.left-right-titles span:first').html());
            $(this).find('.jcarousel-next').attr('title', $(this).find('.left-right-titles span:nth-child(2)').html());
        }
    });

    $('.ten_reasons').css({ visibility: 'visible' });

    var max_height = 0;
    jQuery.each($("#ten_reasons .item_content"), function (i, element)
    {
        if ($(this).height() > max_height)
        {
            max_height = $(this).height();
        }
    });

    $(".ten_reasons .jcarousel-clip, .ten_reasons .jcarousel-list li, .ten_reasons .jcarousel-item").height(max_height);


    if ($(".ten_reasons li").length <= 3)
    {
        $(".ten_reasons .jcarousel-prev").css({ visibility: 'hidden' });
        $(".ten_reasons .jcarousel-next").css({ visibility: 'hidden' });
    }
});
