(function ($) {
    $.fn.jCarouselLite = function (o) {
        o = $.extend({
            btnPrev: null,
            btnNext: null,
            btnGo: null,
            mouseWheel: false,
            auto: null,
            speed: 200,
            easing: null,
            json: false,
            product_type: null,
            base64: true,
            ajaxpath: location.href,
            disabled_class: "disabled",
            center_button: 0,
            vertical: false,
            circular: true,
            visible: 3,
            start: 0,
            scroll: 1,
            beforeStart: null,
            afterEnd: null,
            floatDirection: "left"
        },
        o || {});
        return this.each(function () {
            var running = false,
            animCss = o.vertical ? "top": "left",
            sizeCss = o.vertical ? "height": "width";
            var div = $(this),
            ul = $("ul", div),
            tLi = $("li", ul),
            tl = tLi.size(),
            v = o.visible;
            if (o.circular) {
                ul.prepend(tLi.slice(tl - v - 1 + 1).clone()).append(tLi.slice(0, v).clone());
                o.start += v;
            }
            var li = $("li", ul),
            itemLength = li.size(),
            curr = o.start;
            div.css("visibility", "visible");
            li.css({
                overflow: "hidden",
                float: o.vertical ? "none": o.floatDirection
            });
            ul.css({
                margin: "0",
                padding: "0",
                position: "relative",
                "list-style-type": "none",
                "z-index": "1"
            });
            div.css({
                overflow: "hidden",
                position: "relative",
                "z-index": "2",
                left: "0px"
            });
            var liSize = o.vertical ? height(li) : width(li);
            var ulSize = liSize * itemLength;
            var divSize = liSize * v;
            li.css({
                width: li.width(),
                height: li.height()
            });
            ul.css(sizeCss, ulSize + "px").css(animCss, -(curr * liSize));
            div.css(sizeCss, divSize + "px");
            if (o.center_button) {
                var control_element = $(o.btnPrev);
                if (control_element.height()) {
                    var control_size = parseInt((o.vertical ? li.width() : li.height()) / 2) - parseInt((o.vertical ? width(control_element) : height(control_element)) / 2);
                } else {
                    var control_size = parseInt((o.vertical ? li.width() : li.height()) / 2) - parseInt((o.vertical ? parseInt(control_element.css('width')) : parseInt(control_element.css('height'))) / 2);
                }
                if (o.vertical) {
                    control_element.css('left', control_size + 'px');
                    $(o.btnNext).css('left', control_size + 'px');
                } else {
                    control_element.css('top', control_size + 'px');
                    $(o.btnNext).css('top', control_size + 'px');
                }
            }
            if (o.btnPrev) $(o.btnPrev).click(function () {
                return go(curr - o.scroll);
            });
            if (o.btnNext) $(o.btnNext).click(function () {
                return go(curr + o.scroll);
            });
            if (o.btnGo) $.each(o.btnGo, function (i, val) {
                $(val).click(function () {
                    return go(o.circular ? o.visible + i: i);
                });
            });
            if (o.mouseWheel && div.mousewheel) div.mousewheel(function (e, d) {
                return d > 0 ? go(curr - o.scroll) : go(curr + o.scroll);
            });
            if (o.auto) setInterval(function () {
                go(curr + o.scroll);
            },
            o.auto + o.speed);
            function vis() {
                return li.slice(curr).slice(0, v);
            };
            function go(to) {
                if (!running) {
                    if (o.beforeStart) o.beforeStart.call(this, vis());
                    if (o.circular) {
                        if (to <= o.start - v - 1) {
                            ul.css(animCss, -((itemLength - (v * 2)) * liSize) + "px");
                            curr = to == o.start - v - 1 ? itemLength - (v * 2) - 1 : itemLength - (v * 2) - o.scroll;
                        } else if (to >= itemLength - v + 1) {
                            ul.css(animCss, -((v) * liSize) + "px");
                            curr = to == itemLength - v + 1 ? v + 1 : v + o.scroll;
                        } else curr = to;
                    } else {
                        if (to < 0 || to > itemLength - v) return;
                        else curr = to;
                    }
                    running = true;
                    ul.animate(animCss == "left" ? {
                        left: -(curr * liSize)
                    }: {
                        top: -(curr * liSize)
                    },
                    o.speed, o.easing, function () {
                        if (o.afterEnd) o.afterEnd.call(this, vis());
                        if (!o.circular) {
                            if (o.json && itemLength < (curr + 2)) {
                                $(o.btnPrev + "," + o.btnNext).removeClass(o.disabled_class);
                                $(o.btnNext).addClass(o.loading_class);
                                $(o.btnNext).unbind('click');                                                                
                                $.post(o.ajaxpath, {page: (curr + 2), product_type: o.product_type.replace("crlMedia_", "")},
                                function (data) {
                                    $(o.btnNext).removeClass(o.loading_class);
                                    if (data.length != 0) {
                                        itemLength++;
                                        var style = ul.children("li:last").attr("style");
                                        ul.width(liSize * itemLength);
                                        if (o.base64)
                                        {
                                            if (ul.children("li:last").after(Base64.decode(data)))
                                            {
                                                ul.children("li").attr("style", style);
                                            }
                                        }
                                        else
                                        {
                                            if (ul.children("li:last").after(data))
                                            {
                                                ul.children("li").attr("style", style);                                                
                                            }
                                        }                                        
                                    }
                                    $(o.btnNext).click(function () {
                                        return go(curr + o.scroll);
                                    });
                                    $((curr - o.scroll < 0 && o.btnPrev) || (curr + o.scroll > itemLength - v && o.btnNext) || []).addClass(o.disabled_class);
                                });
                            } else {
                                $(o.btnPrev + "," + o.btnNext).removeClass(o.disabled_class);
                                $((curr - o.scroll < 0 && o.btnPrev) || (curr + o.scroll > itemLength - v && o.btnNext) || []).addClass(o.disabled_class);
                            }
                        }
                        running = false;
                    });
                }
                return false;
            };
        });
    };
    function css(el, prop) {
        return parseInt($.css(el[0], prop)) || 0;
    };
    function width(el) {
        return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
    };
    function height(el) {
        return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
    };
})(jQuery);
var Base64 = {
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    decode: function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;
    },
    _utf8_decode: function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}
