(function ($) {
    var o = (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4);
    if (o) {
        try {
            document.execCommand("BackgroundImageCache", false, true)
        } catch (err) {}
    };
    if ($.proxy === undefined) {
        $.extend({
            proxy: function (a, b) {
                if (a) {
                    proxy = function () {
                        return a.apply(b || this, arguments)
                    }
                };
                return proxy
            }
        })
    };
    var p = function () {};
    $.extend(p.prototype, {
        init: function (c, d) {
            var e = {
                navigation: '',
                control: '',
                width: 500,
                height: 332,
                strips: 20,
                auto: true,
                autopause: true,
                delay: 3000,
                stripSpeed: 400,
                titleOpacity: 0.7,
                titleSpeed: 700,
                titlePosition: 'bottom',
                titleStyle: 'auto',
                direction: 'alternate',
                effect: 'random',
                onComplete: function () {},
                onSlideshowEnd: function () {},
                onTitleShow: function () {},
                onTitleHide: function () {}
            };
            this.dom = {};
            this.img = [];
            this.titles = [];
            this.links = [];
            this.imgInc = 0;
            this.imgInterval = 0;
            this.inc = 0;
            this.order = [];
            this.controls = [];
            this.direction = 0;
            this.options = $.extend({}, e, d);
            this.element = c;
            var f = this.options;
            var g = this;
            $('img', c).each(function (i) {
                var b = $(this);
                g.img[i] = b.attr('src');
                g.titles[i] = b.attr('alt') ? b.attr('alt') : (b.attr('title') ? b.attr('title') : '');
                g.links[i] = b.parent().is('a') ? b.parent().attr('href') : false;
                if (g.options.control) {
                    g.controls[i] = $('<a href="#" class="sexyslider-control" rel="' + i + '"><span>' + (i + 1) + '</span></a>');
                    $(g.options.control).append(g.controls[i]);
                    if (i == 0) {
                        g.controls[i].addClass('active')
                    }
                    g.controls[i].click(function (a) {
                        g.transition($(this).attr('rel'));
                        a.preventDefault()
                    })
                };
                b.hide()
            });
            $(this.element).css({
                'background-image': 'url(' + this.img[0] + ')',
                'background-position': 'top left',
                'position': 'relative',
                'overflow': 'hidden',
                'width': f.width,
                'height': f.height
            });
            this.dom.title = $("<div class='sexyslider-title'>" + this.titles[0] + "</div>");
            if (f.titleStyle == 'auto') {
                this.dom.title.css({
                    'background-color': '#000000',
                    'color': '#FFFFFF',
                    'position': 'absolute',
                    'padding': '5px 10px',
                    'z-index': 5
                })
            }
            if (f.titlePosition == 'bottom') {
                this.dom.title.css({
                    'bottom': 0,
                    'left': 0,
                    'width': f.width - 20
                })
            } else if (f.titlePosition == 'top') {
                this.dom.title.css({
                    'top': 0,
                    'left': 0,
                    'width': f.width - 20
                })
            } else if (f.titlePosition == 'right') {
                this.dom.title.css({
                    'top': 0,
                    'right': 0,
                    'width': parseInt(f.width * 30 / 100),
                    'height': f.height
                })
            } else if (f.titlePosition == 'left') {
                this.dom.title.css({
                    'top': 0,
                    'left': 0,
                    'width': parseInt(f.width * 30 / 100),
                    'height': f.height
                })
            };
            $(this.element).append(this.dom.title);
            if (this.titles[this.imgInc]) {
                $(this.dom.title).css({
                    'opacity': f.titleOpacity
                })
            } else {
                $(this.dom.title).css({
                    'opacity': 0
                })
            };
            var h = parseInt(f.width / f.strips);
            var j = f.width - h * f.strips;
            var k = 0;
            this.dom.strip = [];
            for (i = 0;
            i < f.strips;
            i++) {
                if (j > 0) {
                    tsWidth = h + 1;
                    j--
                } else {
                    tsWidth = h
                }
                var l = $("<div class='sexyslider-strip'></div>").get(0);
                $(l).css({
                    'background-position': -k + 'px top',
                    'width': tsWidth + "px",
                    'height': f.height + "px",
                    'position': 'absolute',
                    'left': k
                });
                this.dom.strip.push(l);
                k += tsWidth;
                this.order[i] = i
            };
            $(this.element).append(this.dom.strip);
            this.dom.link = $("<a class='sexyslider-link'></a>");
            this.dom.link.css({
                'text-decoration': 'none',
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'border': 0,
                'z-index': 8,
                'width': f.width,
                'height': f.height
            });
            $(this.element).append(this.dom.link);
            if (this.links[this.imgInc]) {
                this.dom.link.attr('href', this.links[this.imgInc])
            } else {
                this.dom.link.css({
                    'display': 'none'
                })
            };
            if (f.navigation) {
                var m = $('<a href="#" class="sexyslider-prev"><span>Prev</span></a>');
                var n = $('<a href="#" class="sexyslider-next"><span>Next</span></a>');
                m.click($.proxy(function (a) {
                    this.transition("prev");
                    a.preventDefault()
                }, this));
                n.click($.proxy(function (a) {
                    this.transition("next");
                    a.preventDefault()
                }, this));
                $(f.navigation).append(m, n)
            };
            $(c).bind('mouseover', $.proxy(function () {
                this.setpause(true)
            }, this));
            $(c).bind('mouseout', $.proxy(function () {
                this.setpause(false)
            }, this));
            $(this.dom.title).bind('mouseover', $.proxy(function () {
                this.setpause(true)
            }, this));
            $(this.dom.title).bind('mouseout', $.proxy(function () {
                this.setpause(false)
            }, this));
            f.onComplete.call(this);
            if (f.auto) {
                this.slideshow()
            }
            return this
        },
        slideshow: function () {
            clearInterval(this.imgInterval);
            this.imgInterval = setInterval($.proxy(function () {
                this.transition()
            }, this), this.options.delay + ((this.options.stripSpeed / 6) * this.options.strips) + this.options.stripSpeed)
        },
        setpause: function (a) {
            this.pause = a
        },
        transition: function (b) {
            if (this.pause == true || b == this.imgInc) {
                return false
            };
            this.pause = true;
            this.stripInterval = setInterval($.proxy(function () {
                this.strips(this.order[this.inc])
            }, this), this.options.stripSpeed / 6);
            $(this.element).css({
                'background-image': 'url(' + this.img[this.imgInc] + ')'
            });
            switch (b) {
            case "next":
                this.imgInc = (this.imgInc + 1 >= this.img.length) ? 0 : this.imgInc + 1;
                break;
            case "prev":
                this.imgInc = (this.imgInc - 1 < 0) ? this.img.length - 1 : this.imgInc - 1;
                break;
            case "first":
                this.imgInc = 0;
                break;
            case "last":
                this.imgInc = this.img.length - 1;
                break;
            default:
                if (!isNaN(parseFloat(b)) && isFinite(b)) {
                    this.imgInc = parseInt(b)
                } else {
                    this.imgInc = (this.imgInc + 1 >= this.img.length) ? 0 : this.imgInc + 1
                };
                break
            };
            if (b !== undefined) {
                if (this.options.autopause) {
                    clearInterval(this.imgInterval)
                } else {
                    this.slideshow()
                }
            }
            if (this.titles[this.imgInc] != '') {
                $(this.dom.title).animate({
                    'opacity': 0
                }, this.options.titleSpeed, $.proxy(function () {
                    $(this.dom.title).html(this.titles[this.imgInc]);
                    this.options.onTitleHide.call(this)
                }, this));
                opacity = this.options.titleOpacity
            } else {
                opacity = 0
            };
            $(this.dom.title).animate({
                'opacity': opacity
            }, this.options.titleSpeed, $.proxy(function () {
                this.options.onTitleShow.call(this)
            }, this));
            if (this.links[this.imgInc]) {
                this.dom.link.attr('href', this.links[this.imgInc]);
                this.dom.link.css({
                    'display': 'block'
                })
            } else {
                this.dom.link.css({
                    'display': 'none'
                })
            };
            if (this.options.control) {
                $.each(this.controls, function (i, a) {
                    $(a).removeClass('active')
                });
                $(this.controls[this.imgInc]).addClass('active')
            }
            this.inc = 0;
            switch (this.options.effect) {
            case 'fountain':
                $.proxy(this.effects.fountain, this)();
                break;
            case 'wave':
                $.proxy(this.effects.wave, this)();
                break;
            case 'zipper':
                $.proxy(this.effects.zipper, this)();
                break;
            case 'cascade':
                $.proxy(this.effects.cascade, this)();
                break;
            case 'curtain':
                $.proxy(this.effects.curtain, this)();
                break;
            case 'fade':
                $.proxy(this.effects.fade, this)();
                break;
            case 'random':
                $.proxy(this.effects.random, this)();
                break
            };
            if ((this.options.direction == 'right' && this.order[0] == 1)) {
                this.order.reverse();
                this.direction = 1
            } else if (this.options.direction == 'random') {
                this.order = this.shuffle(this.order)
            } else if (this.options.direction == 'alternate') {
                if (this.direction == 0) {
                    this.direction = 1
                } else {
                    this.order.reverse();
                    this.direction = 0
                }
            } else {
                this.direction = 0
            }
        },
        shuffle: function (a) {
            for (var j, x, i = a.length;
            i;
            j = parseInt(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
            return a
        },
        effects: {
            cascade: function () {
                var a = 1;
                var b = this.order.length;
                var c = parseInt(this.options.strips / 2);
                for (i = 0;
                i < b;
                i++) {
                    $(this.dom.strip[i]).css('bottom', 'auto');
                    this.order[i] = c - (parseInt((i + 1) / 2) * a);
                    a *= -1
                };
                this.order[this.options.strips - 1] = 0
            },
            curtain: function () {
                $.proxy(this.effects.wave, this)()
            },
            wave: function () {
                for (i = 0;
                i < this.order.length;
                i++) {
                    $(this.dom.strip[i]).css('bottom', 'auto');
                    this.order[i] = i
                }
            },
            zipper: function () {
                for (i = 0;
                i < this.order.length;
                i++) {
                    if (i % 2 == 0) {
                        $(this.dom.strip[i]).css('bottom', 0)
                    } else {
                        $(this.dom.strip[i]).css('bottom', 'auto')
                    };
                    this.order[i] = i
                }
            },
            fountain: function () {
                var a = 1;
                var b = this.order.length;
                var c = parseInt(this.options.strips / 2);
                for (i = 0;
                i < b;
                i++) {
                    $(this.dom.strip[i]).css('bottom', 0);
                    this.order[i] = c - (parseInt((i + 1) / 2) * a);
                    a *= -1
                };
                this.order[this.options.strips - 1] = 0
            },
            fade: function () {
                $.proxy(this.effects.wave, this)()
            },
            random: function () {
                var i = parseInt(Math.random() * 4);
                switch (i) {
                case 0:
                    $.proxy(this.effects.fountain, this)();
                    this.options.usewidth = false;
                    break;
                case 1:
                    $.proxy(this.effects.wave, this)();
                    this.options.usewidth = false;
                    break;
                case 2:
                    $.proxy(this.effects.fountain, this)();
                    $.proxy(this.effects.wave, this)();
                    $.proxy(this.effects.zipper, this)();
                    this.options.usewidth = false;
                    break;
                case 3:
                    $.proxy(this.effects.wave, this)();
                    this.options.usewidth = true;
                    break;
                case 4:
                    $.proxy(this.effects.cascade, this)();
                    this.options.usewidth = false;
                    break
                }
            }
        },
        strips: function (a) {
            if (this.inc == this.options.strips) {
                clearInterval(this.stripInterval);
                this.options.onSlideshowEnd.call(this);
                setTimeout($.proxy(function () {
                    this.pause = false
                }, this), this.options.stripSpeed);
                return false
            };
            this.pause = true;
            var b = $(this.dom.strip[a]);
            if (!o || this.options.effect == 'fade') {
                b.css({
                    'opacity': 0
                })
            };
            if (this.options.effect == 'curtain' || this.options.usewidth == true) {
                currWidth = b.width();
                b.css({
                    'width': 0,
                    'background-image': 'url(' + this.img[this.imgInc] + ')'
                });
                b.animate({
                    'width': currWidth,
                    'opacity': 1
                }, this.options.stripSpeed)
            } else if (this.options.effect == 'fade') {
                b.css({
                    'background-image': 'url(' + this.img[this.imgInc] + ')'
                });
                b.animate({
                    'opacity': 1
                }, this.options.stripSpeed)
            } else {
                b.css({
                    'height': 0,
                    'background-image': 'url(' + this.img[this.imgInc] + ')'
                });
                b.animate({
                    'height': this.options.height,
                    'opacity': 1
                }, this.options.stripSpeed)
            }
            this.inc++
        }
    });
    $.fn.SexySlider = function (a) {
        this.each(function () {
            if (!this.SSObject) {
                this.SSObject = new p().init(this, a)
            };
            return this.SSObject
        })
    }
})(jQuery);
