/**
 * Sexy LightBox - for jQuery 1.3.2
 * @name      sexylightbox.v2.3.js
 * @author    Eduardo D. Sada - http://www.coders.me/web-html-js-css/javascript/sexy-lightbox-2
 * @version   2.3.4
 * @date      10-Nov-2009
 * @copyright (c) 2009 Eduardo D. Sada (www.coders.me)
 * @license   MIT - http://es.wikipedia.org/wiki/Licencia_MIT
 * @example   http://www.coders.me/ejemplos/sexy-lightbox-2/
*/

jQuery.bind = function (b, c) {
    var d = Array.prototype.slice.call(arguments, 2);
    return function () {
        var a = [this].concat(d, $.makeArray(arguments));
        return c.apply(b, a)
    }
};
(function ($) {
    SexyLightbox = {
        getOptions: function () {
            return {
                name: 'SLB',
                zIndex: 32000,
                color: 'black',
                find: 'sexylightbox',
                dir: 'sexyimages',
                emergefrom: 'top',
                background: 'bgSexy.png',
                backgroundIE: 'bgSexy.gif',
                buttons: 'buttons.png',
                displayed: 0,
                showDuration: 200,
                closeDuration: 400,
                moveDuration: 1000,
                moveEffect: 'easeInOutBack',
                resizeDuration: 1000,
                resizeEffect: 'easeInOutBack',
                shake: {
                    distance: 10,
                    duration: 100,
                    transition: 'easeInOutBack',
                    loops: 2
                },
                BoxStyles: {
                    'width': 486,
                    'height': 320
                },
                Skin: {
                    'white': {
                        'hexcolor': '#FFFFFF',
                        'captionColor': '#000000',
                        'background-color': '#000000',
                        'opacity': 0.6
                    },
                    'black': {
                        'hexcolor': '#000000',
                        'captionColor': '#FFFFFF',
                        'background-color': '#000000',
                        'opacity': 0.6
                    },
                    'blanco': {
                        'hexcolor': '#FFFFFF',
                        'captionColor': '#000000',
                        'background-color': '#000000',
                        'opacity': 0.6
                    },
                    'negro': {
                        'hexcolor': '#000000',
                        'captionColor': '#FFFFFF',
                        'background-color': '#000000',
                        'opacity': 0.6
                    }
                }
            }
        },
        overlay: {
            create: function (c) {
                this.options = c;
                this.element = $('<div id="' + new Date().getTime() + '"></div>');
                this.element.css($.extend({}, {
                    'position': 'absolute',
                    'top': 0,
                    'left': 0,
                    'opacity': 0,
                    'display': 'none',
                    'z-index': this.options.zIndex
                }, this.options.style));
                this.element.bind('click', $.bind(this, function (a, b) {
                    if (this.options.hideOnClick) {
                        if (this.options.callback) {
                            this.options.callback()
                        } else {
                            this.hide()
                        }
                    }
                    b.preventDefault()
                }));
                this.hidden = true;
                this.inject()
            },
            inject: function () {
                this.target = $(document.body);
                this.target.append(this.element);
                if ($.browser.msie && $.browser.version == "6.0") {
                    var a = parseInt(this.element.css('zIndex'));
                    if (!a) {
                        a = 1;
                        var b = this.element.css('position');
                        if (b == 'static' || !b) {
                            this.element.css({
                                'position': 'relative'
                            })
                        }
                        this.element.css({
                            'zIndex': a
                        })
                    }
                    a = ( !! (this.options.zIndex || this.options.zIndex === 0) && a > this.options.zIndex) ? this.options.zIndex : a - 1;
                    if (a < 0) {
                        a = 1
                    }
                    this.shim = $('<iframe id="IF_' + new Date().getTime() + '" scrolling="no" frameborder=0 src=""></div>');
                    this.shim.css({
                        zIndex: a,
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        border: 'none',
                        opacity: 0
                    });
                    this.shim.insertAfter(this.element)
                }
            },
            resize: function (x, y) {
                this.element.css({
                    'height': 0,
                    'width': 0
                });
                if (this.shim) this.shim.css({
                    'height': 0,
                    'width': 0
                });
                var a = {
                    x: $(document).width(),
                    y: $(document).height()
                };
                var b = $.browser.safari ? (a.x - 25 < document.body.clientWidth ? document.body.clientWidth : a.x) : a.x;
                this.element.css({
                    width: x ? x : b,
                    height: y ? y : a.y
                });
                if (this.shim) {
                    this.shim.css({
                        'height': 0,
                        'width': 0
                    });
                    this.shim.css({
                        width: x ? x : b,
                        height: y ? y : a.y
                    })
                }
                return this
            },
            show: function () {
                if (!this.hidden) return this;
                if (this.transition) this.transition.stop();
                this.target.bind('resize', $.bind(this, this.resize));
                this.resize();
                if (this.shim) this.shim.css({
                    'display': 'block'
                });
                this.hidden = false;
                this.transition = this.element.fadeIn(this.options.showDuration, $.bind(this, function () {
                    this.element.trigger('show')
                }));
                return this
            },
            hide: function () {
                if (this.hidden) return this;
                if (this.transition) this.transition.stop();
                this.target.unbind('resize');
                if (this.shim) this.shim.css({
                    'display': 'none'
                });
                this.hidden = true;
                this.transition = this.element.fadeOut(this.options.closeDuration, $.bind(this, function () {
                    this.element.trigger('hide');
                    this.element.css({
                        'height': 0,
                        'width': 0
                    })
                }));
                return this
            }
        },
        backwardcompatibility: function (a) {
            this.options.dir = a.imagesdir || a.path || a.folder || a.dir;
            this.options.OverlayStyles = $.extend(this.options.Skin[this.options.color], this.options.OverlayStyles || {})
        },
        preloadimage: function (a) {
            img = new Image();
            img.src = a
        },
        initialize: function (c) {
            this.options = $.extend(this.getOptions(), c);
            this.backwardcompatibility(this.options);
            var d = this.options.dir + '/' + this.options.color + '/' + ((((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))) ? this.options.backgroundIE : this.options.background);
            var e = this.options.name;
            this.preloadimage(d);
            this.preloadimage(this.options.dir + '/' + this.options.color + '/' + this.options.buttons);
            this.overlay.create({
                style: this.options.Skin[this.options.color],
                hideOnClick: true,
                zIndex: this.options.zIndex - 1,
                callback: $.bind(this, this.close),
                showDuration: this.options.showDuration,
                showEffect: this.options.showEffect,
                closeDuration: this.options.closeDuration,
                closeEffect: this.options.closeEffect
            });
            this.lightbox = {};
            $('body').append('<div id="' + e + '-Wrapper"><div id="' + e + '-Background"></div><div id="' + e + '-Contenedor"><div id="' + e + '-Top" style="background-image: url(' + d + ')"><a id="' + e + '-CloseButton" href="#">&nbsp;</a><div id="' + e + '-TopLeft" style="background-image: url(' + d + ')"></div></div><div id="' + e + '-Contenido"></div><div id="' + e + '-Bottom" style="background-image: url(' + d + ')"><div id="' + e + '-BottomRight" style="background-image: url(' + d + ')"><div id="' + e + '-Navegador"><strong id="' + e + '-Caption"></strong></div></div></div></div></div>');
            this.Wrapper = $('#' + e + '-Wrapper');
            this.Background = $('#' + e + '-Background');
            this.Contenedor = $('#' + e + '-Contenedor');
            this.Top = $('#' + e + '-Top');
            this.CloseButton = $('#' + e + '-CloseButton');
            this.Contenido = $('#' + e + '-Contenido');
            this.bb = $('#' + e + '-Bottom');
            this.innerbb = $('#' + e + '-BottomRight');
            this.Nav = $('#' + e + '-Navegador');
            this.Descripcion = $('#' + e + '-Caption');
            this.Wrapper.css({
                'z-index': this.options.zIndex,
                'display': 'none'
            }).hide();
            this.Background.css({
                'z-index': this.options.zIndex + 1
            });
            this.Contenedor.css({
                'position': 'absolute',
                'width': this.options.BoxStyles['width'],
                'z-index': this.options.zIndex + 2
            });
            this.Contenido.css({
                'height': this.options.BoxStyles['height'],
                'border-left-color': this.options.Skin[this.options.color].hexcolor,
                'border-right-color': this.options.Skin[this.options.color].hexcolor
            });
            this.CloseButton.css({
                'background-image': 'url(' + this.options.dir + '/' + this.options.color + '/' + this.options.buttons + ')'
            });
            this.Nav.css({
                'color': this.options.Skin[this.options.color].captionColor
            });
            this.Descripcion.css({
                'color': this.options.Skin[this.options.color].captionColor
            });
            this.CloseButton.bind('click', $.bind(this, function () {
                this.close();
                return false
            }));
            $(document).bind('keydown', $.bind(this, function (a, b) {
                if (this.options.displayed == 1) {
                    if (b.keyCode == 27) {
                        this.close()
                    }
                    if (b.keyCode == 37) {
                        if (this.prev) {
                            this.prev.trigger('click', b)
                        }
                    }
                    if (b.keyCode == 39) {
                        if (this.next) {
                            this.next.trigger('click', b)
                        }
                    }
                }
            }));
            $(window).bind('resize', $.bind(this, function () {
                if (this.options.displayed == 1) {
                    this.replaceBox();
                    this.overlay.resize()
                }
            }));
            $(window).bind('scroll', $.bind(this, function () {
                if (this.options.displayed == 1) {
                    this.replaceBox()
                }
            }));
            this.refresh()
        },
        hook: function (a) {
            a = $(a);
            a.blur();
            this.show((a.attr("title") || a.attr("name") || ""), a.attr("href"), (a.attr('rel') || false))
        },
        close: function () {
            this.animate(0)
        },
        refresh: function () {
            var b = this;
            this.anchors = [];
            $("a, area").each(function () {
                if ($(this).attr('rel') && new RegExp("^" + b.options.find).test($(this).attr('rel'))) {
                    $(this).click(function (a) {
                        a.preventDefault();
                        b.hook(this)
                    });
                    if (!($(this).attr('id') == b.options.name + "-Left" || $(this).attr('id') == b.options.name + "-Right")) {
                        b.anchors.push(this)
                    }
                }
            })
        },
        animate: function (a) {
            if (this.options.displayed == 0 && a != 0 || a == 1) {
                this.overlay.show();
                this.options.displayed = 1;
                this.Wrapper.css({
                    'display': 'block'
                })
            } else {
                this.Wrapper.css({
                    'display': 'none',
                    'top': -(this.options.BoxStyles['height'] + 280)
                }).hide();
                this.overlay.hide();
                this.overlay.element.bind('hide', $.bind(this, function () {
                    if (this.options.displayed) {
                        if (this.Image) this.Image.remove();
                        this.options.displayed = 0
                    }
                }))
            }
        },
        replaceBox: function (a) {
            var b = {
                x: $(window).width(),
                y: $(window).height()
            };
            var c = {
                x: $(window).scrollLeft(),
                y: $(window).scrollTop()
            };
            var d = this.options.BoxStyles['width'];
            var e = this.options.BoxStyles['height'];
            if (this.options.displayed == 0) {
                var x = 0;
                var y = 0;
                y = c.x + ((b.x - d) / 2);
                if (this.options.emergefrom == "bottom") {
                    x = (c.y + b.y + 80)
                } else {
                    x = (c.y - e) - 80
                }
                this.Wrapper.css({
                    'display': 'none',
                    'top': x,
                    'left': y
                });
                this.Contenedor.css({
                    'width': d
                });
                this.Contenido.css({
                    'height': e - 80
                })
            }
            a = $.extend({}, {
                'width': this.lightbox.width,
                'height': this.lightbox.height,
                'resize': 0
            }, a);
            if (this.MoveBox) this.MoveBox.stop();
            this.MoveBox = this.Wrapper.animate({
                'left': (c.x + ((b.x - a.width) / 2)),
                'top': (c.y + (b.y - (a.height + (this.navigator ? 80 : 48))) / 2)
            }, {
                duration: this.options.moveDuration,
                easing: this.options.moveEffect
            });
            if (a.resize) {
                if (this.ResizeBox2) this.ResizeBox2.stop();
                this.ResizeBox2 = this.Contenido.animate({
                    height: a.height
                }, {
                    duration: this.options.resizeDuration,
                    easing: this.options.resizeEffect
                });
                if (this.ResizeBox) this.ResizeBox.stop();
                this.ResizeBox = this.Contenedor.animate({
                    width: a.width
                }, {
                    duration: this.options.resizeDuration,
                    easing: this.options.resizeEffect,
                    complete: function () {
                        $(this).trigger('complete')
                    }
                })
            }
        },
        getInfo: function (a, b) {
            a = $(a);
            IEuta = $('<a id="' + this.options.name + '-' + b + '" title="' + a.attr('title') + '" rel="' + a.attr('rel') + '">&nbsp;</a>');
            IEuta.css({
                'background-image': 'url(' + this.options.dir + '/' + this.options.color + '/' + this.options.buttons + ')'
            });
            IEuta.attr('href', a.attr('href'));
            return IEuta
        },
        display: function (a, b, c) {
            return this.show(b, a, '', c)
        },
        show: function (b, c, d, e) {
            this.showLoading();
            var f = c.match(/(.+)?/)[1] || c;
            var g = /\.(jpe?g|png|gif|bmp)/gi;
            var h = c.match(/\?(.+)/);
            if (h) h = h[1];
            var i = this.parseQuery(h);
            if (this.ResizeBox) this.ResizeBox.unbind('complete');
            i = $.extend({}, {
                'width': 0,
                'height': 0,
                'modal': 0,
                'background': '',
                'title': b
            }, i || {});
            i['width'] = parseInt(i['width']);
            i['height'] = parseInt(i['height']);
            i['modal'] = parseInt(i['modal']);
            this.overlay.options.hideOnClick = !i['modal'];
            this.lightbox = $.extend({}, i, {
                'width': i['width'] + 14
            });
            this.navigator = this.lightbox.title ? true : false;
            if (e == 'image' || f.match(g)) {
                this.img = new Image();
                this.img.onload = $.bind(this, function () {
                    this.img.onload = function () {};
                    if (!i['width']) {
                        var a = this.calculate(this.img.width, this.img.height);
                        i['width'] = a.x;
                        i['height'] = a.y;
                        this.lightbox.width = i['width'] + 14
                    }
                    this.lightbox.height = i['height'] - (this.navigator ? 21 : 35);
                    this.replaceBox({
                        'resize': 1
                    });
                    this.ResizeBox.bind('complete', $.bind(this, function () {
                        this.showImage(this.img.src, i)
                    }))
                });
                this.img.onerror = $.bind(this, function () {
                    this.show('', this.options.imagesdir + '/' + this.options.color + '/404.png', this.options.find)
                });
                this.img.src = c
            } else {
                this.lightbox.height = i['height'] + ($.browser.opera ? 2 : 0);
                this.replaceBox({
                    'resize': 1
                });
                if (c.indexOf('TB_inline') != -1) {
                    this.ResizeBox.bind('complete', $.bind(this, function () {
                        this.showContent($('#' + i['inlineId']).html(), this.lightbox)
                    }))
                } else if (c.indexOf('TB_iframe') != -1) {
                    var j = c.split('TB_');
                    this.ResizeBox.bind('complete', $.bind(this, function () {
                        this.showIframe(j[0], this.lightbox)
                    }))
                } else {
                    this.ResizeBox.bind('complete', $.bind(this, function () {
                        $.ajax({
                            url: c,
                            type: "GET",
                            cache: false,
                            error: $.bind(this, function () {
                                this.show('', this.options.imagesdir + '/' + this.options.color + '/404html.png', this.options.find)
                            }),
                            success: $.bind(this, this.handlerFunc)
                        })
                    }))
                }
            }
            this.next = false;
            this.prev = false;
            if (d.length > this.options.find.length) {
                this.navigator = true;
                var k = false;
                var l = false;
                var m = this;
                $.each(this.anchors, function (a) {
                    if ($(this).attr('rel') == d && !l) {
                        if ($(this).attr('href') == c) {
                            k = true
                        } else {
                            if (k) {
                                m.next = m.getInfo(this, "Right");
                                l = true
                            } else {
                                m.prev = m.getInfo(this, "Left")
                            }
                        }
                    }
                })
            }
            this.addButtons();
            this.showNavBar(b);
            this.animate(1)
        },
        calculate: function (x, y) {
            var a = $(window).width() - 100;
            var b = $(window).height() - 100;
            if (x > a) {
                y = y * (a / x);
                x = a;
                if (y > b) {
                    x = x * (b / y);
                    y = b
                }
            } else if (y > b) {
                x = x * (b / y);
                y = b;
                if (x > a) {
                    y = y * (a / x);
                    x = a
                }
            }
            return {
                x: parseInt(x),
                y: parseInt(y)
            }
        },
        handlerFunc: function (a, b) {
            this.showContent(b, this.lightbox)
        },
        addButtons: function () {
            if (this.prev) this.prev.bind('click', $.bind(this, function (a, b) {
                b.preventDefault();
                this.hook(this.prev)
            }));
            if (this.next) this.next.bind('click', $.bind(this, function (a, b) {
                b.preventDefault();
                this.hook(this.next)
            }))
        },
        showNavBar: function () {
            if (this.navigator) {
                this.bb.addClass("SLB-bbnav");
                this.Nav.empty();
                this.innerbb.empty();
                this.innerbb.append(this.Nav);
                this.Descripcion.html(this.lightbox.title);
                this.Nav.append(this.prev);
                this.Nav.append(this.next);
                this.Nav.append(this.Descripcion)
            } else {
                this.bb.removeClass("SLB-bbnav");
                this.innerbb.empty()
            }
        },
        showImage: function (a, b) {
            this.Background.empty().removeAttr('style').css({
                'width': 'auto',
                'height': 'auto'
            }).append('<img id="' + this.options.name + '-Image"/>');
            this.Image = $('#' + this.options.name + '-Image');
            this.Image.attr('src', a).css({
                'width': b['width'],
                'height': b['height']
            });
            this.Contenedor.css({
                'background': 'none'
            });
            this.Contenido.empty().css({
                'background-color': 'transparent',
                'padding': '0px',
                'width': 'auto'
            })
        },
        showContent: function (a, b) {
            this.Background.empty().css({
                'width': b['width'] - 14,
                'height': b['height'] + 35,
                'background-color': b['background'] || '#ffffff'
            });
            this.Contenido.empty().css({
                'width': b['width'] - 14,
                'background-color': b['background'] || '#ffffff'
            }).append('<div id="' + this.options.name + '-Image"/>');
            this.Image = $('#' + this.options.name + '-Image');
            this.Image.css({
                'width': b['width'] - 14,
                'height': b['height'],
                'overflow': 'auto',
                'background': b['height'] || '#ffffff'
            }).append(a);
            this.Contenedor.css({
                'background': 'none'
            })
        },
        showIframe: function (a, b, c) {
            this.Background.empty().css({
                'width': b['width'] - 14,
                'height': b['height'] + 35,
                'background-color': b['background'] || '#ffffff'
            });
            var d = "if_" + new Date().getTime() + "-Image";
            this.Contenido.empty().css({
                'width': b['width'] - 14,
                'background-color': b['background'] || '#ffffff',
                'padding': '0px'
            }).append('<iframe id="' + d + '" frameborder="0"></iframe>');
            this.Image = $('#' + d);
            this.Image.css({
                'width': b['width'] - 14,
                'height': b['height'],
                'background': b['background'] || '#ffffff'
            }).attr('src', a);
            this.Contenedor.css({
                'background': 'none'
            })
        },
        showLoading: function () {
            this.Background.empty().removeAttr('style').css({
                'width': 'auto',
                'height': 'auto'
            });
            this.Contenido.empty().css({
                'background-color': 'transparent',
                'padding': '0px',
                'width': 'auto'
            });
            this.Contenedor.css({
                'background': 'url(' + this.options.imagesdir + '/' + this.options.color + '/loading.gif) no-repeat 50% 50%'
            });
            this.Contenido.empty().css({
                'background-color': 'transparent',
                'padding': '0px',
                'width': 'auto'
            });
            this.replaceBox($.extend(this.options.BoxStyles, {
                'resize': 1
            }))
        },
        parseQuery: function (a) {
            if (!a) return {};
            var b = {};
            var c = a.split(/[;&]/);
            for (var i = 0; i < c.length; i++) {
                var d = c[i].split('=');
                if (!d || d.length != 2) continue;
                b[unescape(d[0])] = unescape(d[1]).replace(/\+/g, ' ')
            }
            return b
        },
        shake: function () {
            var d = this.options.shake.distance;
            var l = this.Wrapper.position();
            l = l.left;
            for (x = 0; x < this.options.shake.loops; x++) {
                this.Wrapper.animate({
                    left: l + d
                }, this.options.shake.duration, this.options.shake.transition).animate({
                    left: l - d
                }, this.options.shake.duration, this.options.shake.transition)
            }
            this.Wrapper.animate({
                "left": l + d
            }, this.options.shake.duration, this.options.shake.transition).animate({
                "left": l
            }, this.options.shake.duration, this.options.shake.transition)
        }
    }
})(jQuery);
