
/**
*Convert date depend language
*/

var pulicUrl = 'http://192.168.52.167/vnc/public';
function getDateByLanguage(date_time_str,lang)
{
	var date_time = new Array();
	var final_date = '';
	date_time = date_time_str.split(',');
	if(lang=='en')
	{
		final_date = date_time[1]+'/'+date_time[2]+'/'+date_time[0];
	}
	
	if(lang=='vi')
	{
		final_date = date_time[2]+'/'+date_time[1]+'/'+date_time[0];
	}
	
	if(lang=='ja')
	{
		final_date = date_time[0]+'年'+date_time[1]+'月'+date_time[2]+'日';
	
	}
	
	return final_date;
	
	
}

/**
*
*Paging ajax
*/
function page(url,number_page,lang)
{
	
		
		new Ajax.Request(url, {
		  method: 'post',
		  parameters: {page: number_page,lang:lang},
		  onLoading:$('list_news').innerHTML = '<img src="'+pulicUrl+'/images/loading.gif" style="margin-left:100px;"/>',
		  onSuccess:function(transport)	  
		  {
		  	  response = transport.responseText;
		  	  start = response.indexOf('$');
		  	  end = response.lastIndexOf('$');
			  response_json = response.substr(start+1,(end-start)-1).evalJSON();
			  //alert(response.substr(start+1,(end-start)-1));
			 //alert(response_json.title_en_arr[2]);
			 /**
			 *
			 *Get data from server and process 
			 * buil all link news
			 */
			  html_str = '';
			  for(i=0;i<response_json.news_arr.length;i++)
			  {//begin for
				  	t = response_json.news_arr[i].published_date;
					date_time = t.split(',');
				  	date = new Date();
					
					html_str +='<div class="a_hotNews">';
					//url of mews
					url = response_json.publicUrl+'/index.php/'+response_json.lang+'/news/'+response_json.news_arr[i].news_id+'-'+response_json.title_en_arr[i];
					news_title = response_json.news_arr[i].news_title;
					//check if ref link available
					if(response_json.news_arr[i].ref_link!='')
					{
							
							news_title = '<a href="'+response_json.news_arr[i].ref_link+'" target="_blank">'+ response_json.news_arr[i].news_title+'</a>';
					}
					
					if(response_json.news_arr[i].news_detail!='')
					{
							
							news_title = '<a href="'+url+'">'+ response_json.news_arr[i].news_title+'</a>';
					}
					//check image and link
					if(response_json.news_arr[i].news_image!='')
					{
								image = '<img src="'+response_json.publicUrl+'/images/news/'+response_json.news_arr[i].news_image+'" />';
								if(response_json.news_arr[i].ref_link!='')
									{
											
											image = '<a href="'+response_json.news_arr[i].ref_link+'" target="_blank"><img src="'+response_json.publicUrl+'/images/news/'+response_json.news_arr[i].news_image+'" /></a>';
									}
									
								if(response_json.news_arr[i].news_detail!='')
								{
										
										image = '<a href="'+url+'"><img src="'+response_json.publicUrl+'/images/news/'+response_json.news_arr[i].news_image+'" /></a>';
								}
							html_str += image; 	
					
                                
					}
								
                                html_str += news_title; 
								 
                                html_str +='<span class="a_hotNews_date">'+date_time[3]+':'+date_time[4]+'  |  '+getDateByLanguage(t,lang)+'</span>'; 
               		html_str +='</div>';
				}//end for
				
			//fetch data tolist	
		  	  $('list_news').innerHTML = html_str;
			 //rebuild link
			 new_link_control = '';
			 first = '';
			 previous = '<img src="'+response_json.publicUrl+'/images/FF.png" />';
			 next = '<img src="'+response_json.publicUrl+'/images/next.png" />';
			 last = '';
			 //buil first
			 first = '<a href="javascript:page(\''+response_json.publicUrl+'/index.php/news/page'+'\',\''+response_json.first+'\',\''+response_json.lang+'\');"\'>';
			 		first +='<img src="'+response_json.publicUrl+'/images/Home.png" />';
					first +='</a>';
			//build previous
			if(response_json.previous>0) 		
			{
				previous = '<a href="javascript:page(\''+response_json.publicUrl+'/index.php/news/page'+'\',\''+response_json.previous+'\',\''+response_json.lang+'\');"\'>';
			 		previous +='<img src="'+response_json.publicUrl+'/images/FF.png" />';
					previous +='</a>';
			}
			//buil next 		
			if(response_json.next>0) 		
			{
				next = '<a href="javascript:page(\''+response_json.publicUrl+'/index.php/news/page'+'\',\''+response_json.next+'\',\''+response_json.lang+'\');"\'>';
			 		next +='<img src="'+response_json.publicUrl+'/images/next.png" />';
					next +='</a>';
			}
			
			//build last
			 last = '<a href="javascript:page(\''+response_json.publicUrl+'/index.php/news/page'+'\',\''+response_json.last+'\',\''+response_json.lang+'\');"\'>';
			 		last +='<img src="'+response_json.publicUrl+'/images/End.png" />';
					last +='</a>';
			//build link control
			new_link_control = first + previous + next + last;
			$('goto_pages').innerHTML = '';
			$('goto_pages').innerHTML = new_link_control;
		  }
		  });

}
// LuyenLV
function pages(url,number_page,lang)
{
	
		
		new Ajax.Request(url, {
		  method: 'post',
		  parameters: {page: number_page,lang:lang},
		  onLoading:$('list_newsextra').innerHTML = '<img src="'+pulicUrl+'/images/loading.gif" style="margin-left:100px;"/>',
		  onSuccess:function(transport)	  
		  {
		  	  response = transport.responseText;
		  	  start = response.indexOf('$');
		  	  end = response.lastIndexOf('$');
			  response_json = response.substr(start+1,(end-start)-1).evalJSON();
			  //alert(response.substr(start+1,(end-start)-1));
			 //alert(response_json.title_en_arr[2]);
			 /**
			 *
			 *Get data from server and process 
			 * buil all link news
			 */
			  html_str = '';
		      html_str +='<div id="a_hotNewsextraajax">';

			  for(i=0;i<response_json.newsextra_arr.length;i++)
			  {//begin for
				  	t = response_json.newsextra_arr[i].published_date;
					date_time = t.split(',');
				  	date = new Date();
					

					//url of mews
					
					url = response_json.newsextra_arr[i].newsextra_url;
					//newsextra_title = response_json.newsextra_arr[i].newsextra_title;
					newsextra_title = '<a href="'+response_json.newsextra_arr[i].newsextra_title+'" target="_blank">'+ response_json.newsextra_arr[i].newsextra_title+'</a>';
					if(response_json.newsextra_arr[i].newsextra_title.length <= 30)
					{
						news_luyen_url = response_json.newsextra_arr[i].newsextra_title.substring(0,30);
					}
					else
					{
						news_luyen_url = response_json.newsextra_arr[i].newsextra_title.substring(0,30) + '...';
					}
					//order number of list write by Le Van Luyen
                        if(i<=15) {
					 	newsextra_title = i+1 + '. &nbsp;&nbsp;<a href="'+url+'" target="_blank">'+ news_luyen_url+'<br /></a>';
						}
						if(response_json.previous>0)
						{
							newsextra_title = i+15+1 + '. &nbsp;&nbsp;<a href="'+url+'" target="_blank">'+ news_luyen_url+'<br /></a>';	
						}	
							if(response_json.next<0)
						{
							newsextra_title = i+15-1 + '. &nbsp;&nbsp;<a href="'+url+'" target="_blank">'+ news_luyen_url+'<br /></a>';	
						}	
				    //end order	
						//check image and link
                                html_str += newsextra_title; 
                                
				}//end for

				html_str += '</div>';
			//fetch data tolist	
		  	  $('list_newsextra').innerHTML = html_str;
			 //rebuild link
			 newextra_link_control = '';
			 first = '';
			 previous = '<img src="'+response_json.publicUrl+'/images/FF.png" />';
			 next = '<img src="'+response_json.publicUrl+'/images/next.png" />';
			 last = '';
			 //buil first
			 first = '<a href="javascript:pages(\''+response_json.publicUrl+'/index.php/newsextra/page'+'\',\''+response_json.first+'\',\''+response_json.lang+'\');"\'>';
			 		first +='<img src="'+response_json.publicUrl+'/images/Home.png" />';
					first +='</a>';
			//build previous
			if(response_json.previous>0) 		
			{
				previous = '<a href="javascript:pages(\''+response_json.publicUrl+'/index.php/newsextra/page'+'\',\''+response_json.previous+'\',\''+response_json.lang+'\');"\'>';
			 		previous +='<img src="'+response_json.publicUrl+'/images/FF.png" />';
					previous +='</a>';
			}
			//buil next 		
			if(response_json.next>0) 		
			{
				next = '<a href="javascript:pages(\''+response_json.publicUrl+'/index.php/newsextra/page'+'\',\''+response_json.next+'\',\''+response_json.lang+'\');"\'>';
			 		next +='<img src="'+response_json.publicUrl+'/images/next.png" />';
					next +='</a>';
			}
			
			//build last
			 last = '<a href="javascript:pages(\''+response_json.publicUrl+'/index.php/newsextra/page'+'\',\''+response_json.last+'\',\''+response_json.lang+'\');"\'>';
			 		last +='<img src="'+response_json.publicUrl+'/images/End.png" />';
					last +='</a>';
			//build link control
			newextra_link_control = first + previous + next + last;
			$('goto_pagesextra').innerHTML = '';
			$('goto_pagesextra').innerHTML = newextra_link_control;
					
			
			 
			 
			 
		  }
		  });

}
