/*
 * Smoothbox v20080623 by Boris Popoff (http://gueschla.com)
 * To be used with mootools 1.2
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

/*
 * Modified for the Earn Your Stars website by DPAC, 2008
 *
 * Please note, the script will no longer work for other purposes
 */

window.addEvent("domready", TB_init);
TB_WIDTH = 0;
TB_HEIGHT = 0;
var TB_doneOnce = 0;
var top;
function TB_init() {
    var chromelessBoxArray= $$("a.chromelessBox");

    var boxArray= $$("a.box");
    chromelessBoxArray.combine(boxArray);
    chromelessBoxArray.each(function (A) {
        A.onclick = TB_bind;
        var originalLink = A.href;
        var newLink = '';
        //alert(A.className)
        if(A.className.substring(0,10)=='chromeless'){
           newLink = originalLink+'?SQ_DESIGN_NAME=chromeless&?keepThis=true&TB_iframe=true&width=600&height=400';
        }else{
           newLink = originalLink+'?keepThis=true&TB_iframe=true&width=600&height=400';
        }
        
        A.setProperty("href", newLink);
    })
}
function TB_bind(B) {
    var B = new Event(B);
    B.preventDefault();
    this.blur();
    var A = this.title || this.name || "";
    var C = this.rel || false;
    TB_show(A, this.href, C);
    this.onclick = TB_bind;
    return false
}
function TB_show(P, E, B) {
    top = window.getHeight() < 725 ? Math.min(window.getScrollTop() + 5, window.getScrollHeight() - 705) : (window.getScrollTop() + (window.getHeight() - (TB_HEIGHT || 690)) / 2);
    if (!$("TB_overlay")) {
        new Element("iframe").setProperty("id", "TB_HideSelect").injectInside(document.body);
        $("TB_HideSelect").setOpacity(0);
        new Element("div").setProperty("id", "TB_overlay").injectInside(document.body);
        $("TB_overlay").setOpacity(0);
        TB_overlaySize();
        new Element("div").setProperty("id", "TB_load").injectInside(document.body);
        $("TB_load").innerHTML = "<img src='./?a=56128' />";
        TB_load_position();
        $$("embed", "object", "select").each(function (U) {
            U.style.visibility = "hidden"
        });
        $("TB_overlay").set("tween", {
            duration: 400
        });
        $("TB_overlay").tween("opacity", 0, 0.6)
    }
    if (!$("TB_Loading")) {
        new Element("div").setProperty("id", "TB_load").injectInside(document.body);
        $("TB_load").innerHTML = "<img src='http://www.earnyourstars.tas.gov.au/__data/assets/image/0004/56128/loading.gif'/>";
        TB_load_position()
    }
    if (!$("TB_window")) {
        new Element("div").setProperty("id", "TB_window").injectInside(document.body);
        $("TB_window").setOpacity(0)
    }
    $("TB_overlay").onclick = TB_remove;
    var Q = E.match(/(.+)?/)[1] || E;
    var N = /\.(jpe?g|png|gif|bmp)/gi;
    if (Q.match(N)) {
        var T = {
            caption: "",
            url: "",
            html: ""
        };
        var K = T,
        L = T,
        A = "";
        if (B) {
            function I(V, W, U) {
                return {
                    caption: V.title,
                    url: V.href,
                    html: "<span id='TB_" + W + "'>  <a href='#'>" + U + "</a></span>"
                }
            }
            var C = [];
            $$("a.smoothbox").each(function (U) {
                if (U.rel == B) {
                    C[C.length] = U
                }
            });
            var S = false;
            for (var O = 0; O < C.length; O++) {
                var J = C[O];
                var G = J.href.match(N);
                if (J.href == E) {
                    S = true;
                    A = "Image " + (O + 1) + " of " + (C.length)
                } else {
                    if (S) {
                        L = I(J, "next", "Next >");
                        break
                    } else {
                        K = I(J, "prev", "< Prev")
                    }
                }
            }
        }
        imgPreloader = new Image();
        imgPreloader.onload = function () {
            imgPreloader.onload = null;
            var W = window.getWidth() - 150;
            var a = window.getHeight() - 150;
            var X = imgPreloader.width;
            var V = imgPreloader.height;
            if (X > W) {
                V = V * (W / X);
                X = W;
                if (V > a) {
                    X = X * (a / V);
                    V = a
                }
            } else {
                if (V > a) {
                    X = X * (a / V);
                    V = a;
                    if (X > W) {
                        V = V * (W / X);
                        X = W
                    }
                }
            }
            TB_WIDTH = X + 30;
            TB_HEIGHT = V + 60;
            $("TB_window").innerHTML += "<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='" + E + "' width='" + X + "' height='" + V + "' alt='" + P + "'/></a><div id='TB_caption'>" + P + "<div id='TB_secondLine'>" + A + K.html + L.html + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div>";
            $("TB_closeWindowButton").onclick = TB_remove;
            function U(b) {
                return function () {
                    $("TB_window").dispose();
                    new Element("div").setProperty("id", "TB_window").injectInside(document.body);
                    TB_show(b.caption, b.url, B);
                    return false
                }
            }
            var Z = U(K);
            var Y = U(L);
            if ($("TB_prev")) {
                $("TB_prev").onclick = Z
            }
            if ($("TB_next")) {
                $("TB_next").onclick = Y
            }
            document.onkeydown = function (b) {
                var b = new Event(b);
                switch (b.code) {
                case 27:
                    TB_remove();
                    break;
                case 190:
                    if ($("TB_next")) {
                        document.onkeydown = null;
                        Y()
                    }
                    break;
                case 188:
                    if ($("TB_prev")) {
                        document.onkeydown = null;
                        Z()
                    }
                    break
                }
            };
            $("TB_ImageOff").onclick = TB_remove;
            TB_position();
            TB_showWindow()
        };
        imgPreloader.src = E
    } else {
        var D = E.match(/\?(.+)/);
        if (D) {
            D = D[1]
        }
        var R = TB_parseQuery(D);
        TB_WIDTH = R.width ? (R.width * 1) + 30 : 350;
        TB_HEIGHT = R.height ? (R.height * 1) + 40 : 300;
        var H = TB_WIDTH - 30,
        M = TB_HEIGHT - 45;
        if (E.indexOf("TB_iframe") != -1) {
            urlNoQuery = E.split("TB_");
            $("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + P + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (H + 29) + "px;height:" + (M + 17) + "px;visibility:hidden' onload='TB_showWindow()'> </iframe>"
        } else {
            $("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + P + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:" + H + "px;height:" + M + "px;'></div>"
        }
        $("TB_closeWindowButton").onclick = TB_remove;
        if (E.indexOf("TB_inline") != -1) {
            $("TB_ajaxContent").innerHTML = ($(R.inlineId).innerHTML);
            TB_position();
            TB_showWindow()
        } else {
            if (E.indexOf("TB_iframe") != -1) {
                TB_position();
                if (frames.TB_iframeContent == undefined) {
                    $(document).keyup(function (V) {
                        var U = V.keyCode;
                        if (U == 27) {
                            TB_remove()
                        }
                    });
                    TB_showWindow()
                }
            } else {
                var F = function () {
                    TB_position();
                    TB_showWindow()
                };
                new Request.HTML({
                    method: "get",
                    update: $("TB_ajaxContent"),
                    onComplete: F
                }).get(E)
            }
        }
    }
    window.onresize = function () {
        TB_position();
        TB_load_position();
        TB_overlaySize()
    };
    document.onkeyup = function (U) {
        var U = new Event(U);
        if (U.code == 27) {
            TB_remove()
        }
    }
}
function TB_showWindow() {
    if (TB_doneOnce == 0) {
        TB_doneOnce = 1;
        $("TB_window").set("tween", {
            duration: 250,
            onComplete: function () {
                if ($("TB_load")) {
                    $("TB_load").dispose()
                }
                $("TB_iframeContent").setStyle("visibility", "visible")
            }
        });
        $("TB_window").tween("opacity", 0, 1)
    } else {
        $("TB_window").setStyle("opacity", 1);
        if ($("TB_load")) {
            $("TB_load").dispose()
        }
    }
}
function TB_remove() {
    $("TB_overlay").onclick = null;
    document.onkeyup = null;
    document.onkeydown = null;
    if ($("TB_imageOff")) {
        $("TB_imageOff").onclick = null
    }
    if ($("TB_closeWindowButton")) {
        $("TB_closeWindowButton").onclick = null
    }
    if ($("TB_prev")) {
        $("TB_prev").onclick = null
    }
    if ($("TB_next")) {
        $("TB_next").onclick = null
    }
    $("TB_window").set("tween", {
        duration: 250,
        onComplete: function () {
            $("TB_window").dispose();
            $$("embed", "object", "select").each(function (A) {
                A.style.visibility = "visible"
            })
        }
    });
    $("TB_window").tween("opacity", 1, 0);
    $("TB_overlay").set("tween", {
        duration: 400,
        onComplete: function () {
            $("TB_overlay").dispose()
        }
    });
    $("TB_overlay").tween("opacity", 0.6, 0);
    window.onscroll = null;
    window.onresize = null;
    $("TB_HideSelect").dispose();
    //TB_init();
    TB_doneOnce = 0;
    return false
}
function TB_position() {
    $("TB_window").set("morph", {
        duration: 75
    });
    $("TB_window").setStyles({
        width: TB_WIDTH + "px",
        left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH) / 2) + "px",
        top: top + "px"
    })
}
function TB_overlaySize() {
    $("TB_overlay").setStyles({
        height: "0px",
        width: "0px"
    });
    $("TB_HideSelect").setStyles({
        height: "0px",
        width: "0px"
    });
    $("TB_overlay").setStyles({
        height: window.getScrollHeight() + "px",
        width: window.getScrollWidth() + "px"
    });
    $("TB_HideSelect").setStyles({
        height: window.getScrollHeight() + "px",
        width: window.getScrollWidth() + "px"
    })
}
function TB_load_position() {
    if ($("TB_load")) {
        $("TB_load").setStyles({
            left: (window.getScrollLeft() + (window.getWidth() - 56) / 2) + "px",
            top: (window.getScrollTop() + ((window.getHeight() - 20) / 2)) + "px",
            display: "block"
        })
    }
}
function TB_parseQuery(C) {
    if (!C) {
        return {}
    }
    var E = {};
    var B = C.split(/[;&]/);
    for (var A = 0; A < B.length; A++) {
        var D = B[A].split("=");
        if (!D || D.length != 2) {
            continue
        }
        E[unescape(D[0])] = unescape(D[1]).replace(/\+/g, " ")
    }
    return E
};