function $(selector, context) {
    if (typeof selector !== "string") {
        return selector;
    }
    var fn = "sByTagName";
    if ("#" === selector.charAt(0)) {
        fn = "ById";
        selector = selector.slice(1);
    }
    return (context || document)["getElement" + fn](selector);
};
function on(element, evt_name, fn) {
    element = $(element);
    if (typeof element.addEventListener === "function") {
        element.addEventListener(evt_name, fn, false);
    } else if (element.attachEvent) {
        element.attachEvent("on" + evt_name, fn);
    } else {
        throw "could not add event listener.";
    }
};
function viewport_size() {
    if (typeof window.innerWidth !== "undefined") {
        return [window.innerWidth, window.innerHeight];
    }
    var de = document.documentElement || $("body");
    return [de.clientWidth, de.clientHeight];
};
function set_opacity(element, opacity) {
    element = $(element);
    element.style.opacity = opacity;
    element.style.filter = "alpha(opacity=" + Math.ceil(opacity * 100) + ")";
}
function fade(element, from, to, step, interv, cb) {
    var opacity = from, dir = from > to ? -1 : 1;
    set_opacity(element, from);
    (function(){
        opacity += step * dir;
        set_opacity(element, opacity);
        if (dir * (to - opacity) > 0) {
            window.setTimeout(arguments.callee, interv);
        } else if (typeof cb === "function") {
            cb();
        }
    })();
}
function set_size(element, w, h) {
    if (typeof h === "undefined") {
        h = w[1];
        w = w[0];
    }
    element = $(element);
    element.style.width = w + "px";
    element.style.height = h + "px";
}
function remove_curtain() {
    var curtain = $("#curtain"), container = $("#popup_container");
    fade(curtain, .6, 0, .05, 25, function() {
        curtain.parentNode.removeChild(curtain);
    });
    fade(container, 1, 0, .1, 25, function() {
        container.parentNode.removeChild(container);
    });
    return false;
}
var gimmick_songs = [
    ["01", "Fuli Tschai"],
    ["04", "Choros"],
    ["06", "Graúna"],
    ["07", "Star of County Down"],
    ["08", "Oshima Bushi"],
    ["09", "Haave Kaukomaasta"],
    ["12", "Johnny Peut Pas Danser"],
    ["14", "Sherele"],
    ["15", "Granada"]
];
function init_gimmick() {
    window.setTimeout(function(){
        var curtain, container, vs = viewport_size(), opacity = 0
        var song, i, file;

        $("body")[0].innerHTML += "<div id=\"curtain\"></div>" +
            "<div id=\"popup_container\">" +
                "<div id=\"popup\">"+
                "   <img src=\"images/vdf-cover.jpg\" "+
                "       alt=\"Cover Via Della Fortuna\" "+
                "       width=\"350\" height=\"315\"/>" +
                "   <p>das neue Album<br/>" +
                "   <strong style=\"font-size: 12pt\">" +
                "       Via Della Fortuna</strong>" +
                "   </p><p id=\"player_out\"></p><p>"+
                "   erhältlich ab dem 24.06.2009 &mdash; " +
		"   schon jetzt bestellen</p>" +
                "   <a href=\"cds.php#vdf\">[ jetzt Probe hören ]</a> &nbsp; "+
                "   <a href=\"bestellen.php?a=4\">[ gleich bestellen ]</a>" +
                "   </p>" +
                "   <p><a href=\"#\" onclick=\"remove_curtain()\">"+
                "       [ zur Webseite ]"+
                "   </a></p>";
		"   <div id=\"close_btn\" " +
                "       onclick=\"remove_curtain()\">X</div>" +
                "</div>" +
            "</div>";

        i = Math.floor(Math.random() * gimmick_songs.length);
        song = gimmick_songs[i];
        file = "emff.swf?src=downloads/vdf/" + song[0] + ".mp3&autostart=yes";

        $("#player_out").innerHTML += "<object "+
            "   type=\"application/x-shockwave-flash\" "+
            "   data=\"" + file + "\" width=\"110\" height=\"34\">" +
            "<param name=\"movie\" value=\"" + file + "\"/>" +
            "<param name=\"quality\" value=\"high\" />" +
            "<param name=\"wmode\" value=\"window\" />" +
            "Um die H&ouml;rbeispiele abzuspielen ben&ouml;tigen Sie den "+
            "Flash Player, zu bekommen " +
            "<a href=\"http://macromedia.com/go/getflash\">" +
            "   auf dieser Webseite</a>.</object>" +
            "";//"<br/>Sie h&ouml;ren: " + song[1] + ".";

        curtain = $("#curtain");
        container = $("#popup_container");

        set_size(curtain, vs);
        set_size(container, vs);

        on(window, "resize", function(){
            var vs = viewport_size();
            set_size(curtain, vs);
            set_size(container, vs);
        });

        fade("#curtain", 0, .6, .015, 25);
        fade("#popup", 0, 1, .03, 25);
    }, 0);
};
