$(document).ready(function()
{
	$('#img-thumbs a:first').addClass('hi');
	$('#img-thumbs a:last').addClass('last');
	$('#img-thumbs a').click(function()
	{
		$('#img-large img:visible').hide();
		$('#main-'+$(this).attr('rel')).show();
		$('#img-thumbs a.hi').removeClass('hi');
		$(this).addClass('hi');
		return false;
	});


	$('#frm_student_lets').change(function()
	{
		if ($(this).is(':checked'))
		{
			$('#frm_bedrooms_from .prof').hide();
			$('#frm_bedrooms_from .student').show();
		}
	});
	$('#frm_professional_lets').change(function()
	{
		if ($(this).is(':checked'))
		{
			$('#frm_bedrooms_from .prof').show();
			$('#frm_bedrooms_from .student').hide();
		}
	});

	theRotator();
});

function theRotator ()
{
	//Set the opacity of all images to 0
	$('ul.rotate-img li').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('ul.rotate-img li:first').css({opacity: 1.0});
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',3000);
}

function rotate ()
{
	//Get the first image
	var current = ((typeof $('ul.rotate-img li.show').attr('class') != 'undefined')?  $('ul.rotate-img li.show') : $('ul.rotate-img li:first'));
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('ul.rotate-img li:first') :current.next()) : $('ul.rotate-img li:first'));
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};

function fadeOut() { $('.register-now').fadeTo(2500, 0.4, function() { fadeIn(); }); }
function fadeIn() { $('.register-now').fadeTo(3000, 1, function() { fadeOut(); }); }

var rules =
{
	'.result-images .img img' : function (element)
	{
		element.onmouseover = function ()
		{
			if (document.getElementById(element.alt))
			{
				var main_img = document.getElementById(element.alt);

				// create temp vars of current main image
				var tmp_img_src = main_img.src;
				var tmp_img_alt = main_img.alt;

				// update main image with new image
				main_img.src = element.parentNode.href;
				main_img.alt = element.src;

				// update image with old main image
				element.parentNode.href = tmp_img_src;
				element.src = tmp_img_alt;
			}
		}

		element.onclick = function ()
		{
			return false;
		}

		element.onkeypress = function ()
		{
			return false;
		}
	}
};

Behaviour.register(rules);
