var emailNotification = {
    feed: null,
    activeRequest: null,
    activeEmail: null,

    subscribe: function() {
        document.getElementById("emailaddr").disabled = true;
        document.getElementById("emailsub").disabled = true;
        document.getElementById("emailajax").style.display = "";
        emailNotification.activeEmail = document.getElementById("emailaddr").value
        var xmlReq;
        if(window.XMLHttpRequest) {
            xmlReq = new XMLHttpRequest();
        }
        else if(window.ActiveXObject) {
            xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlReq.open("POST", emailNotification.feed, true);
        xmlReq.send(emailNotification.activeEmail);
        emailNotification.activeRequest = xmlReq;
        setTimeout(emailNotification.checkRequest, 200);
    },

    checkRequest: function() {
        if(emailNotification.activeRequest.readyState == 4) {
            if(emailNotification.activeRequest.status == 200) {
                emailNotification.handleResponse(emailNotification.activeRequest.responseText);
            }
            else {
                emailNotification.handleResponse("Error adding email subscription: " + emailNotification.activeEmail);
            }
            emailNotification.activeRequest = null;
        }
        else {
            setTimeout(emailNotification.checkRequest, 200);
        }
    },

    handleResponse: function(raw) {
        document.getElementById("emailajax").style.display = "none";
        document.getElementById("emailaddr").disabled = false;
        document.getElementById("emailsub").disabled = false;
        if(raw == "ok") {
            document.getElementById("emailmessage").innerHTML = "<br>Subscribed: " + emailNotification.activeEmail;
        }
        else {
            document.getElementById("emailmessage").innerHTML = "<br>" + raw;
        }
    }
};

var albumFlag = {
    feed: null,
    showingAll: true,
    activeRequest: null,
    activeElement: null,
    activeProcessingImg: null,

    flag: function(element) {
        if(albumFlag.activeRequest != null) {
            return false;
        }

        var flag = element.innerHTML;
        var flagDiv = albumFlag.getFlagDiv(element);
        albumFlag.activeProcessingImg = document.createElement("img");
        albumFlag.activeProcessingImg.src = "/images/ajax-loader-small.gif";
        albumFlag.activeProcessingImg.alt = "flagging...";
        flagDiv.appendChild(albumFlag.activeProcessingImg);
        var albumDiv = albumFlag.getAlbumDiv(element);
        var albumid = albumDiv.id.substr(albumDiv.id.lastIndexOf("-") + 1);
        albumFlag.activeElement = element;

        var xmlReq;
        if(window.XMLHttpRequest) {
            xmlReq = new XMLHttpRequest();
        }
        else if(window.ActiveXObject) {
            xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlReq.open("POST", albumFlag.buildLink(albumid, flag), true);
        xmlReq.send(null);
        albumFlag.activeRequest = xmlReq;
        setTimeout(albumFlag.checkRequest, 200);
        return false;
    },

    getAlbumDiv: function(element) {
        if(element == null || element.className == "post") {
            return element;
        }
        return albumFlag.getAlbumDiv(element.parentNode);
    },

    getFlagDiv: function(element) {
        if(element == null || element.className == "flags") {
            return element;
        }
        return albumFlag.getFlagDiv(element.parentNode);
    },

    getAlbumFlagCountSpan: function(element) {
        return document.getElementById("albumFlagCount-" + element.innerHTML);
    },

    checkRequest: function() {
        if(albumFlag.activeRequest.readyState == 4) {
            if(albumFlag.activeRequest.status == 200) {
                albumFlag.handleResponse(albumFlag.activeRequest.responseText);
            }
            else {
                albumFlag.handleResponse("error");
            }
            albumFlag.activeRequest = null;
        }
        else {
            setTimeout(albumFlag.checkRequest, 200);
        }
    },

    handleResponse: function(raw) {
        albumFlag.activeProcessingImg.parentNode.removeChild(albumFlag.activeProcessingImg);
        var albumDiv = albumFlag.getAlbumDiv(albumFlag.activeElement);
        var albumid = albumDiv.id.substr(albumDiv.id.lastIndexOf("-") + 1);
        if(raw == "ok") {
            if(albumFlag.showingAll) {
                var flagDiv = albumFlag.getFlagDiv(albumFlag.activeElement);
                var flagB = albumFlag.getFlagB(flagDiv);
                var firstA = albumFlag.getFirstFlagA(flagDiv);
                var newFlag;
                if(flagB == null) {
                    flagB = document.createElement("i");
                    flagDiv.insertBefore(flagB, firstA);
                    flagDiv.insertBefore(document.createTextNode("  "), firstA);
                    newFlag = "new";
                }
                else {
                    newFlag = flagB.innerHTML;
                }
                var newA = document.createElement("a");
                newA.innerHTML = newFlag;
                newA.href = albumFlag.buildLink(albumid, newFlag);
                newA.setAttribute("onclick", "try{return albumFlag.flag(this);}catch(err){}");
                flagDiv.insertBefore(newA, albumFlag.activeElement);
                flagB.innerHTML = albumFlag.activeElement.innerHTML;
                albumFlag.activeElement.parentNode.removeChild(albumFlag.activeElement);
            }
            else {
                albumFlag.getAlbumDiv(albumFlag.activeElement).style.display = "none";
            }
            var albumFlagCountSpan = albumFlag.getAlbumFlagCountSpan(albumFlag.activeElement);
            if(albumFlagCountSpan) {
                albumFlagCountSpan.innerHTML = parseInt(albumFlagCountSpan.innerHTML) + 1;
            }
        }
        else {
            var flag = albumFlag.activeElement.innerHTML;
            document.location.href = albumFlag.buildLink(albumid, flag);
        }
    },

    buildLink: function(albumid, flag) {
        return albumFlag.feed + (albumFlag.feed.indexOf('?') > -1 ? '&' : '?') + "release=" + albumid + "&flag=" + flag;
    },

    getFlagB: function(flagDiv) {
        var child = flagDiv.firstChild;
        do {
            if(child.tagName && child.tagName.toLocaleLowerCase() == "i") {
                return child;
            }
            child = child.nextSibling;
        } while(child != null);
        return null;
    },

    getFirstFlagA: function(flagDiv) {
        var child = flagDiv.firstChild;
        do {
            if(child.tagName && child.tagName.toLocaleLowerCase() == "a") {
                return child;
            }
            child = child.nextSibling;
        } while(child != null);
        return null;
    }
};

var albumSources = {
    feed: null,
    sources: null,

    showChooseSources: function() {
        document.getElementById("albumSourcesChooseA").style.display = "none";
        document.getElementById("albumSourcesP").style.display = "";
    },

    go: function() {
        document.location.href = albumSources.buildLink();
    },

    buildLink: function() {
        var link = albumSources.feed + (albumSources.feed.indexOf('?') > -1 ? '&' : '?');
        if(document.getElementById("as-future").checked) {
            link += "sx=" + albumSources.getSourcesString(false);
        }
        else {
            link += "si=" + albumSources.getSourcesString(true);
        }
        return link;
    },

    getSourcesString: function(checked) {
        var sources = "";
        for(var i = 0; i < albumSources.sources.length; i++) {
            if(document.getElementById("as-" + albumSources.sources.charAt(i)).checked == checked) {
                sources += albumSources.sources.charAt(i);
            }
        }
        return sources;
    }
};