var jsonDelicius;
var newsDisplayed = new Array(3);

function getDeliciusJson(){
	$.getJSON("http://feeds.delicious.com/v2/json/porternovelliinthenews?count=30&callback=?", 
		function (json){
			jsonDelicius = 	json;
			//alert(json[0].d);
			fillUpNews();
		}
	);
}

function pageselectCallback(page_index, jq){
	// Get number of elements per pagionation page from form
	var items_per_page = 10;
	var max_elem = Math.min((page_index+1) * items_per_page, jsonDelicius.length);
	var newcontent = '';
	
	cont = 0
	// Iterate through a selection of the content and build an HTML string
	for(var i=page_index*items_per_page;i<max_elem;i++)
	{
		newcontent += "<div class='news_links_alt'>";
		newcontent += "<div class='news_sidebar_date'>"+formatedDate(jsonDelicius[i].dt, true)+"</div>";
		newcontent += "<div class='news_sidebar_link_alt'><a href='"+ jsonDelicius[i].u+"'>"+jsonDelicius[i].d+"</a></div>";
		newcontent += "</div>";
		cont++;
	}
	
	// Replace old content with new content
	$("#newsFeed").html(newcontent);
	
	// Prevent click eventpropagation
	return false;
}

function fillUpNews(){
	var opt = {callback: pageselectCallback, num: 0, items_per_page:10};
	// Create pagination element with options from form
	$("#pagination").pagination(jsonDelicius.length, opt);
}



function formatedDate(dt, blnLeft){
	arrComplete = dt.split("T");
	arrDate = arrComplete[0].split("-");
	//0 = Ano
	//1 = Mês
	//2 = Dia
	if (!blnLeft){
		return arrDate[1] + "/" + arrDate[2] + "/" + arrDate[0]
	}
	else{
		if (arrDate[1].length == 2 && arrDate[1].indexOf("0") == 0){
			strMonth = arrDate[1].replace("0", "");
		}
		else{
			strMonth = arrDate[1];
		}
		if (arrDate[2].length == 2 && arrDate[2].indexOf("0") == 0){
			strDay = arrDate[2].replace("0", "");
		}
		else{
			strDay = arrDate[2];
		}
		return strMonth + "/" + strDay + "/" + arrDate[0].replace("20", "")
	}
}

function toggle(casename){
	if($("#" + casename).is(':hidden'))
		$("#" + casename).fadeIn(600, function(){$("#toggle_"+ casename).html("Go Back");});
	else
		$("#" + casename).fadeOut(600, function(){$("#toggle_" + casename).html("Read More");});
}

function toggleArchive(isFeature) {
	
	var divID;
	
	isFeature==1?divID = "#featureArchive":divID = "#archiveNews";

	if($(divID).is(':hidden'))
		$(divID).fadeIn(300);
		
	else
		$(divID).fadeOut('fast');
}