/*
usage:

jQuery(document).ready(function() {
    jQuery('#carousel1').jcarousel({
        imageWidth: 117 * 6,
        totalWidth: 383,
        totalHeight: 88,
        imageCount: 2,
        speed: 10 * 117 * 6,
        start_delay: 0,
        delay: 0,
        stopOnMousOover: 1,
        dir: -1
    });
});

*/
(function($) {
    $.fn.jcarousel = function(o) {
        return this.each(function() {
            new $jc(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
        vertical: false,
        totalWidth: 0,
        totalHeight: 0,
        imageWidth: 0,
        imageCount: 0,
        speed: 0,   //recomended 10 * imageWidth,
        delay: 0,   //milliseconds delay
        start_delay: 0,   //milliseconds delay
        stopOnMousOover: 1,    // false:0   true:1
        dir: -1,    // left:-1   right:1
        loadedImage: new Array(),
        newMarginLeft: 0,
        isRun: 0
    };

    $.jcarousel = function(e, o) {
        this.options = $.extend({}, defaults, o || {});

        //this.wh = !this.options.vertical ? 'width' : 'height';
        //this.lt = !this.options.vertical ? 'left' : 'top';

        this.list = $(e);
        this.container = this.list.wrap('<div class="ads clearfix"></div>').parent();
        this.container.css('width', this.options.totalWidth + 'px');
        this.container.css('height', this.options.totalHeight + 'px');

        if (!this.list.hasClass('floating_container')) {
            this.list.addClass(this.className('floating_container'));
        }
        if (!this.list.hasClass('clearfix')) {
            this.list.addClass(this.className('clearfix'));
        }
        this.list.css('height', this.options.totalHeight + 'px');

        var self = this;
        var elems = this.list.children('div');
        if (elems.size() > 0) {
            elems.each(function() {
                self.format(this, self.options.imageWidth);
            });
        }

        if (this.options.stopOnMousOover == "1") {
            //var self = this;
            self.list.hover(function() {
                self.options.isRun = 0;
                self.list.stop();
            }, function() {
                self.options.isRun = 1;
                self.slideAds();
            });
            /*
            this.container.mouseover(function() {
            self.list.stopAll();
            }).mouseout(function() {
            self.slideAds();
            });*/
        }

        if ($.browser.safari) {
            $(window).bind('load', function() { self.setup(); });
        } else
            this.setup();
    };

    // Create shortcut for internal use
    var $jc = $.jcarousel;


    $jc.fn = $jc.prototype = {
        jcarousel: '0.1.1'
    };

    $jc.fn.extend = $.extend;

    $jc.fn.extend({
        setup: function() {
            var curMarginLeft = this.options.dir == -1 ? 0 : this.options.totalWidth - (this.options.imageWidth * this.options.imageCount);
            this.list.css({ marginLeft: curMarginLeft });
            this.options.newMarginLeft = curMarginLeft + (this.options.dir * this.options.imageWidth);
            if (this.options.start_delay > 0) {
                var self = this;
                setTimeout(function() { self.options.isRun++; self.slideAds(); }, this.options.start_delay);
            }
            else {
                this.slideAds();
            }
            //this.slideAds();
        },

        slideAds: function() {
            if (this.options.isRun != 1) return;
            this.options.isRun++;
            //return;
            var curMarginLeft = parseInt(this.list.css("marginLeft").replace("px", ""));
            var newSpeed = (this.options.speed / this.options.imageWidth) * Math.abs(curMarginLeft - this.options.newMarginLeft);
            var self = this;
            this.list.animate({ marginLeft: this.options.newMarginLeft }, newSpeed, function() { self.wait(); });
        },

        wait: function() {
            var curMarginLeft = this.options.dir == -1 ? 0 : this.options.curMarginLeft = this.options.totalWidth - (this.options.imageWidth * this.options.imageCount);
            this.options.isRun--;
            if (this.options.dir == -1) {
                //this.list.find(".profile:first").appendTo(this.list);
                this.list.children(":first").appendTo(this.list);
            }
            else if (this.options.dir == 1) {
                //this.list.find(".profile:last").prependTo(this.list);
                this.list.children(":last").prependTo(this.list);
            }
            this.list.css({ marginLeft: curMarginLeft });
            this.options.newMarginLeft = curMarginLeft + (this.options.dir * this.options.imageWidth);
            this.options.lastMove = new Date();
            if (this.options.delay > 0) {
                var self = this;
                setTimeout(function() { self.slideAds(); }, this.options.delay);
            }
            else {
                this.slideAds();
            }
        },

        format: function(e, width) {
            var e = $(e).addClass(this.className('profile'));
            e.css('width', width + 'px');
            return e;
        },

        className: function(c) {
            //return c + ' ' + c + (!this.options.vertical ? '-horizontal' : '-vertical');
            return c;
        }

    });
    /*
    $jc.extend({
    defaults: function(d) {
    return;
    return $.extend(defaults, d || {});
    },

        margin: function(e, p) {
    return;
    if (!e)
    return 0;

            var el = e.jquery != undefined ? e[0] : e;

            if (p == 'marginRight' && $.browser.safari) {
    var old = { 'display': 'block', 'float': 'none', 'width': 'auto' }, oWidth, oWidth2;

                $.swap(el, old, function() { oWidth = el.offsetWidth; });

                old['marginRight'] = 0;
    $.swap(el, old, function() { oWidth2 = el.offsetWidth; });

                return oWidth2 - oWidth;
    }

            return $jc.intval($.css(el, p));
    },

        intval: function(v) {
    return;
    v = parseInt(v);
    return isNaN(v) ? 0 : v;
    }
    });
    */
})(jQuery);
