$(function() {
    function slider_initCallback(carousel) {
        $('#featured .frame').before('<ul class="switcher" />');
        for (var i = 0; i < $('#featured .frame li').length; i++) {
            $('#featured .switcher').append('<li><a></a></li>');
        }
        $('#featured .switcher li:first').addClass('active');
        $('#featured .switcher a').live('click', function() {
            carousel.scroll($.jcarousel.intval($(this).parent().index() + 1));
            return false;
        });
    }

    function slider_itemFirstInCallback(carousel, item, idx, state) {
        $('#featured .switcher li').eq(idx - 1).addClass('active').siblings('.active').removeClass('active');
    }

    $('.field, textarea').focus(
        function() {
            if (this.title == this.value) {
                this.value = '';
            }
        }).blur(function() {
            if (this.value == '') {
                this.value = this.title;
            }
        });

    $(".bot-nav a:not(.ui-state-disabled)").live('click', function(event) {
        event.preventDefault();
        var id = $(this).closest("li").attr("id");
        $(this).linkWithAjax({
            dataType : 'json',
            callbacks : {
                success : function(json) {
                    if (json.code == 0) {
                        var html = $(json.products);
                        var list = $("#category-products");
                        if (list.length) {
                            list.html(html);
                        } else {
                            $("#" + id).find(".cat-entry").html(html.find(".cat-entry").html());
                            $("#" + id).find(".cat-foot").html(html.find(".cat-foot").html());
                        }
                    }
                }
            }
        });
    });

    $('.accordion .cat-header').click(function() {
        if ($(this).parents('li:eq(0)').siblings('.open').length) {
            $('.accordion .open').find('.cat-entry, .cat-foot').slideUp(function() {
                $(this).parents('.open').removeClass('open');
            });
        }

        $(this).parents('li:eq(0)').find('.cat-entry, .cat-foot').slideDown(function() {
            $(this).parents('li:eq(0)').addClass('open');
        });
    });

    // Product image choose
    $(".thumbs a.change").live('click', function(event) {
        event.preventDefault();

        $("#product_main_image").attr("src", $(this).attr("rel"));
    });

    if ($('#featured .frame').length) {
        $('#featured .frame > ul').jcarousel({
            visible: 1,
            scroll: 1,
            auto: $("#featured .frame > ul").attr("data-time") / 1000,
            easing: $("#featured .frame > ul").attr("data-easing"),
            wrap: "both",
            initCallback: slider_initCallback,
            itemFirstInCallback: slider_itemFirstInCallback,
            buttonPrevHTML: null,
            buttonNextHTML: null
        });
    }

    if ($('.product-slider').length) {
        $('.product-slider .thumbs ul').jcarousel({
            scroll: 1,
            wrap: "both"
        });
    }

    //$('[rel="popup"]').showModal(null, {freezing : $('body')});
    //$('.error-message .close').live('click', function() {
    //    $('.error-message').fadeOut();
    //});
    //$('#nav li').hover(function() {
    //    $(this).find('ul:eq(0)').toggle();
    //});
});

function truncate (word, max_length){
  if (word.length > max_length-3){
    word = word.substr(0,max_length) + '...'
  }
  return word
}

