$(document).ready(function() {
	
// extention
	jQuery.extend({
			random: function(X) {
			    return Math.floor(X * (Math.random() % 1));
			},
			randomBetween: function(MinV, MaxV) {
			  return MinV + jQuery.random(MaxV - MinV + 1);
			}
		});
	
// lightbox	
	$('.lightbox').fancybox();
	$('.postimg').fancybox();
	
// home slider
	var homeimgcount = $('#home-slide-main').children().size(); //number of images
	var current = 1;
	var next = current + 1; // next slide to come
	if (next == homeimgcount + 1) {
		next = 1;
	}
	$('#homemain-' + current).addClass("selected");
	$('#home-slide-main li[class!=selected]').hide();
	fader_delay = 5000; //time between automatic change (milliseconds)
	animation_duration = 500;
	animation_fast = 300;
	z_counter = 1000;
	clickCounter = 0;
	counter = 0;
	oldcounter = 0;
	animate=true;
	
//	alert(homeimgcount + ", " + current + ", " + next);
	
	function nextImg() {
		animate=false;
		z_counter++;
		counter++;
		oldcounter = counter;
		$('#homemain-' + next).show().css("opacity","1") //displays the next image to come in the background
		$('#home-slide-main .selected').animate({ opacity:0 } , animation_duration ).css("z-index",z_counter);; //fades the current image out
		setTimeout(function() { // delays the next function
			$('#home-slide-main .selected').removeClass('selected'); // removes the selected class from the previous
			$('#homemain-' + next).addClass('selected'); // adds the selected class to the new image
		}, animation_duration);
		setTimeout(function() {
			if(oldcounter == counter){
				animate=true;
				playNext(); //runs the command to loop
			}
		}, animation_duration);
	};
	
	function playNext() {
		if(next==homeimgcount) { // loops the variable
			next = 1;
		} else {
			next++;
		}
		if(current==homeimgcount) { // loops the variable
			current = 1;
		} else {
			current++;
		}
		setTimeout(function() { // delays the next function
			nextImg(); // runs the effect
		}, fader_delay);
	};
	
	function playFirst() {
		setTimeout(function() {
			if(homeimgcount>=2) { // only runs if there is at least two images
				$('#homemain-' + next).show().css("opacity","1"); //displays the next image to come in the background
				$('#home-slide-main .selected').animate({ opacity:0 } , animation_duration ); //fades the current image out
				setTimeout(function() { // delays the next function
					$('#home-slide-main .selected').removeClass('selected'); // removes the selected class from the previous
					$('#homemain-' + next).addClass('selected'); // adds the selected class to the new image
					playNext(); //runs the command to loop
				}, animation_duration);
			};
		}, fader_delay);
	}
	
	$('#home-slide-nav a').click(function(event){
		animate=false;
		event.preventDefault();
		clickNext = $(this).attr('rel');
		clickCounter++;
		counter++;
		z_counter++;
		$('#homemain-' + clickNext).show().css("opacity","1"); //displays the next image to come in the background
		$('#home-slide-main .selected').animate({ opacity:0 } , animation_duration ).css("z-index",z_counter); //fades the current image out
		setTimeout(function() { // delays the next function
			$('#home-slide-main .selected').removeClass('selected'); // removes the selected class from the previous
			$('#homemain-' + clickNext).addClass('selected'); // adds the selected class to the new image
			next = clickNext;
			animate=true;
			playNext(); //runs the command to loop
		}, animation_duration);	
	})	
	
	playFirst();

});
