/**
 * SlideDeck 1.2.5 Lite - 2011-06-01
 * Copyright (c) 2011 digital-telepathy (http://www.dtelepathy.com)
 * 
 * Support the developers by purchasing the Pro version at http://www.slidedeck.com/download
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * More information on this project:
 * http://www.slidedeck.com/
 * 
 * Requires: jQuery v1.3+
 * 
 * Full Usage Documentation: http://www.slidedeck.com/usage-documentation 
 * Usage:
 *     $(el).slidedeck(opts);
 * 
 * @param {HTMLObject} el    The <DL> element to extend as a SlideDeck
 * @param {Object} opts        An object to pass custom override options to
 */
var SlideDeck;
var SlideDeckSkin = {};
(function ($) {
    window.SlideDeck = function (p, q) {
        var r = this,
            p = $(p);
        var u = "1.2.5";
        this.options = {
            speed: 500,
            transition: 'swing',
            start: 1,
            activeCorner: true,
            index: true,
            scroll: true,
            keys: true,
            autoPlay: false,
            autoPlayInterval: 5000,
            hideSpines: false,
            cycle: false
        };
        this.classes = {
            slide: 'slide',
            spine: 'spine',
            label: 'label',
            index: 'index',
            active: 'active',
            indicator: 'indicator',
            activeCorner: 'activeCorner',
            disabled: 'disabled',
            vertical: 'slidesVertical',
            previous: 'previous',
            next: 'next'
        };
        this.current = 1;
        this.deck = p;
        this.spines = p.children('dt');
        this.slides = p.children('dd');
        this.controlTo = 1;
        this.session = [];
        this.disabledSlides = [];
        this.pauseAutoPlay = false;
        this.isLoaded = false;
        var v = navigator.userAgent.toLowerCase();
        this.browser = {
            chrome: v.match(/chrome/) ? true : false,
            firefox: v.match(/firefox/) ? true : false,
            firefox2: v.match(/firefox\/2/) ? true : false,
            firefox30: v.match(/firefox\/3\.0/) ? true : false,
            msie: v.match(/msie/) ? true : false,
            msie6: (v.match(/msie 6/) && !v.match(/msie 7|8/)) ? true : false,
            msie7: v.match(/msie 7/) ? true : false,
            msie8: v.match(/msie 8/) ? true : false,
            msie9: v.match(/msie 9/) ? true : false,
            chromeFrame: (v.match(/msie/) && v.match(/chrome/)) ? true : false,
            opera: v.match(/opera/) ? true : false,
            safari: (v.match(/safari/) && !v.match(/chrome/)) ? true : false
        };
        for (var b in this.browser) {
            if (this.browser[b] === true) {
                this.browser._this = b
            }
        }
        if (this.browser.chrome === true && !this.browser.chromeFrame) {
            this.browser.version = v.match(/chrome\/([0-9\.]+)/)[1]
        }
        if (this.browser.firefox === true) {
            this.browser.version = v.match(/firefox\/([0-9\.]+)/)[1]
        }
        if (this.browser.msie === true) {
            this.browser.version = v.match(/msie ([0-9\.]+)/)[1]
        }
        if (this.browser.opera === true) {
            this.browser.version = v.match(/version\/([0-9\.]+)/)[1]
        }
        if (this.browser.safari === true) {
            this.browser.version = v.match(/version\/([0-9\.]+)/)[1]
        }
        var w;
        var x;
        var y, spine_outer_width, slide_width, spine_half_width;
        var z = function (a) {
                if (r.browser.msie && !r.browser.msie9) {
                    var b = a.css('background-color');
                    var c = b;
                    if (c == "transparent") {
                        b = "#ffffff"
                    } else {
                        if (c.match('#')) {
                            if (c.length < 7) {
                                var t = "#" + c.substr(1, 1) + c.substr(1, 1) + c.substr(2, 1) + c.substr(2, 1) + c.substr(3, 1) + c.substr(3, 1);
                                b = t
                            }
                        }
                    }
                    b = b.replace("#", "");
                    cParts = {
                        r: b.substr(0, 2),
                        g: b.substr(2, 2),
                        b: b.substr(4, 2)
                    };
                    var d = "#";
                    var e = "01234567890ABCDEF";
                    for (var k in cParts) {
                        cParts[k] = Math.max(0, (parseInt(cParts[k], 16) - 1));
                        cParts[k] = e.charAt((cParts[k] - cParts[k] % 16) / 16) + e.charAt(cParts[k] % 16);
                        d += cParts[k]
                    }
                    a.find('.' + r.classes.index).css({
                        'filter': 'progid:DXImageTransform.Microsoft.BasicImage(rotation=1) chroma(color=' + d + ')',
                        backgroundColor: d
                    })
                }
            };
        var A = {
            <!--id: "SlideDeck_Bug" + (Math.round(Math.random() * 100000000)),-->
			id: "SlideDeck_Bug1" + (Math.round(Math.random() * 100000000)),
            styles: "position:absolute !important;height:" + 13 + "px !important;width:" + 130 + "px !important;display:none !important;margin:0 !important;overflow:hidden !important;visibility:visible !important;opacity:1 !important;padding:0 !important;z-index:20000 !important",
            width: 130,
            height: 13
        };
        var B = function () {
                if (!document.getElementById(A.id)) {
                    var a = document.createElement('A');
                    a.id = A.id;
                    a.href = "http://www.slidedeck.com/?utm_source=LiteUser&utm_medium=Link&utm_campaign=SDbug";
                    a.target = "_blank";
                    var b = document.createElement('IMG');
                    b.src = (document.location.protocol == "https:" ? "https:" : "http:") + "//www.slidedeck.com/6885858486f31043e5839c735d99457f045affd0/" + u + "/lite";
                    b.alt = "Powered by SlideDeck&trade;";
                    b.width = A.width;
                    b.height = A.height;
                    b.border = "0";
                    a.appendChild(b);
                    A.top = (p.offset().top + p.height() + 5);
                    A.left = p.offset().left + p.width() - A.width;
                    var s = document.createElement('STYLE');
                    s.type = "text/css";
                    var c = '#' + A.id + '{top:' + A.top + 'px;left:' + A.left + 'px;' + A.styles + '}' + '#' + A.id + ' img{top:0 !important;left:0 !important;' + A.styles + '}';
                    if (s.styleSheet) {
                        s.styleSheet.cssText = c
                    } else {
                        s.appendChild(document.createTextNode(c))
                    }
                    $('head').append(s);
                    if (Math.random() < 0.5) {
                        $(document.body).prepend(a)
                    } else {
                        $(document.body).append(a)
                    }
                    $(window).resize(function () {
                        B()
                    })
                }
                A.top = (p.offset().top + p.height() + 5);
                A.left = p.offset().left + p.width() - A.width;
                $('#' + A.id).css({
                    top: A.top + "px",
                    left: A.left + "px"
                })
            };
        var C = function () {
                gotoNext = function () {
                    if (r.pauseAutoPlay === false) {
                        if (r.options.cycle === false && r.current == r.slides.length) {
                            r.pauseAutoPlay = true
                        } else {
                            r.next()
                        }
                    }
                };
                setInterval(gotoNext, r.options.autoPlayInterval)
            };
        var D = function () {
                if ($.inArray(p.css('position'), ['position', 'absolute', 'fixed'])) {
                    p.css('position', 'relative')
                }
                p.css('overflow', 'hidden');
                for (var i = 0; i < r.slides.length; i++) {
                    var d = $(r.slides[i]);
                    if (r.spines.length > i) {
                        var e = $(r.spines[i])
                    }
                    var f = {
                        top: parseInt(d.css('padding-top'), 10),
                        right: parseInt(d.css('padding-right'), 10),
                        bottom: parseInt(d.css('padding-bottom'), 10),
                        left: parseInt(d.css('padding-left'), 10)
                    };
                    var g = {
                        top: parseInt(d.css('border-top-width'), 10),
                        right: parseInt(d.css('border-right-width'), 10),
                        bottom: parseInt(d.css('border-bottom-width'), 10),
                        left: parseInt(d.css('border-left-width'), 10)
                    };
                    for (var k in g) {
                        g[k] = isNaN(g[k]) ? 0 : g[k]
                    }
                    if (i < r.current) {
                        if (i == r.current - 1) {
                            if (r.options.hideSpines !== true) {
                                e.addClass(r.classes.active)
                            }
                            d.addClass(r.classes.active)
                        }
                        offset = i * spine_outer_width;
                        if (r.options.hideSpines === true) {
                            if (i == r.current - 1) {
                                offset = 0
                            } else {
                                offset = 0 - (r.options.start - i - 1) * p.width()
                            }
                        }
                    } else {
                        offset = i * spine_outer_width + slide_width;
                        if (r.options.hideSpines === true) {
                            offset = (i + 1 - r.options.start) * p.width()
                        }
                    }
                    r.slide_width = (slide_width - f.left - f.right - g.left - g.right);
                    d.css({
                        position: 'absolute',
                        left: offset,
                        zIndex: 1,
                        height: (x - f.top - f.bottom - g.top - g.bottom) + "px",
                        width: r.slide_width + "px",
                        margin: 0,
                        paddingLeft: f.left + spine_outer_width + "px"
                    }).addClass(r.classes.slide).addClass(r.classes.slide + "_" + (i + 1));
                    if (r.options.hideSpines !== true) {
                        var h = {
                            top: parseInt(e.css('padding-top'), 10),
                            right: parseInt(e.css('padding-right'), 10),
                            bottom: parseInt(e.css('padding-bottom'), 10),
                            left: parseInt(e.css('padding-left'), 10)
                        };
                        for (var k in h) {
                            if (h[k] < 10 && (k == "left" || k == "right")) {
                                h[k] = 10
                            }
                        }
                        var j = h.top + "px " + h.right + "px " + h.bottom + "px " + h.left + "px";
                        var l = {
                            position: 'absolute',
                            zIndex: 3,
                            display: 'block',
                            left: offset,
                            width: (x - h.left - h.right) + "px",
                            height: y + "px",
                            padding: j,
                            rotation: '270deg',
                            '-webkit-transform': 'rotate(270deg)',
                            '-webkit-transform-origin': spine_half_width + 'px 0px',
                            '-moz-transform': 'rotate(270deg)',
                            '-moz-transform-origin': spine_half_width + 'px 0px',
                            '-o-transform': 'rotate(270deg)',
                            '-o-transform-origin': spine_half_width + 'px 0px',
                            textAlign: 'right'
                        };
                        if (!r.browser.msie9) {
                            l.top = (r.browser.msie) ? 0 : (x - spine_half_width) + "px";
                            l.marginLeft = ((r.browser.msie) ? 0 : (0 - spine_half_width)) + "px";
                            l.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'
                        }
                        e.css(l).addClass(r.classes.spine).addClass(r.classes.spine + "_" + (i + 1));
                        if (r.browser.msie9) {
                            e[0].style.msTransform = 'rotate(270deg)';
                            e[0].style.msTransformOrigin = Math.round(parseInt(p[0].style.height) / 2) + 'px ' + Math.round(parseInt(p[0].style.height) / 2) + 'px'
                        }
                    } else {
                        if (typeof (e) != "undefined") {
                            e.hide()
                        }
                    }
                    if (i == r.slides.length - 1) {
                        d.addClass('last');
                        if (r.options.hideSpines !== true) {
                            e.addClass('last')
                        }
                    }
                    if (r.options.activeCorner === true && r.options.hideSpines === false) {
                        var m = document.createElement('DIV');
                        m.className = r.classes.activeCorner + ' ' + (r.classes.spine + '_' + (i + 1));
                        e.after(m);
                        e.next('.' + r.classes.activeCorner).css({
                            position: 'absolute',
                            top: '25px',
                            left: offset + spine_outer_width + "px",
                            overflow: "hidden",
                            zIndex: "20000"
                        }).hide();
                        if (e.hasClass(r.classes.active)) {
                            e.next('.' + r.classes.activeCorner).show()
                        }
                    }
                    if (r.options.hideSpines !== true) {
                        var n = document.createElement('DIV');
                        n.className = r.classes.index;
                        if (r.options.index !== false) {
                            var o;
                            if (typeof (r.options.index) != 'boolean') {
                                o = r.options.index[i % r.options.index.length]
                            } else {
                                o = "" + (i + 1)
                            }
                            n.appendChild(document.createTextNode(o))
                        }
                        e.append(n);
                        e.find('.' + r.classes.index).css({
                            position: 'absolute',
                            zIndex: 2,
                            display: 'block',
                            width: y + "px",
                            height: y + "px",
                            textAlign: 'center',
                            bottom: ((r.browser.msie) ? 0 : (0 - spine_half_width)) + "px",
                            left: ((r.browser.msie) ? 5 : 20) + "px",
                            rotation: "90deg",
                            '-webkit-transform': 'rotate(90deg)',
                            '-webkit-transform-origin': spine_half_width + 'px 0px',
                            '-moz-transform': 'rotate(90deg)',
                            '-moz-transform-origin': spine_half_width + 'px 0px',
                            '-o-transform': 'rotate(90deg)',
                            '-o-transform-origin': spine_half_width + 'px 0px'
                        });
                        if (r.browser.msie9) {
                            e.find('.' + r.classes.index)[0].style.msTransform = 'rotate(90deg)'
                        }
                        z(e)
                    }
                }
                B();
                if (r.options.hideSpines !== true) {
                    r.spines.bind('click', function (a) {
                        a.preventDefault();
                        r.goTo(r.spines.index(this) + 1)
                    })
                }
                if (r.options.keys !== false) {
                    $(document).bind('keydown', function (a) {
                        if ($(a.target).parents().index(r.deck) == -1) {
                            if (a.keyCode == 39) {
                                r.pauseAutoPlay = true;
                                r.next()
                            } else if (a.keyCode == 37) {
                                r.pauseAutoPlay = true;
                                r.prev()
                            }
                        }
                    })
                }
                if (typeof ($.event.special.mousewheel) != "undefined") {
                    p.bind("mousewheel", function (a) {
                        if (r.options.scroll !== false) {
                            var b = a.detail ? a.detail : a.wheelDelta;
                            if (r.browser.msie || r.browser.safari || r.browser.chrome) {
                                b = 0 - b
                            }
                            var c = false;
                            if ($(a.originalTarget).parents(r.deck).length) {
                                if ($.inArray(a.originalTarget.nodeName.toLowerCase(), ['input', 'select', 'option', 'textarea']) != -1) {
                                    c = true
                                }
                            }
                            if (c !== true) {
                                if (b > 0) {
                                    switch (r.options.scroll) {
                                    case "stop":
                                        a.preventDefault();
                                        break;
                                    case true:
                                    default:
                                        if (r.current < r.slides.length || r.options.cycle == true) {
                                            a.preventDefault()
                                        }
                                        break
                                    }
                                    r.pauseAutoPlay = true;
                                    r.next()
                                } else {
                                    switch (r.options.scroll) {
                                    case "stop":
                                        a.preventDefault();
                                        break;
                                    case true:
                                    default:
                                        if (r.current != 1 || r.options.cycle == true) {
                                            a.preventDefault()
                                        }
                                        break
                                    }
                                    r.pauseAutoPlay = true;
                                    r.prev()
                                }
                            }
                        }
                    })
                }
                $(r.spines[r.current - 2]).addClass(r.classes.previous);
                $(r.spines[r.current]).addClass(r.classes.next);
                if (r.options.autoPlay === true) {
                    C()
                }
                r.isLoaded = true
            };
        var E = function (a) {
                a = Math.min(r.slides.length, Math.max(1, a));
                return a
            };
        var F = function (a, b) {
                a = E(a);
                var c = true;
                if (a < r.current) {
                    c = false
                }
                var d = [r.classes.active, r.classes.next, r.classes.previous].join(' ');
                r.current = a;
                r.spines.removeClass(d);
                r.slides.removeClass(d);
                p.find('.' + r.classes.activeCorner).hide();
                $(r.spines[r.current - 2]).addClass(r.classes.previous);
                $(r.spines[r.current]).addClass(r.classes.next);
                for (var i = 0; i < r.slides.length; i++) {
                    var e = 0;
                    if (r.options.hideSpines !== true) {
                        var f = $(r.spines[i])
                    }
                    var g = $(r.slides[i]);
                    if (i < r.current) {
                        if (i == (r.current - 1)) {
                            g.addClass(r.classes.active);
                            if (r.options.hideSpines !== true) {
                                f.addClass(r.classes.active);
                                f.next('.' + r.classes.activeCorner).show()
                            }
                        }
                        e = i * spine_outer_width
                    } else {
                        e = i * spine_outer_width + slide_width
                    }
                    if (r.options.hideSpines === true) {
                        e = (i - r.current + 1) * p.width()
                    }
                    var h = {
                        duration: r.options.speed,
                        easing: r.options.transition
                    };
                    g.stop().animate({
                        left: e + "px",
                        width: r.slide_width + "px"
                    }, h);
                    if (r.options.hideSpines !== true) {
                        z(f);
                        if (f.css('left') != e + "px") {
                            f.stop().animate({
                                left: e + "px"
                            }, {
                                duration: r.options.speed,
                                easing: r.options.transition
                            });
                            f.next('.' + r.classes.activeCorner).stop().animate({
                                left: e + spine_outer_width + "px"
                            }, {
                                duration: r.options.speed,
                                easing: r.options.transition
                            })
                        }
                    }
                }
                B()
            };
        var G = function (a, b) {
                var c = a;
                if (typeof (a) === "string") {
                    c = {};
                    c[a] = b
                }
                for (var d in c) {
                    b = c[d];
                    switch (d) {
                    case "speed":
                    case "start":
                        b = parseFloat(b);
                        if (isNaN(b)) {
                            b = r.options[d]
                        }
                        break;
                    case "scroll":
                    case "keys":
                    case "activeCorner":
                    case "hideSpines":
                    case "autoPlay":
                    case "cycle":
                        if (typeof (b) !== "boolean") {
                            b = r.options[d]
                        }
                        break;
                    case "transition":
                        if (typeof (b) !== "string") {
                            b = r.options[d]
                        }
                        break;
                    case "complete":
                    case "before":
                        if (typeof (b) !== "function") {
                            b = r.options[d]
                        }
                        break;
                    case "index":
                        if (typeof (b) !== "boolean") {
                            if (!$.isArray(b)) {
                                b = r.options[d]
                            }
                        }
                        break
                    }
                    r.options[d] = b
                }
            };
        var H = function () {
                x = p.height();
                w = p.width();
                p.css('height', x + "px");
                y = 0;
                spine_outer_width = 0;
                if (r.options.hideSpines !== true && r.spines.length > 0) {
                    y = $(r.spines[0]).height();
                    spine_outer_width = $(r.spines[0]).outerHeight()
                }
                slide_width = w - spine_outer_width * r.spines.length;
                if (r.options.hideSpines === true) {
                    slide_width = w
                }
                spine_half_width = Math.ceil(y / 2)
            };
        var I = function (a) {
                if ((r.browser.opera && r.browser.version < "10.5") || r.browser.msie6 || r.browser.firefox2 || r.browser.firefox30) {
                    if (typeof (console) != "undefined") {
                        if (typeof (console.error) == "function") {
                            console.error("This web browser is not supported by SlideDeck. Please view this page in a modern, CSS3 capable browser or a current version of Inernet Explorer")
                        }
                    }
                    return false
                }
                if (typeof (a) != "undefined") {
                    for (var b in a) {
                        r.options[b] = a[b]
                    }
                }
                if (r.spines.length < 1) {
                    r.options.hideSpines = true
                }
                if (r.options.hideSpines === true) {
                    r.options.activeCorner = false
                }
                r.current = Math.min(r.slides.length, Math.max(1, r.options.start));
                if (p.height() > 0) {
                    H();
                    D()
                } else {
                    var c;
                    c = setTimeout(function () {
                        H();
                        if (p.height() > 0) {
                            clearInterval(c);
                            H();
                            D()
                        }
                    }, 20)
                }
            };
        var J = function (a) {
                var b;
                b = setInterval(function () {
                    if (r.isLoaded == true) {
                        clearInterval(b);
                        a()
                    }
                }, 20)
            };
        this.loaded = function (a) {
            J(a);
            return r
        };
        this.next = function (a) {
            var b = Math.min(r.slides.length, (r.current + 1));
            if (r.options.cycle === true) {
                if (r.current + 1 > r.slides.length) {
                    b = 1
                }
            }
            F(b, a);
            return r
        };
        this.prev = function (a) {
            var b = Math.max(1, (r.current - 1));
            if (r.options.cycle === true) {
                if (r.current - 1 < 1) {
                    b = r.slides.length
                }
            }
            F(b, a);
            return r
        };
        this.goTo = function (a, b) {
            r.pauseAutoPlay = true;
            F(Math.min(r.slides.length, Math.max(1, a)), b);
            return r
        };
        this.setOption = function (a, b) {
            G(a, b);
            return r
        };
        I(q)
    };
    $.fn.slidedeck = function (a) {
        var b = [];
        for (var i = 0; i < this.length; i++) {
            if (!this[i].slidedeck) {
                this[i].slidedeck = new SlideDeck(this[i], a)
            }
            b.push(this[i].slidedeck)
        }
        return b.length > 1 ? b : b[0]
    }
})(jQuery);
