﻿// NEWS
var myJsonVar;

var req;
var bIExplorer = false;
var bPocketIE = false;


function getBrowser(){
    
  if (window.ActiveXObject) {
    bIExplorer=true;        
    //if (!document.all & !document.layer & !document.getElementById) {
    // more recent pocket ie has document.all
    if (!document.layer & !document.getElementById) {
      bPocketIE=true;
    }
  }
  
}


function showNewsJS(){

getBrowser();

var iNews=0;
var iFocus=0;
var iField=0;
var JSitem;          	           	
          	 

var sDefaultPhotoAddress='/homepage/homephoto.jpg';
var sPhotoAddress=sDefaultPhotoAddress;

if (typeof fieldnews=='undefined') {fieldnews=0;}
if (typeof fieldnewsposition=='undefined') {fieldnewsposition=0;}

// check if javascript was available and variable loaded
if (typeof myJsonVar=='undefined') {
	 // did not load for some reason - just exit
	 return;
	 }

  for (JSitem in myJsonVar.items) {   
         
    if (myJsonVar.items[JSitem].type=='news') {      
      
      // load first available photo URL any
      if (sPhotoAddress==sDefaultPhotoAddress){      		      	
      	if (typeof myJsonVar.items[JSitem].imageSrc!='undefined'){      	      		
      		sPhotoAddress=myJsonVar.items[JSitem].imageSrc;
      		// sCaption=myJsonVar.items[JSitem].imageCaption;
      		sCredit=myJsonVar.items[JSitem].imageAlt;
      	  createTextPhoto(sPhotoAddress,sCredit);
          }
      	}
      	 
      iNews=iNews+1;
      
      if (iNews==1) {        
        createTextJS_NewsFocus(1, myJsonVar.items[JSitem]);                      
      }
      if (iNews==2 | iNews==3) {                           
        createTextJS_NewsFocus(iNews, myJsonVar.items[JSitem]);                      
      } 
    } 
    
  } // for jsitem

  // photo - set default if empty
    if (sPhotoAddress==''){      	
    	  // not needed as the image is already there
     	  // createTextPhoto(sDefaultPhotoAddress, '©FAO/L.Spaventa');   
     }   
     
  // done both in style and js
  // if browser.. 
  if (bPocketIE) {
  	//top.hp1photo
  	
  } else {
    document.getElementById("hp1photo").style.visibility="visible";
    document.getElementById("news1top").style.visibility="visible";
    document.getElementById("news1topsumm").style.visibility="visible";
    document.getElementById("news2").style.visibility="visible";
    document.getElementById("news3").style.visibility="visible";
  }

}  // end function


// new for photo
function createTextPhoto(sThePhotoURL, sTheCredit) {

if (bPocketIE) {          
  theArea=top.piehp1photo;   
} else { 
  theArea=document.getElementById('hp1photo');
} 

theArea.innerHTML ='<img src="' + sThePhotoURL + '" width="400" border="0" alt="' + sTheCredit + '" title="' + sTheCredit + '"/>' 

}

         
//ppc 2002 has document.something
//ppc 2003 does not - needs top.something or simply something
function createTextJS_NewsFocus(divnum, jsItem) {

sTheArea2Text='';

switch(divnum) {
          // 1 is special case for top news
        case 1:          
          if (bPocketIE) {          
            theArea=top.pienews1top;
            theArea2=top.pienews1topsumm;                 
          } else { 
            theArea=document.getElementById('news1top');
            theArea2=document.getElementById('news1topsumm');
          }           
          break;
        case 2:
          if (bPocketIE) {
            theArea=top.pienews2;
          } else { 
            theArea=document.getElementById('news2');
          } 
          break;
        case 3:
          if (bPocketIE) {
            theArea=top.pienews3;
          } else { 
            theArea=document.getElementById('news3');
          } 
          break;
        default:
        
      } // switch (iNews)

// generate abstract  
if (divnum==1) {  
  if (theArea2) { 
      	if (typeof language!='undefined') {
            if (language=='fr') {               
                sMoreText='suite...';
              } else if (language=='es') {
                sMoreText='más...';
              } else if (language=='ar') {
                // sMoreText='للمزي...';
                sMoreText='للمزيد...';
              } else if (language=='zh') {
                sMoreText='更多…';
              } else if (language=='ru') {
                sMoreText='more...';
              } else {
              	sMoreText='more...';
              }   
          } else {
          	    if (jsItem.url.length>=31) {
                  // guess lang from address
                  var sNewsURLLang=jsItem.url.substring(0,33);      
                  if (sNewsURLLang=='http://www.fao.org/news/story/en/') {
                    sMoreText='more...';
                  } else if (sNewsURLLang=='http://www.fao.org/news/story/fr/') {
                    sMoreText='suite...';
                  } else if (sNewsURLLang=='http://www.fao.org/news/story/es/') {
                    sMoreText='más...';
                  } else if (sNewsURLLang=='http://www.fao.org/news/story/ar/') {            
                    sMoreText='للمزيد...';
                  } else if (sNewsURLLang=='http://www.fao.org/news/story/zh/') {
                    sMoreText='更多…';
                  } else if (sNewsURLLang=='http://www.fao.org/news/story/ru/') {
                    sMoreText='more...';
                  }
               }
          	}       
         
    
  sTheArea2Text = '<p class="bodytext">' + jsItem.abstract + ' ' + '[<a href="' + jsItem.url + '">' + sMoreText + '</a>]</p>'          			 
  }
}

if (theArea) {  
  if (bPocketIE) {  
     theArea.innerHTML = '<a class=\'headl\' href=\'' + jsItem.url + '\'>' + jsItem.title + '</a>'        
     theArea.innerHTML = theArea.innerHTML + '<br>' + jsItem.summary + ' ';     
   
  } else {
     sAreaText = '<a href="' + jsItem.url + '" class="headl">' + jsItem.title + '</a>' ;
     if (divnum==1) {  
	     sAreaText = '<h1 class="csc-firstHeader">' + sAreaText + '</h1>' 
	   } 	   
	   if (theArea!= null) theArea.innerHTML = sAreaText  	;   
     // alert (jsItem.title);                        
     if (divnum==1) {        
       if (theArea2) { 
        theArea2.innerHTML = sTheArea2Text
       }
     }
  }
} // end check if theArea exists   
   
} //end createTextJS_NewsFocus


var Url = {
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 		}
 		return utftext;
	},
 	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 		while ( i < utftext.length ) {
 			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 		}
 		return string;
	}
}

function createPhoto4OtherNews(numId, sThePhotoURL, sTheCredit, sUrl) {
	if (bPocketIE) {          
		  theArea=top.otherNews+numId+Photo;   
	} else { 
		  theArea=document.getElementById('otherNews'+numId+'Photo');
	} 
	theArea.innerHTML ='<a href="'+sUrl+'" target="_top" class="external-link-new-window"><img src="' + sThePhotoURL + '" width="130" border="0" alt="' + Url.decode(sTheCredit) + '" title="' + Url.decode(sTheCredit) + '"/></a>' 
}
function createText4OtherNews(numId, sTitle, sSummary, sUrl) {
	theSummary='';
	if (bPocketIE) {          
		  theArea=top.otherNews+numId+Caption;   
	} else { 
		  theArea=document.getElementById('otherNews'+numId+'Caption');
	} 
	if(typeof sSummary!='undefined'){
		theSummary='<br><a href="'+sUrl+'" target="_top" class="external-link-new-window">'+Url.decode(sSummary)+'</a>';
	}
	theArea.innerHTML ='<a href="'+sUrl+'" target="_top" class="external-link-new-window"><b>'+Url.decode(sTitle)+'</b></a>'+theSummary;
}


function feedOtherNews(){
	getBrowser();
	if (typeof myOtherJsonVar=='undefined') {
		 // did not load for some reason - just exit
		 return;
		 }
	var counter =0;
	for (JSitem in myOtherJsonVar.items) {   
		counter++;
		if (myOtherJsonVar.items[JSitem].type=='news') {      
		  // load first available photo URL any
			if (typeof myOtherJsonVar.items[JSitem].imageSrc!='undefined'){      	      		
					sUrl=myOtherJsonVar.items[JSitem].url;
					sPhotoAddress=myOtherJsonVar.items[JSitem].imageSrc;
					sCredit=myOtherJsonVar.items[JSitem].imageAlt;
			  		createPhoto4OtherNews(counter, sPhotoAddress, sCredit, sUrl);
			}
			if (typeof myOtherJsonVar.items[JSitem].title!='undefined'){
					sTitle=myOtherJsonVar.items[JSitem].title;
					sSummary=myOtherJsonVar.items[JSitem].summary;
					sUrl=myOtherJsonVar.items[JSitem].url;
					createText4OtherNews(counter, sTitle, sSummary, sUrl);
			}
		} 
	} // for jsitem
	document.getElementById("on_contenttable").style.visibility="visible";

} // feedOtherNews end





