function exit(url, key, type, pos) {
    if(document.images) {
        var encode=encodeURIComponent||escape;
        (new Image).src="/exit?type="+type+"&pos="+pos+"&key="+key+"&url="+encode(url.replace(/#.*/,"")).replace(/\+/g,"%2B");
    }
    return true;
}

/***
 * Toggle between image information and description box
 ***/
function toggle_cool_search() {
    $("#search_image_info_id").toggle()
    $("#cool_search_desc_id").toggle()
}

// Toggle the permalink box
function toggle_link_to_search() {
    $("#link_to_search_url").toggle()
    $("#link_to_search").toggle()
}

/***
 * Cool search description handler
 ***/
function submit_cool_search(hash) {
    // Check for length of description, if check passes then submit
    if ($("#cool_search_desc_ta").val().length > 1000) {
        alert("Please limit your description to 1,000 characters.");
    }
    else if(window.confirm("Are you sure you want to submit this as a cool search?")) {
        $.getJSON("/profile/submit_cool_search", {'hash': hash, 'desc': $("#cool_search_desc_ta").val()}, submit_cool_search_callback);
    }
}

/***
 * Cool search description callback
 * Displays whether the description information was sent successfully
 ***/
function submit_cool_search_callback(data) {
    toggle_cool_search();
    $('#recommend_cool_search')[0].innerHTML = data['message'];
    $('#recommend_cool_search').removeClass();
    if(data['is_valid']) {
        $('#recommend_cool_search').addClass('info');
    }
    else {
        $('#recommend_cool_search').addClass('error');
    }
}

/***
 * Flash searchbox handlers 
 ***/
function file_dialog_complete(numFilesSelected, numFilesQueued) {
    if (numFilesSelected != 1) {
        return;
    }
    try {
        // Start the upload
        this.startUpload();
    } catch (ex) {
        hide_throbber();
        this.debug(ex);
    }
}

function progress_handler()
{
    display_throbber();
}

function display_throbber()
{
    $("#throbber").css("visibility", "visible");
    $("#url_box")[0].setAttribute("readonly", "readonly");
    $("#url_submit")[0].disabled = true;
}

function hide_throbber()
{
    $("#throbber").css("visibility", "hidden");
    $("#url_box")[0].removeAttribute("readonly");
}

function upload_handler(file, serverData) {
    parts = serverData.split('\t');
    if(parts[0] == "key") {
        window.location = "/search/" + parts[1];
    } else if(parts[0] == "error") {
        hide_throbber();
        alert(parts[1]);
    }
}

function upload_error_handler(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                alert("Your image is too big. Try uploading an image smaller than 1 megabyte.");
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                alert("Your file is not a valid image. Try uploading a different file.");
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                alert("Your image type is not supported. Try uploading an image that is a JPEG, GIF or PNG.");
                break;
            case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                alert("Your image is still uploading, please wait.");
                break;
            default:
                alert("Could not upload your image. Try uploading a different image.");
                break;
        }
        hide_throbber();
    } catch (ex) {
        this.debug(ex);
    }
}
