﻿ 
function itemInterface(){

//parse querystring to get section & subsection IDs
var query = window.location.search;
if (query.substring(0, 1) == '?') {
    query = query.substring(1);
}
var data = query.split(',');
for (i = 0; (i < data.length); i++) {
    data[i] = unescape(data[i]);
}
var sectionID = data[1]; //unused at present - can't pass to ddaccordion.init!
var subID = data[2];
//alert(sectionID+","+subID);

//main content handler
$('div.itemwrapper').each(function(n){
    var item = $(this);
    //add blue line to bottom of last item
    if (n==7){
        item.children('.itemMain').addClass('btmLine');
        item.children('.itemNav').addClass('btmLine');
         }
    //identify target areas
    var itemMain = item.find('div.itemMain');
    var itemRelated = item.find('div.itemRelated');
    
    //section content
    var mainContent = item.find('div.mainContent');
    var mainRelated = item.find('div.mainRelated');
    
    //function to initialise page counter
    function initCounter(c){
    //alert(c);
    counter = 0;
    return counter;
    }
    
    //subnav click handler
    item.find('span.subNavLink').each(function(){
    
        $(this).bind('click', function(){
            //first hide main section content & related info
            mainContent.hide();
            mainRelated.hide();
            
            //style selected link
            item.find('span.subNavLink').removeClass('selSubLink');
            $(this).addClass('selSubLink');
            
            var subitem = $(this).parent('li');
            
            //subitem.find('div.subNavRelated').addClass('hilite');
            itemRelated.find('div.subNavRelated').remove();
            subitem.find('div.subNavRelated').clone().appendTo(itemRelated);
            
            //but hide related title if no content
            itemRelated.find('.noRelated').parent().hide();
            
            //subitem.find('div.subNavContent').addClass('hilite');
            itemMain.find('div.subNavContent').remove();
            subitem.find('div.subNavContent').clone().appendTo(itemMain);
            
            //Initialise column pagination for subnav
            totalBoxes = item.find('div.itemMain div.subNavContent div.col').size();
            totalBoxes --; //counting from zero
            
            initCounter(counter);
            
            item.find('div.itemMain div.subNavContent div.col').hide();
            item.find('div.pagNav a').hide();

            //show 1st & 2nd cols & apply column classes
            item.find("div.itemMain div.subNavContent div.col:eq(0)").addClass("columnLeft").show();
            item.find("div.itemMain div.subNavContent div.col:eq(1)").addClass("columnRight").show();
            
            //show first nav btn if more than 2 columns
            if (totalBoxes>1){
                item.find('div.pagNav').show()
                item.find('div.pagNav a:last').show()
            }
            
        });
    });
    //h2 click handler
    $('h2.menuheader').click(function(){
            mainContent.show();
            mainRelated.show();
            
            //hide Related title if no content
            mainRelated.find('.noRelated').parent().hide();
            
            //remove any selected sublinks
            item.find('span.subNavLink').removeClass('selSubLink');
            
            itemMain.find('div.subNavContent').remove();
            itemRelated.find('div.subNavRelated').remove();
            //Initialise column pagination business
            totalBoxes = item.find('div.itemMain div.col').size();
            totalBoxes --; //counting from zero

            initCounter(counter);
            
            item.find('div.itemMain div.col').hide();
            item.find('div.pagNav a').hide();

            //show 1st & 2nd cols & apply column classes
            item.find("div.itemMain div.col:eq(0)").addClass("columnLeft").show();
            item.find("div.itemMain div.col:eq(1)").addClass("columnRight").show();
            
            //show first nav btn if more than 2 columns
            if (totalBoxes>1){
                item.find('div.pagNav').show()
                item.find('div.pagNav a:last').show()
            }
    });

    
function clickHandler(page){
    item.find("div.itemMain div.col").hide();
    
    item.find("div.itemMain div.mainContent div.col:eq("+page+")").addClass("columnLeft").show();
    item.find("div.itemMain div.mainContent div.col:eq("+(page+1)+")").addClass("columnRight").show();
    //if subnav content present then work on that...
    item.find("div.itemMain div.subNavContent div.col:eq("+page+")").addClass("columnLeft").show();
    item.find("div.itemMain div.subNavContent div.col:eq("+(page+1)+")").addClass("columnRight").show();
    
    item.find("div.pagNav a").hide();
        if (page == 0){
            item.find("div.pagNav a:last").show();
        }
        if (page > (totalBoxes-2)){
            item.find("div.pagNav a:first").show();
        }
        if (page < (totalBoxes-1) && page > 1){
            item.find("div.pagNav a").show();
        }
    }//fn
    
    //****Initialise column pagination business
    
    //first check for subnav to be opened on init
    item.find('span.subNavLink').each(function(){
        current = $(this).attr('cid');
        if (current == subID){
            mainContent.hide();
            mainRelated.hide();
            
            //style selected link
            item.find('span.subNavLink').removeClass('selSubLink');
            $(this).addClass('selSubLink');
            
            window.subbing = true;
            var subitem = $(this).parent('li');
            
            //subitem.find('div.subNavRelated').addClass('hilite');
            itemRelated.find('div.subNavRelated').remove();
            subitem.find('div.subNavRelated').clone().appendTo(itemRelated);
            
            //hide related title if no content
            itemRelated.find('.noRelated').parent().hide();
            
            //subitem.find('div.subNavContent').addClass('hilite');
            itemMain.find('div.subNavContent').remove();
            subitem.find('div.subNavContent').clone().appendTo(itemMain);
            
        }
    });
    var counter = 0;
    var advance = 2;//2 columns per page
    
    item.find('div.itemMain div.col').hide();
    item.find('div.pagNav a').hide();
    
    if (window.subbing){
            //Initialise column pagination for subnav
            totalBoxes = item.find('div.itemMain div.subNavContent div.col').size();
            totalBoxes --; //counting from zero
            
            initCounter(counter);
            
            item.find('div.itemMain div.subNavContent div.col').hide();
            item.find('div.pagNav a').hide();
            if (totalBoxes>1){
                item.find('div.pagNav').show()
                item.find('div.pagNav a:last').show()
            }
            //show 1st & 2nd cols & apply column classes
            item.find("div.itemMain div.subNavContent div.col:eq(0)").addClass("columnLeft").show();
            item.find("div.itemMain div.subNavContent div.col:eq(1)").addClass("columnRight").show();
            
            //highlight correct nav item*******TO DO!!**************
            //$('.subNavLink').removeClass('selSubLink');
            //$('#contentBlock4 div.itemNav').append('remove');
            
    } else {
        //if no subID then show section content as normal
            var totalBoxes = item.find('div.itemMain div.col').size();
            totalBoxes --; //counting from zero
            
            //show 1st & 2nd cols & apply column classes
            item.find("div.itemMain div.col:eq(0)").addClass("columnLeft").show();
            item.find("div.itemMain div.col:eq(1)").addClass("columnRight").show();
    }
    
    //show first nav btn if more than 2 columns
    if (totalBoxes>1){
        item.find("div.pagNav a:last").show()
    } //else {
        //else reduce height of itemNav and itemMain - no good as content gets cut
        //item.find('.itemNav').height(345);
        //item.find('.itemMain').height(345);
   // }
    
    //back btn
    item.find("div.pagNav a:first").click(function(){
        counter=counter-advance;
        clickHandler(counter);
    });
    //continue btn
    item.find("div.pagNav a:last").click(function(){
        counter=counter+advance;
        clickHandler(counter);
    });
    	                
});//end main content handler function

//extra bit for 3 column page
$('.colThirds:last').addClass('lastThird');

//subnav hover effect for IE
$('ul.subNav li span').each(function(){
    $(this).hover(function(){$(this).addClass('ovr');}, function(){$(this).removeClass('ovr');});
    });
        
};//end itemInterface function

function itemOpen(){
      //special styling for homepage
      if ($('#homeWrapper').prev('h2').hasClass('openheader'))
      {
          $('#homeWrapper').css('margin-bottom','-3px').next('h2').css('border','none');
          } else {
          $('#homeWrapper').css('margin-bottom','0px').next('h2').css('border-top','2px solid #38383a');
          $('#homeWrapper').css('margin-bottom','0px').next('h2.openheader').css('border-top','2px solid #00a5ed');
      }
      //special news section
      if ($('.itemwrapper:eq(5)').prev('h2').hasClass('openheader'))
      {
        $('.itemwrapper:eq(5) ul.subNav li span:first').addClass('selSubLink');
      }
}//end itemOpen function
