/*****************************************************************************
 * JavaScript for Controls
 ****************************************************************************/
dojo.require("dojo.io.*");
dojo.require("dojo.collections.*");

var wcontrolsCache = new dojo.collections.Dictionary();

/*****************************************************************************
 * Tab Component
 ****************************************************************************/
function changeTab(linkId, contentId, tabId, tgId, activeClass, inactiveClass) {
    var tg = getTabGroup(tgId);
    var cached = null;
    if (tg.cacheEnabled) {            
        //Save the selected tab content (state) so that we can restore it later 
        //if the tab is clicked again.
        cacheCurrentTabContent(tg.selectedTabId, contentId);    
        //fetch the clicked tabs content from cache, if any
        cached = getCachedContent(tabId);
    }
    
    if (cached != null) {
        changeTabContent(tabId, tgId, contentId, activeClass, inactiveClass, cached);
    } else { 
        var url = dojo.byId(linkId).href;

        var bindArgs = {
            url: url,
            mimetype: "text/json",
            load: function(type,data) {
                changeTabContent(tabId, tgId, contentId, activeClass, inactiveClass, data);
                //cache result, if caching enabled
                if (tg.cacheEnabled) {
                    setCachedContent(tabId, data);
                }
            },
            error: handleBindError
        };
        dojo.io.bind(bindArgs);
    }
    return false;                         
}

function changeTabContent(tabId, tgId, contentId, activeClass, inactiveClass, data) {
    if (!hasErrors(data)) {                   
        var contentDiv = dojo.byId(contentId);
        if (data.script != null) {
            eval(data.script);
        } 

        if (data.body.content) {            
            //set tab content
            contentDiv.innerHTML = data.body.content;
            //change the tab state
            changeTabState(tabId, tgId, activeClass, inactiveClass);
        }
    }    
}

function changeTabState(tabId, tgId, activeClass, inactiveClass) {
    var tg = getTabGroup(tgId);
    
    //Unselect previous tab
    dojo.byId(tg.selectedTabId).className = inactiveClass;                

    //set new selection and change to active
    tg.selectedTabId = tabId;
    dojo.byId(tabId).className = activeClass;
}

function cacheCurrentTabContent(tabId, contentId) {
    if (tabId != null) {
        var tabMsg = getCachedContent(tabId);
        if (tabMsg != null) {
            tabMsg.body.content = dojo.byId(contentId).innerHTML
        } else {
            //no existing message (first tab is not renderd via ajax request) so create a basic one
            setCachedContent(tabId, createSimpleTabMsg(dojo.byId(contentId).innerHTML));
        }
    }
}

function getTabGroup(tgId) {
    return eval(tgId);
}

function createSimpleTabMsg(content) {
    var msg = { 
        head : { 
            status : "200" 
        }, 
        body : { 
            content : content
        }
    };
    return msg;
}

/*****************************************************************************
 * Utility Functions
 ****************************************************************************/
function getCachedContent(cacheKey) {
    var item = wcontrolsCache.item(cacheKey);
    if (item != null) {
        return item.value;
    } else {
        return null;
    }
}

function setCachedContent(cacheKey, data) {
    wcontrolsCache.add(cacheKey, data);
}

function hasErrors(data) {
    var result = false;
    try {        
        if (data != null) {        
            var status = parseInt(data.head.status)
            switch (status) {
                case 200:
                    result = false;
                    break;
                //3XX codes handle redirects
                case 301: //Moved Permanently
                case 302: //Found
                case 303: //See Other
                case 304: //Not Modified
                case 305: //Use Proxy
                case 306: //Unused
                case 307: //Temporary Redirect
                    if (data.head.data.url) {
                        document.location.href = data.head.data.url;
                    }
                    result = true;
                    break;
                default:
                    if (status >= 400 && status < 600) {
                        result = true;
                        break;
                    }
                    //we don't know how to interpret the status
            }        
        } else {
            //no data to read, error
            result = true;
        }    
    } catch(error) {
      //do nothing  
    }
    return result;
}

function handleBindError(type, data) {
    //do nothing
}
