/*
 *	Silk - A Developer's Toolbox
 *	
 *	(c) 2007 - 2008 adamnfish
 *	http://silk.adamnfish.com
 *	
 *	MIT-style licence (http://silk.adamnfish.com/licence)
 *	
 *	Thanks to the MooTools devs, http://mootools.net
 */
var Silk = function() {};
(function() {
    this.version = 0.02;
    this.namespace = "Silk";
    if (!C) {
        var C = new Hash({
            image: function(L, K) {
                K = $merge({
                    onload: $empty,
                    onabort: $empty,
                    onerror: $empty
                },
                K);
                var M = new Image();
                var J = $(M) || new Element("img");
                ["load", "abort", "error"].each(function(N) {
                    var O = "on" + N;
                    var P = K[O];
                    delete K[O];
                    M[O] = function() {
                        if (!M) {
                            return
                        }
                        if (!J.parentNode) {
                            J.width = M.width;
                            J.height = M.height
                        }
                        M = M.onload = M.onabort = M.onerror = null;
                        P.delay(1, J, J);
                        J.fireEvent(N, J, 1)
                    }
                });
                M.src = J.src = L;
                if (M && M.complete) {
                    M.onload.delay(1)
                }
                return J.setProperties(K)
            },
            images: function(M, L) {
                L = $merge({
                    onComplete: $empty,
                    onProgress: $empty
                },
                L);
                if (!M.push) {
                    M = [M]
                }
                var J = [];
                var K = 0;
                M.each(function(O) {
                    var N = new C.image(O, {
                        onload: function() {
                            L.onProgress.call(this, K, M.indexOf(O));
                            K++;
                            if (K == M.length) {
                                L.onComplete()
                            }
                        }
                    });
                    J.push(N)
                });
                return new Elements(J)
            }
        })
    }
    var A = /^\.|^#|^.+\s.+$/;
    var E = function(K, J) {
        K.each(function(L) {
            $splat(J.options[L]).each(function(O) {
                var N = [];
                switch ($type(O)) {
                case "string":
                    var M = O.match(/.*\[([0-9]+)\]$/);
                    if (M) {
                        M[1].split(",").each(function(P) {
                            N.push(P.trim())
                        })
                    }
                    O = $(O) || O;
                case "array":
                    O = $$(O);
                case "element":
                    O.addEvent("click", function(P) {
                        P.preventDefault();
                        J[L](N);
                        this.blur()
                    })
                }
            })
        })
    };
    Number.implement({
        mod: function(K) {
            var J = this % K;
            return J < 0 ? J + K: J
        }
    });
    Array.implement({
        shuffle: function() {
            for (var L = 1, K = this.length; L < K; L++) {
                var J = $random(0, K - L);
                this.push(this[J]);
                this.splice(J, 1)
            }
            return this
        },
        permute: function(J) {
            for (var L = 0, K = this.length; L < K; L++) {
                this[(L + J).mod(K) + K] = this[L]
            }
            this.splice(0, K);
            return this
        }
    });
    this.installed = function(J) {
        return this.modules.some(function(K) {
            return J.toLowerCase() == K.toLowerCase()
        })
    };
    this.modules = ["slideshow", "font", "player", "scroll", "sideshow"];
    this.Slideshow = new Class({
        Implements: [Events, Options],
        options: {
            displayDuration: 10,
            transitionDuration: 2000,
            nextChoice: "randomOrder",
            onTransitionStart: $empty,
            onTransitionEnd: $empty
        },
        initialize: function(K, L, J) {
            this.setOptions(J);
            this.current = $(K);
            this.stopped = true;
            E(["stop", "start"], this);
            if ((this.z = this.current.getStyle("z-index")) == "auto") {
                this.current.setStyle("z-index", this.z = 2)
            }
            this.imgs = this.options.nextChoice == "randomOrder" ? L.shuffle() : L;
            this.count = 0;
            this.styles = this.current.style.cssText;
            this.attrs = this.current.getProperties("id", "class", "alt", "title", "width", "height");
            this.start()
        },
        start: function() {
            this.stopped = false;
            this.timer = this.change.bind(this).delay(this.options.displayDuration * 1000);
            this.loaded = this.waiting = 0;
            var J = this.options.nextChoice == "random" ? $random(0, this.imgs.length) : this.count;
            if (this.imgs[J % this.imgs.length] == this.current.get("src")) {
                J++;
                this.count++
            }
            this.next = new C.image(this.imgs[J % this.imgs.length], {
                onload: function() {
                    this.next.setProperties(this.attrs);
                    this.next.style.cssText = this.styles;
                    this.loaded = 1;
                    if (this.waiting) {
                        this.change()
                    }
                }.bind(this)
            });
            this.count += 1;
            return this
        },
        stop: function() {
            this.stopped = true;
            this.timer = $clear(this.timer);
            return this
        },
        change: function() {
            if (this.loaded) {
                this.current.set("tween", {
                    duration: this.options.transitionDuration,
                    onComplete: function() {
                        this.next.setStyle("z-index", this.z);
                        this.current.destroy();
                        this.current = this.next;
                        this.fireEvent("onTransitionEnd", 10);
                        if (!this.stopped) {
                            this.start()
                        }
                    }.bind(this)
                });
                this.next.setStyle("z-index", this.z - 1);
                this.next.injectAfter(this.current);
                this.current.tween("opacity", 0);
                this.fireEvent("onTransitionStart", 10)
            } else {
                this.waiting = 1
            }
            return this
        }
    });
    var I = 0;
    this.Font = new Class({
        Implements: [Options],
        options: {
            useCookie: true,
            start: 1,
            fx: {
                duration: 200,
                link: "cancel"
            },
            cookie: {
                path: "/",
                duration: 365
            },
            namespace: ""
        },
        initialize: function(K, L, J) {
            this.setOptions(J);
            this.els = $$($(K) || K);
            this.sizes = L.sort(function(O, N) {
                return O - N
            });
            this.current = this.options.start;
            if (this.options.useCookie) {
                this.instance = I++;
                this.cookie = new Cookie("Silk.Font." + this.options.namespace + this.instance, this.options.cookie);
                var M = this.cookie.read();
                if (M) {
                    this.current = M.toInt()
                }
            }
            this.els.each(function(N) {
                N.store("Silk.Font.Fx", new Fx.Tween(N, this.options.fx).set("fontSize", this.sizes[this.current]))
            },
            this);
            E(["increase", "decrease", "max", "min", "set"], this)
        },
        change: function(J) {
            this.current = (this.current + J).limit(0, this.sizes.length - 1);
            this.els.each(function(K) {
                K.retrieve("Silk.Font.Fx").start("fontSize", this.sizes[this.current])
            },
            this);
            if (this.options.useCookie) {
                this.cookie.write(this.current)
            }
        },
        increase: function() {
            this.change(1);
            return this
        },
        decrease: function() {
            this.change( - 1);
            return this
        },
        max: function() {
            this.change(this.sizes.length - this.current - 1);
            return this
        },
        min: function() {
            this.change( - this.current);
            return this
        },
        set: function(J) {
            this.change(J - this.current);
            return this
        }
    });
    var G = ["onFlowPlayerReady", "onClipDone", "onClipChanged", "onLoadBegin", "onStreamNotFound", "onPlay", "onStop", "onPause", "onResume", "onCuePoint", "onStartBuffering", "onBufferFull", "onBufferFlush", "onMetaData"];
    var B = {};
    var D = 1;
    G.each(function(J) {
        B[J] = [];
        window[J] = function() {
            var K = arguments;
            B[J].each(function(L) {
                L.run(K)
            })
        }
    });
    var H = this.addCallback = function(L, J, K) {
        B[L].push(J)
    };
    var F = this.addCallbacks = function(J) {
        for (callback in J) {
            B[callback].push(J[callback])
        }
    };
    this.Player = new Class({
        Implements: [Options, Events],
        flashVars: {
            type: "application/x-shockwave-flash",
            pluginspage: "http://www.adobe.com/go/getflashplayer",
            quality: "high",
            allowscriptaccess: "always",
            allowfullscreen: false,
            width: 320,
            height: 240,
            bgcolor: "#000000"
        },
        options: {
            playerVars: {
                autoPlay: false,
                loop: false,
                autoRewind: true,
                controlsOverVideo: "ease",
                initialScale: "scale",
                autoBuffering: true,
                useNativeFullScreen: true,
                usePlayOverlay: true,
                showMenu: false,
                showPlayButton: true,
                controlBarGloss: "low",
                showFullScreenButton: false,
                menuItems: "[1, 1, 1, 1, 0, 0, 0]"
            },
            thumb: false,
            onFail: $empty,
            onFlowPlayerReady: $empty,
            onClipDone: $empty,
            onClipChanged: $empty,
            onLoadBegin: $empty,
            onStreamNotFound: $empty,
            onPlay: $empty,
            onStop: $empty,
            onPause: $empty,
            onResume: $empty,
            onCuePoint: $empty,
            onBufferFull: $empty,
            onBufferFlush: $empty,
            onMetaData: $empty,
            onStartPlaying: $empty,
            onStopPlaying: $empty
        },
        initialize: function(K, O, N, L) {
            this.container = $(K);
            this.current = 0;
            this.ready = false;
            this.playing = false;
            this.stack = [];
            this.playlist = [];
            O = $type(O) == "string" ? {
                src: O
            }: O;
            this.flashVars = $merge(this.flashVars, O);
            this.flashVars.onFail = this.options.onFail;
            this.setOptions(L);
            this.playerVars = this.options.playerVars;
            this.playerId = D++;
            E(["play", "pause", "toggle", "forward", "backwards", "stop", "prev", "next"], this);
            this.thumb = this.options.thumb || this.playerVars.splashImageFile;
            if (this.playerVars.videoFile) {
                this.playlist.push({
                    url: this.playerVars.videoFile
                });
                if (this.playerVars.splashFileImage) {
                    this.playlist[0].thumb = this.playerVars.splashFileImage
                }
            }
            if (N) {
                this.makePlaylist(N)
            }
            delete this.playerVars.splashImageFile;
            delete this.playerVars.videoFile;
            var M = (function(P) {
                if (P.playerId == this.playerId) {
                    this.playing = true;
                    if (this.ready && this.api.getIsPlaying()) {
                        this.fireEvent("startPlaying", P)
                    }
                }
            }).bind(this);
            var J = (function(P) {
                if (P.playerId == this.playerId) {
                    this.playing = false;
                    this.fireEvent("stopPlaying", P)
                }
            }).bind(this);
            F({
                onFlowPlayerReady: function() { (function() {
                        if (this.api.getVersion) {
                            this.fireEvent("flowPlayerReady");
                            this.fireEvent("stopPlaying");
                            if (this.stack) {
                                this.stack.each(function(P) {
                                    P()
                                });
                                this.stack = []
                            }
                            this.api.setPlayerId(this.playerId);
                            this.ready = true
                        }
                    }).delay(300, this)
                }.bind(this),
                onPlay: M,
                onResume: M,
                onClipDone: J,
                onClipChanged: J,
                onPause: J
            });
            G.each(function(P) {
                if (P != "onFlowPlayerReady") {
                    H(P, function(Q) {
                        if (Q.playerId == this.playerId) {
                            this.fireEvent(Events.removeOn(P), Q)
                        }
                    }.bind(this))
                }
            },
            this);
            this.injectVideo(0)
        },
        injectVideo: function(L) {
            this.ready = false;
            this.playing = false;
            var M = [];
            var K = this.playlist[L].url;
            var J = this.playlist[L].thumb || this.thumb;
            if (J && this.playerVars.usePlayOverlay) {
                M.push({
                    url: J,
                    overlayId: "play"
                })
            } else {
                if (J) {
                    M.push({
                        url: J
                    })
                }
            }
            M.push({
                url: K
            });
            this.api = this.embed(this.container, this.flashVars, {
                config: $merge(this.playerVars, {
                    playList: M
                })
            });
            this.current = L
        },
        embed: function(J, L, K) {
            return flashembed(J, L, K)
        },
        makePlaylist: function(M) {
            switch ($type(M)) {
            case "object":
                this.playlist = [M];
                break;
            case "array":
                if (M.setStyle) {
                    M.each(this.addLinks, this)
                } else {
                    this.playlist = M
                }
                break;
            case "string":
                var J = $$(M);
                if (J.length) {
                    J.each(function(O, N) {
                        this.addLink(O, N)
                    }.bind(this));
                    break
                } else {
                    if (!$(M)) {
                        if (M != this.playerVars.videoFile) {
                            this.playlist.push({
                                url: M
                            })
                        }
                        break
                    }
                }
            case "element":
                var L = $(M);
                if (L.get("tag") == "ul") {
                    var K = L.getElements("li");
                    K.each(function(N, P) {
                        var O = N.getElement("a.play") || N.getElement("a");
                        if (O) {
                            this.addLink(O, P)
                        }
                    }.bind(this));
                    break
                } else {
                    L.getElements("a").each(this.addLink, this)
                }
            default:
                return false
            }
        },
        addLink: function(L, K) {
            this.playlist.push({
                url: L.href
            });
            if (this.thumb) {
                this.playlist[this.playlist.length - 1].thumb = this.thumb
            }
            var J = this.playlist.length - 1;
            $(L).addEvent("click", function(M) {
                M.preventDefault();
                this.play(J);
                $(L).blur()
            }.bind(this))
        },
        remote: function(K, J) {
            if (this.ready) {
                return this.api[K](J) || this
            } else {
                this.stack.push(this.remote.pass([K, J], this));
                return false
            }
        },
        play: function(J) {
            if ($chk(J) && J != this.current) {
                this.injectVideo(J);
                this.stack.push(this.play.bind(this))
            } else {
                this.remote("DoPlay")
            }
            return this
        },
        pause: function() {
            this.remote("Pause");
            return this
        },
        toggle: function() {
            if (this.ready) {
                if (this.playing) {
                    this.pause()
                } else {
                    this.play()
                }
            } else {
                this.stack.push(function() {
                    this.toggle()
                }.bind(this))
            }
            return this
        },
        seek: function(K) {
            var J = this.remote("getDuration");
            return $type(J) == "number" ? this.remote("Seek", K.limit(0, J.floor())) : false
        },
        move: function(L, J) {
            L = L || 5;
            var K = this.remote("getTime");
            return $chk(K) ? this.seek(K.toFloat() + (L * J)) : false
        },
        forward: function(J) {
            this.move(J, 1);
            return this
        },
        backwards: function(J) {
            this.move(J, -1);
            return this
        },
        stop: function() {
            this.remote("DoStop");
            return this
        },
        next: function() {
            this.fireEvent("stopPlaying");
            this.play((this.current + 1).mod(this.playlist.length));
            return this
        },
        prev: function() {
            this.fireEvent("stopPlaying");
            this.play((this.current - 1).mod(this.playlist.length));
            return this
        }
    });
    this.Scroll = new Class({
        Extends: Fx.Scroll,
        initialize: function(K, L) {
            L = L || document;
            this.doc = L.getDocument(),
            win = L.getWindow();
            this.parent(this.doc, K);
            this.links = this.options.links ? $$(this.options.links) : $$(this.doc.links);
            var J = win.location.href.match(/^[^#]*/)[0] + "#";
            this.links.each(function(N) {
                if (N.href.indexOf(J) != 0) {
                    return
                }
                var M = N.href.substr(J.length);
                if (M && this.doc.getElement("a[name=" + M + "]")) {
                    this.useLink(N, M)
                }
            },
            this)
        },
        useLink: function(K, J) {
            K.addEvent("click", function(L) {
                L.preventDefault();
                this.anchor = J;
                var M = this.doc.getElement("a[name=" + J + "]");
                this.toElement(M);
                M.id += "_";
                window.setTimeout(function() {
                    M.id = J
                },
                0);
                K.blur()
            }.bind(this))
        }
    });
    if (!Fx.Elements) {
        Fx.Elements = new Class({
            Extends: Fx.CSS,
            initialize: function(K, J) {
                this.elements = this.subject = $$(K);
                this.parent(J)
            },
            compute: function(P, Q, R) {
                var L = {};
                for (var M in P) {
                    var J = P[M],
                    N = Q[M],
                    O = L[M] = {};
                    for (var K in J) {
                        O[K] = this.parent(J[K], N[K], R)
                    }
                }
                return L
            },
            set: function(K) {
                for (var L in K) {
                    var J = K[L];
                    for (var M in J) {
                        this.render(this.elements[L], M, J[M], this.options.unit)
                    }
                }
                return this
            },
            start: function(L) {
                if (!this.check(arguments.callee, L)) {
                    return this
                }
                var Q = {},
                R = {};
                for (var M in L) {
                    var O = L[M],
                    J = Q[M] = {},
                    P = R[M] = {};
                    for (var K in O) {
                        var N = this.prepare(this.elements[M], K, O[K]);
                        J[K] = N.from;
                        P[K] = N.to
                    }
                }
                return this.parent(Q, R)
            }
        })
    }
    this.Sideshow = new Class({
        Implements: [Options, Events],
        options: {
            speed: 5,
            height: 134,
            loadingSrc: "",
            loadingHTML: "Loading<br/>",
            clickLocation: false,
            onPause: $empty,
            onPlay: $empty
        },
        initialize: function(K, L, J) {
            this.setOptions(J);
            this.el = $(K).setStyles({
                overflow: "hidden",
                height: this.options.height
            }).empty();
            if (this.el.getStyle("position") == "static") {
                this.el.setStyle("position", "relative")
            }
            this.container = new Element("div", {
                styles: {
                    textAlign: "center"
                }
            }).inject(this.el).set("html", this.options.loadingHTML);
            if (this.options.clickLocation) {
                this.container.addEvent("click", function() {
                    document.location = this.options.clickLocation
                }.bind(this));
                this.container.setStyle("cursor", "pointer")
            }
            if (this.options.loadingSrc) {
                this.loading = new Element("img", {
                    src: this.options.loadingSrc,
                    alt: "loading",
                    styles: {
                        margin: "10px auto"
                    }
                }).injectInside(this.container)
            }
            this.paths = L.shuffle();
            this.widths = [];
            this.totalWidth = 0;
            this.pictureSet = new Element("div", {
                styles: {
                    position: "absolute",
                    top: "0px",
                    left: "0px",
                    height: this.options.height
                }
            });
            this.imgs = new C.images(this.paths, {
                onComplete: function() {
                    this.imgs.each(function(M, N) {
                        this.widths[N] = Math.floor(this.imgs[N].width * (this.options.height / this.imgs[N].height));
                        this.imgs[N].setStyles({
                            position: "absolute",
                            top: "0px",
                            left: this.totalWidth,
                            height: this.options.height,
                            width: this.widths[N],
                            msInterpolationMode: "bicubic"
                        });
                        this.totalWidth += this.widths[N]
                    },
                    this);
                    $$(this.imgs).injectInside(this.pictureSet);
                    this.pictureSet.setStyle("width", this.totalWidth);
                    this.slides = [this.pictureSet.clone().injectInside(this.container), this.pictureSet.clone().setStyle("left", this.totalWidth).injectInside(this.container)];
                    this.duration = 15 * this.totalWidth * (10 / this.options.speed);
                    this.effect = new Fx.Elements($$(this.slides), {
                        duration: this.duration,
                        transition: Fx.Transitions.linear,
                        onComplete: function() {
                            this.start()
                        }.bind(this)
                    });
                    this.pauseEffect = {};
                    this.start()
                }.bind(this)
            });
            E(["pause"], this)
        },
        start: function() {
            this.effect.start({
                "0": {
                    left: [0, -this.totalWidth]
                },
                "1": {
                    left: [this.totalWidth, 0]
                }
            });
            this.fireEvent("play");
            return this
        },
        pause: function() {
            if (this.effect.timer) {
                this.effect.cancel();
                this.fireEvent("pause")
            } else {
                if (this.pauseEffect.timer) {
                    this.pauseEffect.cancel();
                    this.fireEvent("pause")
                } else {
                    this.restart()
                }
            }
            return this
        },
        restart: function() {
            var J = this.slides[1].getStyle("left").toInt();
            var K = this.duration * (J / this.totalWidth);
            this.pauseEffect = new Fx.Elements($$(this.slides), {
                duration: K,
                transition: Fx.Transitions.linear,
                onComplete: function() {
                    this.start()
                }.bind(this)
            }).start({
                "0": {
                    left: [J - this.totalWidth, -this.totalWidth]
                },
                "1": {
                    left: [J, 0]
                }
            });
            this.fireEvent("play");
            return this
        }
    })
}).apply(Silk);