$(function(){
	/*if (slideShow = true) {
		$(".selected").load(function(){
			if (moving == false){
				moving = true;
				clearInterval(timeout);
				timeout = setInterval(scrollDown, 2000);
			}
			return false;
		});
	};*/
	//Transparency
	$("#homeBox").css({opacity: 0.8});
	//Align h3 in the middle
	$("#homeScroll ul").find("li").each(function(){
		$(this).find("h3:first").css('marginTop', (((($(this).height()-$(this).find("h3:first").height())/2))-3)+'px');
	});
	//Scroll
	$('#main').mousewheel(function(event, delta){
		if (delta > 0){
			if (moving == false){
				slideShow = false;
				moving = true;
				scrollUp();
			}
		} else if (delta < 0) {
			if (moving == false){
				slideShow = false;
				moving = true;
				scrollDown();
			}
		}
	});
	$(".up").mousedown(function(){
		if (moving == false){
			slideShow = false;
			moving = true;
			scrollUp();
			clearInterval(timeout);
			timeout = setInterval(scrollUp, 400);
		}
		return false;
	});
	$(".up").mouseup(function(){
		clearInterval(timeout);
	});
	$(".down").mousedown(function(){
		if (moving == false){
			slideShow = false;
			moving = true;
			scrollDown();
			clearInterval(timeout);
			timeout = setInterval(scrollDown, 400);
		}
		return false;
	});
	$(".down").mouseup(function(){
		clearInterval(timeout);
	});
	
	//[AJAX] First Picture
	changeImg($("#homeScroll").find("li:first a"));
	
	//Change Info and Picture [AJAX]
	$("#homeScroll li a").click(function(){
		slideShow = false;
		changeImg($(this));
		return false;	
	});
	
	//SlideShow
	var numberLi = $("#homeScroll li").length;
});

//Scroll
var timeout;
var moving = false;
var slideShow = true;
var elementSelected = 0;

function scrollUp(){
	var topActual = ($("#homeScroll ul").offset().top-$("#scroller").offset().top);
	if ($("#homeScroll ul").offset().top-$("#scroller").offset().top < 0) {
		$("#homeScroll ul").animate({top: (topActual+77)}, 300, null, function(){moving = false;});
	}else{
		moving = false;
	}
}

function scrollDown(){
	var topActual = ($("#homeScroll ul").offset().top-$("#scroller").offset().top);
	var lastLi = $("#homeScroll ul").find("li:last").offset().top;
	if ($("#homeScroll ul").height() > $("#scroller").height() && lastLi > $("#scroller").height()+(2*$("#scroller ul li").height())) {
		$("#homeScroll ul").animate({top: (topActual-77)}, 300, null, function(){moving = false;});
	}else{
		moving = false;
	}
}

function changeImg(object){
	$("#homeScroll li").find(".selected").removeClass();
	object.addClass('selected');
	object.blur();
	var img = $('#loading');
	var imgBug = $(".png32");
	$('#ajaxContent').fadeOut(300);
	$.ajax({
	  type:"GET",
	  url: object.attr('href'),
	  success:function(data){
		$('#ajaxContent').empty().html(data);
		$('.png32').load(function(){
			img.hide();
			$('#ajaxContent').fadeIn(400);
			$("#homeBox").animate({top: 270}, 1000, 'easeout');
			$("#homeBox").css({opacity: 0.9});
			setTimeout(nextImg, 2500);
		});
	  },
	  complete: function(){
	  	//img.hide();
	  },
	  beforeSend: function(){
	  	img.show();
	  }
	});
}

function nextImg(){
	if (slideShow == true) {
		elementSelected++;
		if ($("#homeScroll li").size() == elementSelected) {
			changeImg($("#homeScroll li:first a"));
			//$("#homeScroll li:first a").click();
			elementSelected = 0;
			$("#homeScroll ul").animate({top: 0}, 300, null, function(){moving = false;});
		}else{
			changeImg($("#homeScroll .selected").parent().next().find('a'));
			//$("#homeScroll .selected").parent().next().find('a').click();
		}
		
		if (elementSelected >= 4) {
			scrollDown();
		};
	};
}
