/*
 * JavaScript functions necessary for flickr captcha 
 *
 * filename: flickr.captcha.js
 * @author (Copyright) Joel Heenan 13/12/2008
 *
 * This file is part of Flickr-CAPTCHA
 * 
 * Flickr-CAPTCHA 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 3 of the License, or
 * (at your option) any later version.
 *
 * Flickr-CAPTCHA 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 Flickr-CAPTCHA.  If not, see <http://www.gnu.org/licenses/>.
 */

// when the DOM is ready
$(function () {
    var imgs = $("div").filter(".fc_Picture");
    var parentDiv;
    for(i=0; i<imgs.length; i++) {
        var img = new Image();
        var imageid = $(imgs[i]).attr('id').split("_")[2];
        $(img)
            .load(function() { 
                    $(this).hide()
                    var imageid = $(this).attr('id').split('_')[2];
                    var parentDiv = $('#fc_Picture_'+imageid);
                    $(parentDiv).removeClass('fc_loader');
                    $(parentDiv).append(this);
                    $(this).fadeIn();
                })            
            .attr('src','/fc_image.php?imageid='+imageid)
            .attr('width','50')
            .attr('id','fc_Image_'+imageid)
    }
});


/* @shortdesc show the picture under the mouse cursor
 */
function showPreview(pictureId, pictureUrl,pictureTitle,ownerName,ownerURL) {
    var picture = document.getElementById(pictureId);
    if(picture.className.indexOf('fc_loader') > -1) {
        return;
    }
    if(picture.className != "fc_selected") {
        picture.className = "fc_border";
    }
    var preview = document.getElementById('previewImage');
    preview.src = pictureUrl;
    preview.style.marginTop  = (240 - preview.height)/2;
    preview.style.marginLeft = (240 - preview.width) /2;

    var title = document.getElementById('previewTitle');
    var HTML = '';
    if(pictureTitle != '') {
        HTML += pictureTitle + " - ";
    }
    HTML += "<a href='" + ownerURL + "'>" + ownerName + "</a>";
    title.innerHTML = HTML;
}

/* @shortdesc remove the class showing this image is selected
 */
function hidePreview(pictureId) {
    var picture = document.getElementById(pictureId);
    if(picture.className.indexOf('fc_loader') > -1) {
        return;
    }
    if(picture.className != "fc_selected") {
        picture.className = "fc_Picture";
    }
}

/* @shortdesc store in the hidden input field that this
 * image is selected and show a box around it to make it 
 * clear to the user
 */
function selectImage(pictureId, imageId) {
    var picture = document.getElementById(pictureId);
    var selected = document.fc_form.selected.value.split(",");
    if(picture.className == "fc_selected") {
        picture.className = "fc_border";
        selected.splice(selected.indexOf(imageId),1);
    } else {
        picture.className = "fc_selected";
        selected.push(imageId);
    }
    document.fc_form.selected.value = selected.join(",");
}
