var objTimeout;
$(document).ready(function () {
	var strDefaultKeywordsText, strDefaultLocationText, intCurrent, intTotal, intTime, mixFadeTime, stopTimer, startTimer, rotateNextBanner;
	strDefaultKeywordsText = 'Keywords (eg Tennis)';
	strDefaultLocationText = 'Location (eg London)';
	intCurrent = 0;
	intTotal = $('.banner-3Rotator').length;
	objTimeout = null;
	intTime = 7000;
	mixFadeTime = 'slow';


	// Show the first one
	$('.banner-3Rotator:eq(0)').fadeIn(mixFadeTime);

	stopTimer = function () {
		if (objTimeout !== null) {
			clearTimeout(objTimeout);
			objTimeout = null;
		}
	};

	rotateNextBanner = function () {
		stopTimer();
		var intNext = intCurrent + 1;
		if (intNext > (intTotal - 1)) {
			intNext = 0;
		}
		$('.banner-3Rotator:eq(' + intCurrent + ')').fadeOut(mixFadeTime);
		$('.banner-3Rotator:eq(' + intNext + ')').fadeIn(mixFadeTime, startTimer);
		intCurrent = intNext;
	};

	startTimer = function () {
		if (objTimeout === null) {
			objTimeout = setTimeout(rotateNextBanner, intTime);
		}
	};


	startTimer();


	$('div#rotateContainer').hover(function () {
		stopTimer();
	}, function () {
		startTimer();
	});

	if ($('#searchKeywords').val() == '') {
		$('#searchKeywords').val(strDefaultKeywordsText).css('color', '#666');
	}

	$('#searchKeywords').bind('focus', function () {
		if ($('#searchKeywords').val() == strDefaultKeywordsText) {
			$('#searchKeywords').css('color', '#333').val('');
		}
	});
	$('#searchKeywords').bind('blur', function () {
		if ($('#searchKeywords').val() == '') {
			$('#searchKeywords').css('color', '#666').val(strDefaultKeywordsText);
		}
	});

	$('#strLocation').val(strDefaultLocationText).css('color', '#666').bind('focus', function () {
		if ($('#strLocation').val() == strDefaultLocationText) {
			$('#strLocation').css('color', '#333').val('');
		}
	}).bind('blur', function () {
		if ($('#strLocation').val() == '') {
			$('#strLocation').css('color', '#333').val(strDefaultLocationText);
		}
	});


	$('#btnSearch').bind('click', function () {
		if ($('#searchKeywords').val() == strDefaultKeywordsText) {

			$('#searchKeywords').val('');
			//alert('Please enter a keyword');
			//$('#searchKeywords').focus();
			//return false;
		}

		if ($('#strLocation').val() == strDefaultLocationText) {
			alert('Please enter a location');
			$('#strLocation').focus();
			return false;
		}
	});

	// Number of items in the scrolly bit
	var intNewsScrollerItems = $('div#scroller_container div.scroll_box').length;
	var strMinLeft = 900 - (intNewsScrollerItems * 300) + 'px';
	var blnScroll = false;

	$('#dRAD').hide();
	$('#dLAD').hide();

	var checkArrows = function () {
		var strPos = $('div#scroller_container').css('left');
		if (strPos == strMinLeft) {
			$('#dRAD').hide();
		} else {
			$('#dRAD').show();
		}

		if (strPos == '0px') {
			$('#dLAD').hide();
		} else {
			$('#dLAD').show();
		}

		//
	}
	blnScroll = true;
	$('div#scroller_container').animate({left: '0px'}, function () {checkArrows();blnScroll = false});


	$('div#right_arrow').bind('click', function(){
		if (!blnScroll && strMinLeft != $('div#scroller_container').css('left')) {
			blnScroll = true;
			$('div#scroller_container').animate({left: '-=300'}, function(){checkArrows();blnScroll = false});
		}
	});

	$('div#left_arrow').bind('click', function(){
		if (!blnScroll && '0px' != $('div#scroller_container').css('left')) {
			blnScroll = true;
			$('div#scroller_container').animate({left: '+=300'},function(){checkArrows();blnScroll=false});
		}
	});

});
