//<script language="JavaScript">
//<!--

/**
 * JavaScript functions for MP3 storage calculations
 *
 * Copyright (c) 2008 Silicon Bay Limited
 * http://www.siliconbay.co.uk/
 *
 * @author      Richard Farrar
 * @version     1.0.0
 */

// Define average album/song length and artwork size
var SECONDS_PER_ALBUM    = 2700;  // in seconds = 45 mins
var SECONDS_PER_SONG     = 210;   // in seconds = 3:30 mins
var ALBUM_COVER_STORAGE  = 51200; // in bytes = 50kB in bytes


// MP3 song space calculation
function CalcSongs()
{
    // Get key variables from HTML form
    intMemory  = document.getElementById("memory").value;
    intBitrate = document.getElementById("bitrate").value;

    // Calculate total time available in seconds for given memory and bitrate
    intTotalTime = (intMemory * 8) / intBitrate;

    // Convert total time in seconds to hours and minutes
    intHours = Math.floor(intTotalTime / 3600);
    intMins = Math.floor((intTotalTime - (intHours * 3600)) / 60);

    // Estimate how many songs and albums can be stored
    intAlbums = (intTotalTime / SECONDS_PER_ALBUM);
    intSongs = Math.floor(intTotalTime / SECONDS_PER_SONG);

    // Show results on web page in allocated fields
    document.getElementById("hours").value  = intHours + ':' + intMins;
    document.getElementById("albums").value = intAlbums.toFixed(1);
    document.getElementById("songs").value  = intSongs;
}


// MP3 storage space calculation
function CalcStorage()
{
    // Get key variables from HTML form
    intAlbums  = document.getElementById("albums2").value;
    intBitrate = document.getElementById("bitrate2").value;

    // Calculate total time for all albums
    intTotalTime = intAlbums * SECONDS_PER_ALBUM;

    // Estimate how much storage is required for audio in M
    intStorage = intTotalTime * (intBitrate / 8);

    // Estimate how much storage is required for one artwork per album
    intArtStorage = intAlbums * ALBUM_COVER_STORAGE;

    // Estimate total storage
    intStorage = intStorage + intArtStorage;

    // Work out multplier: k, M, G
    if (intStorage > 1073741823)
        intStorage = Math.floor(intStorage / 1073741824) + 'G';
    else if (intStorage > 1048575)
        intStorage = Math.floor(intStorage / 1048576) + 'M';
    else if (intStorage > 1023)
        intStorage = Math.floor(intStorage / 1024) + 'k';

    // Show results on web page in allocated fields
    document.getElementById("storage").value  = intStorage + 'B';
}


//-->
//</script>