// expects js/cookies.js to have already been parsed
// expects lbInit() from js/lightbox.js to have been executed
// expects global baseUrl to be set with trailing slash

// add island too favs cookie
function addToFavs(id, balloon, url, imgsrc, which) {
    // make sure page has loaded & lbInit() has been run
    if (!lb_favs) return;
    
    var favs, i;
    // get current favourites cookie, if exists
    var favsCookie = readCookie('favs');
    if (favsCookie) {
        favs = favsCookie.split('|||');
        // check if island is already in favourites
        for (i = 0; i < favs.length; i++) {
            var eachFav = favs[i].split('||');
            if ((eachFav[0] == id) &&
                (eachFav[4] == which))
            {
                // display 'already in your favourites' message and end
                alreadyFav();
                return;
            }
        }
        // if still running, favs are not empty and island will need to be
        // appended, so check if favs are full (max 15)
        if (favs.length >= 15) {
            favsLimit();
            return;
        }
    } else {
        favs = new Array();
    }
    // if still running, should add the island to favourites array and update cookie
    favs = favs.concat([id, balloon, url, imgsrc, which].join('||'));
    createCookie('favs', favs.join('|||'), 7);
    window.open(baseUrl+'favs-added.htm', '', 'width=400,height=115');
    lbShowFavs();
}

// re-reads favs cookie and rewrites lightbox favs display through innerHTML
function refreshFavs() {
    // make sure page has loaded & lbInit() has been run
    if (!lb_favs_items || 4!=lb_favs_items.length) return;
    
    var favsCookie = readCookie('favs');
    var favs;
    if (favsCookie) favs = favsCookie.split('|||');
    else favs = new Array();
    var startIndex = (favs.length > 4) ? (favs.length - 4) : 0;
    var endBeforeIndex = startIndex + 4;
    
    // this will loop 4 times no matter what, and fill in each lb_item
    // either with a thumbnail of the fav island, or a placeholder image if
    // there are no more favs.
    for (var i = startIndex; i < endBeforeIndex; i++) {
        if (favs[i]) {
            var eachFav = favs[i].split('||');
            lb_favs_items[i-startIndex].innerHTML = '<a href="'+baseUrl+eachFav[2]+'"\n'+
                '   onmouseover="tooltipOnLeft(\''+eachFav[1]+'\');"\n'+
                '   onmouseout="tooltipOffLeft();"><img src="'+picUrls[eachFav[4]]+eachFav[3]+'" alt="" width="60" height="48"></a>';
        } else {
            lb_favs_items[i-startIndex].innerHTML = '<img src="'+baseUrl+'images/empty-thumb.gif" alt="(placeholder)" width="60" height="48">';
        }
    }
}

// message window: this island is already in your favourites
function alreadyFav() {
    window.open(baseUrl+'favs-alreadythere.htm', '', 'width=400,height=115');
}

// message window: there's no more room in your favourites
function favsLimit() {
    window.open(baseUrl+'favs-limit.htm', '', 'width=400,height=150');
}
