﻿function clsTagCtrl() {
    this.tagtype = "";

    this.flag_del = false;
    this.flag_slt = false;
}


var ctrlTag = new clsTagCtrl();

clsTagCtrl.prototype.initTag = function(e)
{
    this.divTag = document.getElementById("divTag");
    this.treeTag = document.getElementById("treeTag");

    //this.spTag_graph = document.getElementById("spTag_graph");
    //this.spTag_location = document.getElementById("spTag_location");

    //this.divTag_Add = document.getElementById("divTag_Add");

    this.cbSaveTagPublished = document.getElementById("cbSaveTagPublished");

    this.iptSaveTagTitle = document.getElementById("iptSaveTagTitle");
    this.txtSaveTagComment = document.getElementById("txtSaveTagComment");

    this.btTagSelect = document.getElementById("btTagSelect");
    this.btTagDelete = document.getElementById("btTagDelete");

    $("#divSaveTag").dialog({ bgiframe: true, autoOpen: false, width: 230, height: 350, modal: true,
        buttons: {
            'Save': function()
            {
                ctrlTag.clicked_TagSubmit();
                $(this).dialog('close');
            },
            Cancel: function() { $(this).dialog('close'); }
        },
        close: function()
        {
            ctrlLayout.AfterDIalog();
        }
    });
}

clsTagCtrl.prototype.userClicked_AddTag_location = function() {
    var exist = false;
    for (var i = 0; i < arrLocations.length; i++) {
        if (arrLocations[i].selected == true) {
            exist = true;
            break;
        }
    }
    if (exist == false) {
        ctrlDesc.EffectedNotice("Please select locations before taging them");
        return true;
    }

    this.iptSaveTagTitle.value = "";
    this.txtSaveTagComment.innerHTML = "";
    this.cbSaveTagPublished.checked = false;

    ctrlLayout.BeforeDIalog();
    this.tagtype = 'location';
    $("#divSaveTag").dialog('open');
}

clsTagCtrl.prototype.userClicked_AddTag_graphs = function(e) {
    var exist = false;
    for (var i = 0; i < arrGraphs.length; i++) {
        if (arrGraphs[i].rendered == true) {
            exist = true;
            break;
        }
    }
    if (exist == false) {
        ctrlDesc.NoticeDialog("My Data -Notice-", "Quick graph cannot be saved. Please select and Draw graph before taging them");
        return true;
    }

    this.iptSaveTagTitle.value = "";
    this.txtSaveTagComment.innerHTML = "";
    this.cbSaveTagPublished.checked = false;

    ctrlLayout.BeforeDIalog();
    this.tagtype = 'graphs';
    $("#divSaveTag").dialog('open');
}

clsTagCtrl.prototype.Close_AddTagDialog = function() {
    $("#divSaveTag").dialog('close');
    this.render_Tags();
}

clsTagCtrl.prototype.clicked_TagSubmit = function() {
    if (ctrlUserdata.login) {
        if (this.tagtype == "location") {
            var strLocs = "";
            for (var i = 0; i < arrLocations.length; i++) {
                if (arrLocations[i].selected) {
                    strLocs += arrLocations[i].id + ",";
                }
            }
            strLocs = strLocs.substr(0, strLocs.length - 1);
            ctrlAjax.CallAjaxFunction("InsertNewTag", "userid=" + ctrlUserdata.userID + "&tagtype=" + this.tagtype + "&title=" + this.iptSaveTagTitle.value + "&value=" + strLocs + "&desc=" + this.txtSaveTagComment.innerHTML + "&published=" + this.cbSaveTagPublished.checked);
        }
        else if (this.tagtype == "graphs") {
            var value = "";
            for (i = 0; i < arrGraphs.length; i++) {
                if (arrGraphs[i].rendered == true) {
                    var strLoc = "";
                    for (var j = 0; j < arrGraphs[i].arrLocations.length; j++)
                        strLoc += arrGraphs[i].arrLocations[j].id + ",";

                    value += strLoc.substr(0, strLoc.length - 1) + "^";
                    value += arrGraphs[i].sensorType + "^";
                    var stt = arrGraphs[i].startDate.split(' ');
                    var ed = arrGraphs[i].endDate.split(' ');
                    value += stt[0] + "^";
                    value += ed[0] + "|";
                }
            }
            if (value != "") {
                ctrlAjax.CallAjaxFunction("InsertNewTag", "userid=" + ctrlUserdata.userID + "&tagtype=" + this.tagtype + "&title=" + this.iptSaveTagTitle.value + "&desc=" + this.txtSaveTagComment.innerHTML + "&value=" + value.substr(0, value.length - 1) + "&published=" + this.cbSaveTagPublished.checked);
            }
        }
    }
    else {
        ctrlUserdata.Logout();
        ctrlDesc.Notice("Please login first");
    }
}

clsTagCtrl.prototype.render_Tags = function() {
    var innerhtml = "";
    var i = 0;
    var lochtml = "<li><span>Location</span><ul style='background-color:Transparent;'>";
    var grphtml = "<li><span>Graph</span><ul style='background-color:Transparent;'>";

    if (ctrlUserdata.login == true) {
        innerhtml += "<li>" + "<span>My Data (" + ctrlUserdata.userID + ")</span> <ul style='background-color:Transparent;'>";

        for (i = 0; i < arrTags.length; i++) {
            if (arrTags[i].userid == ctrlUserdata.userID && arrTags[i].published == false) {
                if (arrTags[i].tagtype == 'location')
                    lochtml += "<li id='tag" + arrTags[i].tagid + "' onmousedown='javascript:ctrlTag.user_clickedTag(this.id);' ><span>" + arrTags[i].title + "</span></li>\n";
                else if (arrTags[i].tagtype == 'graphs')
                    grphtml += "<li id='tag" + arrTags[i].tagid + "' onmousedown='javascript:ctrlTag.user_clickedTag(this.id);' ><span>" + arrTags[i].title + "</span></li>\n";
            }
        }

        lochtml += "</ul></li>";
        grphtml += "</ul></li>";
        innerhtml += lochtml;
        innerhtml += grphtml;
        innerhtml += "</ul></li>";
    }

    innerhtml += "<li >" + "<span>Public Data</span><ul style='background-color:Transparent;' >";
    lochtml = "<li><span>Location</span><ul style='background-color:Transparent;'>";
    grphtml = "<li><span>Graph</span><ul style='background-color:Transparent;'>";
    for (i = 0; i < arrTags.length; i++) {
        if (arrTags[i].published == true) {
            if (arrTags[i].tagtype == 'location')
                lochtml += "<li id='tag" + arrTags[i].tagid + "' onmousedown='javascript:ctrlTag.user_clickedTag(this.id);' ><span>" + arrTags[i].title + "</span></li>\n";
            else if (arrTags[i].tagtype == 'graphs')
                grphtml += "<li id='tag" + arrTags[i].tagid + "' onmousedown='javascript:ctrlTag.user_clickedTag(this.id);' ><span>" + arrTags[i].title + "</span></li>\n";
        }
    }
    lochtml += "</ul></li>";
    grphtml += "</ul></li>";
    innerhtml += lochtml;
    innerhtml += grphtml;
    innerhtml += "</ul></li>";
    this.treeTag.innerHTML = innerhtml;
    $("#treeTag").treeview({
        animated: "fast",
        collapsed: true,
        unique: true,
        persist: "cookie",
        toggle: function() {
            window.console && console.log("%o was toggled", this);
        }
    });


    var elLI = null;
    if (ctrlUserdata.login == true) {
        for (i = 0; i < arrTags.length; i++) {
            elLI = document.getElementById("tag" + arrTags[i].tagid);
            if (arrTags[i].selected) {
                elLI.style.color = "#FFF";
                elLI.style.backgroundColor = "#09F";
            }
            else {
                elLI.style.color = "#000";
                elLI.style.backgroundColor = "Transparent"
            }
        }
    }
    else {
        for (i = 0; i < arrTags.length; i++) {
            if (arrTags[i].published == true) {
                elLI = document.getElementById("tag" + arrTags[i].tagid);
                if (arrTags[i].selected) {
                    elLI.style.color = "#FFF";
                    elLI.style.backgroundColor = "#09F";
                }
                else {
                    elLI.style.color = "#000";
                    elLI.style.backgroundColor = "Transparent"
                }
            }
        }
    }
}

clsTagCtrl.prototype.clicked_TagSelect = function() {
    for (var i = 0; i < arrTags.length; i++) {
        if (arrTags[i].selected == true) {
            if (arrTags[i].tagtype == 'location') {
                ctrlSiteLocation.LoadLocationTags(arrTags[i].value);
            }

            else if (arrTags[i].tagtype == 'graphs') {
                ctrlPlot.CreateTags(arrTags[i].tagid);
            }
        }
    }
}

clsTagCtrl.prototype.clicked_TagDelete = function() {
    for (var i = 0; i < arrTags.length; i++) {
        if (arrTags[i].selected == true) {
            ctrlAjax.CallAjaxFunction("DeleteUserTag", "userid=" + ctrlUserdata.userID + "&tagid=" + arrTags[i].tagid);
        }
    }

}

clsTagCtrl.prototype.LoadTags = function() {
    if (ctrlUserdata.login) {
        ctrlAjax.CallAjaxFunction("LoadUserTag", "userid=" + ctrlUserdata.userID);
    }
    else {
        ctrlAjax.CallAjaxFunction("LoadPublicTag", "dummy=0");
    }

}

clsTagCtrl.prototype.user_clickedTag = function(elID) {
    var tagid = elID.substr(3, elID.length);
    var elLI = null;
    for (var i = 0; i < arrTags.length; i++) {
        elLI = document.getElementById("tag" + arrTags[i].tagid);
        if (arrTags[i].tagid == tagid) {
            //ctrlDesc.TagInfo(arrTags[i].fname,arrTags[i].tagtype,arrTags[i].description);
            elLI.style.color = "#FFF";
            elLI.style.backgroundColor = "#09F";
            arrTags[i].selected = true;
        }
        else {
            elLI.style.color = "#000";
            elLI.style.backgroundColor = "Transparent"
            arrTags[i].selected = false;
        }
    }
    this.flag_del = false;
    this.flag_slt = false;
    for (i = 0; i < arrTags.length; i++) {
        if (arrTags[i].userid == ctrlUserdata.userID && arrTags[i].selected == true) {
            this.flag_del = true;
        }
        if (arrTags[i].selected == true) {
            this.flag_slt = true;
        }
    }
    this.ButtonDisEnable();
}

clsTagCtrl.prototype.ButtonDisEnable = function() {
    if (this.flag_del == true) {
        if ($("#btTagDelete").hasClass("ui-state-disabled") == true) {
            $("#btTagDelete").removeClass("ui-state-disabled");
        }
    }
    else {
        if ($("#btTagDelete").hasClass("ui-state-disabled") == false)
            $("#btTagDelete").addClass("ui-state-disabled");
    }

    if (this.flag_slt == true) {
        if ($("#btTagSelect").hasClass("ui-state-disabled") == true)
            $("#btTagSelect").removeClass("ui-state-disabled");
    }
    else {
        if ($("#btTagSelect").hasClass("ui-state-disabled") == false)
            $("#btTagSelect").addClass("ui-state-disabled");
    }
    if (this.flag_slt == true)
    {
        if ($("#btnTagURL").hasClass("ui-state-disabled") == true)
            $("#btnTagURL").removeClass("ui-state-disabled");
    }
    else
    {
        if ($("#btnTagURL").hasClass("ui-state-disabled") == false)
            $("#btnTagURL").addClass("ui-state-disabled");
    }
}

clsTagCtrl.prototype.CleanUp_objects = function() {
    for (var i = 0; i < arrTags.length; i++) {
        delete arrTags[i].tagid;
        delete arrTags[i].userid;
        delete arrTags[i].fname;
        delete arrTags[i].tagtype;
        delete arrTags[i].title;
        delete arrTags[i].value;
        delete arrTags[i].description;
        delete arrTags[i].selected;
        delete arrTags[i].published;

        arrTags[i].tagid = null;
        arrTags[i].userid = null;
        arrTags[i].fname = null;
        arrTags[i].tagtype = null;
        arrTags[i].title = null;
        arrTags[i].value = null;
        arrTags[i].description = null;
        arrTags[i].selected = null;
        arrTags[i].published = null;
    }
    arrTags.splice(0, arrTags.length);
    arrTags = null;
    arrTags = new Array();
}

clsTagCtrl.prototype.UserLogin = function() {
    //ctrlTag.Show_AddTagButton();
    ctrlTag.render_Tags();

    document.getElementById('tbTag_graph').style.visibility = "visible"
    document.getElementById('tbTag_location').style.visibility = "visible"


    document.getElementById('tbTag_graph').innerHTML = "<table style='padding:0 0 0 0; margin:0 0 0 0;' ><tr style='padding:0 0 0 0; margin:0 0 0 0;' onclick='ctrlTag.userClicked_AddTag_graphs();'><td style='padding:0 0 0 0; margin:0 0 0 0;'>GRAPH</td><td style='color:#0073EA; cursor:pointer;padding:0 0 0 0; margin:0 0 0 0;'>Save</td><td><span style='color:#0073EA; cursor:pointer;padding:0 0 0 0; margin:0 0 0 0;' class='ui-icon ui-icon-arrowthickstop-1-s'></span></td></tr></table>";
    document.getElementById('tbTag_location').innerHTML = "<table style='padding:0 0 0 0; margin:0 0 0 0;' ><tr style='padding:0 0 0 0; margin:0 0 0 0;' onclick='ctrlTag.userClicked_AddTag_location();'><td style='padding:0 0 0 0; margin:0 0 0 0;'>SENSING LOCATION</td><td style='color:#0073EA;cursor:pointer;padding:0 0 0 0; margin:0 0 0 0;'>Save</td><td><span style='color:#0073EA; cursor:pointer;padding:0 0 0 0; margin:0 0 0 0;' class='ui-icon ui-icon-arrowthickstop-1-s'></span></td></tr></table>";

}

clsTagCtrl.prototype.UserLogout = function() {
    //ctrlTag.Hide_AddTagButton();
    //ctrlTag.CleanUp_objects();
    ctrlTag.render_Tags();
    document.getElementById('tbTag_graph').innerHTML = "GRAPH";
    document.getElementById('tbTag_location').innerHTML = "SENSING LOCATION";

}

clsTagCtrl.prototype.runTag = function(tagid)
{
    for (var i = 0; i < arrTags.length; i++)
    {
        if (arrTags[i].tagid == tagid)
        {
            if (arrTags[i].tagtype == 'location')
            {
                ctrlSiteLocation.LoadLocationTags(arrTags[i].value);
            }

            else if (arrTags[i].tagtype == 'graphs')
            {
                ctrlPlot.CreateTags(arrTags[i].tagid);
                ctrlLayout.switchToGraphView();
                ctrlGraph.DrawTagByID(arrTags[i].tagid);
            }
            break;
        }
    }
}
