var News = {
  objects: [],
  mainBox: null,
  current: 0,
  timeout: 10000,
  duration: 1500,
  resizeDuration: 150,
  currentTimeout: null,
  clicked: -1,
  
  downloadList: function() {
  	$.ajax({
		  url: "/newsXmlList.xml",
		  success: News.retrievingSuccessful,
		  error: News.retrievingFailed
		});
  },
  
  retrievingSuccessful: function(data, textStatus, jqXHR) {
  	var jData = $( data );
  	var items = jData.find("item");
  	
  	News.mainBox = $( ".newsImageBox" )[0];
  	
  	$("<div>", {
  		"class": "message",  
  		text: 'Ładowanie...'
  		}).appendTo(News.mainBox);
  	
  	var table = $("#littleTable")[0];
  	
  	for(var i = 0; i < $(items).size(); i++)	{
  		var item = items[i];
  		var tmpObj = { imagesLoaded: 0, t: i }
  		News.objects.push( tmpObj );
  		
  		tmpObj.box = $("<div>", {
  			"class": "newsImageBox"  
  		});
  		
  		tmpObj.newsSnippetBox = $("<div>", {
  			"class": "newsSnippetBox"
  		}).appendTo(tmpObj.box);
  		
  		$("<a>", {
  			"class": "newsTitleBox",
  			"href" : "/news/details/" + $(item).find('id').text(),
  			text: $(item).find('title').text()
  			
  		}).appendTo(tmpObj.newsSnippetBox); 
  		
  		var txt = $("<div>", {
  			"class": "newsSnippet",
  			text: htmlEntitiesConvert($(item).find('content').text())
  		}).appendTo(tmpObj.newsSnippetBox)[0];
  		
  		$("<a>", {
  			"href" : "/news/details/" + $(item).find('id').text(),
  			"class": "newsMore",
  			text: "więcej"
  		}).appendTo(txt);
  		
  		
  		
  		tmpObj.littleBox = $("<td>", {
  			"class": "littleImageBox"
  		}).appendTo(
  				$("<tr>").appendTo(table)
  				);
  		
  		tmpObj.bwImage = $("<img>", {
  			"class": "newsLittleImage" 
  		}).bind("load", tmpObj,  News.pictureLoaded)[0];
  		
  		tmpObj.colImage = $("<img>", {
  			"class": "newsLittleImage" 
  		}).bind("load", tmpObj,  News.pictureLoaded)[0];
  		
  		tmpObj.loadingImage = $("<img>", {
  			"class": "message",  
  			"src": '/images/loading.gif'
  		}).appendTo(tmpObj.littleBox);
  		
  		tmpObj.loadingImage2 = $("<img>", {
  			"class": "message",  
  			"src": '/images/loading.gif'
  		}).appendTo(tmpObj.box);
  		
  		
  		tmpObj.bigImage = $("<a>", {
  			"href" : "/news/details/" + $(item).find('id').text(),
  			"class": "newsMore"
  		}).append($("<img>", {
  			"class": "newsImage",
  			"src": "/images/news/bw/" + $(item).find('image').text()  
  		}));
  		
  		
  		$(tmpObj.bwImage).attr("src", "/images/news/bw/" + $(item).find('image').text());
  		$(tmpObj.colImage).attr("src", "/images/news/" + $(item).find('image').text());
  		
  		
  		$(tmpObj.colImage).bind("mouseover", tmpObj.colImage,  News.littleMouseIn)
  			.bind("mouseout", tmpObj.colImage,  News.littleMouseOut);
  		
  		$(tmpObj.bwImage).bind("mouseover", tmpObj.bwImage,  News.littleMouseIn)
  			.bind("mouseout", tmpObj.bwImage,  News.littleMouseOut)
  			.bind("click", ($(News.objects).size() - 1), News.littleClicked);
  		
  	}
  	
  	$(News.mainBox).empty();
  	
  	$(News.mainBox).append(News.objects[0].box);

  	News.currentTimeout = setTimeout("News.moveNews(1)", News.timeout);
  	
  },
  retrievingFailed: function(data, textStatus, jqXHR) {
  	
  },
  
  littleClicked: function (event) {
  	var number = event.data;
  	if(News.currentTimeout != null) {
  		clearTimeout(News.currentTimeout);
  		News.moveNews(number);
  	} else News.clicked = number;
  },
  
  littleMouseIn: function (event) {
  	$(event.data).stop(false, false);
  	$(event.data).animate(
  			{maxHeight: 74, maxWidth: 107}, 
  			{ duration: News.resizeDuration, easing: 'easeOutExpo'});
  },
  
  littleMouseOut: function (event) {
  	$(event.data).stop(false, false);
  	$(event.data).animate(
  			{maxHeight: 54, maxWidth: 79}, 
  			{ duration: News.resizeDuration, easing: 'easeOutExpo'});
  },
  
  
  pictureLoaded: function (event) {
  				var tmpObj = event.data;
  				tmpObj.imagesLoaded++;
  				
  				if(tmpObj.imagesLoaded == 2) {
  					$(tmpObj.loadingImage).remove();
  					$(tmpObj.loadingImage2).remove();
  					$(tmpObj.bigImage).appendTo(tmpObj.box);
  					
  					
  					if(News.objects[News.current] == tmpObj) {
  						$(tmpObj.colImage).appendTo(tmpObj.littleBox);
  						$(tmpObj.colImage).bind("mouseover", tmpObj.colImage,  News.littleMouseIn)
	  						.bind("mouseout", tmpObj.colImage,  News.littleMouseOut);
  						}
  					else {
  						$(tmpObj.bwImage).appendTo(tmpObj.littleBox);
  						$(tmpObj.bwImage).bind("mouseover", tmpObj.bwImage,  News.littleMouseIn)
	  						.bind("mouseout", tmpObj.bwImage,  News.littleMouseOut)
	  						.bind("click", News.objects.indexOf(tmpObj),  News.littleClicked);
  						}	
  				} 
  				
  			},
  
 
  switchTo: function(event) {
  	clearTimeout(News.currentTimeout);
  	News.currentTimeout = setTimeout("News.moveNews(" + event.data.number + ")", News.duration);
  },
  
  moveNews: function(next) {
  	News.currentTimeout = null;
  	
  	if(News.current != next) {
	  	var t1 = News.objects[News.current];
	  	var t2 = News.objects[next];
	  	
	  	if(t1.imagesLoaded == 2) {
		  	$(t1.colImage).remove();
		  	$(t1.bwImage).appendTo(t1.littleBox);
		  	
		  	$(t1.bwImage).bind("mouseover", t1.bwImage,  News.littleMouseIn)
	  			.bind("mouseout", t1.bwImage,  News.littleMouseOut)
	  			.bind("click", News.current,  News.littleClicked);
	  	}
	  	if(t2.imagesLoaded == 2) {
		  	$(t2.bwImage).remove();
		  	$(t2.colImage).appendTo(t2.littleBox);
		  	
		  	$(t2.colImage).bind("mouseover", t2.colImage,  News.littleMouseIn)
	  			.bind("mouseout", t2.colImage,  News.littleMouseOut);
	  		
	  		}
	  		
	  	
	  	$(t2.box).css('left', "680px");  	
	  	$(News.mainBox).append(t2.box);
	  	
	  	$(t1.box).animate({left: -680}, { duration: News.duration, easing: 'easeOutExpo', complete: function() {
	  		$(this).detach();
	  		}});
	  		
	  	$(t2.box).find("a.newsTitleBox").css('visibility', 'hidden');	
	  	$(t2.box).animate({left: 0}, { duration: News.duration, easing: 'easeOutExpo', complete: function() {
	  		$("a.newsTitleBox").css('visibility', 'visible');
	  		$("a.newsTitleBox").typewriter();
	  		if(News.clicked >= 0) {
	  			var tmp = News.clicked;
	  			News.clicked = -1;
	  			News.moveNews(tmp);
	  		}
	  	}});
	  	News.current = next;
	  	}
	  	
  	var nextNumber = (News.current + 1) % $(News.objects).size();
  	News.currentTimeout = setTimeout("News.moveNews(" + nextNumber + ")", News.timeout);
  }
}

var Cooperation = {
	images1: [],
	current1: 0,
	speed: 700,
	timeout: 3000,
	number1: 0,
	images2: [],
	current2: 0,
	number2: 0,
	
	init: function() {
		var tmp = $('div#coop').find("a");
		
		Cooperation.number1 = $(tmp).size();
		
		for(i = 0; i < Cooperation.number1; i++)
		{
			Cooperation.images1[i] = $(tmp).get(i);
			$(Cooperation.images1[i]).css("top", "90px");
		} 
		if(Cooperation.number1 > 0) $(Cooperation.images1[0]).animate({top: 0}, Cooperation.speed);
		
		tmp = $('div#part').find("a");
		
		Cooperation.number2 = $(tmp).size();
		
		for(i = 0; i < Cooperation.number2; i++)
		{
			Cooperation.images2[i] = $(tmp).get(i);
			$(Cooperation.images2[i]).css("top", "90px");
		} 
		if(Cooperation.number2 > 0) $(Cooperation.images2[0]).animate({top: 0}, Cooperation.speed);
		setInterval("Cooperation.move()", Cooperation.timeout);
	
	},
	
	move: function() {
		if(Cooperation.number1 > 0) {
			var next = (Cooperation.current1 + 1) % Cooperation.number1;
			$(Cooperation.images1[Cooperation.current1]).animate({top: -90}, Cooperation.speed);
			$(Cooperation.images1[next]).css("top", 90).animate({top: 0}, Cooperation.speed);
			Cooperation.current1 = next;
			}
		if(Cooperation.number2 > 0) {
			var next2 = (Cooperation.current2 + 1) % Cooperation.number2;
			$(Cooperation.images2[Cooperation.current2]).animate({top: -90}, Cooperation.speed);
			$(Cooperation.images2[next2]).css("top", 90).animate({top: 0}, Cooperation.speed);
			Cooperation.current2 = next2;
			}
	}

};


$(document).ready(News.downloadList);
$(document).ready(Cooperation.init);
