﻿// JScript File
/*
    Knowledge js Base Code
    Author: Carl Ashby
    Date: 1/31/2008
    
*/
	
function hideLoadingGIF(){
    jQuery('.kb_Load').removeClass('kb_Load_Show');
    jQuery('.kb_Load').addClass('kb_Load_Hide');
    return true;
}

function showLoadingGIF(){
    jQuery('.kb_Load').removeClass('kb_Load_Hide');
    jQuery('.kb_Load').addClass('kb_Load_Show');
    return true;
}

function getBreadcrumbPath(taxNodeId){
    jQuery.ajax({
        type: "POST",
        url: "UserControls/Callback.ashx",
        data: "taxNodeId=" + taxNodeId,
        success: function(ret){
            jQuery('.taxBreadcrumb').fadeOut(300,function(){
                jQuery('.taxBreadcrumb').html(ret).fadeIn(300);
            });
        }
    });
}

function formatSearchResultLink(){
    jQuery(".kbSearchGoBtn").unbind("click");
    jQuery(".kbSearchGoBtn").click(function(){
        var pageNumber = this.id;
        var searchText = jQuery(".kbSearchInput").val();
        var searchTax = jQuery(".hdnTaxValue").val();
        showLoadingGIF();
        doAjaxSearch(searchText,searchTax,pageNumber);
        return false;
    });
}

function doAjaxSearch(searchText,searchTax,pageNumber){
    jQuery.ajax({
        type: "POST",
        url: "UserControls/Callback.ashx",
        data: "kbSearchText=" + searchText + "&kbSearchNode=" + searchTax + "&kbPageNumber=" + pageNumber,
        success: function(ret){
            jQuery('.kbSearchResults').fadeOut(300,function(){
                jQuery('.kbSearchResults').html(ret).fadeIn(300, function(){
                    formatSearchResultLink();
                });
                
            },hideLoadingGIF());
        }
    });
    
    //Set Cookie
    var ek_kb_searchParams = new Array();
    ek_kb_searchParams[0] = escape(searchText);
    ek_kb_searchParams[1] = searchTax;
    ek_kb_searchParams[2] = pageNumber;
    
    storedCookieData = getEKKBCookie('ek_kb_search');
    if (storedCookieData!=null && storedCookieData!=""){
        setEKKBCookie('ek_kb_search',ek_kb_searchParams,1);    
    }else{
        deleteEKKBCookie('ek_kb_search');
        setEKKBCookie('ek_kb_search',ek_kb_searchParams,1);
    }
}

function getEKKBCookie(cookieName){
    if (document.cookie.length>0){
        clientCookieStart=document.cookie.indexOf(cookieName + "=");
        if (clientCookieStart!=-1){ 
            clientCookieStart=clientCookieStart + cookieName.length+1; 
            clientCookieEnd=document.cookie.indexOf(";",clientCookieStart);
            if (clientCookieEnd==-1) clientCookieEnd=document.cookie.length;
                return unescape(document.cookie.substring(clientCookieStart,clientCookieEnd));
        } 
    }
    return "";
}

function setEKKBCookie(cookieName,value,expDays){
    var expdate=new Date();
    expdate.setDate(expdate.getDate()+expDays);
    document.cookie=cookieName+ "=" +escape(value)+((expDays==null) ? "" : "; expires="+expdate.toDateString());
}  

function deleteEKKBCookie(cookieName){
    document.cookie = cookieName + "=; expires=Fri, 10 Jan 1982 04:12:24 GMT;";
}    

function getBreadcrumbPath(taxNodeId){
    jQuery.ajax({
        type: "POST",
        url: "UserControls/Callback.ashx",
        data: "taxNodeId=" + taxNodeId,
        success: function(ret){
            jQuery('.taxBreadcrumb').fadeOut(300,function(){
                jQuery('.taxBreadcrumb').html(ret).fadeIn(300);
            });
        }
    });
}

function opento(path){
    pathpieces = path.split(",");
    thisone = pathpieces.shift();
    if(thisone != jsrootnode){
        jQuery("li#"+thisone+" > div.hitarea").click();
    }
    path = pathpieces.join(",");
    if(pathpieces.length > 0) whenavail("li#"+pathpieces[0]+ " > div.hitarea", path);
}

function whenavail(item, path){
    if(jQuery(item).length > 0){
        opento(path);
    }else{
        window.setTimeout("whenavail('"+item+"','"+path+"')",100);
    }
}

function toggleDisplay(id){
    var objElem = document.getElementById(id);
    if (objElem)
    {
	    var objStyle = objElem.style;
	    objStyle.display = (objStyle.display != 'block' ? 'block' : 'none');
    }
}

function setUpSearchTab(){
    //Comes from search tab and tax search nodes.
    jQuery(".kbSearch").click(function(){
        showLoadingGIF();
        jQuery(".holder").fadeOut(500,function(){
            jQuery(".kbSearchContainer").fadeIn(500);
            hideLoadingGIF();
        });
        
        //Prepare for search
        jQuery('.hdnTaxValue').val(jQuery(this).attr('href'));
        getBreadcrumbPath(jQuery(this).attr('href'));
        jQuery('.kbSearchResults').html('');
    });
}

function tryReloadSearch(){
    //Check for search Cookie and reload search
    var ek_kb_searchParams = new Array();
    if(getEKKBCookie('ek_kb_search').length>0){
        ek_kb_searchParams = getEKKBCookie('ek_kb_search').split(',');
        jQuery(".kbSearchInput").val(unescape(ek_kb_searchParams[0]));
        jQuery(".hdnTaxValue").val(ek_kb_searchParams[1]);
        jQuery(".holder").fadeOut(500,function(){
            jQuery(".kbSearchContainer").fadeIn(500);
            hideLoadingGIF();
        });
        doAjaxSearch(ek_kb_searchParams[0],ek_kb_searchParams[1],ek_kb_searchParams[2]);
    }
}

function setUpBrowseTab(){
    jQuery(".browseTab").click(function(){
        showLoadingGIF();
        jQuery(".kbSearchContainer").fadeOut(500, function(){
            jQuery(".holder").fadeIn(500);
            hideLoadingGIF();
        });
        deleteEKKBCookie('ek_kb_search');
    });
}

function setUpSearchClick(){
    jQuery(".kbSearchGoBtn").click(function(){
        var pageNumber = this.id;
        var searchText = jQuery(".kbSearchInput").val();
        var searchTax = jQuery(".hdnTaxValue").val();
        showLoadingGIF();
        doAjaxSearch(searchText,searchTax,pageNumber);
        return false;
    });
}

function setUpClearButton(){
    jQuery('#btnClearSearch').click(function(){
        deleteEKKBCookie('ek_kb_search');
        jQuery('.kbSearchInput').val('');
        return false;
    });
}
