﻿

Navigation = {

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    properties
    
*/ 
    //Variable which identifies nav type to load
    // Onix - 
    navType: null,
    
    //Variable used to count the numbers of attempts to retrieve reference to subnav holder
    loadCount: 0,
    
    //Variable to store current item
    item: null,
    
    //Variable to store nav items
    items: new Array(),
    
    //Variable to store current subitem
    subitem: null,
    
    //Variable to store nav items
    subitems: new Array(),
    


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    methods
    
*/ 

    isCompatible: function()
    {
        //If Dom Helper is not defined
        if(!DH
            || !DH.createAnchor){        
            return false;
        }
        
        if(!LOG
            || !LOG.printULwithH1){
            return false;
        }
        return true;    
    },

/*----------------------------------------------------------------------------------------------------------

    load
    
*/
    //Load main items
    loadMainNavItems: function()
    {
		this.items.push(new NavigationItem('/secondary31/site/divisions?open&cat=The Arts%3EArt&template=domSecondary','Arts','Art'));
		this.items.push(new NavigationItem('/secondary31/site/divisions?open&cat=Drama>General&template=domSecondary','Drama','Drama'));
        this.items.push(new NavigationItem('homepages/Commerce+and+Business?open&template=domSecondary','Business & Commerce','Commerce and Business'));
        this.items.push(new NavigationItem('homepages/English?open&template=domSecondary','English','English'));
        this.items.push(new NavigationItem('homepages/ELT?open&template=domSecondary','ELT','ELT'));
        this.items.push(new NavigationItem('/secondary31/site/divisions/?open&label=onix&view=subjects&type=cat&div=Secondary&cat=Food%20Technology&start=1&count=500','Food Technology','Food Technology'));
        this.items.push(new NavigationItem('homepages/Geography?open&template=domSecondary','Geography','Geography'));
        this.items.push(new NavigationItem('homepages/Health+and+PE?open&template=domSecondary','Health and PE','Health and PE'));
        this.items.push(new NavigationItem('homepages/History?open&template=domSecondary','History','History'));
        this.items.push(new NavigationItem('homepages/LOTE?open&template=domSecondary','LOTE','LOTE'));
        this.items.push(new NavigationItem('homepages/Mathematics?open&template=domSecondary','Mathematics','Mathematics'));
        this.items.push(new NavigationItem('homepages/Science?open&template=domSecondary','Science','Science'));
        this.items.push(new NavigationItem('/secondary31/site/divisions/?open&label=onix&view=areas&type=cat&div=Secondary&cat=Study+Guides>General&start=1&count=500','Study Guides','Study Guides'));
		this.items.push(new NavigationItem('homepages/Macmillan+Library?open&template=domSecondary','Macmillan Library','Macmillan Library'));
    },
    
    //Load sub nav items
    loadSubNavItems: function()
    {
        switch(this.subject)
        {
            case 'commerce and business':				
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>Accounting&template=domSecondary&start=1&count=20','Accounting','Accounting'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>business+studies?open&template=domSecondary','Business Studies','Business Studies'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>commerce?open&template=domSecondary','Commerce','Commerce'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>economics?open&template=domSecondary','Economics','Economics'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>industry+work+studies?open&template=domSecondary','Industry / Work Studies','Industry Work Studies'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>legal+studies?open&template=domSecondary','Legal Studies','Legal Studies'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>study+guides?open&template=domSecondary','Study Guides','Study Guides'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Commerce+and+Business>vcta?open&template=domSecondary','VCTA','VCTA'));
				break;
			case 'english':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>NSW&template=domSecondary','NSW','NSW'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Queensland&template=domSecondary','Queensland','Queensland'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Victoria&template=domSecondary','Victoria','Victoria'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>National&template=domSecondary','National','National'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Dictionaries&template=domSecondary','Dictionary','Dictionaries'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Drama&template=domSecondary','Drama','Drama'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Language and Grammar&template=domSecondary','Language / Grammar','Language and Grammar'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Poetry&template=domSecondary','Poetry','Poetry'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Shakespeare&template=domSecondary','Shakespeare','Shakespeare'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Short Stories&template=domSecondary','Short Stories','Short Stories'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=English>Study Guides&template=domSecondary','Study Guides','Study Guides'));
				break;
			case 'elt':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Australian+titles&template=domSecondary','Australian titles','Australian titles'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Business+and+Professional&template=domSecondary','Business and Professional','Business and Professional'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Course+books&template=domSecondary','Course books','Course books'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Dictionaries&template=domSecondary','Dictionaries','Dictionaries'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Exams&template=domSecondary','Exams','Exams'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>General&template=domSecondary','Macmillan ELT','General'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Macmillan+Readers&template=domSecondary','Macmillan Readers','Macmillan Readers'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Other+resources&template=domSecondary','Other resources','Other resources'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=ELT>Skills,+Grammar+and+Vocab&template=domSecondary','Skills, Grammar and Vocab','Skills, Grammar and Vocab'));
			    break;
			case 'geography':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>Atlas&template=domSecondary','Atlases','Atlas'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>NSW&template=domSecondary','NSW','NSW'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>Queensland&template=domSecondary','Queensland','Queensland'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>Victoria&template=domSecondary','Victoria','Victoria'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>National&template=domSecondary','National','National'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Geography>Study Guides&template=domSecondary','Study Guides','Study Guides'));
				break;
			case 'health and pe':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Health and PE>Queensland&template=domSecondary','Queensland','Queensland'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Health and PE>NSW&template=domSecondary','NSW','NSW'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Health and PE>Victoria&template=domSecondary','Victoria','Victoria'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Health and PE>National&template=domSecondary','National','National'));
				break;
			case 'history':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=History>NSW&template=domSecondary','NSW','NSW'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=History>Victoria&template=domSecondary','VIC','Victoria'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=History>National&template=domSecondary','National','National'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=History>Study Guides&template=domSecondary','Study Guides','Study Guides'));
				break;
			case 'mathematics':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>CAS&template=domSecondary','CAS','CAS'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>NSW&template=domSecondary','NSW','NSW'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>Queensland&template=domSecondary','Queensland','Queensland'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>Victoria&template=domSecondary','Victoria','Victoria'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>National&template=domSecondary','National','National'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>Study Guides&template=domSecondary','Study Guides','Study Guides'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Mathematics>Homework&template=domSecondary','Homework','Homework'));	
				break;
			case 'lote':
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=LOTE>Japanese&template=domSecondary','Japanese','Japanese'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=LOTE>Indonesian&template=domSecondary','Indonesian','Indonesian'));
				break;
			case 'science':
				//this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Senior General Science&template=domSecondary','Senior General Science'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Queensland&template=domSecondary','Queensland','Queensland'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>NSW&template=domSecondary','NSW','NSW'));
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Victoria&template=domSecondary','Victoria','Victoria'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Biology&template=domSecondary','Biology','Biology'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>National&template=domSecondary','National','National'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Psychology&template=domSecondary','Psychology','Psychology'));	
				this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Science>Study Guides&template=domSecondary','Study Guides','Study Guides'));	
				break;
			case 'macmillan library':
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>Humanities&template=domSecondary','Humanities','Humanities'));
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>Science&template=domSecondary','Science','Science'));
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>Health&template=domSecondary','Health','Health'));
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>General Hardback Library&template=domSecondary','General Hardback Library','General Hardback Library'));
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>Fiction&template=domSecondary','Fiction','Fiction'));
			    this.subitems.push(new NavigationItem('/secondary31/site/divisions?open&cat=Macmillan Library>Reference&template=domSecondary','Reference','Reference'));
			    break;	
            default:
        }
    },

/*----------------------------------------------------------------------------------------------------------

    attach
    
*/

    attach: function()
    {
    
        try{//attempt to get reference to subnavigation module
            var oHolder = document.getElementById('subNavModule');
        } catch(oError){//if navigation module has not loaded
                            if(this.loadCount < 10)//if there have been less than 10 attempts
                            {
                                this.loadCount++;
                                setTimeout('Navigation.attach()',100);
                                return;
                            } else {//if attempts have been exceeded   
                                        throw new Error('Navigation.attach() - element \'subNavModule\' not defined');
                                   }
                       }
       
        //create UL element to hold list of links              
        var oUl = document.createElement('ul');
        oUl.setAttribute('id','subNav');
        
        //Loop through nav items and attach them to ul Holder
        for(var i = 0; i < this.items.length; i++)
        {
            var oLi = document.createElement('li');
            
            //Array to store class for list item
            var classArray = new Array();
            classArray.push('main');
            if(i == this.items.length - 1)classArray.push('last');
            oLi.className = classArray.join(' ');
            
            //create anchor tag and attach it to list item
            var oAnchor = this.items[i].attach();
            //if current attach class name
            if(this.isMainItemSubCatCurrent(this.items[i].subcat))oAnchor.className = 'current';
            
            oLi.appendChild(oAnchor);
            
            //If it is the current link attach subitems
            //if(this.subject && this.subject == this.items[i].title.toLowerCase())
            if(this.isMainItemSubCatCurrent(this.items[i].subcat))
            {
                this.attachSubitems(oLi);
            }
            
            //Check if there are categories and add them
            oUl.appendChild(oLi);
        }  
        
        oHolder.appendChild(oUl); 
    }, 
    
    isMainItemSubCatCurrent: function(subcat)
    {
        switch(this.subject)
        {
            case 'the arts':
            case 'reference':
                if(this.category)
                {
                    if(this.category == subcat.toLowerCase())
                    {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
                break;
            case null:
                break;
            default:
                if(this.subject == subcat.toLowerCase())
                {
                    return true;
                } else {
                    return false;
                }
        }
    },
    
    attachSubitems: function(obj)
    {
		if(this.subitems.length == 0) return;
	
        var ul = document.createElement('ul');
        
        for(var i = 0 ; i < this.subitems.length; i++)
        {
            
           
            //create li tag
            var li = document.createElement('li');
            
            //Create anchor tag and attach it to list item
            var oAnchor = this.subitems[i].attach();
            //If it is the current link attach class name
            if(this.category && this.category == this.subitems[i].subcat.toLowerCase())
            {
                oAnchor.className = 'current';
            }
                
            li.appendChild(oAnchor);
            
            //attach li to ul
            ul.appendChild(li);
        }
        obj.appendChild(ul);        
    },  
    
    printVariables: function()
    {
        var vars = new Array();
        
        //Add variables to array
        if(this.navType)vars.push('navType: ' + this.navType);
        if(this.subject)vars.push('subject: ' + this.subject);
        if(this.category)vars.push('category: ' + this.category);
        
        if(Secondary._LOG_ENABLED)LOG.printULwithH1('Secondary Navigation Variables',vars);
    },
    

/*----------------------------------------------------------------------------------------------------------

    init
    
*/   
    init: function ()
    {  
        //Check whether Navigation is compatible
        if(!this.isCompatible) throw new Error('Navigation is not compatible');
        
        //If there are no arguments
        if(arguments.length == 0)throw new Error('Navigation.init() expects at least on argument');
        
        //Set navigation type
        this.navType = arguments[0];
        
        
        //If item exists make sure it's valid
        if(typeof arguments[1] != 'undefined' && typeof arguments[1] != 'string')
        {
            //throw new Error('Navigation.init() expects argument \'item\' to be of \'string\' type');
            return false;
        }
        
        //If subitem exists make sure it's valid
        if(typeof arguments[2] != 'undefined' && typeof arguments[2] != 'string')
        {
            //throw new Error('Navigation.init() expects argument \'subitem\' to be of \'string\' type');
            return false;
        }
        
       
        
        switch(this.navType)
        {
            case 'main'://main pages
            case 'onix'://book index page
             
                //Get subject - to be compared to item later
                this.subject = (typeof arguments[1] != 'undefined') ? arguments[1].replace(/\+/gi,' ').toLowerCase() : null;
                //Get category - to be compared to subitem later
                this.category = (typeof arguments[2] != 'undefined') ? arguments[2].replace(/\+/gi,' ').toLowerCase() : null;
                
                //Load main nav items into 'items' array
                this.loadMainNavItems();
                
                //load 'subitems'                
                if(this.subject)this.loadSubNavItems();                     
                       
                break;                
            default:
              
        }
        
        this.printVariables();
        
    }


}

function NavigationItem(href,title,subcat)
{
    this.title = title;
    this.href = href;
    this.subcat = subcat;
    
    if(typeof NavigationItem._initialized == 'undefined')
    {
        NavigationItem.prototype.attach = function()
        {
            return DH.createAnchor(this.title,this.href,'','',this.title);
        };
    }
    NavigationItem._initialized = true;
}

