//if you want to use jquery
$(document).ready(function () {


	/* add SearchButtonHover class to Search Go Button as :hover does not work in IE8 */
	$('.SearchButton').hover(function () {
		$(this).addClass('SearchButtonHover')
	}, function () {
		$(this).removeClass('SearchButtonHover')
	})

	globalNavSetup();
	searchClear();
	$("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'light_rounded' });
	
	$('#bookmark').jFav();

	// for all map instances
	$('.findUsMap').each(function () {
		// for all pop up instances
		$('.popup').each(function () {
			// common vars
			var $popUp = $(this);
			var id = $popUp.attr('id');
			var hideDelayTimer = null;

			// effect values
			var distance = 10;
			var time = 250;
			var hideDelay = 500;
			var beingShown = false;
			var shown = false;

			// binds a 'show' event to the pop up
			$popUp.bind('show', function () {
				// stops the hide event if we move from the trigger to the popup element
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				// don't perform if show or currently being shown
				if (beingShown || shown)
					return;
				else {
					beingShown = true;

					// vars
					var $this = $(this);
					var school = $this.attr('id').replace('popup', '');
					var popupHeight = $this.height();
					var posTop = 0;
					var posLeft = 0;

					// layout depending on popup being shown
					switch (school.toLowerCase()) {
						case 'hillingdon':
							posTop = -145 - popupHeight;
							posLeft = 36;
							break;
						case 'egham':
							posTop = -90 - popupHeight;
							posLeft = -19;
							break;
						case 'cobham':
							posTop = -57 - popupHeight;
							posLeft = 35;
							break;
						case 'doha':
							posTop = -80 - popupHeight;
							posLeft = 68;
							break;
					}

					// reset position of popup box
					$this.css({
						top: posTop,
						left: posLeft,
						display: 'block' // brings the popup back in to view
					}).animate({ // (we're using chaining on the popup) now animate it's opacity and position
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function () {
						// once the animation is complete, set the tracker variables
						beingShown = false;
						shown = true;
					});
				}
			});

			// binds a 'hide' event to the pop up
			$popUp.bind('hide', function () {
				// reset the timer if we get fired again - avoids double animations
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				// store the object
				var $this = $(this);

				// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					$this.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						// hide the popup entirely after the effect (opacity alone doesn't do the job)
						$this.css('display', 'none');
						shown = false;
					});
				}, hideDelay);
			});
		});

		// attach mouseover trigger to call the show method
		var triggers = $('.trigger');
		triggers.mouseover(function () {
			var $this = $(this);
			var popUpId = $this.attr('id').replace('area', 'popup');
			$('#' + popUpId).trigger('show');
		});

		// attach mouseout trigger to call the hide method
		triggers.mouseout(function () {
			var $this = $(this);
			var popUpId = $this.attr('id').replace('area', 'popup');
			$('#' + popUpId).trigger('hide');
		});
	});


	inputClear('SeachInput', 'Search this Site');

	if ($('#find_us').length > 0) {
		$('#find_us').tabs();
	}


});



// Clears input box onFocus, and adds back in if empty onBlur
// takes the ID of the input box and the default text value
function inputClear(inputID, inputText) {
    $('.' + inputID).focus(function () {
        if ($(this).attr('value').toLowerCase() == inputText.toLowerCase())
            $(this).attr('value', '');
    });
    $('.' + inputID).blur(function () {
        if ($(this).attr('value') == '')
            $(this).attr('value', inputText);
    });
}


function globalNavSetup() {
    activeItem = '';

    $('#globalNav li').not('.hoverMenuWrap a').hover(
	    function() {
	        $(this).addClass('hover');
	        activeItem = $(this).parent('ul').attr('class');
	        $(this).attr('xClass', activeItem)
	        $(this).parent('ul').removeClass().addClass('navPos' + ($(this).index() + 1));
	    },
	    function() {
	        $(this).removeClass('hover');
	        activeItem = $(this).attr('xClass');
	        $(this).parent('ul').removeClass().addClass(activeItem);
	    });


    $('#globalNav').hover(
		function() {
		    $('#bannerGrad').stop().animate({ 'opacity': '0.75' });
		},
		function() {
		    $('#bannerGrad').stop().animate({ 'opacity': '0' });
		});

}

/* used to get rid of _blank so your code validates w3c stylee */
function setExternalLinks() {
    var el_list = document.getElementsByTagName('A');
    for (i = 0; i < el_list.length; i++) {
        if (el_list[i].getAttribute('rel') == 'external') {
            el_list[i].setAttribute('target', '_blank');
        }
    }
}

// Clears search text onFocus, and adds back in if empty onBlur
function searchClear() {
    $('#searchText').focus(function() {
        if ($(this).attr('value').toLowerCase() == 'search this site')
            $(this).attr('value', '');
    });
    $('#searchText').blur(function() {
        if ($(this).attr('value') == '')
            $(this).attr('value', 'Search this site');
    });
}

/* Check's users version of Flash */
function checkFlash(version) {
    return DetectFlashVer(version, 0, 0);
}


function imageCarousel() {

    /* Creates the padding on the left to ensure if more items have been added then the boxes wont overlap the text */
    var carNavList = $('#carNav').outerWidth() + 20;
    $('#bannerMessage').css('padding-left', carNavList);

    var currentAd = 0;

    function fadeBanner() {
        if (currentAd < $('#carNav a').length - 1) {
            currentAd++;
        } else {
            currentAd = 0;
        }

        goToBanner(currentAd);
    }

    function goToBanner(bannerNum) {
        $currentBanner = $('#banner img:visible');
        $currentMessage = $('#bannerMessage div:visible');

        $currentBanner.css('z-index', '1');
        $currentMessage.css('z-index', '1');

        $('#banner img').eq(bannerNum).css('z-index', '2').fadeIn('slow', function() {
            $currentBanner.fadeOut('fast');
        });

        $('#bannerMessage div').eq(bannerNum).css('z-index', '2').fadeIn('medium', function() {
            $currentMessage.fadeOut('medium');
        });

        $('#carNav li').removeClass('active').eq(bannerNum).addClass('active');
    }


    $('img.hide').removeClass('hide').hide();

    timeOut = setInterval(fadeBanner, 5000);

    $('#carNav a').click(function(event) {
        if (!($(this).parent('li').hasClass('active')) && $('#banner img:animated').length == 0) {
            clearTimeout(timeOut);
            currentAd = $('#carNav a').index($(this));
            goToBanner(currentAd);
            timeOut = setInterval(fadeBanner, 5000);
        }

        event.preventDefault();
    });




    $('.bannerNav a').click(function(event) {
        if (!($(this).hasClass('active'))) {

            clearTimeout(timeOut);
            currentAd = $('.bannerNav a').index($(this));
            goToBanner(currentAd);

            timeOut = setInterval(fadeBanner, 8000);
        }
        event.preventDefault();
    });    

    


}

