﻿/*
 * This file is part of Argonaute.
 *
 * Copyright (C) 2006 Xavier Lepaul <gaerun@gmail.com>
 *
 * Argonaute 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.
 *
 * Argonaute 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 Argonaute; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * Create and display a covering <div>
 */
function createTopDiv() {
    var onTopDiv = document.createElement("div");
    onTopDiv.id = "ontop";
    $$("body")[0].appendChild(onTopDiv);
    return onTopDiv;
}

function loadSlideShow(event) {
    createTopDiv();
    var bigImg = document.createElement("div");
    bigImg.id = "bigImg";
    bigImg.className = "bigImg";
    $$("body")[0].appendChild(bigImg);
    Event.stop(event);
}

/**
 * displays a player
 * It also remove the existing players
 */
function showPlayer(event,playerType,fileUrl) {
    createTopDiv();

    // create the player div
    playerDiv = document.createElement("div");
    playerDiv.id = "player";
    
    // create the close button
    var closeLink = document.createElement("span");
    closeLink.id = "closebutton";
    Event.observe(closeLink,"click",function() {
        $("ontop").remove();
        // remove the object itself before removing the containing div
        // to deal with an IE limitation
        $$("object")[0].remove();
        $("player").remove();
    });
    var closeButton = document.createTextNode("X");
    closeLink.appendChild(closeButton);
    playerDiv.appendChild(closeLink);
    
    // insert the player
    $$("body")[0].appendChild(playerDiv);
    var aj = new Ajax.Updater("player",".",{
        method: "get",
        parameters: {ajaxplayer: playerType, file: fileUrl},
        insertion: Insertion.Bottom
    });

    Event.stop(event);
}

/**
 * displays the flash mp3 player
 */
function showMp3Player(event) {
    showPlayer(event,"mp3",this.href);
}

/**
 * displays the flash flv player
 */
function showFlvPlayer(event) {
    showPlayer(event,"flv",this.href);
}

/**
 * displays the flash multiple mp3 player
 */
function showMp3MultiPlayer(event) {
    showPlayer(event,"mp3_multi",null);
}

/**
 * displays the flash multiple mp3 player
 */
function showFlvMultiPlayer(event) {
    showPlayer(event,"flv_multi",null);
}

/**
 * decorates the flv and mp3 play-links
 */
Event.observe(window,"load",function() {
    $$(".playmp3").each(function(playLink) {
        Event.observe(playLink,"click",showMp3Player.bindAsEventListener(playLink));
    });
    $$(".playflv").each(function(playLink) {
        Event.observe(playLink,"click",showFlvPlayer.bindAsEventListener(playLink));
    });
    if($("slideshowlink")) {
        Event.observe($("slideshowlink"),"click",loadSlideShow);
    }
    if($("allmp3link")) {
        Event.observe($("allmp3link"),"click",showMp3MultiPlayer.bindAsEventListener($("allmp3link")));
    }
    if($("allflvlink")) {
        Event.observe($("allflvlink"),"click",showFlvMultiPlayer.bindAsEventListener($("allflvlink")));
    }
});

