/*
 * Copyright (C) 2008 Bao-Long NGUYEN-TRONG
 *
 * 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

function addParam(element, name, value) {
    var param = document.createElement('param');
    param.name = name;
    param.value = value;
    element.appendChild(param);
}//addParam

function playVideo(posterId, playerId, videoUrl) {
    var poster = document.getElementById(posterId);
    poster.style.display = 'none';

    var player = document.getElementById(playerId);
    player.className = 'QuickSmug_player';
    player.style.width = poster.style.width;
    player.style.height = poster.style.height;

    //IE is not found of this JS
    //It fails on the EMBED tag and does not start QT properly without
    /*
    var quicktime = document.createElement('object');
    quicktime.setAttribute('classid', 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B');
    quicktime.setAttribute('codebase', 'http://www.apple.com/qtactivex/qtplugin.cab');
    quicktime.width = poster.style.width;
    quicktime.height = poster.style.height;
    addParam(quicktime, 'src', videoUrl);
    //No controls on IE as they do not appear (IE bug)
    addParam(quicktime, 'controller', 'false');
    addParam(quicktime, 'autoplay', 'true');
    addParam(quicktime, 'bgcolor', 'black');
    addParam(quicktime, 'scale', 'aspect');

    var embed = document.createElement('embed');
    embed.setAttribute('src', videoUrl);
    embed.setAttribute('plunginspage', 'http://www.apple.com/quicktime/download/');
    embed.setAttribute('controller', 'true');
    embed.setAttribute('autoplay', 'true');
    embed.setAttribute('bgcolor', 'black');
    embed.setAttribute('scale', 'aspect');
    embed.setAttribute('width', poster.style.width);
    embed.setAttribute('height', poster.style.height);

    quicktime.appendChild(embed);
    player.appendChild(quicktime);
    */
    player.innerHTML =
        '<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b"'
        + ' width="' + poster.style.width
        + '" height="' + poster.style.height
        + '" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'
        + '<param name="src" value="' + videoUrl
        //No controls on IE as they do not appear (IE bug)
        + '" /><param name="controller" value="false" />'
        + '<param name="autoplay" value="true" />'
        + '<param name="bgcolor" value="black" />'
        + '<param name="scale" value="aspect" />'
        + '<embed src="' + videoUrl
        + '" width="' + poster.style.width
        + '" height="' + poster.style.height
        + '" autoplay="true" scale="aspect" controller="true" pluginspage="http://www.apple.com/quicktime/download/">'
        + '</embed>';
}//playVideo

function QuickSmug_Write(videoIdKey, posterIdKey, size) {
    if (!posterIdKey) {
        posterIdKey = videoIdKey;
    }

    var width;
    var height;
    if (!size) {
        size = 'S';
    }
    switch (size) {
        case 'S':
        case 's':
            width   = '320';
            height  = '250';
            break;
        case 'M':
        case 'm':
            width   = '640';
            height  = '490';
            break;
    }//switch

    var posterUrl = 'http://www.smugmug.com/photos/' + posterIdKey + '-' + size.toUpperCase() + '.jpg';
    var videoUrl = 'http://www.smugmug.com/photos/' + videoIdKey + '-' + width + '.mp4';

    var random = Math.random();
    var posterId = 'QuickSmug_Poster_' + random;
    var playerId = 'QuickSmug_Player_' + random;
    document.writeln('<div id="'+posterId+'"></div>');
    document.writeln('<div id="'+playerId+'"></div>');

    var poster = document.getElementById(posterId);
    poster.className = 'QuickSmug_poster';
    poster.style.backgroundImage = 'url(' + posterUrl + ')';
    poster.style.width = width + 'px';
    poster.style.height = height + 'px';
    var playButton = document.createElement('a');
    playButton.className = 'QuickSmug_playButton';
    playButton.onclick = function() {
        playVideo(posterId, playerId, videoUrl);
    }
    playButton.appendChild(document.createTextNode('Click to Play'));
    poster.appendChild(playButton);
}//QuickSmug_Write
