﻿$(document).ready(function() {
    $(".lang_check :checkbox").click(function() {
        if ($(".lang_check :checkbox:checked").length == 0) {
            $(".lang_submit").attr("disabled", "disabled");
        }
        else {
            $(".lang_submit").removeAttr("disabled");
        }
    });
    $(".pay_check :radio").click(function() {
        $(".pay_submit").removeAttr("disabled");
    });
    
    $(".jcarousel.scroll5").jcarousel({
        scroll: 5,
        initCallback: initCarousel
    });
    $(".jcarousel.scroll2").jcarousel({
        scroll: 2
    });
    $(".jcarousel-skin-promo").jcarousel({
        scroll: 1,
        auto: 7,
        wrap: 'last',
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: initPromo,
        itemFirstInCallback: updatePromo
    });

    $(".item_info").ellipsis();

    $(".gallery a").lightBox({ fixedNavigation: true });
});

function initCarousel(carousel) {
    var el = $(carousel.container[0]);
    var count = el.find("li").length;
    var control = $('<div class="jcarousel-control"></div>');
    el.closest(".items_row").find("h3").append(control);
    for (var i = 1; i <= Math.floor(count / 5); i++) {
        var first = (i == 1) ? ' class="current"' : '';
        control.append('<a href="#"' + first + '><span>' + (((i - 1) * 5) + 1) + '</span></a>');
    }
    el.closest(".items_row").find(".jcarousel-control a").bind('click', function(e) {
        e.preventDefault();
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        $(this).closest(".items_row").find(".jcarousel-control a").removeClass("current");
        $(this).addClass("current");
    });
    el.find('.jcarousel-next').click(function(e) {
        e.preventDefault();
        var index = $(this).closest(".items_row").find(".jcarousel-control a").index($(this).closest(".items_row").find(".jcarousel-control a.current"));
        index++;
        $(this).closest(".items_row").find(".jcarousel-control a").removeClass("current");
        $(this).closest(".items_row").find(".jcarousel-control a").eq(index).addClass("current");
    });
    el.find('.jcarousel-prev').click(function(e) {
        e.preventDefault();
        var index = $(this).closest(".items_row").find(".jcarousel-control a").index($(this).closest(".items_row").find(".jcarousel-control a.current"));
        index--;
        $(this).closest(".items_row").find(".jcarousel-control a").removeClass("current");
        $(this).closest(".items_row").find(".jcarousel-control a").eq(index).addClass("current");
    });
}

function initPromo(carousel) {
    $('.tabslist a').bind('click', function(e) {
        e.preventDefault();
        carousel.scroll(jQuery.jcarousel.intval($(this).attr("href").charAt($(this).attr("href").length - 1)));
    });
}

function updatePromo(carousel, item, idx, state) {
    $('.tabslist li').removeClass("current");
    $('.tabslist li').eq(idx - 1).addClass("current");
}