﻿var GEO_PATH = "/_ui/media/geo.aspx";
var GEO_SIGNUP = { id: "GEO_SIGNUP", title: "Sign Up", path: GEO_PATH, width: "550", height: "500" };
var GEO_SIGNIN = { id: "GEO_SIGNIN", title: "Sign In", path: GEO_PATH, width: "385", height: "200" };
var GEO_SHOWVIDEO = { id: "GEO_SHOWVIDEO", title: "Video", path: GEO_PATH, width: "425", height: "344" };


//Actions
function geoAction() { };

geoAction.prototype = {
    serverPath: GEO_PATH,
    addToPost: function(postId) {

        if ($("CommentText").value == "")
            return;

        var data = new dataObject();
        data.add("page", "Commenter");
        data.add("method", "AddToPost");

        data.add("text", $("CommentText").value);
        data.add("postId", postId);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.showComment(result.data); });
    },
    showComment: function(result) {
        if (result.success) {
            $("CommentBox").innerHTML = result.html;
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    },
    followSpot: function(id) {

        var data = new dataObject();
        data.add("page", "Follow");
        data.add("method", "FollowSpot");

        data.add("id", id);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processFollow(result.data); });
    },
    followPost: function(id) {

        var data = new dataObject();
        data.add("page", "Follow");
        data.add("method", "FollowPost");

        data.add("id", id);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processFollow(result.data); });
    },
    followUser: function(id) {

        var data = new dataObject();
        data.add("page", "Follow");
        data.add("method", "FollowUser");

        data.add("id", id);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processFollow(result.data); });
    },
    followEntity: function(id) {

        var data = new dataObject();
        data.add("page", "Follow");
        data.add("method", "FollowEntity");

        data.add("id", id);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processFollow(result.data); });
    },
    processFollow: function(result) {
        if (result.success) {
            $("FollowLink").innerHTML = result.newText;
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    },
    request: function(targetId, path, hide) {

        if (hide && $(targetId).innerHTML != "") {
            $(targetId).innerHTML = "";
            return;
        }

        var data = new dataObject();
        data.add("targetId", targetId);
        data.add("path", path);

        lara.indicateOn();
        lara.request(this.serverPath + ":RequestData", data, function(result) { ga.showResult(result); });
    },
    showResult: function(result) {
        if (result.html) {
            $(result.targetId).innerHTML = result.html;

            //Google analytics
            try {
                pageTracker._trackPageview(BASE_URL + result.requestPath);
            }
            catch (err) {
            }
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    },
    signIn: function() {

        if ($("SigninEmail").value == "" || $("SigninPassword").value == "")
            return false;

        var data = new dataObject();
        data.add("page", "SignIn");
        data.add("method", "Authenticate");

        data.add("email", $("SigninEmail").value);
        data.add("password", $("SigninPassword").value);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processSignIn(result.data); });
    },
    signUp: function() {

        if ($("SignupEmail").value == "" || $("SignupName").value == "" || $("SignupPassword").value == "")
            return;

        if ($("SignupPassword").value != $("SignupConfirm").value) {
            alert("Passwords do not match.");
            return;
        }

        var data = new dataObject();
        data.add("page", "SignUp");
        data.add("method", "Create");

        data.add("email", $("SignupEmail").value);
        data.add("name", $("SignupName").value);
        data.add("password", $("SignupPassword").value);
        data.add("spotId", $("SignupSelectorSpotId").value);

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processSignUp(result.data); });
    },
    processSignUp: function(result) {
        if (result.success) {
            $("GeoWinFrame").innerHTML = result.html;
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    },
    processSignIn: function(result) {
        if (result.success) {
            if (result.redirect)
                window.location = result.redirect;
            else {
                $("SignInResult").innerHTML = result.error;
                lara.indicateOff();
            }
        }
        else {
            $("SignInResult").innerHTML = result.error;
            $("SigninPassword").value = "";
            $("SigninEmail").focus();
            lara.indicateOff();
        }

    },
    signOut: function() {

        var data = new dataObject();
        data.add("page", "SignIn");
        data.add("method", "SignOut");

        lara.indicateOn();
        lara.request(this.serverPath + ":Pass", data, function(result) { ga.processSignOut(result.data); });
    },
    processSignOut: function(result) {
        if (result.success) {
            window.location = window.location;
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    }
};

var ga = new geoAction();



//TREE
function geoTree() { };

geoTree.prototype = {
    serverPath: GEO_PATH,
    g: function(node) {
        this.clickedNode = node;

        var childDiv = node.parentNode.parentNode.getElementsByTagName("div")[0];
        if (childDiv) {
            if (childDiv.style.display == "none") {
                childDiv.style.display = "block";
                this.clickedNode.parentNode.firstChild.className = "e";
            }
            else {
                childDiv.style.display = "none";
                this.clickedNode.parentNode.firstChild.className = "c";
            }
        }
        else {
            this.newTree = document.createElement("div");
            this.newTree.className = "v";
            this.newTree.innerHTML = "<input type=\"button\" class=\"em\"/>loading...";

            this.clickedNode.parentNode.parentNode.appendChild(this.newTree);
            this.clickedNode.parentNode.firstChild.className = "e";

            var data = new dataObject();
            data.add("id", node.parentNode.parentNode.id);

            lara.request(this.serverPath + ":GetSubTree", data, function(result) { gt.showSubtree(result); });
        }
    },
    showSubtree: function(result) {
        if (result.success)
            this.newTree.innerHTML = result.subtree;
    },
    spotSelected: function(dropdown) {

        this.current = dropdown;
        this.id = this.getSelectorId(dropdown);

        //Remove any subdrowndowns.
        var childDiv = dropdown.parentNode.getElementsByTagName("div")[0];
        if (childDiv) {
            dropdown.parentNode.removeChild(childDiv);
        }

        //Get the next dropdown from the server
        selectedId = dropdown.options[dropdown.selectedIndex].value;
        this.disableSelectors(true);

        lara.indicateOn();
        lara.request(this.serverPath + ":GetSelector", "id=" + selectedId, function(result) { gt.showDropDown(result); });

        //Save the result so it can be used.
        $(this.id + "SpotId").value = selectedId;

    },
    getSelectorId: function(dropdown) {
        var parent = dropdown.parentNode;
        while (parent.className != "SpotSelector" && parent.nodeName != "BODY") {
            parent = parent.parentNode;
        }
        return parent.id;
    },
    showDropDown: function(result) {
        if (result.html) {
            newdiv = document.createElement("div");
            newdiv.className = "selectorChildContainer";
            newdiv.innerHTML = result.html;
            this.current.parentNode.appendChild(newdiv);
        }
        this.disableSelectors(false);
        lara.indicateOff();
    },
    disableSelectors: function(disabled) {
        var selectors = $getByClass("selector", "select");
        for (var i = 0; i < selectors.length; i++) {
            selectors[i].disabled = disabled;
        }
    },
    addToPost: function(postId) {

        if ($("CommentName").value == "" || $("CommentText").value == "")
            return;

        if ($("CommentTest").value != "578") {
            alert("Oops, wrong numbers.");
            $("CommentTest").value = "";
            return;
        }

        var data = new dataObject();
        data.add("name", $("CommentName").value);
        data.add("spotId", $("CommentSelectorSpotId").value);
        data.add("text", $("CommentText").value);
        data.add("postId", postId);

        lara.indicateOn();
        lara.request(this.serverPath + ":AddToPost", data, function(result) { gt.showComment(result); });
    },
    showComment: function(result) {
        if (result.html) {
            $("CommentBox").innerHTML = result.html;
        }
        else {
            alert(result.error);
        }
        lara.indicateOff();
    }
};

var gt = new geoTree();
