var NZ = {		
	newsArray:[],
	currIndex:0,
	direction:'forward',				// which direction to animate the news in - back or forward
	isAnimating:false
};	


NZ.loadSection = function(){
	this.currIndex = 0;
	NZ.isAnimating = false;
	$('#news .content').empty();
	this.newsArray.length = 0;
	
	
	$('#news a.nextNews').die('click', NZ.goNextNews);
	$('#news a.nextNews').live('click', NZ.goNextNews);
	$('#news a.prevNews').die('click', NZ.goPrevNews);
	$('#news a.prevNews').live('click', NZ.goPrevNews);
	
	//read xml
	$.ajax({
	  type: "GET",
		 url: 'content/' + lang + '/news.xml',
		 context:$('#news'),
		 dataType: "xml",
		 success: function(xml) {
			$(xml).find('item').each(function(){
				var i = {};
				i.title = $(this).attr('title');
				i.date = $(this).attr('date');
				i.text = $(this).text();
				NZ.newsArray.push(i);
			});
			
			$(this).find('h1').html('news');
			Cufon.replace('h1');
			//$(this).find('.content').html($(divs[0]).html());
			NZ.showNews();
			PO.sectionLoaded = true;
			if(BGO.imageLoaded && !PO.showingSplashPage){
				PO.animateSection();
				return;
			}	
		 }
	}); //close $.ajax
	
	$(window).bind('keydown.news', function(e) {
		if(e.keyCode == 37){
			NZ.goPrevNews(e);
		}
		if(e.keyCode == 39){
			NZ.goNextNews(e);
		}
	});
};

NZ.showNews = function(){
	NZ.isAnimating = true;
	if(this.direction == 'back'){
		if($('#news .content').find('.newsItem').length > 0){
			$('#news .content .newsItem:first').animate({'left':$(window).width()}, 500, function(){
					$('#news .content .newsItem:first').remove();																		
			})
		}
		if($('#news .content').find('.nextItem').length > 0){
			$('#news .content .nextItem:first').animate({'left':-$(window).width()}, 500, function(){
					$('#news .content .nextItem:first').remove();																		
			})
		}
		if($('#news .content').find('.prevItem').length > 0){
			$('#news .content .prevItem:first').animate({'left':-$(window).width()}, 500, function(){
					$('#news .content .prevItem:first').remove();																		
			})
		}
	}else{
		if($('#news .content').find('.newsItem').length > 0){
			$('#news .content .newsItem:first').animate({'left':-$(window).width()}, 500, function(){
					$('#news .content .newsItem:first').remove();																		
			})
		}
		if($('#news .content').find('.nextItem').length > 0){
			$('#news .content .nextItem:first').animate({'left':$(window).width()}, 500, function(){
					$('#news .content .nextItem:first').remove();																		
			})
		}
		if($('#news .content').find('.prevItem').length > 0){
			$('#news .content .prevItem:first').animate({'left':$(window).width()}, 500, function(){
					$('#news .content .prevItem:first').remove();																		
			})
		}	
	}
	
	
	var newsObject = this.newsArray[this.currIndex];
	var newsItem = $('<div class="newsItem"></div>');
	newsItem.append('<div class="title">' + newsObject["title"] + '</div>');
	newsItem.append('<div class="date">' + newsObject["date"] + '</div>');
	newsItem.append('<div class="text">' + newsObject["text"] + '</div>');
	newsItem.appendTo($('#news .content'));
	if(this.direction == 'back'){
		newsItem.css({'left' : -$(window).width()});
	}else{
		newsItem.css({'left' : $(window).width()});
	}
	newsItem.animate({'left':0}, 500);
	
	var nextIndex = this.currIndex+1;
	if(nextIndex == this.newsArray.length){
		nextIndex = 0;			
	}
	
	if(this.newsArray.length <= 1){
		return;
	}
	
	var nextObject = this.newsArray[nextIndex];
	var nextItem = $('<div class="nextItem"></div>');
	nextItem.append('<div class="title"><a href="" class="nextNews">' + nextObject["date"] + ' . ' + nextObject["title"] + '</a></div>');
	nextItem.appendTo($('#news .content'));
	if(this.direction == 'back'){
		nextItem.css({'left' : -$(window).width()});
		nextItem.animate({'left':$(window).width()}, 10);
	}else{
		nextItem.css({'left' : -$(window).width()});
	}
	
	//need timeout otherwise 1st item has no width
	setTimeout(function(){nextItem.animate({'left':600 - nextItem.width()}, 500, function(){
		NZ.isAnimating = false;																				  
		});},100);
	
	if(this.newsArray.length <= 2){
		return;
	}
	
	var prevIndex = this.currIndex-1;
	if(prevIndex < 0){
		prevIndex = this.newsArray.length-1;			
	}
	
	var prevObject = this.newsArray[prevIndex];
	var prevItem = $('<div class="prevItem"></div>');
	prevItem.append('<div class="title"><a href="" class="prevNews">' + prevObject["date"] + ' . ' + prevObject["title"] + '</a></div>');
	prevItem.appendTo($('#news .content'));
	if(this.direction == 'back'){
		prevItem.css({'left' : $(window).width()});
	}else{
		prevItem.css({'left' : -$(window).width()});
	}
	
	setTimeout(function(){prevItem.animate({'left':0}, 500);},100);
}

NZ.goNextNews = function(e){
	if(PO.newSection != 'news'){
		$(window).unbind('.news');
		return;
	}	
	if(NZ.isAnimating){
		return;				  
	}
	NZ.direction='forward';
	e.preventDefault();
	NZ.currIndex++;
	if(NZ.currIndex == NZ.newsArray.length){
		NZ.currIndex = 0;			
	}
	NZ.showNews();
}

NZ.goPrevNews = function(e){
	if(PO.newSection != 'news'){
		$(window).unbind('.news');
		return;
	}	
	if(NZ.isAnimating){
		return;				  
	}
	NZ.direction='back';
	e.preventDefault();
	NZ.currIndex--;
	if(NZ.currIndex < 0){
		NZ.currIndex = NZ.newsArray.length-1;			
	}
	NZ.showNews();
}
