﻿var Profiles = {
    active: null,
    x: null,
    pageSize: 12,
    _page: 1,
    resultSet: null,
    guids: [],
    resultMap: {},
    show: function(obj) {
        if ($("#profilesList .profileName").index($(".profileName", this.active)) != $("#profilesList .profileName").index($(".profileName", obj)))
        {
            if (this.active) this.hide(this.active);
            
            this.active = obj;
            
            var profileHolder = $(obj).parent().parent();
            
            $("div.popupProfile", profileHolder).css('top', -1 * $("div.popupProfile").index($("div.popupProfile", profileHolder)));
            $("div.popupProfile", profileHolder).fadeIn(200);
            $(profileHolder).css({'z-index': '1000'});
            $(profileHolder).addClass("selected");
        }
    },
    hide: function(obj) {
        this.active = null;
        var profileHolder = $(obj).parent().parent();
        $("div.popupProfile", profileHolder).fadeOut(10);
        $(profileHolder).css({'z-index': '100'});
        $(profileHolder).removeClass("selected");
    },
    init: function(url) {
        $.get(url, function(xml) { Profiles.load(xml); }, "xml");
    },
    events: function() {
        $("a.profileName").each(function() {    
            $(this).mouseenter(function() { Profiles.show($(this)); });
            $(this).parent().parent().mouseleave(function() { Profiles.hide($(".profileName", $(this).parent())); });
        });
    },
    load: function(xml) {
        if (document.all && typeof xml.selectNodes != "undefined")
        {
            xml.setProperty("SelectionLanguage", "XPath");
        }
        this.x = xml;
        
        if (document.getElementById("searchProfileName").value != "Type name or department")
        {
            this.search(document.getElementById("searchProfileName").value.replace("Type name or department", ""));
        }
        else
        {
            var result = this.XPath("//profile");
            this.chunk(document.getElementById("sort"));
        }
        $("#profilesList").show();
    },
    XPath: function(query) {
        if (typeof this.x.evaluate != "undefined")
        {
            var xResult = this.x.evaluate(query, this.x.documentElement, null, XPathResult.ANY_TYPE, null);
            var result = [];
            var item;
            
            while (item = xResult.iterateNext())
            {
                result[result.length] = item;
            }
            
            return result;
        }
        else if (typeof this.x.selectNodes != "undefined")
        {
            var result = [];
            var xResult = this.x.selectNodes(query);
            
            for (var i = 0, l = xResult.length; i < l; i++)
            {
                result[result.length] = xResult[i];
            }
            return result;
        }
        else
        {
            // no XPath
            return null;
        }
    },
    XPathSingle: function(query)
    {
        var result = this.XPath(query);
        if (result.length == 0)
        {
            return null;
        }
        else
        {
            return result[0];
        }
    },
    fnSort: function(a, b) {
        // these need to be switched to lastname:
        
        var x = Profiles.tagValue(a, "lastname").toLowerCase();
        var y = Profiles.tagValue(b, "lastname").toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    },
    tagValue: function(node, tag)
    {
        if ((tmp = node.getElementsByTagName(tag)).length > 0)
        {
            return (document.all ? tmp[0].text : tmp[0].textContent);
        }
        else
        {
            return "";
        }
    },
    popups: function()
    {
        var i;
        var ie;
        var tmp;
        
        $("div.popupProfile").remove();
    
        for (var i = 0, l = this.guids.length; i < l; i++)
        {
            var div = document.getElementById("profileHolder" + this.guids[i]);
            if (typeof this.resultMap[this.guids[i]] != "undefined")
            {
                var node = this.resultSet[this.resultMap[this.guids[i]]];
                
                var _name = this.tagValue(node, "name");
                var _image = this.tagValue(node, "image").replace(".jpg", "_rdax_107x126.jpg");
                var _link = this.tagValue(node, "link");
                var _summary = this.tagValue(node, "summary");
                
                var html;
                html  = "          <div class=\"popupProfile\" style=\"position:absolute;left:170px;z-index:100;display:none\">\r\n";
                html += "            <div class=\"popupImage\"> <a href=\"" + _link + "\"><img src=\"" + _image + "\" /></a> </div>\r\n";
                html += "            <div class=\"text\">\r\n";
                html += "              <h4>" + _name + "</h4>\r\n";
                html += _summary + "\r\n";
                html += "            </div>\r\n";
                html += "          </div>\r\n";
                $(div).append(html);
            }
        }
        
        this.events();
    },
    profiles: function()
    {
        $("#profilesList").empty();
        
        this.guids = [];
        this.resultMap = {};
        
        for (var i = 0; i < this.resultSet.length; i++)
        {
            var node = this.resultSet[i];
            
            var _guid = node.getAttribute("guid");
            var _name = this.tagValue(node, "name");
            var _department = this.tagValue(node, "department");
            var _link = this.tagValue(node, "link");
            
            this.guids.push(_guid);
            this.resultMap[_guid] = i;
            
            var html;
            
            html  = "        <div class=\"profilesListItem\" id=\"profileHolder" + _guid + "\" style=\"position:relative;z-index:110;\">\r\n";
            html += "          <p><a class=\"profileName\" href=\"" + _link + "\">" + _name + "</a><br />\r\n";
            html += "            " + _department + "</p>\r\n";
            html += "        </div>\r\n";
            $("#profilesList").append(html);
        }
        $("#searchResultsPage").text(this.resultSet.length);
        this.popups();
    },
    search: function(str) {
        if (str)
        {
            this.resultSet = this.XPath("//profile[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '" + str.replace("'", "\\'") + "')]");
            this.resultSet.sort(this.fnSort);
        }
        else
        {
            var result = this.XPath("//profile");
            this.chunk(document.getElementById("sort"));
        }
        
        this.profiles();
    },
    chunk: function(obj) {
        var str = obj.options[obj.selectedIndex].text;
        
        var letters = str.split('-');
        
        var n1 = letters[0].toLowerCase().charCodeAt(0);
        var n2 = letters[1].toLowerCase().charCodeAt(0);
        
        var query = "//profile[";
        
        for (var i = n1; i <= n2; i++)
        {
            if (query.substr(query.length - 1) != "[")
            {
                query += " or ";
            }
            query += "starts-with(translate(lastname, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '" + String.fromCharCode(i) + "')";
        }
        
        query += "]";
        
        this.resultSet = this.XPath(query);
        this.resultSet.sort(this.fnSort);
        
        document.getElementById("searchProfileName").value = "";
        
        this.profiles();
    }
};
