// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var LastPlayedTrack;

function ap_stopAll()
{
}

function preloadImages()
{
    var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    { d.p[j]=new Image; d.p[j++].src='/images/' + a[i] + '.gif';}}
}

function Load()
{
    preloadImages('all', 'allchecked', 'mainstream', 'mainstreamchecked', 'indie', 'indiechecked');
}

function PlayFlash(track_id, url, title, artist, seed, rank, mash)
{
    if (LastPlayedTrack)
    {
        var div = document.getElementById('rate' + LastPlayedTrack);
        if (div)
        {
            div.innerHTML = '';
        }
    }

    var player = document.getElementById('player');

    if (player)
    {
        player.innerHTML = "<div class='flashplayer'> \
            <object type='application/x-shockwave-flash' data='/flash/wordpress/player.swf' id='audioplayer' height='24' width='290'> \
            <param name='movie' value='/flash/wordpress/player.swf'> \
            <param name='FlashVars' value='playerID=1&amp;soundFile=" + url + "&amp;autostart=yes&amp;bg=0xE9F1F6&leftbg=0x41606b&rightbg=0x41606b'> \
            <param name='quality' value='high'> \
            <param name='menu' value='false'> \
            <param name='bgcolor' value='E9F1F6' /> \
            </object><br /> \
            <a href='/acoustic/" + track_id + "'>" + title + " by " + artist + "</a> \
            </div>";
    }
    
    // alert('rate' + track_id);

    var div = document.getElementById('rate' + track_id);
    if (div)
    {
        LastPlayedTrack = track_id;

        var rate = '<a href="javascript: void(0);" onclick="OnRate(' + seed + ',' + track_id + ',' + rank + ',';
        div.innerHTML = 'Rate the relevance of this match: &nbsp;<ul class="rating nostar"><li class="one">' + rate + '0);" title="Lousy (songs have little in common)">1</a></li><li class="two">' + rate + '50);" title="Average (songs have some similarities)">2</a></li><li class="three">' + rate + '100);" title="Excellent (songs are a great fit)">3</a></li></ul>';
    } 
}

function Search(query)
{
    if ( (query == "Enter a track or artist you like here") || (query.length == 0) )
    {
        alert("Please type the title of a song or the name of an artist first");
        return false;
    } else
    {
        var url = "/search/all/" + encodeURIComponent(query) + "?by_hand=1";

        // alert("goFish4:" + url);

        location.href = url; 
        // alert(keywords);
    }
}

function CreateCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function EraseCookie(name) {
    CreateCookie(name,"",-1);
}

function ToggleBookmarkHelper(csv, track_id) {

    var removed = new Boolean(false);
    var dup = new Array();
    // alert(csv + ' t= ' + track_id);

    if (csv && csv.length > 0)
    {
        var values = csv.split('%2C');
        
        for(var i=0; i<values.length; i++)
        {
            (values[i] != track_id) ? dup.push(values[i]) : removed = true;
        }
    }

    if (removed == false)
    {
        dup.push(track_id);
    }

    return [dup.join('%2C'), removed];
}

function ToggleBookmark(track_id, refreshPage) {

    var cookie = ReadCookie("BOOKMARKS");

    // alert(cookie);

    var res = ToggleBookmarkHelper(cookie, track_id);

    CreateCookie("BOOKMARKS", res[0], 90);

    // alert(res[0]);
    // alert('refresh is ' + refresh);

    if (refreshPage)
    {
        if (refreshPage > 1)
        {
            window.location = window.location.pathname + '?page=' + refreshPage;
        } else
        {
            window.location = window.location.pathname;
        }
    } else
    {   // now we just need to update the bitmap
        // and also the tooltip
        document['BOOKMARK' + track_id].src = (res[1] == true) ? '/images/bookmark.gif' : '/images/unbookmark.gif';
        document['BOOKMARK' + track_id].title = (res[1] == true) ? 'Add this track to your bookmarks' : 'Remove this track from your bookmarks';
    }
}

function OnRate(seed, track_id, rank, rating)
{
    var pars = '/' + seed + '/' + track_id + '/' + rank + '/' + rating;

    // alert(pars);

    var myAjax = new Ajax.Updater('rate' + track_id, '/match_feedback' + pars, {method: 'get'} );
}

function FilterOver(over, active)
{
    document['filter_' + active].src = '/images/' + active + '.gif';
    document['filter_' + over].src = '/images/' + over + 'checked.gif';

    return true;    
}

function FilterOut(over, active)
{
    document['filter_' + active].src = '/images/' + active + 'checked.gif';

    if (over != active)
    {
        document['filter_' + over].src = '/images/' + over + '.gif';
    }

    return true;
}
