﻿function itemInterface(){

//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
var totalBoxes = item.find('div.itemMain div.col').size();
totalBoxes --; //counting from zero

var counter = 0;
var advance = 2;//2 columns per page

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 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 - unused
$('.colThirds:last').addClass('lastThird');

//last item bottom border
$('h2.menuheader:last').addClass('lastClosed');

//subnav hover effect
$('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