﻿
String.prototype.strip = function()
{
    return this.replace(/\W/gi,function(s){if(s==" "){return " ";}else{return "";}}).toLowerCase().replace(/\s/gi,'');
};


Array.prototype.exist = function(str)
{
    for(var i = 0; i < this.length; i++)
    {
       //alert(str.strip() + '\n' + this[i].strip());
       if(str.toString().strip() == this[i].toString().strip())return true; 
    }
    return false;
};

function buildInTheMagazineLink(strIssueVol)
{
   strIssueVol = (unescape(strIssueVol)).split(">");
   strVol = strIssueVol[0].substr(2);
   strIssue = parseInt(strIssueVol[1]);
   
   //alert(strIssueVol);

   switch(strIssue)
   {
      case 1:
         strIssue = "January";
         break;
      case 2:
         strIssue = "February";
         break;
      case 3:
         strIssue = "March";
         break;
      case 4:
         strIssue = "April";
         break;
      case 5:
         strIssue = "May";
         break;
      case 6:
         strIssue = "June";
         break;
      case 7:
         strIssue = "July";
         break;
      case 8:
         strIssue = "August";
         break;
      case 9:
         strIssue = "September";
         break;
      case 10:
         strIssue = "October";
         break;
      case 11:
         strIssue = "November";
         break;
      case 12:
         strIssue = "December";
         break;
      default:
         strIssue = "Month";
         break;
   }
   return (strIssue + " " + strVol);
}

function Navigation()
{
    
    this.sections = new Array();
    
    if(typeof Navigation._initialized == 'undefined')
    {
        Navigation.prototype.addSection = function(sec,title)
        {
            this.sections.push(new NavSection(sec,title));
        };        
        
        Navigation.prototype.printMainNav = function(curSec)
        {
            if(!this.sections)return;
            var secIndex = this.getMainNavSectionIndex(curSec);
            
            document.write('<div id="mainNav">');
            
            for(var i = 0; i < this.sections.length; i++)
            {                
                var secUrl = '/' + this.sections[i].name.toString().replace(/\s/gi,'_');
                if(this.sections[i].name == 'home')secUrl = '/';
                if(eval('this.secLink_' + this.sections[i].name.toString().strip()))
                {
                    secUrl = eval('this.secLink_' + this.sections[i].name.toString().strip());
                }
                if(this.notOnMainNav.length == 0 || (!this.notOnMainNav.exist(this.sections[i].name.toString().strip())))
                {
                    var classArray = new Array();
                    classArray.push(this.sections[i].name.toString().strip());
                    
                    if(secIndex == i)
                    {
                        classArray[0] = classArray[0] + 'CurSection';
                    }
                    
                                           
                    document.write('<a class="',classArray.join(' '),'" href="',secUrl,'" title="',this.sections[i].title,'">',this.sections[i].name,'</a>');
                    
                }
                
            } 
            //document.write('<a class="contactus" href="mailto:wheels@wheelsmag.com.au" title="contact us">contact us</a>');           
            document.write('</div>');
        };
        
        Navigation.prototype.getMainNavSectionIndex = function(curSec)
        {
            for(var i = 0; i < this.sections.length; i++)
            { 
                if(curSec.toString().strip() == this.sections[i].name.toString().strip())
                {
                    return i;
                }
            }
            return 0;
        };
        
        Navigation.prototype.loadSubsections = function(section)
        {
            if(!document.forms[0].SubsectionItems && !eval('this.secAsSub_' + section.toString().strip()))return;//If no subsections returned do nothing
            //if(!document.forms[0].SubsectionItems)return;
            if(this.notToBeListed.exist(section))return;//If subsections not to be listed return
            
            if(eval('this.secName_' + section.toString().strip()))//If different name is to be used for section swap it
            {
                section = eval('this.secName_' + section.toString().strip());
            }
            
            //If section does not have subsection items return
            if(unescape(document.forms[0].SubsectionItems.value).toString().strip().indexOf(section.toString().strip()) == -1)return;
            
            //split secpaths
            var secPaths = unescape(document.forms[0].SubsectionItems.value).split('; ');
            
            this.subsections = new Array();
            
            //loop through secpaths and extract subsections
            for(var i = 0; i < secPaths.length; i++)
            {
                if(secPaths[i].toString().strip().indexOf(section.toString().strip()) != -1)
                {
                    var secsub = secPaths[i].split('>');
                    this.subsections.push(new NavSubsection(secsub[1]));
                }
            }
            
            
            //Append sections to the subsection list if navigation.secAsSub has been set
            if(eval('this.secAsSub_' + section.toString().strip()))
            {                
                var subList = eval('this.secAsSub_' + section.toString().strip());
                for(var i = 0; i < subList.length; i++)
                {
                    this.subsections.push(new NavSubsection(subList[i]));
                }
            }
        };        
        
        Navigation.prototype.printSubnav = function(curSection,curSubsection)
        {
            if(!this.subsections || !curSection)return;
            
            //if there is a custum function for this subsection
            if(eval('this.printSubNav_' + curSection.toString().strip()))
            {
                eval('this.printSubNav_' + curSection.toString().strip() + '("' + curSection + '","' + curSubsection + '")');
                return;
            }
            
            var subIndex = this.getSubNavSubsectionIndex(curSubsection);
            
            document.write('<div id="subNav" >');
            for(var i = 0; i < this.subsections.length; i++)
            {
                //url for link
                //var secUrl = '/wheels/site/index?open&type=Subsections&cat=' + curSection + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=10';
                var secUrl = '/wheels/site/index?open&type=Subsections&cat=' + this.getLinkSectionName(curSection) + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=10';
                 if (this.subsections[i].name.toString()=="Galleries") secUrl = '/wheels/site/index?open&type=Subsections&cat=' + this.getLinkSectionName(curSection) + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=12';
                //if link uses a different type of url
                var realCurSection = unescape(document.forms[0].Section.value);
                //alert('this.subLink_' + curSection.toString().strip() + '_' + this.subsections[i].name.toString().strip());
                if(eval('this.subLink_' + realCurSection.toString().strip() + '_' + this.subsections[i].name.toString().strip()))  
                {
                    
                    secUrl = eval('this.subLink_' + realCurSection.toString().strip() + '_' + this.subsections[i].name.toString().strip());
                } 
                //if the current section has subsection links to be displayed
                if(this.notOnSubNav.length == 0 || (!this.notOnSubNav.exist(curSection.toString().strip() + this.sections[i].name.toString().strip())))
                { 
                    var classArray = new Array();
                    if(subIndex == i)
                    {
                        classArray.push('CurSubsection');
                    } 
                    
                    if(i == this.subsections.length - 1)
                    {
                        classArray.push('last');
                    } 
                    
                    if(classArray.length > 0)
                    {
                        document.write('<a class="',classArray.join(' '),'" href="',escape(secUrl),'" title="',this.subsections[i].name,'">',this.subsections[i].name,'</a>');
                   } else {
                            document.write('<a href="',escape(secUrl),'" title="',this.subsections[i].name,'">',this.subsections[i].name,'</a>');
                          } 
                }
            }
            document.write('</div>');
        };
        
        Navigation.prototype.getLinkSectionName = function(curSection)
        {            
            switch(curSection.toLowerCase())
            {
                case 'car of the year':
                    return unescape(document.forms[0].Section.value);
                    break;
                default:
                    return (curSection);
            }
        };
        
        Navigation.prototype.getSubNavSubsectionIndex = function(curSubsection)
        {
            for(var i = 0; i < this.subsections.length; i++)
            { 
                if(curSubsection.toString().strip() == this.subsections[i].name.toString().strip())
                {
                    return i;
                }
            }
            return -1;
        };
       
    }
    Navigation._initialized = true;
}

function NavSection(name,title)
{
    this.name = name;
    this.title = title;
    
    if(typeof NavSection._initialized == 'undefined')
    {
        
    }
    NavSection.initialized = true;
}

function NavSubsection(name,title)
{
    this.name = name;
    this.title = title;
    
    if(typeof NavSubsection._initialized == 'undefined')
    {
        
    }
    NavSubsection.initialized = true;
}



var navigation = new Navigation();


//START - main sections [ name , title ]
//P.S. These are the names used for the main nav
navigation.addSection('home','home');
navigation.addSection('news','news');

navigation.addSection('blogs','blogs');
navigation.addSection('features','features');
navigation.addSection('road tests','road tests');
//navigation.addSection('showroom','showroom');
//navigation.addSection('multimedia','multimedia');
navigation.addSection('videos','videos');
navigation.addSection('galleries','galleries');
navigation.addSection('in the magazine','in the magazine');
navigation.addSection('car of the year','car of the year');
navigation.addSection('car of the year 2006','car of the year 2006');
navigation.addSection('car of the year 2007','car of the year 2007');
navigation.addSection('design awards');


//END - main sections

//START - sections not to appear on main nav
navigation.notOnMainNav = new Array();
navigation.notOnMainNav.push('car of the year 2007');
navigation.notOnMainNav.push('car of the year 2006');
//END - sections not to appear on main nav

//START - main section links
//P.S. add section string to end while removing spaces and non-alphabetical characters
navigation.secLink_multimedia = '/wheels/site/index?open&type=Sections&cat=Multimedia&start=1&count=10';
/*
var magSecLink = getInTheMagLatestIssueLink();
if(magSecLink)
{
    navigation.secLink_inthemagazine = magSecLink[1];
   
} else {
            navigation.secLink_inthemagazine = '/wheels/site/index?open&type=Subsections&cat=In the Magazine>2009&ui=dom&template=domWheels&start=1&count=10';
       }
 */
 navigation.secLink_inthemagazine = _INTHEMAGAZINE;
//END - main section links


//START - no subsections to be listed for these sections
navigation.notToBeListed = new Array();
navigation.notToBeListed.push('showroom');
navigation.notToBeListed.push('car of the year');
navigation.notToBeListed.push('videos');
//navigation.notToBeListed.push('tyre test');
//END - no subsections to be listed for these sections

//START - section name to be used on nav if the default is not suitable
navigation.secName_tyretest = "Road Tests";
//END - section name to be used on nav


//START - using sections as subsections
navigation.secAsSub_caroftheyear = new Array();//not working at the moment
navigation.secAsSub_caroftheyear.push('car of the year 2006');
navigation.secAsSub_caroftheyear.push('car of the year 2007');

navigation.secAsSub_roadtests = new Array();
navigation.secAsSub_roadtests.push('Tyre Test');


//END - using sections as subsections



//START - subsection links - to be used if subsection uses any url other than the subsection index
//P.S. add section string to end while removing spaces and non-alphabetical characters
navigation.subLink_caroftheyear2005_andthewinneris = '/wheels/site/articleIDs/5A96BA7E1344E4F2CA25745E001FC9A1?open&template=domWheels&ui=dom';
navigation.subLink_caroftheyear2007_thewinneris = '/wheels/site/articleIDs/37A6204666BF14E6CA2573D3007A51A2?open&template=domWheels&ui=dom';
navigation.subLink_designawards_2005youngdesigneroftheyear = '/wheels/site/articleIDs/5FCD6650E8AB2C12CA25731E0026B595?open&template=domWheels&ui=dom';
navigation.subLink_designawards_2008youngdesigner = '/wheels/site/homepages/4397B290E900C552CA25748E0074BBB3?open&template=domWheels';
navigation.subLink_news_motorshows = '/wheels/site/homepages/Motor+shows?open&template=domWheels&ui=dom';
navigation.subLink_roadtests_tyretest = '/wheels/site/homepages/Tyre+Test?open&template=domWheels&ui=dom';
//END - subsection links

//START - subsections not to appear on subnav
navigation.notOnSubNav = new Array();
//navigation.notOnMainNav.push('car of the year 2007');
//END - subsections not to appear on subnav

//START - mark section as current
// use this variable to set a section on the main nav as current
navigation.secEquivalent_tyretest = 'Road Tests'; 
navigation.secEquivalent_caroftheyear2003 = 'Car of the Year';
navigation.secEquivalent_caroftheyear2004 = 'Car of the Year';
navigation.secEquivalent_caroftheyear2005 = 'Car of the Year';
navigation.secEquivalent_caroftheyear2006 = 'Car of the Year';
navigation.secEquivalent_caroftheyear2007 = 'Car of the Year'; 
//END - mark section as current


Navigation.prototype.printSubNav_inthemagazine = function(curSection,curSubsection)
{  
    function isCurIssueCurSection(docId)//Check if current article  is the latest published
    {
        if(typeof document.forms[0].PageType != 'undefined' && (unescape(document.forms[0].PageType.value)) == 'article')        
        {
            if(docId == unescape(document.forms[0].DocID.value))
            {
                return true;
            }
        }
        return false;
    } 
    //Retrieve latest in the magazine article published
    var indexInMag = new StoriesIndex();
    var searchQuery = '(([PageType]=article)+and+([SectionPath]=In the Magazine))';
    indexInMag.addSearch(searchQuery,'1','1','3','latestissue');   
    indexInMag.loadSearch(); 
    //Check if current issue is being viewed
    var currentIssue = (typeof indexInMag.searchStories[0].viewDocID != 'undefined' && indexInMag.searchStories[0].viewDocID == document.forms[0].DocID.value);
    //var currentIssue = (document.forms[0].DocID.value == '25655EA919E22D9ACA25748300211A0C');
           
            
    var subIndex = this.getSubNavSubsectionIndex(curSubsection);
            
    document.write('<div id="subNav" >');
    
    //Print current issue link
    if(typeof indexInMag.searchStories[0].viewDocID != 'undefined')//If search has retrieved the current issue's details
    {
        //Get article variables
        var tempCurIssueSection = unescape(indexInMag.searchStories[0].viewSection).replace(/\s/ig,"_");
        var curIssueSection = (tempCurIssueSection.indexOf('>') == -1) ? tempCurIssueSection : tempCurIssueSection.split('>')[0];        
        var curIssueDocID = unescape(indexInMag.searchStories[0].viewDocID);
        var curIssueTitle = unescape(indexInMag.searchStories[0].viewTitle).replace(/\s/ig,"+");
//---------------HARD CODED CURRENT ISSUE
curIssueDocID = '60FC7DA37AE8C764CA257544007FCE54';
curIssueTitle = 'February 2009';
//---------------------------------------
        //Get article link
        //var storyLink = getStoryLink(curIssueSection,curIssueDocID,curIssueTitle);
        var storyLink = getStoryLink(unescape(indexInMag.searchStories[0].viewSection),curIssueDocID,curIssueTitle);
        
        if(currentIssue)//If viewing current issue
        {
            document.write('<a class="CurSubsection" href="',_INTHEMAGAZINE,'" title="Current issue - ',curIssueTitle,'">Current Issue</a>');
        } else {
                    document.write('<a href="',_INTHEMAGAZINE,'" title="Current issue - ',curIssueTitle,'">Current Issue</a>');
               }
     }   
     				
     for(var i = 0; i < this.subsections.length; i++)
     {
        //url for link
        var secUrl = '/wheels/site/index?open&type=Subsections&cat=' + curSection + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=10';
        //if link uses a different type of url
        if(eval('this.subLink_' + curSection.toString().strip() + '_' + this.subsections[i].name.toString().strip()))   
        {
            secUrl = eval('this.subLink_' + curSection.toString().strip() + '_' + this.subsections[i].name.toString().strip());
        }  
                
        //if the current section has subsection links to be displayed
        if(this.notOnSubNav.length == 0 || (!this.notOnSubNav.exist(curSection.toString().strip() + this.sections[i].name.toString().strip())))
        { 
            var classArray = new Array();
            if(subIndex == i && !currentIssue)
            {
                classArray.push('CurSubsection');
            }  
            
            if(i == this.subsections.length - 1)
            {
                classArray.push('last');
            }      
            
            if(classArray.length > 0)
            {
                document.write('<a class="',classArray.join(' '),'" href="',escape(secUrl),'" title="All Issues in ',this.subsections[i].name,'">All Issues in ',this.subsections[i].name,'</a>');
            } else {
                    document.write('<a href="',escape(secUrl),'" title="All Issues in ',this.subsections[i].name,'">All Issues in ',this.subsections[i].name,'</a>');
                   } 
         }
    }
    document.write('</div>');
};

Navigation.prototype.printSubNav_features = function(curSection,curSubsection)
{
    if(!this.subsections || !curSection)return;
    var subIndex = this.getSubNavSubsectionIndex(curSubsection);
         
    document.write('<div id="subNav" >');
    for(var i = 0; i < this.subsections.length; i++)
    {
        if(this.subsections[i].name.toLowerCase() != 'specials')
        {
            //url for link
            var secUrl = '/wheels/site/index?open&type=Subsections&cat=' + this.getLinkSectionName(curSection) + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=10';
            //if (this.subsections[i].name.toString()=="Galleries") secUrl = '/wheels/site/index?open&type=Subsections&cat=' + this.getLinkSectionName(curSection) + '>' + this.subsections[i].name.toString() + '&ui=dom&template=domWheels&start=1&count=12';
            //if link uses a different type of url
            var realCurSection = unescape(document.forms[0].Section.value);
            //alert('this.subLink_' + curSection.toString().strip() + '_' + this.subsections[i].name.toString().strip());
            if(eval('this.subLink_' + realCurSection.toString().strip() + '_' + this.subsections[i].name.toString().strip()))  
            {
                secUrl = eval('this.subLink_' + realCurSection.toString().strip() + '_' + this.subsections[i].name.toString().strip());
            } 
            //if the current section has subsection links to be displayed
            if(this.notOnSubNav.length == 0 || (!this.notOnSubNav.exist(curSection.toString().strip() + this.sections[i].name.toString().strip())))
            { 
                var classArray = new Array();
                if(subIndex == i)
                {
                    classArray.push('CurSubsection');
                } 
                 /*           
                if(i == this.subsections.length - 1)
                {
                    classArray.push('last');
                } 
                 */           
                if(classArray.length > 0)
                {
                    document.write('<a class="',classArray.join(' '),'" href="',escape(secUrl),'" title="',this.subsections[i].name,'">',this.subsections[i].name,'</a>');
                } else {
                            document.write('<a href="',escape(secUrl),'" title="',this.subsections[i].name,'">',this.subsections[i].name,'</a>');
                       } 
            }
        }
    }
    
    //Retrieve latest in the magazine article published
    var specialsIndex = new StoriesIndex();
    var searchQuery = '(([PageType]=article)+and+([SectionPath]=Features)+and+([Subsection]=Specials))';
    specialsIndex.addSearch(searchQuery,'1','1','3','featurespecials');   
    specialsIndex.loadSearch(); 
    
    if(specialsIndex.searchStories)//if records exist
    {
        var indexLength = 3;
        if(specialsIndex.searchStories.length < 3)indexLength = specialsIndex.searchStories.length;
        for(var i = 0; i < specialsIndex.searchStories.length; i++)
        {
            if(typeof specialsIndex.searchStories[i].viewDocID != 'undefined')//If search has retrieved the current issue's details
            {
                //Get article variables
                var tempCurIssueSection = unescape(specialsIndex.searchStories[i].viewSection).replace(/\s/ig,"_");
                var curIssueSection = (tempCurIssueSection.indexOf('>') == -1) ? tempCurIssueSection : tempCurIssueSection.split('>')[0];        
                var curIssueDocID = unescape(specialsIndex.searchStories[i].viewDocID);
                var curIssueTitle = unescape(specialsIndex.searchStories[i].viewTitle).replace(/\s/ig,"+");
                //Get article link
                //var storyLink = getStoryLink(curIssueSection,curIssueDocID,curIssueTitle);  
                var storyLink = getStoryLink(unescape(specialsIndex.searchStories[i].viewSection),curIssueDocID,curIssueTitle);
                var classArray = new Array();
                
                if(document.forms[0].DocID.value ==  specialsIndex.searchStories[i].viewDocID)
                {
                    classArray.push('CurSubsection');
                }
                
                if(i == specialsIndex.searchStories.length - 1)
                {
                    classArray.push('last');
                } 
                
                var curIssueTitle = curIssueTitle.replace(/\+/ig," ");
                   
                if(classArray.length > 0)
                {
                    document.write('<a class="',classArray.join(' '),'" href="',storyLink,'" title="',curIssueTitle,'">',curIssueTitle,'</a>');
                } else {
                            document.write('<a href="',storyLink,'" title="',curIssueTitle,'">',curIssueTitle,'</a>');
                       }                   
            }    
        }       
    }
    
    
    document.write('</div>');
};


function getCurrentSection()
{
    if(currentSection == 'Library' && currentSubsection == 'Showroom')return 'showroom';
    if(eval('navigation.secEquivalent_' + currentSection.toString().strip()))
    {
        return (eval('navigation.secEquivalent_' + currentSection.toString().strip()));
    } else {
                return currentSection;
           }
    
}

navigation.printMainNav(getCurrentSection());

navigation.loadSubsections(currentSection);

navigation.printSubnav(getCurrentSection(),currentSubsection);


