$(function() {

	// banner rotator.
	$('.rotatingBannerContainer').bannerRotator({
		'overlayClass' : 'bannerOverlay',
		'textOverlay': true,
		'addDots': true,
		'textOverlayClass': 'bannerOverlayText',
		'dotImage' : '/images/site/grey-dot.png',
		'activeDotImage' : '/images/site/red-dot.png',
		'dotContainerClass' : 'bannerDots'
	});

	/* email input */
	if ( $('#cm-name').val() == '' ) {
		$('#cm-name').val('Your name...');
	}

	$('#cm-name').focus(function() {
		if ( $('#cm-name').val() == 'Your name...' ) {
			$('#cm-name').val('');
		}
	});

	$('#cm-name').blur(function() {
		if ( $('#cm-name').val() == '' ) {
			$('#cm-name').val('Your name...');
		}
	});

	$('#subForm').submit(function() {
		if ( $('#cm-name').val() == 'Your name...' ) {
			$('#cm-name').val('');
		}
	});

	/* renewal date input */
	if ( $('#mlluhd-mlluhd').val() == '' ) {
		$('#mlluhd-mlluhd').val('Your email...');
	}

	$('#mlluhd-mlluhd').focus(function() {
		if ( $('#mlluhd-mlluhd').val() == 'Your email...' ) {
			$('#mlluhd-mlluhd').val('');
		}
	});

	$('#mlluhd-mlluhd').blur(function() {
		if ( $('#mlluhd-mlluhd').val() == '' ) {
			$('#mlluhd-mlluhd').val('Your email...');
		}
	});

	$('#subForm').submit(function() {
		if ( $('#mlluhd-mlluhd').val() == 'Your email...' ) {
			$('#mlluhd-mlluhd').val('');
		}
	});


 	// suppliers slider
 	$('#slidingImages').imageSlider({
  		slideElement: 'div.slidingImage',
 		cloneHolder: false
 	});

	// comment slider
	$('#commentHolder').imageSlide({
 		slideElement: 'div.commentBlock',
 		nextHandle: 'div.commentRight',
 		previousHandle: 'div.commentLeft'
	});

	// code for the news slider.
	$('.newsController').click(function(event) {
		event.preventDefault;

		var movement = 0;
		// see which way we are going.
		if ( $(this).attr('alt') == 'down' ) {
			movement = $('.newsArticle').outerHeight();
		} else if ( $(this).attr('alt') == 'up' ) {
			movement -= $('.newsArticle').outerHeight();
		}

		// do the movement.
		$('#newsContainer').animate({scrollTop: $('#newsContainer').scrollTop() + movement});

		// check to see if we need to load more.
		var containerHeight = $('.newsArticle').length * $('.newsArticle').outerHeight();

		// we have 3 entries visible so use that to decide if we need to load more.
		// but we want to load before the last one appears. So use 4 as an entry.
		var tipPoint = 4 * $('.newsArticle').outerHeight();

		if ( $('#newsContainer').scrollTop() >= (containerHeight - tipPoint)-1 ) {

			// show the processing spinner.
			$('#newsProcessing').show();

			// do the ajax load.
			// build the data to send
			var postData = '';
			postData += 'action=newsLookup';
			postData += '&currentCount=' + $('div.newsArticle').length;

			// send it
			$.ajax({
				type: 'POST',
				url: '/',
				data: postData,
				success: function (responseData, textStatus, XMLHttPRequest) {

					$(responseData.articles).each(function(index, value) {
				 		content = '<div class="newsArticle">';
				 		content += '<div class="newsImage">';
				 		content += '<div class="imageHolder">';
				 		content += '<img src="' + value.imageUrl + '" alt="' + value.imageTitle + '" />';
				 		content += '</div></div>';
				 		content += '<div class="newsStory">';
				 		content += '<div class="storyTitle">' + value.newsTitle + '</div>';
				 		content += '<div class="storyDate">' + value.datePublished + '</div>';
				 		content += '<a href="' + value.url + '">read more &raquo;</a>';
				 		content += '</div><div class="clear"></div></div>';

						$(content).appendTo('#newsContainer');
					});

				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				},
				complete: function (event, ui) {
					$('#newsProcessing').hide();
				},
				dataType: 'json'
			});


		}


		return false;
	});


});

