var $j = jQuery.noConflict();

// Use jQuery via jQuery(...)
jQuery(document).ready(function(){

	// hide all elements with a class of hide
	$j('.hide').hide();

	// id of the current school being displayed
	var id = '';
	// the class of the school info
	var school_class = '';
	// is a school already being displayed
	var active = false;

	// get the current mouse position and move the school info
	var positionSchool = function(event) {
		// get co-ordinates
		var tPosX = event.pageX-590;
		var tPosY = event.pageY-300;

		// only set if no school's are active
		if(active==false) {
			$j(school_class).css({top: tPosY, left: tPosX});
			// set active so that only one school is displayed at a time
			active = true;
		}
	};

	// when a link is hovered over/out
	$j(".school").hover(
		function(e) {
			// get the id of the current school
			id = $j(this).attr('id');
			// create the school class
			school_class = '.'+id;

			// only show a single school info box
			if(active == false) {
				// set div position
				positionSchool(e);
				// show the school info
				$j(school_class).fadeIn("normal");
			}
		},
		function() {
			// hide the schoolinfo
			//$j(school_class).hide();
		}
	//).mousemove(positionSchool);
	);
	
	// close the school
	$j(".cancel").click(function(){
		// hide school
		$j(this).parent().fadeOut("normal",function(){
			// set status to false
			active = false;
		});

	return false;
	});

	// remove the click action from the school
	$j(".school").click(function(){
		return false;
	});

	var start 		= 1;
	var num_images 	= 0;
	var displaying 	= 3;
	var images = new Array();

	// get number of images
	if( $j('#images').length ) {
		// get all images
		images = $j('#images .image');
		// get number of images
		num_images = images.length;
		// add all images to the left
		$j('#images .image:first').before( $j('#images').html() );
		// calc offset
		var offset = 175 * num_images;
		$j('#images').css({'left':'-'+offset+'px'});
	}
	
	$j('#left_link').click(function(){
		// move right
		$j('#images').animate({"left":"+=175px"},'normal','',function(){
			pos = (num_images*-1)+1;
			if(start == pos) {
				// move back to start
				$j('#images').css({'left':'-'+offset+'px'});
			}
		});

		// decrement start image
		start = start-1;
		
		// reset index
		if(start == (num_images*-1)) {
			start=0;
		}

		//console.log('start:'+start);
	return false;
	});


	$j('#right_link').click(function(){
		// move left
		$j('#images').animate({"left":"-=175px"},'normal','',function(){
			if( start==(num_images-2) ) {
				$j('#images').css({'left':'-875px'});
			}
		});
		// increment start image
		start = start+1;
		//console.log(start);

		if(start > num_images) {
			start = 1;
		}

		/*
		if(start+displaying > num_images) {
			// get next image
			var index = start+displaying-num_images;
			if(index >= num_images) {
				//console.log('start index:'+index);
				for(var i=index; i>(num_images-1) ; i=i-num_images) {
					index = i;
				}
				index = i;
				//console.log('end index:'+i);
			}
			imageHTML = images[index].innerHTML;
			$j('#images .image:last').after('<div class="image">'+imageHTML+'</div>');
			console.log('added image index:'+index);
		}
		*/
	return false;
	});
	
});