/**
 * @author Jerry
 */

var perPage=8;//Number of news items per page
var curPage=1;//Current page number
var numPages;//Number of pages

function news_init(lang){

//Begin news_init()

var xmlUrl;
switch(lang){
	case "cn":
	xmlUrl="news_cn.xml";
	break;

	case "de":
	xmlUrl="news_de.xml";
	break;

	default:
	xmlUrl="news_en.xml";
}

$(function(){
	$.ajax({
		dataType:"xml",
		url:xmlUrl,
		success:function(xml){
			//Calculate the number of pages.
			numPages=Math.ceil($("item",xml).length/perPage);
			
			//Build the first layer(page) to hold news items.
			var news_list_top=$("<div/>").addClass("news_list_top");
			var news_list=$("<div/>").addClass("news_list");
			$("item",xml).each(function(i){
				//News item
				var news_item=$("<div/>").addClass("news_item");


				//News title
				var news_title=$("<div/>").addClass("news_title")
					.hover(function(){$(this).addClass("news_title_hover");},
						function(){$(this).removeClass("news_title_hover");});
				$("<span/>")
					.text(generateDate($("pubDate",this).text()))
					.appendTo(news_title);
				$("<span/>").text($("title",this).text()).appendTo(news_title);
				news_title.appendTo(news_item);
				
				//News body
				var news_body=$("<div/>").addClass("news_body");
				//News image
				$("<img/>").attr("src",$("image",this).text())
					.attr("align","left")
					.css("margin-right","10px")
					.appendTo(news_body);
				//News text
				$("<p/>").html($("description",this).text())
					.appendTo(news_body);
				$("<div/>").css("clear","both").appendTo(news_body);
				$(news_body).appendTo(news_item);

				news_item.appendTo(news_list);

				//Decide whether to build a new layer
				if(i==$("item",xml).length-1)
					$(news_list).appendTo(news_list_top);
				else if((i+1)%perPage==0)
				{
					$(news_list).appendTo(news_list_top);
					news_list=$("<div/>").addClass("news_list");
				}
			});
			$(news_list_top).appendTo($(".content_details"));
			//Show the first layer only.
			$(".news_list:not(:first)").hide();
			//Show the first news item of each layer.
			$(".news_list").find(".news_title:first").addClass("news_title_active");
			$(".news_list").find(".news_item:first").addClass("news_item_active");
			$(".news_list").find(".news_body:not(:first)").hide();
			//When clicked, the news item will expand, while contracting the others.
			$(".news_title").click(function(){
				$(this).next(".news_body").slideDown("slow");
				$(this).parent().siblings().each(function(){
					$(".news_body:visible",this).slideUp("slow");
					$(".news_title_active",this).removeClass("news_title_active");
					$(this).removeClass("news_item_active");
				});
				$(this).addClass("news_title_active");
				$(this).parent().addClass("news_item_active");
			});
			
			if(typeof(active_news_item_number)!='undefined'){
				$(".news_list").find(".news_title:eq("+active_news_item_number+")").click();
			}

			//Pagination
			var news_page=$("<div/>").addClass("news_page");
			//Previous page button
			$("<img/>").attr("src","images/news/previous.gif")
				.css("cursor","pointer")
				.hover(function(){$(this).attr("src","images/news/previous_act.gif");},
					function(){$(this).attr("src","images/news/previous.gif");})
				.click(function(){if(curPage>1) $(".news_page > span").eq(curPage-2).click();})
				.appendTo(news_page);
			//Generate all page label buttons.
			for(var i=1;i<=numPages;++i)
			{
				$("<span/>").text(i)
					//When the page label is clicked, show the corresponding page.
					.click(function(){
						if(curPage!=$(this).text()){
							$(".news_list").eq($(this).text()-1).slideDown("slow")
								.siblings(".news_list:visible").slideUp("slow");
							curPage=Number($(this).text());
							$(this).addClass("active");
							$(this).siblings(".active").removeClass("active");
						}
					})
					.hover(function(){$(this).addClass("hover");},
						function(){$(this).removeClass("hover");})
					.appendTo(news_page);
			}
			//The first page is shown by default.
			$("> span:eq(0)",news_page).addClass("active");
			//Next page button
			$("<img/>").attr("src","images/news/next.gif")
				.css("cursor","pointer")
				.hover(function(){$(this).attr("src","images/news/next_act.gif");},
					function(){$(this).attr("src","images/news/next.gif");})
				.click(function(){if(curPage<numPages) $(".news_page > span").eq(curPage).click();})
				.appendTo(news_page);
			$(news_page).appendTo($(".content_details"));

				for(var i=0;i<3;++i)
			{
				var news_item=$("item",xml).eq(i);
				$("<a/>").text(generateDate($("pubDate",news_item).text())+$("title",news_item).text())
				.attr({href:"news_"+lang+".html?active="+i,target:"_self"})
				.hover(function(){$(this).addClass("news_title_hover");},
					function(){$(this).removeClass("news_title_hover");})
				.appendTo($(".hap_newslist"));
				$("<br/>").appendTo($(".hap_newslist"));
				
			}
			
		}
	});
});
			
			

}

/**
 * Generate a date string for English
 *
 * @param string _pubDate
 * @return string
 */
function generateDate(_pubDate)
{
	var pubDate=new Date(_pubDate);
	return pubDate.getFullYear()+"-"
		+((pubDate.getMonth()+1)<10 ? ("0"+(pubDate.getMonth()+1)) : (pubDate.getMonth()+1))+"-"
		+(pubDate.getDate()<10 ? ("0"+pubDate.getDate()) : pubDate.getDate());
}