function generateLink(id, topic, title, section){
	return "/" + escape(topic.replace(/\s+/g, "-")) + "/" + escape(title.replace(/\s+/g, "-")) + "/" +  section + "/"+ id + ".phtml"	
}

function generateSrcIconUrl(folder, iconurl){
	return '/ze/uploads/publications/section_news/'+folder+'/'+iconurl;
}

function tmplHomeFirstNews(obj){
	var link = generateLink(obj.idpublication, obj.topic, obj.title, 'news');
	$("#HomeFirstNews .image").html('<a href="' + link + '"><img src="'+generateSrcIconUrl(obj.folder, obj.iconurl)+'"></a>');
	$("#HomeFirstNews .title").html('<a href="' + link + '">' + obj.title + '</a>');
	$("#HomeFirstNews .date").html(obj.date_publication);
	$("#HomeFirstNews .subtitle").html(' <span class="hour">' + obj.hour+'</span> - ' + obj.synopsis);
	$("#HomeFirstNews .link").html('<a href="' + link + '">segue &gt;&gt;</A>');
}

function tmplNews(link, iconurl, title, date, topic){
	var buff='';
	buff += '<div class="pub">';										
			buff += '<div class="image"><a href="'+link+'"><img src="'+iconurl+'" width="100" height="60" border="0"/></a></div>';
			buff += '<div class="info">';
				buff += '<div class="title"><a href="'+link+'">' + title + '</a></div>';
				buff += '<div>';
					buff += '<div class="date">'+date+'</div>';
					buff += '<div class="topic">'+topic+'</div>';
				buff += '</div>';
			buff += '</div>';
		buff += '</div>';
	return buff;
}


function processElementXML(xml){
	var buff= "";
	var id = xml.find('id').text();
	var title = xml.find('title').text();
	var topic = xml.find('topic').text();
	var date = xml.find('date').text();
	var iconurl = xml.find('url').text();	
	var folder = xml.find('folder').text();	
	var link = generateLink(topic, title, 'news'); 
	return tmplNews(generateLink(id, topic, title, 'news'), generateSrcIconUrl(folder, iconurl) , title, date, topic);	
}

function processElementJSON(json){
	var buff= "";
	var id = json.idpublication;
	var title = json.title;
	var topic = json.topic;
	var date = json.date_publication;
	var hour = json.hour;
	var iconurl = json.iconurl;
	var folder = json.folder;
	return tmplNews(generateLink(id, topic, title, 'news'), generateSrcIconUrl(folder, iconurl) , title, date, topic);	
}



function home_getLatestNews(){
			$.ajax({
			  type: 'POST',
			  url: 'get.news.latest.php',
			  data: {'type': 'json'},
			  success: function(jsonArr){											
				var newHTML = '';
				$(jsonArr).each(function(index, element){ 
					if(index==0){
						 tmplHomeFirstNews(element);
						
					}else{
						newHTML += processElementJSON(element);
					}
				});
				$('#vetrinaNewsHome').html(newHTML);
			  },
			  error: function(){
				console.log('failure');
			  },
			  dataType: "json"
			});	
		}	
		

				
