
/*-------------------------------------------------------
---- BEHAVIOUR (Primary Javascript file) ----------------
--------------------------------------------------------*/
var $rr = jQuery.noConflict();

$rr(document).ready( function() { 																                          	//When the document is ready
	
	
//Open external links in new windows
		
		var external = $rr('a[rel="external"]');														                      //Get all links with rel="external" and place them into the 'external'
	
		external.attr("title", external.attr("title") + " - Link opens in a new window..");			//Take the links current title and append it with the external link message
			
		external.click( function() {																                            //When an external link is clicked
			
		window.open( $rr(this).attr('href') );														                        //Open its href in a new window
        
		return false;																				                                    //Cancel the links default behaviour
		
		});																							                                        //End function


//Add page print utility

    $rr('.pageTools ul').prepend('<li class="print"><a href="#">Print</a></li>');
    
    $rr('.print').click(function(){window.print();return false;})



//MEGA Dropdown! ----------------------------------

  $rr('.primaryNavigation li').hover( function(){
    
    $rr(this).children('.megaDropdown').show();
    
  }, function(){
    
    $rr(this).children('.megaDropdown').hide();    
    
  })
  
  $rr('.primaryNavigation li').focusin( function(){
    
    $rr(this).children('.megaDropdown').show();
    
  })
  
   $rr('.btnBlogOpen').focusin( function(){
    
    $rr('.megaDropdown').hide();    
    
  })


// Popout blog ----------------------------------------------------
  
  $rr('.pageOverlayBlog').height($rr('body').innerHeight());

  $rr('.btnBlogOpen').click( function(){

    $rr('.popoutBlog').show();     
    $rr('.pageOverlayBlog').show();
    $rr('.popoutBlog').animate({right: '+=619'});

    return false;
    
  })
  
  $rr('.btnBlog').click( function(){  
    
    $rr('.popoutBlog').animate({right: '-=619px'}, function(){
    
    $rr('.pageOverlayBlog').hide();
    
    })
    
    return false;
 })   
 
  $rr('.popoutBlog .btnClose').click( function(){
  
   
    $rr('.popoutBlog').animate({right: '-=619px'}, function(){
    
    $rr('.pageOverlayBlog').hide();
    
    })
    
    $rr('.popoutBlog').show();  
    
    return false;
    
  })
		
		
//Email this page --------------------------------------------------

  $rr('.pageOverlayEmail').height($rr('body').innerHeight());

  $rr('.email').click( function(){
       
    $rr('.pageOverlayEmail').show();
    $rr('.emailPage').show();
 
    if(typeof document.body.style.maxHeight === "undefined") {
      
      //alert("ie6");
      
      $rr(window).scrollTop(0);
      
    }
  
  return false;
  
  })
  
  
  $rr('.emailPage .btnClose').click( function(){
  
    $rr('.pageOverlayEmail').hide();
    $rr('.emailPage').hide();
    
    return false;
    
  })
		

		
//Customisable Accordion function ----------------------------------

  $rr('.accordion li div').not('.accordionContentInner').hide();
  $rr('.startOpen').show();
  $rr('.startOpen').siblings('.accordionHeading').addClass("active");
  
  $rr('.accordion .accordionHeading').click( function(){
  

      if($rr(this).parent().find('.accordionContent').is(":hidden")){     
       
        
        if ($rr(this).parent().parent('.accordion').hasClass("persist")) {
          
          $rr(this).parent().parent().find('.accordionContent').slideUp('fast');
          $rr(this).parent().parent().find('.accordionHeading').removeClass('active');
        
        };
        
        $rr(this).parent().find('.accordionContent').slideDown('fast');
        $rr(this).addClass('active');
        
      } else {    
        
        $rr(this).parent().find('.accordionContent').slideUp('fast');
        $rr(this).parent().find('.accordionHeading').removeClass('active');
              
      }
      
      return false;

  });
  
 
  
  
  
//Expandable box --------------------------------------------------

  $rr('.expandableBox h2 span').css({'background-image':'url("/assets/images/site/backgrounds/accordionFirstArrow.png")'});

  $rr('.expandableBox  h2').click( function(){
    
      if($rr(this).siblings('div').is(":hidden")){
       
       $rr(this).siblings('div').slideDown();
        
      } else {
        
       $rr(this).siblings('div').slideUp();
        
      }
      
      return false;
    
  })  
  



//Tabbed Panes function ------------------------------------------

  if('.tabbedContent'){
    
    $rr('.tabbedContent').addClass('panes');
    
    var height = 0;
    
    $rr('.panes').before("<ul class='tabs'></ul>");
    
    var i = 0;
  
    $rr('.panes div.pane').each( function(i){
      
      $rr(this).addClass('pane' + i);
      
      $rr('ul.tabs').append("<li id='" + i + "'><a href='#'><span>" + $rr(this).find('.panelHeading').text() + "</span></a></li>");
      
      $rr(this).find('.panelHeading').remove();
      
      if($rr(this).height() > height){
        
        //$rr('.panes').css({'height':$rr(this).height() + 40});
 
      }
      
    });

    var tabsLength = $rr('ul.tabs li');
    
    var tabID = parseInt(tabsLength.length - 1);
    
    $rr('ul.tabs li#' + tabID).addClass('active'); 

    //$rr('.panes div.pane').hide();
    $rr('.panes div.pane').addClass("throwMe");
    //$rr('.panes div.pane:last').show();
    $rr('.panes div.pane:last').removeClass("throwMe");

    $rr('ul.tabs a').not('li.video a').click( function(){
    
      $rr('ul.tabs li').removeClass('active');
      $rr(this).parent('li').addClass('active');
    
      var pane = $rr(this).parent('li').attr('id');
      
      $rr('.panes div.pane').addClass("throwMe");
     // $rr('.panes div.pane' + pane).show();
      $rr('.panes div.pane' + pane).removeClass("throwMe");
      
      return false;
      
    });

  }


// Latest news ticker (The array of news items  are in the header include)

if($rr('.latestNewsTickerInner').length > 0){

  var count = 0;
  
  function changeNews(newsItem,newsHref){
    
    //alert(newsHref);
    
    $rr('.latestNewsTickerInner p').remove();
    $rr('.latestNewsTickerInner').append("<p style='display:none;'><a href='" + newsHref + "'>" + newsItem + "</a></p>");
    $rr('.latestNewsTickerInner p').fadeIn(3000);
    
    if(count == newsArray.length - 2){
    
      count = 0;
    
    } else {

      count = count + 2;

    }
    
  }
  
  setInterval(function(){changeNews(newsArray[count],newsArray[count + 1])},8000);

}



//Show and hide placeholder text on search field 
if ($rr('.frmSiteSearch input[type=text]').attr('title')) {//first check the search field exists
    
    var placeholdertext = $rr('.frmSiteSearch input[type=text]').attr('title');//get the field's title text

    $rr('.frmSiteSearch input[type=text]').attr('value', placeholdertext);//copy the title text to the field's value
    
    $rr('.frmSiteSearch input[type=text]').click(function(){//when clicked/focused
    
        if ($rr(this).attr('value') == placeholdertext) {//if the field's value is the same as the title text
        
            $rr(this).attr('value', "");//blank the value
            
        }
        
        return false;
        
    });
    
    $rr('.frmSiteSearch input[type=text]').blur(function(){//on focus out
    
        if ($rr(this).attr('value') == "") {//if the field's value is blank
        
            $rr(this).attr('value', placeholdertext);//copy the title text back into the value
            
        }
        
    });

}

    var changeCountry = function () {
        var country = $rr(this).val(),
            united_states = 'United States',
            united_states_class = '.USState'
            canada = 'Canada',
            canada_class = '.CanaState',
            $hidden_dropdowns = $rr('.frmRowState');
            
        if (country === united_states) {            
    
            $hidden_dropdowns.filter(united_states_class).show();
            $hidden_dropdowns.filter(canada_class).hide();
    
        } else if (country === canada) {
            
            $hidden_dropdowns.filter(canada_class).show();
            $hidden_dropdowns.filter(united_states_class).hide();
            
        } else {
    
            $hidden_dropdowns.hide();
    
        }        
    };

    $rr('.frmRowState').hide();
    changeCountry.call($rr('.fldCountry')[0]);

    $rr('.fldCountry').change(function() {

        changeCountry.call(this)
        
    });
    
}); 																								                                        //End document.ready












