var activeTab = 1;
var timerDelay = 10000; // first iteration is Residential Roofing, so give it a bit more time
var currentTimer = setTimeout("flipRight()", timerDelay); 

$(document).ready(function()
{
	/* EXTERNAL LINKS *********************************************************/
	externalLinks();
	function externalLinks() {
	  if (!document.getElementsByTagName)
	    return;
	  
	  var anchors = document.getElementsByTagName("a");
	  
	  for (var i = 0; i < anchors.length; i++) {
	    var anchor = anchors[i];
	    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
	      anchor.target = "_blank";
	  }
	}

	/* HOME PAGE HEADERS ******************************************************/
	$("#h-btn-flip-l").click(function() { flipLeft(); });
	$("#h-btn-flip-r").click(function() { flipRight(); });

	/* PHOTO GALLERIES (GALLERIFFIC) ******************************************/
	if($('#thumbs').length != 0) { // otherwise a js error happens on every page load
		var gallery = $('#thumbs').galleriffic({
	        delay:                     3000, // in milliseconds
	        numThumbs:                 10, // The number of thumbnails to show page
	        preloadAhead:              40, // Set to -1 to preload all images
	        enableTopPager:            false,
	        enableBottomPager:         true,
	        maxPagesToShow:            5,  // The maximum number of pages to display in either the top or bottom pager
	        imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
	        controlsContainerSel:      '#controls', // The CSS selector for the element within which the slideshow controls should be rendered
	        captionContainerSel:       '#caption', // The CSS selector for the element within which the captions should be rendered
	        renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
	        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
	        prevLinkText:              'Prev',
	        nextLinkText:              'Next',
	        nextPageLinkText:          'Next',
	        prevPageLinkText:          'Prev',
	        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
	        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
	        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	        defaultTransitionDuration: 600 // If using the default transitions, specifies the duration of the transitions
	    });
	}
	
	/* CONTACT FORM ***********************************************************/	
	$("#findus").change(function(event)
	{
		if($('#findus').val() == 'Internet Search')
		{
			$("#findus-link-box").slideUp(500);
			$("#findus-other-box").slideUp(500);
			$("#findus-search-box").slideDown(500);
		}
		else if($('#findus').val() == 'Link to our Website')
		{
			$("#findus-search-box").slideUp(500);
			$("#findus-other-box").slideUp(500);
			$("#findus-link-box").slideDown(500);
		}
		else if($('#findus').val() == 'Other')
		{
			$("#findus-search-box").slideUp(500);
			$("#findus-link-box").slideUp(500);
			$("#findus-other-box").slideDown(500);
		}
		else
		{
			$("#findus-search-box").slideUp(500);
			$("#findus-link-box").slideUp(500);
			$("#findus-other-box").slideUp(500);
		}
	});
});

/* HOME PAGE HEADER - Left button */
function flipLeft() {
	clearTimeout(currentTimer); // stop timer, in case button manually clicked
	if(activeTab >= 2) {
		$(".header-slide").animate({"left": "+=916px"}, 600);
		activeTab -= 1;
	}
	else {
		$(".header-slide").animate({"left": "-=1832px"}, 600);
		activeTab = 3;
	}
	resetTimer(); // regardless if button was clicked, or if flip was automatic, restart timer
}

/* HOME PAGE HEADER - Right button */
function flipRight() {
	clearTimeout(currentTimer); // stop timer, in case button manually clicked
	if(activeTab <= 2) {
		$(".header-slide").animate({"left": "-=916px"}, 600);
  		activeTab += 1;
	}
	else {
		$(".header-slide").animate({"left": "+=1832px"}, 600);
		activeTab = 1;
	}
	resetTimer(); // regardless if button was clicked, or if flip was automatic, restart timer
}

/* HOME PAGE HEADER - Start a new timer, based on which division's header image is currently displayed */
function resetTimer() {
	activeTab == 1 ? timerDelay = 10000 : timerDelay = 7000; // Residential Roofing displayed a bit longer than others 
	currentTimer = setTimeout("flipRight()", timerDelay);
}
