

var first_time = true;



$(document).ready(function(){
	setTimeout(slideshow, 2000);
	
	$("div.SScrlDescr:eq(0)").css("left", $("div.SScrlTitle:eq(0)").width()+15);
	
});
				  
var SScrl_count;
var current_SScrl=0;
var titleChanged = descrChanged = false;
var currentTitle = 0;
var currentDescr = 0;



$.fn.equals = function(compareTo) {
        if (!compareTo || !compareTo.length || this.length!=compareTo.length) return false;
        if(this.html() != compareTo.html()) return false;
        return true;
}		


function SScrl_rotate() {
	if (first_time) {first_time = false; return;}
	
  old_SScrl = current_SScrl % SScrl_count;
  new_SScrl = ++current_SScrl % SScrl_count;
  titleChanged = descrChanged = false;
  
  //se sono diverse sposta via la vecchia descrizione
  if(! $("div.SScrlDescr:eq(" + currentDescr + ")").equals($("div.SScrlDescr:eq(" + new_SScrl + ")")) ) {
  	escrChanged = true;
    $("div.SScrlDescr:eq(" + currentDescr + ")").show().animate({left: 810},1000,function() {
      // quando hai finito sposta dentro la nuova descrizione
      $("div.SScrlDescr:eq(" + new_SScrl + ")").show().animate({left: $("div.SScrlTitle:eq(" + currentTitle + ")").width()+15},1000);
    });
	currentDescr = new_SScrl;
  }
  
  
  //se sono diversi sposta via il vecchio titolo
  if(! $("div.SScrlTitle:eq(" + currentTitle + ")").equals($("div.SScrlTitle:eq(" + new_SScrl + ")")) ) {
	titleChanged = true;
    $("div.SScrlTitle:eq(" + currentTitle + ")").show().animate({top: -20},1000, function() {
      // quando hai finito sposta dentro il nuovo titolo
      $("div.SScrlTitle:eq(" + new_SScrl + ")").show().animate({top: 5},1000);
	  if(titleChanged && !descrChanged) // se è cambiato il titolo ma non la descrizione sposto la descrizione alla fine del titolo
  	$("div.SScrlDescr:eq(" + currentDescr + ")").show().animate({left: $("div.SScrlTitle:eq(" + new_SScrl + ")").width()+15},"slow");
    });
	currentTitle = new_SScrl;
  }
  
  
  
  
  
}
	


function loadGallery() {
	
	$.ajax({
    type: "GET",
    url: "images/gallery.xml",
    dataType: "xml",
    success: parseXml
  });
	
}

function parseXml(xml) {
	
	var $ss = $('#slideshow');
	var $sD = $('#SplaceScroll');
	
	$(xml).find("image").each(function()
  	{
		$sD.append( '<div class="SScrlTitle">'+ $(this).find("title").text()+'</div><div class="SScrlDescr">'+ $(this).find("descr").text()+'</div>' );
		$ss.append('<img alt="'+$(this).find("alt").text()+'" src="'+$(this).find("link").text()+'" />');
  	});
	
	//$('#slideshow img:first-child').addClass('first');
	
}




	
function slideshow () {
	
	SScrl_count = $("div.SScrlTitle").size();
	
	
	$('#slideshow').cycle({ 
        	fx:      'fade',
			speed:	2000,
        	timeout:  2000,
        	before: SScrl_rotate,
        	next:   '#slideshow',
        	pause: 1
    	});
	
	
		
		  
	
 
    // start the slideshow 
    
	
}




function newsTest() {
	//keep the news wide as the photos
	var news = document.body.appendChild(document.createElement("div"));
	news.setAttribute("id","newstest");
	news.setAttribute("style","font-size:11px");
	news.innerHTML = '<span class="gray">05.2010</span> <span class="green">Splace has been selected for the international competition in Gamabach (CH) </span><span class="gray"> &nbsp;|&nbsp; 06.2010</span> <span class="pink">Splace has been awarded with special mention at FAV2010 in Monpellier (FR) </span>';

	var news = document.getElementById("newstest");
	var s;
	if(news.style.fontSize) {
		while(news.clientWidth>809) {
	
			s = news.style.fontSize.replace("px","")/1;
			s -= 1;
			news.style.fontSize = s+"px";
		
		}
	}
	document.getElementById("news").style.fontSize = s+"px";
	document.body.removeChild(news);
}


/*
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

function newsScroll () {
  headline_count = $("div.headline").size();
  $("div.headline:eq("+current_headline+")").css('top', '2px');
 
  headline_interval = setInterval(headline_rotate,8000);
  $('#scrollup').hover(function() {
    clearInterval(headline_interval);
  }, function() {
    headline_interval = setInterval(headline_rotate,8000);
    headline_rotate();
  });
}


function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count;
  $("div.headline:eq(" + old_headline + ")")
    .animate({top: -30},"slow", function() {
      $(this).css('top', '30px');
    });
  $("div.headline:eq(" + current_headline + ")")
    .animate({top: 2},"slow");  
  old_headline = current_headline;
}*/
