var versus=-3;
var minvel=2;
var mouseX=0;
var oldmouse=0;
var isOver=false;
var socDebug=false;
var startPoint=10;
var quante=0;
var larghezza=0;

$(document).ready(function(){
	initialAnimation();
	fW=$(document).width();
	if(socDebug && $('#slideView').length!=0){
		$("<div id='socDebug'></div>").appendTo('body');
	}
	$("#slideView").mouseenter(function(e){
		oldmouse = e.pageX;
		isOver=true;
	}).mouseleave(function(){
	    isOver=false;
	});
	$(document).mousemove(function(e){
		if (isOver) mouseX = e.pageX;
	});
	$("div").each(function (index, domEle) {
        if ($(this).attr("class")=="slideCard") {
			quante++;
			larghezza = quante * (20 + $(this).width());
			$(this).mouseenter(function(){
				isOver=false;
				if (versus != 0) {
					if (versus < 0) 
						versus = -minvel;
					else 
						versus = minvel;
				}
			}).mouseleave(function(){
			    isOver=true;
			});
		}
    });
	var move=window.setInterval("doMove()",30);
	var mouse=window.setInterval("calcVersus()",300);
});

function initialAnimation() {
	$("div").each(function (index, domEle) {
        if ($(this).attr("class")=="mosaico" && $(this).css("display")=="none") {
			$(this).show(300,initialAnimation);
			return false;
		}
    });
}	
function enlarge(div){
	calcVersus();
	//$("#"+div).dequeue();
	$("#"+div).animate({width: 250,height: 300,marginTop:-30}, 300,function(){
		$("#"+div).dequeue();
		$("#"+div).children("div").slideDown(300);
	});
}
function reduce(div){
	//$("#" + div).dequeue();
	$("#" + div).animate({width: 200,height: 200,marginTop: 20}, 300,function(){
		$("#"+div).dequeue();
		$("#" + div).children("div").slideUp(300);
	});
}

function calcVersus(){
	var coeff;
	if (oldmouse - mouseX>100) versus = (oldmouse-mouseX)/15+minvel;
	else if (oldmouse - mouseX<-100) versus = (oldmouse-mouseX)/15-minvel;
	else {
		if (isOver)coeff=($(document).width()/larghezza)*3;
		else coeff=($(document).width()/larghezza)*20;
		if (versus > minvel + coeff )versus=versus-coeff;
		else if (versus < -(minvel + coeff)) versus=versus+coeff;
	}
	oldmouse = mouseX;
}

function doMove(){
	if (larghezza > $(document).width()) {
		var scheda;
		conta = 0;
		$("div").each(function(index, domEle){
			if ($(this).attr("class") == "slideCard") {
				if (!isNaN(parseInt($(this).css('left').substr(0, $(this).css('left').length - 2)))) {
					pos = parseInt($(this).css('left').substr(0, $(this).css('left').length - 2));
				}
				else {
					pos = conta;
				}
				if ($(this).attr("id") == "scheda0" && versus > 0 && pos + versus > startPoint + $(document).width() / 20) 
					versus = 0;
				if ($(this).attr("id") == "scheda0" && versus < 0 && pos + versus + larghezza < $(document).width() - $(document).width() / 10) 
					versus = 0;
				$(this).css("left", (pos + versus) + "px");
				conta++;
			}
		});
	}
}

function ahah(url, target) {
  $('#'+target).text('Loading video...');
  $.ajax({ 
  url: url, 
  context: document.body, 
  success: function(data) {
  	$('#'+target).html(data);
   }});
}  
function changevideo(video){
	ahah("../portavideo.php?video="+video,"portavideo");
}
