﻿
// Constants

var URL_REPORT = "server/Abuse.ashx";

var ARG_ID = "id";
var ARG_FORMAT = "format";

// Elements

var urlElmt;    // the textbox <div> or <table> container
var urlInput;   // the actual <input>
var embedElmt;  // the textbox <div> or <table> container
var embedInput; // the actual <input>
var abuseControl;
var abuseLink;

// Core functions

function init() {
    initElements();
    initControls();
}

function initElements() {
    urlElmt = $("url-textbox");
    urlInput = $("url-output");
    embedElmt = $("embed-textbox");
    embedInput = $("embed-output");
    abuseControl = $("abuse-control");
}

function initControls() {
    initUrlTextbox();
    initEmbedTextbox();
    initAbuseControl();
}

// URL control

function initUrlTextbox() {
    // select the text when the field gets focus, but only on "focus", not on
    // "click". the user should be able to place the caret then (bug 4064).
    Seadragon.Utils.addEvent(urlInput, "focus", onUrlInputFocus);

    // let clicking on the textbox container also focus and select the text.
    // give a text cursor affordance over it also.
    Seadragon.Utils.addEvent(urlElmt, "click", onUrlElmtClick);
    urlElmt.style.cursor = "text";
}

function onUrlInputFocus(event) {
    urlInput.select();
}

function onUrlElmtClick(event) {
    urlInput.focus();
}

// Embed control

function initEmbedTextbox() {
    // select the text when the field gets focus, but only on "focus", not on
    // "click". the user should be able to place the caret then (bug 4064).
    Seadragon.Utils.addEvent(embedInput, "focus", onEmbedInputFocus);

    // let clicking on the textbox container also focus and select the text.
    // give a text cursor affordance over it also.
    Seadragon.Utils.addEvent(embedElmt, "click", onEmbedElmtClick);
    embedElmt.style.cursor = "text";
}

function onEmbedInputFocus(event) {
    embedInput.select();
}

function onEmbedElmtClick(event) {
    embedInput.focus();
}

// Abuse control

function initAbuseControl() {
    // the report abuse control won't always be there
    if (abuseControl) {
        var links = abuseControl.getElementsByTagName("a");
        abuseLink = links.length > 0 ? links[0] : null;
    }
    
    // even if it's there, it may not have the link
    if (abuseLink) {
        Seadragon.Utils.addEvent(abuseLink, "click", onAbuseLinkClick);
    }
}

function onAbuseLinkClick(event) {
    Seadragon.Utils.cancelEvent(event);

    // remove this link, and change the text
    Seadragon.Utils.removeEvent(abuseLink, "click", arguments.callee);
    abuseControl.innerHTML = "Reporting abuse...";

    var urlParts = [];

    urlParts.push(URL_REPORT);
    urlParts.push("?");
    urlParts.push(ARG_ID);
    urlParts.push("=");
    urlParts.push(SERVER.Id);
    urlParts.push("&");
    urlParts.push(ARG_FORMAT);
    urlParts.push("=");
    urlParts.push("json");

    // normally, we'd also want to add a random parameter to prevent browsers
    // from caching this request. but in this case, it's actually a benefit; it
    // prevents someone from reporting abuse on the same image multiple times.

    Seadragon.Utils.makeAjaxRequest(urlParts.join(''), onAbuseServerResponse);
}

function onAbuseServerResponse(xhr) {
    // we're not currently bothering to check the response; it isn't necessary
    abuseControl.innerHTML = "Abuse reported.";
}

// Immediate execution

init();
