﻿Mortenson.Resources = function()
{
    var _industryDDL;
    var _topicDDL;
    Ext.onReady(this.Initialize.createDelegate(this));
}

Ext.extend(Mortenson.Resources, Ext.util.Observable,
{
    Initialize : function()
    {
        this._industryDDL = document.getElementById('ddlIndustry');
        this._topicDDL = document.getElementById('ddlTopic');
        this.addEvents({"onData": true});
    },
    
    SetDDLFilters : function()
    {
        var cookie = new Mortenson.Cookie();
        var industry = cookie.GetIndustryCookie();
        var topic = cookie.GetTopicCookie();
        
        if (industry)
            this._industryDDL.value = industry;
            
        if (topic)
            this._topicDDL.value = topic;
    },
    
    IndustryChange : function()
    {
        var cookie = new Mortenson.Cookie();
        cookie.SetIndustryCookie(this._industryDDL.value);
    },
    
    TopicChange : function()
    {
        var cookie = new Mortenson.Cookie();
        cookie.SetTopicCookie(this._topicDDL.value);
    },
    
    ResourceSearchPageResults : function()
    {
        Mortenson.AjaxAPI.MortensonAjax.ResourceSearchPageResults(this._industryDDL.value, this._topicDDL.value, -1, this.ResourceSearchPageResults_callback.createDelegate(this));
    },
    ResourceSearchPageResults_callback : function(res)
    {
        if (res.error)
        {
            alert(res.error.Message);
        }
        else
        {
            var divResources = Ext.get("divResources");
            divResources.update(res.value);
//            //var x = res.value.documentElement;
//            var x = Ext.DomQuery.selectNode('/', res.value);
//             x = Ext.DomQuery.selectNode('/data', res.value);
//            x = res.value;
//            var a = Ext.DomQuery.selectNode('data', res.value);
//            for (var i = 0, child; child = x.childNodes[i]; i++)
//            {
//                if (child.nodeName == 'Resources')
//                {
//                    var divResources = Ext.get("divResources");
//                    divResources.update(child.firstChild.nodeValue); 
//                }
//                else if (child.nodeName == 'RecentResources')
//                {
//                    var divRecentResources = Ext.get("divRecentResources");
//                    divRecentResources.update(child.firstChild.nodeValue);                 
//                }
//                else if (child.nodeName == 'PressRoom')
//                {
//                    var divFromOurPressRoom = Ext.get("divFromOurPressRoom");
//                    divFromOurPressRoom.update(child.firstChild.nodeValue);  
//                }                
//            }
        }
    },
    
    ResourceSearch : function(resourceID)
    {
        Mortenson.AjaxAPI.MortensonAjax.ResourceSearch(this._industryDDL.value, this._topicDDL.value, resourceID, this.ResourceSearch_callback.createDelegate(this));
    },
    ResourceSearch_callback : function(res)
    {
        if (res.error)
        {
            alert(res.error.Message);
        }
        else
        {
            var divResources = Ext.get("divResources");
            divResources.update(res.value);
//            var x = res.value.documentElement;
//            for (var i = 0, child; child = x.childNodes[i]; i++)
//            {
//                if (child.nodeName == 'Resources')
//                {
//                    var divResources = Ext.get("divResources");
//                    divResources.update(child.firstChild.nodeValue); 
//                }
//                else if (child.nodeName == 'RecentResources')
//                {
//                    var divRecentResources = Ext.get("divRecentResources");
//                    divRecentResources.update(child.firstChild.nodeValue);                 
//                }
//                else if (child.nodeName == 'PressRoom')
//                {
//                    var divFromOurPressRoom = Ext.get("divFromOurPressRoom");
//                    divFromOurPressRoom.update(child.firstChild.nodeValue);  
//                }                
//            }
        }
    }
});



