﻿

Secondary = {

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    properties
    
*/ 
    section: null,
    subsection: null,
    pageType: null,
    subject: null,
    category: null,
    
	//Variable which enables the log
	
    //_LOG_ENABLED: (EDDESK.getFormInput('DocID') && (EDDESK.getFormInput('DocID') == '8CC36E685372FC4CCA25754D0013A6E3')) ? true : null,
    _LOG_ENABLED: (DH.getURLParam('log')) ? true : null,
    

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    methods
    
*/ 

    isCompatible: function()
    {
        if(!DH){
            return false;
        }
        
        if(!EDDESK
            || !EDDESK.getFormInput){
            return false;    
        }
        
        if(!LOG
            || !LOG.print
            || !LOG.printH1){
            return false;
        }
        
        return true; 
    },  
/*----------------------------------------------------------------------------------------------------------

    functions for edDesk form
    
 
    validFormInput: function(str)//function which checks whether an edDesk form input is valid
    {
        var item = null;
        
        try {//check whether the input exists and get a reference to it
                item = eval('document.forms[0].' + str);
        } catch (oError) {return null; }
       
        //if input is valid
        if(!item || item.value.length == 0 || item.value == ' ')return null;
        
        return true;      
    },
    
    getFormInput: function(str)//function which retrieces edDesk Form Input
    {
        //NOTE: use function validFormInput() to validate input first
        return unescape(eval('document.forms[0].' + str + '.value'));
    },
*/       
    printFormVariables: function()
    {
        var vars = new Array();
        
        //Add variables to array
        if(this.section)vars.push('section: ' + this.section);
        if(this.subsection)vars.push('subsection: ' + this.subsection);
        if(this.pageType)vars.push('pageType: ' + this.pageType);
        if(this.subject)vars.push('subject: ' + this.subject);
        if(this.category)vars.push('category: ' + this.category);
		if(this.DocID)vars.push('DocID: ' + this.DocID);
        
        //Create heading for LOG window
        var h = document.createElement('h1');
        h.appendChild(document.createTextNode('Secondary DOM Form variables'));
        var li = document.createElement('li');
        li.appendChild(h);
        
        //Create element to hold variable list
        var ul = document.createElement('ul');    
        //attach heading
        ul.appendChild(li);
        
        //Loop through variable list and add items    
        for(var i = 0; i < vars.length; i++)
        {
            var li = document.createElement('li');
            li.appendChild(document.createTextNode(vars[i]));
            ul.appendChild(li);
        }
        
        //Print list into LOG window        
        if(this._LOG_ENABLED)LOG.print(ul);
        
    },

/*----------------------------------------------------------------------------------------------------------

    urlvars
    
*/

    getSubjectAndCategory: function()
    {
		/*
        var winLoc = (window.location.toString()).split('?')[0];
        
        winLoc = winLoc.split('/');
        
        var index = 0;
        for(var i = 0; i < winLoc.length; i++)
        {
            if(winLoc[i] == 'divisions')index = i;
        }
        
        if(index == 0)throw new Error('Secondary.getSubjectAndCategory() expects the string \'divisions\' in url string');
        
        var oRe = /_/gi;
        this.subject = winLoc[index + 1].replace(oRe,' ');
        this.category = winLoc[index + 2].replace(oRe,' ');
		*/
		
		var params = (DH.getURLParam('cat')) ? DH.getURLParam('cat').split('>') : null;
		
		if(params instanceof Array)
		{
			this.subject = params[0];
			if(params.length == 2)
			{
				this.category = params[1];
			}
			
			/*
			//Check if it is the Study Guide section
			if((this.subject && this.category)
			    && (this.subject == 'reference' && this.category == 'study guides'))
			{
			    this.subject = 'Study Guides';
			    this.category = null;
			}
			*/
		}
		
    },

 
/*----------------------------------------------------------------------------------------------------------

    page
    
*/

    loadPage: function()
    {
        switch(this.pageType)
        {
            case 'home':
                this.loadPageHome();
                break;
            case 'article':
                this.loadPageArticle();
                break;
            case 'library':
                this.loadPageLibrary();
                break;    
            case 'Error'://for 404 pages
                this.loadPageError();
                break;
			case 'onixProduct':
				this.loadPageOnixProduct();
				break;
            case null:
                throw new Error('Secondary.loadPage() - pageType is not set');
                break;
            default:
            	this.loadPageLibrary();       
            	 }
        
        //If not site's main home page parse contentRight
        if(!this.section || this.section != 'Home')
        {
            this.loadPageContentRight();
        }
    },
    
    loadPageContentRight: function()
    {
        if(!loadPageContentRightCount)var loadPageContentRightCount = 0;
		
		//Get reference to holder element
		var oParent = document.getElementById('contentRight');
		if(!oParent)
		{
			//If exceeded the number of determined iterations
			if(loadPageContentRightCount > 10)
			{
				throw new Error('Secondary.loadPageContentRight() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageContentRightCount++;
			setTimeout('Secondary.loadPageContentRight()',100);
			return;
		}	
		
		if(Cart)Cart.attachMiniCart(oParent);
    },
    
    loadPageHome: function()
    {
		switch(this.section)
		{
			case 'Home':
        		this.loadSiteHomePage();
				break;
			default:
				this.loadPageHomeSubjects();
		}
	},
	
//Function to attach home page elements for subjects	
	loadPageHomeSubjects: function()
	{
		if(!loadPageHomeSubjectsCount)var loadPageHomeSubjectsCount = 0;
		
		//Get reference to holder element
		var oParent = document.getElementById('contentLeft');
		var oSibling = document.getElementById('edDeskBody');
		if(!oParent || ! oSibling)
		{
			//If exceeded the number of determined iterations
			if(loadPageHomeSubjectsCount > 10)
			{
				throw new Error('Secondary.loadPageHomeSubjects() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageHomeSubjectsCount++;
			setTimeout('Secondary.loadPageHomeSubjects()',100);
			return;
		}	
		
		
        Navigation.init('main',this.section);
        Navigation.attach();
		
		//Attach strap
		oParent.insertBefore(this.attachSubjectStrap(),oSibling);
		
	},

//Function to attach body of main home page	
	loadSiteHomePage: function()
	{
	    //START - Code to get HTML element reference
	    if(typeof loadSiteHomePageCount == 'undefined')loadSiteHomePageCount = 0;
		
		var oHolder = document.getElementById('wrapper');
		var  mainNavModule = document.getElementById('mainNavModule');
		if(!oHolder || !mainNavModule)
		{
			//If exceeded the number of determined iterations
			if(loadSiteHomePageCount > 10)
			{
				throw new Error('Secondary.loadSiteHomePage() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadSiteHomePageCount++;
			setTimeout('Secondary.loadSiteHomePage()',100);
			return;
		}
		//END - Code to get HTML element reference
		
		//Set class of wrapper tag to siteHome
		oHolder.className = 'siteHome';
		
		//Attach class to body tag
		//document.getElementsByTagName('body')[0].style.backgroundImage = 'url(templates/site_home/$file/siteHomeBodyBkg.jpg)';
		document.getElementsByTagName('body')[0].className = 'bodySiteHome';
		
		//Attach spacing element to mainNavModule
		var oDiv = DH.createDiv('','spacingDiv');
		mainNavModule.insertBefore(oDiv,mainNavModule.firstChild);
		
		//Attach navigation
        Navigation.init('main',this.section);
        Navigation.attach();
        
        //Initialize features
        HomePageFeatures.init();
	},
	
	loadPageArticle: function()
	{
	    switch(this.section)
	    {
	        case 'Teacher Support':
	            this.loadPageArticleTeacherSupport();
	            break;   
	        default:
	            this.loadPageArticleDefault();    
	    }
	},
	
	loadPageArticleDefault: function()
	{
		if(typeof loadPageArticleCount == 'undefined')loadPageArticleCount = 0;
		
		//Get reference to holder element
		var oParent = document.getElementById('contentLeft');
		
		if(!oParent)
		{
			//If exceeded the number of determined iterations
			if(loadPageArticleCount > 10)
			{
				throw new Error('Secondary.loadPageArticle() exceeded its limit of iterations');
			}
			 
			//Try again in 100 miliseconds
			loadPageArticleCount++;
			setTimeout('Secondary.loadPageArticle()',100);
			return;
		}	
		
    	//Attach introduction heading
		var oChild = document.getElementById('edDeskBody');
		if (this.DocID && this.DocID == '951725866ADC945FCA2575830018EFA5'){
		oChild.style.width = '664px';	
		}
		else {
		oChild.style.width = '601px';
	  	oChild.style.marginLeft = '45px';
		oChild.style.paddingTop = '20px';
		}
		//alert(oChild.offsetWidth);
		var h3 = document.createElement('h3');
		h3.setAttribute('class','article');
		h3.style.margin = '0';
		h3.appendChild(document.createTextNode(unescape(document.forms[0].Headline.value)));
		oChild.insertBefore(h3,oChild.firstChild);

		
		//this.attachSubjectCategoryStrap();	
		if (this.pageType == 'message') {
	   		Navigation.init('main','home');
	   	}
	   	else {
	   		Navigation.init('main',this.section);	   	
		}	
		Navigation.attach();		
		
	},
	
	loadPageArticleTeacherSupport: function()
	{
	    if(typeof loadPageArticleTeacherSupportCount == 'undefined')loadPageArticleTeacherSupportCount = 0;
		
		//Get reference to holder element
		var oParent = document.getElementById('contentLeft');
		var oChild = document.getElementById('edDeskBody');
		if(!oParent || !oChild)
		{
			//If exceeded the number of determined iterations
			if(loadPageArticleTeacherSupportCount > 10)
			{
				throw new Error('Secondary.loadPageArticleTeacherSupport() exceeded its limit of iterations');
			}
			
			 
			//Try again in 100 miliseconds
			loadPageArticleTeacherSupportCount++;
			setTimeout('Secondary.loadPageArticleTeacherSupport()',100);
			return;
		}			
		
		oChild.className = 'teacherSupportTemplate';
		oChild.style.paddingLeft = '45px';
		oChild.style.paddingTop = '20px';
		oChild.style.marginTop = '20px';
		oChild.style.borderTop = '1px solid #DDDEDF';
		
		//Attach introduction heading
		var h3 = document.createElement('h3');
		h3.style.margin = '0';
		h3.appendChild(document.createTextNode('Introduction'));
		oChild.insertBefore(h3,oChild.firstChild);
		
		//Attach strap
		this.attachSubjectCategoryStrap();
		
		//Attach navigation
		Navigation.init('main',this.subject);
		Navigation.attach();
		
		//Get isbn
		if((EDDESK.getFormInput('MetaKeywords')) 
		    && (EDDESK.getFormInput('MetaKeywords').length == 13))
		{
		    var isbn = EDDESK.getFormInput('MetaKeywords');
		    if(this._LOG_ENABLED)LOG.printH1('Secondary.loadPageArticleTeacherSupport() - isbn from EDDESK FORM:  [' + isbn + ']');
		}
		
		//If isbn exists
		if(isbn)
		{
		    //Search for item details
		    var itemSearch = new OnixItemSearch(isbn,'TeacherSupportItemSearch');
		    //If item loaded
		    if(itemSearch.loaded)
		    {
		        var item = new OnixItem('series',itemSearch.tsIndex[0]);
		        item.attachOnixProductTeacherSupport(oParent,oChild);
		    }
        }
        
        TeacherSupportTable.init(oChild);
	},
    
    loadPageLibrary: function()
    {
        switch(this.subsection)
        {
            case 'Onix':
                this.loadPageLibraryOnix();    
                break;
		    case 'Forms':
		         this.loadPageArticleDefault();
		        break;               
   		    case 'Info':
		         this.loadPageArticleDefault();
		        break;               
			case 'Search':
				this.loadPageLibrarySearch();
				break;
		    case 'Teacher Support':
		        this.loadPageLibraryTeacherSupport();
		        break;
		    case 'Checkout':
		        var isOverviewPage = (this.DocID && this.DocID == '514829CC98A98DA3CA257557001A4C52') ? true : null;
		        if(isOverviewPage)
		        {
		            this.loadPageLibraryCheckoutOverview();
		        } else {
		            this.loadPageLibraryCheckout();
		        }
		        break;
            default:
	            this.loadPageArticleDefault();
        }
    },
    
    loadPageLibraryCheckoutOverview: function()
	{
		if(typeof loadPageLibraryCheckoutOverviewCount == 'undefined')loadPageLibraryCheckoutOverviewCount = 0;
		
		var oParent = document.getElementById('contentLeft');
		var oChild = document.getElementById('edDeskBody');
		if(!oParent || !oChild)
		{
			//If exceeded the number of determined iterations
			if(loadPageLibraryCheckoutOverviewCount > 10)
			{
				throw new Error('Secondary.loadPageLibraryCheckout() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageLibraryCheckoutOverviewCount++;
			setTimeout('Secondary.loadPageLibraryCheckout()',100);
			return;
		}
		
		oParent.className = 'checkout';
		
		if(this.articleTitle)
		{
		    var h2 = document.createElement('h2');
		    h2.appendChild(document.createTextNode(this.articleTitle));
		    oParent.insertBefore(h2,oChild);
		}
		
		//Attach navigation
		Navigation.init('main',this.section);
        Navigation.attach();
        
       //Create summary tag
       var div = document.createElement('div');
       div.setAttribute('id','cartSummary');
       
       //Attach summary
       if(Cart)Cart.attachSummary(div);
       
       /*
       //Attach buttons
       var fieldset = document.createElement('fieldset');
       
       //Attach continue shopping button
       var oInput = document.createElement('input');
       oInput.setAttribute('type','button');
       oInput.setAttribute('value','Continue shopping');
       oInput.setAttribute('tabindex','1');
       oInput.className = 'button';
       var ContinueShoppingBtn_onClick = function(oEvent)
       {
            var target = DH.stopEvent(oEvent);
            history.go(-1);
       };
       DH.addEvent(oInput,'click',ContinueShoppingBtn_onClick);
       fieldset.appendChild(oInput);
       
       //Attach check out button
       if(Cart.hasItems())
       {
           var oInput = document.createElement('input');
           oInput.setAttribute('type','button');
           oInput.setAttribute('value','Checkout now');
           oInput.setAttribute('tabindex','2');
           oInput.className = 'button';
           var CheckoutNowBtn_onClick = function()
           {
                window.location = '/secondary31/site/articleIDs/8BBB301D7E16233BCA257555001631ED?open&template=domSecondary';
           };
           DH.addEvent(oInput,'click',CheckoutNowBtn_onClick);
           fieldset.appendChild(oInput);
       }
       
       //Attach fieldset
       div.appendChild(fieldset);
       */
       oChild.appendChild(div);
		
	},
	
	loadPageLibraryCheckout: function()
	{
		if(typeof loadPageLibraryCheckoutCount == 'undefined')loadPageLibraryCheckoutCount = 0;
		
		var oParent = document.getElementById('contentLeft');
		var oChild = document.getElementById('edDeskBody');
		if(!oParent || !oChild)
		{
			//If exceeded the number of determined iterations
			if(loadPageLibraryCheckoutCount > 10)
			{
				throw new Error('Secondary.loadPageLibraryCheckout() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageLibraryCheckoutCount++;
			setTimeout('Secondary.loadPageLibraryCheckout()',100);
			return;
		}
		
		oParent.className = 'checkout';
		/*
		if(this.articleTitle)
		{
		    var h2 = document.createElement('h2');
		    h2.appendChild(document.createTextNode(this.articleTitle));
		    oParent.insertBefore(h2,oChild);
		}
		*/
		//Attach navigation
		Navigation.init('main',this.section);
        Navigation.attach();
       /*
       //Attach step holder
       var div = document.createElement('div');
       div.setAttribute('id','checkoutStep');
       oChild.appendChild(div);
       */
       if(Checkout)
       {
            Checkout.init();
            Checkout.attach();
       }
	   
	},
	
	
    
    loadPageLibraryTeacherSupport: function()
	{
		if(typeof loadPageLibraryTeacherSupportCount == 'undefined')loadPageLibraryTeacherSupportCount = 0;
		
		var oParent = document.getElementById('contentLeft');
		var oChild = document.getElementById('edDeskBody');
		if(!oParent || !oChild)
		{
			//If exceeded the number of determined iterations
			if(loadPageLibraryTeacherSupportCount > 10)
			{
				throw new Error('Secondary.loadPageLibraryTeacherSupport() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageLibraryTeacherSupportCount++;
			setTimeout('Secondary.loadPageLibraryTeacherSupport()',100);
			return;
		}
		
		var imgPath = 'templates/category_straps/$file/teacher_support.jpg';
		var oTitle = 'Teacher Support';
		
		//Attach strap
		oParent.insertBefore(DH.createImg(imgPath,oTitle,null),oChild);
		
		//Attach navigation
		Navigation.init('main',this.section);
        Navigation.attach();
        
        switch(this.subject)
        {
            case 'business and commerce':
                var tsFilter = new TeacherSupportFilter(this.subject,this.category);
                tsFilter.loadAjax();
                if(tsFilter.loaded)
                {
                    tsFilter.attachIndex3(oChild);
                } else {
                    tsFilter.attachNoRecordsMessage(oChild,'There are no entries for this division.');
                }
            break;
            default:
                var tsFilter = new TeacherSupportFilter(this.subject,null);
                tsFilter.loadAjax();
                if(tsFilter.loaded)
                {
                    tsFilter.attachIndex2(oChild);
                } else {
                    tsFilter.attachNoRecordsMessage(oChild,'There are no entries for this subject.');
                }
		}
		
	},
	
	loadPageLibrarySearch: function()
	{
		if(typeof loadPageLibrarySearchCount == 'undefined')loadPageLibrarySearchCount = 0;
		
		var oHolder = document.getElementById('contentLeft');
		if(!oHolder)
		{
			//If exceeded the number of determined iterations
			if(loadPageLibrarySearchCount > 10)
			{
				throw new Error('Secondary.loadPageLibrarySearch() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageLibrarySearchCount++;
			setTimeout('Secondary.loadPageLibrarySearch()',100);
			return;
		}
		
		Navigation.init('main',this.section);
        Navigation.attach();
		
		//Create div to hold index
		var div = DH.createDiv('index2','');
		oHolder.appendChild(div);
				
		Search.init();
		Search.attachResults();
	},
	
	loadPageOnixProduct: function()
	{
		if(typeof loadPageOnixProductCount == 'undefined')loadPageOnixProductCount = 0;
		
		//Get reference to holder element
		var oParent = document.getElementById('contentLeft');
		
		if(!oParent)
		{
			//If exceeded the number of determined iterations
			if(loadPageOnixProductCount > 10)
			{
				throw new Error('Secondary.loadPageOnixProduct() exceeded its limit of iterations');
			}
			
			 
			//Try again in 100 miliseconds
			loadPageOnixProductCount++;
			setTimeout('Secondary.loadPageOnixProduct()',100);
			return;
		}	
		
		var oChild = document.getElementById('edDeskBody');
		//Add padding to edDeskBody
		//oChild.style.paddingLeft = '45px';
		
		//Get onix type
		var onixType = DH.getURLParam('onixtype');
		
		var detailsTable = document.getElementById('edDeskBody').getElementsByTagName('table')[0];
		detailsTable.setAttribute('id','onixProductTable');
		if(onixType)detailsTable.className = 'noDividerBorder';
		var detailsHolder0 = detailsTable.getElementsByTagName('td')[0];
		detailsHolder0.className = 'bookCover'; 
		var detailsHolder1 = detailsTable.getElementsByTagName('td')[1];
		detailsHolder1.className = 'divider'; 
		var detailsHolder2 = detailsTable.getElementsByTagName('td')[2];
		detailsHolder2.className = 'bookDetails';
		
		this.attachSubjectCategoryStrap();	
		
		if(!this.category)
		{
        	Navigation.init('onix',this.subject);
		} else {
			Navigation.init('onix',this.subject,this.category);
		}
		Navigation.attach();
		
		var item = new OnixItem('onixProduct');
		item.attachOnixProduct(oParent,oChild);	
		
		
		if(!onixType)
		{
		    item.attachOnixProductLinks();
		    item.attachOnixProductRelatedTitles(detailsHolder2);
		}
	},
    
	
	
    loadPageLibraryOnix: function()
    {
        if(!this.subject)throw new Error('Secondary.loadPageLibraryOnix() expects a subject type');
        //if(!this.category)throw new Error('Secondary.loadPageLibraryOnix() expects a category type');
        
		if(!this.category)
		{
        	Navigation.init('onix',this.subject);
		} else {
			Navigation.init('onix',this.subject,this.category);
		}
		Navigation.attach();
		
		//Attach strap
		this.attachSubjectCategoryStrap();		
		
		var subcat = (this.category) ? this.subject + '>' + this.category  : this.subject;
		
        var search = {
            view: (DH.getURLParam('view')) ? DH.getURLParam('view') : 'areas',
            start: (DH.getURLParam('start')) ? DH.getURLParam('start') : null,
            count: (DH.getURLParam('count')) ? DH.getURLParam('count') : null,
            type: 'cat',
            cat: subcat   
        }
        
		if(this.category)
		{
        	//Series.init(this.subject,this.category,search);
			Series.init(this.subject,this.category,search);
		} else {
			Series.init(this.subject,'',search);	
		}
		
        //If loaded successfully
        if(Series.loaded == true)
        {
			switch(this.subject)
			{
			/*
				case 'commerce and business':					
					Series.attachSeriesIndex2(document.getElementById('content'));
					break;
				case 'the arts':
					Series.attachSeriesIndex1(document.getElementById('content'));
					break;
				case 'english':
					switch(this.category)
					{
						case 'dictionaries':
						case 'drama':
						case 'language grammar':
							Series.attachSeriesIndex2(document.getElementById('content'));
							break;
						default:
							Series.attachSeriesIndex1(document.getElementById('content'));
					}
					break;
				case 'geography':
					switch(this.category)
					{
						case 'atlas':
							Series.attachSeriesIndex1(document.getElementById('content'));
							break;
						default:
							Series.attachSeriesIndex2(document.getElementById('content'));
					}
					break;
				case 'food technology':
					Series.attachSeriesIndex1(document.getElementById('content'));
					break;
			*/
				default:
					Series.attachSeriesIndex2(document.getElementById('content'));
			}
        } else {
			
			//Attach message to page
			this.loadPageMessage('There are 0 results for ' + this.category + ' - ' + this.subject);
			
            if(this._LOG_ENABLED)LOG.printH1('Secondary.loadPageLibraryOnix() - No results for ' + this.subject + '>' + this.category);
        }
    },
    
    loadPageError: function()
    {
        if(typeof pageErrorCount == 'undefined')pageErrorCount = 0;
        
        try{
            var oBody = document.getElementById('body');
        } catch(oError){
                            if(pageErrorCount < 10)
                            {
                                pageErrorCount++;
                                setTimeout('Secondary.loadPageError()',100);
                                return;
                            } else {
                                        throw new Error('Secondary.loadPageError() has exceeded its number of iterations');
                                   }
                       }
        oBody.style.paddingLeft = '159px';
    },
	
//Function which prints message on body of page
    loadPageMessage: function(message)// obj
    {
        if(typeof loadPageMessageCount == 'undefined')loadPageMessageCount = 0;
		
		//Get reference to holder element
		var oHolder = document.getElementById('contentLeft');
		if(!oHolder)
		{
			//If exceeded the number of determined iterations
			if(loadPageMessageCount > 10)
			{
				throw new Error('Secondary.loadPageMessage() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			loadPageMessageCount++;
			setTimeout('Secondary.loadPageMessage()',100);
			return;
		}
    	
		//Create message holder
		var div = DH.createDiv('','message');
		
		var p = DH.createP(message,'','');
		div.appendChild(p);
		oHolder.appendChild(div);
       
    },
//function to attach strap for subject>category pages
	attachSubjectCategoryStrap: function()
	{
	
		if(typeof attachSubjectCategoryStrapCount == 'undefined')attachSubjectCategoryStrapCount = 0;

		//Get reference to holder element
		var oParent = document.getElementById('contentLeft');
		var oChild = document.getElementById('edDeskBody');
		if(!oParent || !oChild)
		{
			//If exceeded the number of determined iterations
			if(attachSubjectCategoryStrapCount > 10)
			{
				throw new Error('Secondary.attachSubjectCategoryStrap() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			attachSubjectCategoryStrapCount++;
			setTimeout('Secondary.attachSubjectCategoryStrap()',100);
			return;
		}
			
	
		//Get image's file name	
		if(this.category)
		{
			var imgPath = 'templates/category_straps/$file/' + this.subject.replace(/\s|\+/gi,'_') + '___' + this.category.replace(/\s|\+/gi,'_') + '.jpg';
			var oTitle = this.subject + ' - ' + this.category;
			
		} else {
			var imgPath = 'templates/category_straps/$file/' + this.subject.replace(/\s|\+/gi,'_') + '.jpg';
			var oTitle = this.subject;
		}
		//Attach image
		oParent.insertBefore(DH.createImg(imgPath,oTitle,SubjectCategoryStrapError),oChild);
		
		
	},
	
	//function to attach strap for subject>category pages
	attachSubjectStrap: function()
	{
		//Get image's file name	
		var imgPath = 'homepages/' + this.section.replace(/\s/gi,'+') + '/$file/strap.jpg';
		//Attach image
		return DH.createImg(imgPath,this.section,null);		
	},
	
	getSubjectAndCategoryFromURL: function()
	{
	    var cat = DH.getURLParam('cat');
	    if(cat)
	    {
	        cat = cat.split('>');
	        this.subject = cat[0];
	        if(typeof cat[1] != 'undefined')
	        {
	            this.category = cat[1];
	        } else {
	            this.category = null;
	        }
	    } else {
	        this.subject = null;
	    }
	},
    
    
/*----------------------------------------------------------------------------------------------------------

    init
    
*/   
    init: function ()
    { 
        //check whether Secondary is compatible
        if(!this.isCompatible) throw new Error('Secondary is not compatible');
        
        //Load edDesk variables  
        if(EDDESK.getFormInput('Section'))this.section = EDDESK.getFormInput('Section');
        if(EDDESK.getFormInput('Subsection'))this.subsection = EDDESK.getFormInput('Subsection');
        if(EDDESK.getFormInput('PageType'))this.pageType = EDDESK.getFormInput('PageType');
		if(EDDESK.getFormInput('DocID'))this.DocID = EDDESK.getFormInput('DocID');
		if(EDDESK.getFormInput('Title'))this.articleTitle = EDDESK.getFormInput('Title');
        
        //If it is an Onix index page
        if((this.pageType && this.pageType == 'onixProduct') 
			|| (this.section && this.subsection && this.section == 'Library' && this.subsection == 'Onix'))
        {
            //Get subject and category
            this.getSubjectAndCategory();
        }
        
        //If it is a Teacher Support article page
        if((this.pageType && this.pageType == 'article')
            && (this.section && this.section == 'Teacher Support'))
        {
            this.subject = 'teacher support';
        }
        
       
        //If it is a teacher support index page
        if((this.section && this.section == 'Library')
            && (this.subsection && this.subsection == 'Teacher Support'))
        {
            this.getSubjectAndCategoryFromURL();
        }
        
        
    }
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

//Initiate cart
if(Cart)Cart.init();

Secondary.init();
Secondary.printFormVariables();
Secondary.loadPage();


/*---------------------------------------------------------------------------------------------------------------------------------------------------------------
*/


function SubjectCategoryStrapError()
{
	var originalPath = this.getAttribute('src');
	
	var pathSplit = this.getAttribute('src').split('/');
	var imgName = pathSplit[pathSplit.length - 1];
	
	if(imgName.indexOf('jpg') != -1)
	{
		var newName = imgName.split('___');
		this.src = (originalPath.replace(imgName,newName[0] + '.jpg'));
	}
}


