﻿jQuery(document).ready(function() {

    // Initialise the HIA Slider doovey
    initHiaSlider();

    // Initialise the hia partners slider doovey
    initHiaPartners();

    // Initialise the slider in the footer
    initFooterSlider();

	// Reset search text on clikc
	jQuery('#header-search .searchtextbox').focus(function() {
		
		// if text is default search, clear it.
		if (jQuery('#header-search .searchtextbox').val() == "Search") {
			jQuery('#header-search .searchtextbox').val("");	
		}
		
	});
	
//========================================================================================================
//modified search function action. Since search button was the first submit button on the page, IE was firing search
//unexpectedly.
	jQuery('.searchbutton').click(function() {
		return  (jQuery('#header-search .searchtextbox').val() != "Search");
	});
//========================================================================================================
	// Reset search text on clikc
	jQuery('#header-search .searchtextbox').blur(function() {
		
		// if text is default search, clear it.
		if (jQuery('#header-search .searchtextbox').val() == "") {
			jQuery('#header-search .searchtextbox').val("Search");	
		}
		
	});

});


function initHiaSlider() {

    scrollPos = 1;
    jQuery(document).ready(function() {

        var container = jQuery('#hscroll');
        var ul = jQuery('ul.scroll-win li', container);
        var listLength = ((jQuery('ul.scroll-win li').length) * (jQuery('ul.scroll-win li').width() + 6))

        jQuery.each(jQuery.browser, function(object, val) {

            if (object == "msie") {
                listLength = listLength + 8;
            }

        });

        jQuery('#hscroll ul.scroll-win').css('width', listLength);
        var itemsWidth = listLength - (container.outerWidth() - 7);

        jQuery('#hscroll-bar', container).slider({
            min: 0,
            max: itemsWidth,
            handle: '#hscroller',
            stop: function(event, ui) {
                ul.animate({ 'left': ui.value * -1 }, 500);
            },
            slide: function(event, ui) {
                ul.css('left', ui.value * -1);
            }
        });

        // Moves the scollbar and scroll window to proper position when link is clicked
        jQuery('#whatsnew').click(function() {
            ul.animate({ 'left': 0 }, 500);
            jQuery('#hscroller').animate({ 'left': 0 }, 500);
            clearInterval(timer);
        });

        jQuery('#trainingevents').click(function() {
            ul.animate({ 'left': -651 }, 500);
            jQuery('#hscroller').animate({ 'left': 165 }, 500);
            clearInterval(timer);
        });

        jQuery('#store').click(function() {
            ul.animate({ 'left': -1303 }, 500);
            jQuery('#hscroller').animate({ 'left': 329 }, 500);
            clearInterval(timer);
        });

        jQuery('#apprentices').click(function() {
            ul.animate({ 'left': -1955 }, 500);
            jQuery('#hscroller').animate({ 'left': 490 }, 500);
            clearInterval(timer);
        });



        timer = setInterval(function() {
            var scroller = jQuery('#hscroller').css('left');
            if (scrollPos == 1) {
                ul.animate({ 'left': -651 }, 500);
                jQuery('#hscroller').animate({ 'left': 165 }, 500);
                scrollPos = 2;
            } else
                if (scrollPos == 2) {
                ul.animate({ 'left': -1303 }, 500);
                jQuery('#hscroller').animate({ 'left': 329 }, 500);
                scrollPos = 3;
            } else
                if (scrollPos == 3) {
                ul.animate({ 'left': -1955 }, 500);
                jQuery('#hscroller').animate({ 'left': 490 }, 500);
                scrollPos = 4;
            } else
                if (scrollPos == 4) {
                ul.animate({ 'left': 0 }, 500);
                jQuery('#hscroller').animate({ 'left': 0 }, 500);
                scrollPos = 1;
            }

        }, 8000);

    });

}

function initHiaPartners() {

    // Sets the width of the ul
    var listWidth = ((jQuery('#hia-partners-content ul li').length) * (jQuery('#hia-partners-content ul li').width() + 60))
    jQuery('#hia-partners-content ul').css('width', listWidth);

    // Adds a numbered ID to each list item inside the footer-logos UL
    jQuery('#hia-partners-content ul li').each(function(i) {
        this.id = "partner-" + i;
    });

    var currentState = 0;

    // Left Click Function
    jQuery('#hia-partners-content a.left-ctrl').click(function() {

        // checks if current state is at the maximum length
        // and if it is, do nothing
        if (currentState == 0) {
            return;
        }

        var moveSize = 199 * (currentState - 3);

        jQuery('#hia-partners-content ul li').animate({ right: moveSize.toString() + "px" });

        currentState = currentState - 3;

    });

    // Right Click Function
    jQuery('#hia-partners-content a.right-ctrl').click(function() {

        // checks if current state is at the maximum length
        // and if it is, do nothing
        
        if ((currentState + 2) == jQuery('#hia-partners-content ul li').length - 1) {
            return;
        }

        // show the new logo

        var moveSize = 199 * (currentState + 3);

        jQuery('#hia-partners-content ul li').animate({ right: moveSize + "px" });

        // Increment current state
        currentState = currentState + 3;

    });

}

function initFooterSlider() {

    // Adds a numbered ID to each list item inside the footer-logos UL					   
    jQuery('ul.footer-logos li').each(function(i) {
        this.id = "fl" + i;
        jQuery('ul.footer-logos li').hide();
    });

    jQuery('ul.footer-logos li#fl0').fadeIn(1000);

    currentState = 0;

    // Interval Function	
    setInterval(function() {
        // fade out old logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeOut(1000);
        currentState = currentState + 1;
        // checks if current state is at the maximum length
        // and if it is, resets it back to 0
        if (currentState > jQuery('ul.footer-logos li').length - 1) {
            currentState = 0;
        }
        // fade in new logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeIn(1000);

        // Vertically centers images inside list items
        topPad = (95 - jQuery('ul.footer-logos li#fl' + currentState + ' img').height()) / 2;
        jQuery('ul.footer-logos li#fl' + currentState).css('padding-top', topPad);

    }, 6000);

    // Left Click Function
    jQuery('a.l-ctrl').click(function() {
        // fade out old logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeOut(1000);
        if (currentState == 0) {
            currentState = jQuery('ul.footer-logos li').length;
        }
        currentState = currentState - 1;
        // fade in new logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeIn(1000);

        // Vertically centers images inside list items
        topPad = (95 - jQuery('ul.footer-logos li#fl' + currentState + ' img').height()) / 2;
        jQuery('ul.footer-logos li#fl' + currentState).css('padding-top', topPad);

    });

    // Right Click Function
    jQuery('a.r-ctrl').click(function() {
        // fade out old logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeOut(1000);
        currentState = currentState + 1;
        // checks if current state is at the maximum length
        // and if it is, resets it back to 0
        if (currentState > jQuery('ul.footer-logos li').length - 1) {
            currentState = 0;
        }
        // fade in new logo
        jQuery('ul.footer-logos li#fl' + currentState).fadeIn(1000);

        // Vertically centers images inside list items
        topPad = (95 - jQuery('ul.footer-logos li#fl' + currentState + ' img').height()) / 2;
        jQuery('ul.footer-logos li#fl' + currentState).css('padding-top', topPad);

    });

}


// Popup window code
function popUp(url) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', '');");
}
