$(document).ready( function(){

	/*-----
		Homepage Banner
		-----*/
		// Speed of the transition of the banner
		var bannerSpeed = 1500; //was 400 to start
		var bannerShouldRotate = true;
		var allBanners = $('#homeBanner div.banner');
		var allBannerH1 = $('#homeBanner div.banner h1');
		var allBannerH2 = $('#homeBanner div.banner h2');

		function randomNumber(maxValue){ // Add this function
			maxValue = maxValue;
			var randomnumber = Math.floor(Math.random()*maxValue);
			return randomnumber;
		}
				
		var currentBannerShowing = randomNumber(allBanners.length); // change this from 0

		
		var bannerEasingMethod = 'easeInQuart';
	
		// hide all tab content on page load
		$(allBanners).css('opacity', 0);
		// remove class hiding tabbed content
		$(allBanners[currentBannerShowing]).removeClass('hide');
		$(allBanners[currentBannerShowing]).animate({ opacity: 1 }, 0, bannerEasingMethod, function(){});
		
		function showBanner(){
			$(allBanners[currentBannerShowing]).removeClass('hide');
			$(allBanners[currentBannerShowing]).animate({ opacity: 1 }, bannerSpeed, bannerEasingMethod, function(){});
		}
		
		function nextBanner(){
			// if the current Banner is not the last
			if(currentBannerShowing < allBanners.length-1){
				// Fade out current Banner
				$(allBanners[currentBannerShowing]).animate({ opacity: 0 }, bannerSpeed, bannerEasingMethod, function(){
					$(allBanners).addClass('hide');
					currentBannerShowing++;
					// Fade in current Banner
					showBanner();
				});
			// if the current Banner is the last show the first
			} else {
				// Fade out current Banner
				$(allBanners[currentBannerShowing]).animate({ opacity: 0 }, bannerSpeed, bannerEasingMethod, function(){
					$(allBanners).addClass('hide');
					currentBannerShowing = 0;
					// Fade in current Banner
					showBanner();
				});
			}	
		}
		
		// Homepage Banner Previous Button Click Function
		$('#homeBanner a.previousBtn').click( function(){
			bannerShouldRotate = false;
			clearInterval(bannerInterval);
			// if the current news item is not the first
			if(currentBannerShowing > 0){
				// Fade out current Banner
				$(allBanners[currentBannerShowing]).animate({ opacity: 0 }, bannerSpeed, bannerEasingMethod, function(){
					$(allBanners).addClass('hide');
					currentBannerShowing--;
					// Fade in current Banner
					showBanner();
				});
			// if the current Banner is the first show the last Banner
			} else {
				// Fade out current Banner
				$(allBanners[currentBannerShowing]).animate({ opacity: 0 }, bannerSpeed, bannerEasingMethod, function(){
					$(allBanners).addClass('hide');
					currentBannerShowing = allBanners.length-1;
					// Fade in current Banner
					showBanner();
				});
			}
			// Prevent link action occuring
			return false;
		});
		
		// Homepage Banner Next Button Click Function
		$('#homeBanner a.nextBtn').click( function(){
			bannerShouldRotate = false;
			clearInterval(bannerInterval);
			nextBanner();
			// Prevent link action occuring
			return false;
		});
	
    /*-----
    Homepage Tabbed Section
    -----*/
    var allTabs = $('div.tabs a');
    var allTabbedContent = $('div.tabbedContent div.tabContent');
    // store the currently selected tab
    var previousTab = 0;
    // speed of the transition of the tabs
    var tabSpeed = 400;
    // check to see if tabs are currently animating
    var tabChanging = false;
    // hide all tab content on page load
    //$(allTabbedContent).css('opacity', 0);
    // remove class hiding tabbed content
    //$(allTabbedContent[0]).removeClass('hide');
    // fade on the first tabbed content
    //$(allTabbedContent[0]).animate({ opacity: 1 }, 250, function() { });

    var tabEasingMethod = 'easeInQuart';

    // Clicking the homepage tabs
    $('div.tabs a').click(function() {
        // make sure tabs are not still animating
        if (!tabChanging) {
            // prevent double clicking
            tabChanging = true;
            // Get the clicked tabs index
            var currentTab = $(allTabs).index($(this));

            if (currentTab != previousTab) {
                // remove previously selected button
                $(allTabs).removeClass('selected');
                // make current tab selected
                $(this).addClass('selected');

                // Fade Tabs
                //$(allTabbedContent[previousTab]).animate({ opacity: 0 }, tabSpeed, tabEasingMethod, function() {
                    $(allTabbedContent).addClass('hide');

                    // store tab for fading when next tab is clicked
                    previousTab = currentTab;

                    $(allTabbedContent[currentTab]).removeClass('hide');
                    // fade on new tabbed content
                    //$(allTabbedContent[currentTab]).animate({ opacity: 1 }, tabSpeed, tabEasingMethod, function() {
                        // after the animation is complete allow other tabs to be clicked
                        tabChanging = false;
                    //});
                //});
            } else {
                tabChanging = false;
            }
        }
        return false;
    });

	/*-----
		NewsTicker Transitions
		-----*/
		// Speed of the transition on the news ticker
		var tickerSpeed = 600;
		var allNews = $('#newsTicker td.news a');
		var newsItemShown = 0;
		var tickerEasingInMethod = 'easeOutQuart';
		var tickerEasingOutMethod = 'easeOutQuart';
		var tickerAnimating = false;
		
		function nextNewsItemSlide(){
			// Fade in current news item
			$(allNews[newsItemShown]).show();
			$(allNews[newsItemShown]).css('margin-top', '-15px');
			$(allNews[newsItemShown]).animate({ opacity: 1, marginTop: '0'}, tickerSpeed, tickerEasingOutMethod, function(){
				tickerAnimating = false;
			});
		}
		
		function prevNewsItemSlide(){
			// Fade in current news item
			$(allNews[newsItemShown]).show();
			$(allNews[newsItemShown]).css('margin-top', '15px');
			$(allNews[newsItemShown]).animate({ opacity: 1, marginTop: '0'}, tickerSpeed, tickerEasingOutMethod, function(){
				tickerAnimating = false;
			});
		}
		
		function nextNewsItem(){
			// make sure the ticker is not still animating
			if(!tickerAnimating){
				// prevent double clicking
				tickerAnimating = true;
				// if the current news item is not the last
				if(newsItemShown < allNews.length-1){
					// Fade out current news item
					$(allNews[newsItemShown]).animate({ opacity: 0, marginTop: '15px' }, tickerSpeed, tickerEasingInMethod, function(){
						$(allNews).hide();
						newsItemShown++;
						nextNewsItemSlide();
					});
				// if the current news item is the last show the first
				} else {
					// Fade out current news item
					$(allNews[newsItemShown]).animate({ opacity: 0, marginTop: '15px' }, tickerSpeed, tickerEasingInMethod, function(){
						$(allNews).hide();
						newsItemShown = 0;
						nextNewsItemSlide();
					});
				}
			}
		}
		// Hide all news items on page load
		
		//JAA addding fix
		//$(allNews).hide();
		//$(allNews[0]).show();
		
		$(allNews).animate({ opacity: 0 }, 0, function(){
			$(allNews[newsItemShown]).css('margin-top', '-15px');
			$(allNews[newsItemShown]).animate({ opacity: 1, marginTop: '0'}, 0, tickerEasingInMethod, function(){});
			$(allNews[0]).css('z-index', 1);
		});
		
		// Homepage News Ticker Previous Button Click Function
		$('#newsTicker td.navigation a.previousBtn').click( function(){
			// make sure the ticker is not still animating
			if(!tickerAnimating){
				// prevent double clicking
				tickerAnimating = true;
				// if the current news item is not the first
				if(newsItemShown > 0){
					// Fade out current news item
					$(allNews[newsItemShown]).animate({ opacity: 0, marginTop: '-15px'}, tickerSpeed, tickerEasingInMethod, function(){
						newsItemShown--;
						$(allNews).hide();
						prevNewsItemSlide();
					});
				// if the current news item is the first show the last item
				} else {
					// Fade out current news item
					$(allNews[newsItemShown]).animate({ opacity: 0, marginTop: '-15px'}, tickerSpeed, tickerEasingInMethod, function(){
						newsItemShown = allNews.length-1;
						$(allNews).hide();
						prevNewsItemSlide();
					});
				}
			}
			// Prevent link action occuring
			return false;
		});
		
		// Homepage News Ticker Next Button Click Function
		$('#newsTicker td.navigation a.nextBtn').click( function(){
			nextNewsItem();
			// Prevent link action occuring
			return false;
		});
		
		// call the next banner 
		var bannerInterval = setInterval(nextBanner, 15000); //Was 5000 to start
	
	
	/*-----
		Homepage Timed Elements
		-----*/
		// On hover of the home banner stop auto rotation of images
		$('#homeBanner').hover( function(){
			clearInterval(bannerInterval);
		}, function(){
			if(bannerShouldRotate){
				// when the mouse moves out of the banner start auto rotation of images
				bannerInterval = setInterval(nextBanner, 15000); //Was 5000 to start
			}
		});
		
		// call the next banner 
		var tickerInterval = setInterval(nextNewsItem, 5000);
		
		// On hover of the home banner stop auto rotation of images
		$('.newsTickerRight').hover( function(){
			clearInterval(tickerInterval);
		}, function(){
			// when the mouse moves out of the banner start auto rotation of images
			tickerInterval = setInterval(nextNewsItem, 5000);
		});
		
		
		// Added for homepage Custom Selects
		var allSelects = $('select');
		for (i = 0; i < allSelects.length; i++) {
			var allOptions = $(allSelects[i]).find('option');
			var newSelectText = '<div class="customSelect customSelect' + [i] + '"><a href="#" class="selectBtn">' + $(allOptions[0]).html() + '</a><div class="selectDDL">';
			for (j = 1; j < allOptions.length; j++) {
				newSelectText += '<a href="' + $(allOptions[j]).val() + '">' + $(allOptions[j]).html() + '</a>';
			}
			newSelectText += '</div></div>';
			$(allSelects[i]).before(newSelectText);
	
			$('div.customSelect' + i + ' a.selectBtn').bind('click', function(i) {
				$(this).parent('div.customSelect').toggleClass('expanded');
				$(this).siblings('div.selectDDL').toggle();
				return false;
			});
		}
	
		function hideCustomSelects() {
			$("div.selectDDL").hide();
			$("div.customSelect").removeClass('expanded');
		}
	
		var customSelectTimer;
		$('div.customSelect').hover(function() {
			clearTimeout(customSelectTimer);
		}, function() {
			customSelectTimer = setTimeout(hideCustomSelects, 200);
		});
});


