﻿/************************************************************** 
    EXTENDED UI FUNCTIONS
    Date: 08/12/2009
 **************************************************************/

function initScrollableNavWithSideTabs() {

    function resetNavWithTab() {
        
        $(this)
            .find('div.sideTabsPanel')
                .hide()
            .end()
            .find('div.sideTabsPanel:first')
                .show()
            .end()
            .find('div.sideTabs ul li')
                .removeClass('selected')
            .end()
            .find('div.sideTabs ul li:first')
                .addClass('selected')
            .end()
            .hide();
        
    }
    
    //highlight selected side tab
    function highlightSideTab() {
        $('#innerContentWrapper .sideTabs a[href$=#' + $(this).attr('id') + ']')
            .parent('li')
                .addClass('selected')
                .siblings()
                    .removeClass('selected');
    };
     
    function setNavWithTab(locHash, isInitialPageLoad) {

        $('#innerContentWrapper div.sideTabsWrapper').each(resetNavWithTab);
        
        //Hash can be category (Gene Reg) or sub-category (Focused Geno). Default display is first category.
        var defaultToDisplay = (locHash.length>1) ? $(locHash) : $('div.sideTabsWrapper:first');
        var selectedTabWrapper, selectedTabPanel;
        if (defaultToDisplay.hasClass('.sideTabsWrapper')) { //tab wrapper

            selectedTabWrapper = defaultToDisplay;
            selectedTabPanel = defaultToDisplay.find('div.sideTabsPanel:first');
            selectedTabPanel.show().addClass('selected') //show the first child
        }
        else { //tab panel                

            selectedTabPanel = defaultToDisplay;
            selectedTabWrapper = defaultToDisplay.parent().parent('div.sideTabsWrapper');
            selectedTabWrapper.show().addClass('selected') //show the parent
            //defaultToDisplay.find('.toggleChild').show().end().find('.toggleParent').hide(); //auto show for hash
        }
        defaultToDisplay.show().addClass('selected').siblings('div.sideTabsPanel').hide();                    

        $('#innerContentWrapper .items a[href$=#' + selectedTabWrapper.attr('id') + ']')
            .parent('div')
                .siblings('div')
                    .removeClass('active')
                .end()
            .addClass('active');
            
        highlightSideTab.call(selectedTabPanel);
        if (locHash.length>1 && isInitialPageLoad)
            $.scrollTo('div.scrollable',{duration:400});
            
        return false;
    }

    setNavWithTab(location.hash, true);
    
    //triggers
    var target;  
                                
    //$('#innerContentWrapper a[href*=applications.ilmn#]').click(function() {
    $('#innerContentWrapper a[href*=#]').click(function() {
        var hashVal = $(this).attr("href").split('#')[1];
        if ($('.sideTabs a[href*=#' + hashVal + ']').length ||
            $('.items a[href*=#' + hashVal + ']').length) {
            return setNavWithTab("#" + hashVal, false);
        }                
    });
    
    setTimeout("applicationsMainNavFix()",500); //delay because of dependency on top nav

}

function applicationsMainNavFix() {
    //if clicking app link from main nav, make sure app page gets updated since browsers auto cache
    $('#mainNav .panelSectionContent a[href*=applications.ilmn#]').click(function() {
        location.href = $(this).attr('href');
        location.reload();
    });
}

$.fn.ilmnTableRowToggle = function(){
    
    $(this).find("tr:odd").addClass("odd").hide();       
    $(this).find("tr th:not(.odd)").click(function(){
        var target = $(this).parent().next("tr"); //jQuery toggle issues for table rows in IE8
        if (target.hasClass("shown"))
            target.hide().removeClass("shown");
        else
            target.show().addClass("shown");
    });
    
}

function initFilter() {

    var allResults = $('#results tbody tr');
    var catDropDown = $('#categoryID');
    var subCatDropDowns = $('#formFilter select.subCat');
    var catval;
    
    catDropDown.change(function(){
        catval = $(this).val();
        if (catval != ''){
 	        subCatDropDowns.hide();
	        $('#formFilter select#sub' + catval).show();
        }
    });
    	
    subCatDropDowns.change(function(){
        catval = $(this).val();
        if (catval != ''){
	        allResults.hide();
	        $('#results tbody tr.' + catval).show();
        } else {
	        allResults.show();
        }
    });

    if (location.hash.length>1) {
        var hashSplit = location.hash.split(':');
	    var catID = 'cat' + hashSplit[1];
    	
	    var subCatDropDown = $('select option[value=' + catID + ']').parent();
	    if (subCatDropDown.length) {
	        catDropDown.val(subCatDropDown.attr('id').replace('sub',''));  //set to cat
	        subCatDropDown                                                 //set to sub and hide others
	            .val(catID)
	            .show()
	            .change()
	                .siblings('.subCat').hide();
	    }
    }

}
