// GOOGLE SEARCH
function GoogleBox(lang, search) {
    //debugger;
    var box = document.getElementById('googleBox');
    var script = document.getElementById('googleScript');
    if (lang=='it' || lang=='en' || lang=='fr' || lang=='es' || lang=='zh' || lang=='ar') {
        box.innerHTML = '<form id="searchbox_002043666363145718078:wvmn6ji7qsa" action="results.html"> ' +
            '<input type="hidden" name="cx" value="002043666363145718078:wvmn6ji7qsa" /> ' +
            '<input type="hidden" name="cof" value="FORID:9" /> ' +
            '<input name="q" type="text" size="20" /> ' +
            '<input type="submit" name="sa" value="' + search + '" /> ' +
          '</form>'; 
        script.src = "http://google.com/coop/cse/brand?form=searchbox_002043666363145718078%3Awvmn6ji7qsa";
    }
}

function GoogleResults(lang) {
    var div = document.getElementById('googleResults');
    if (lang=='it' || lang=='en' || lang=='fr' || lang=='es' || lang=='zh' || lang=='ar') {
        div2 = document.createElement('div');
        div2.id = "results_002043666363145718078:wvmn6ji7qsa";
        div.appendChild(div2);
        var googleSearchIframeName = "results_002043666363145718078:wvmn6ji7qsa"; 
        var googleSearchFormName = "searchbox_002043666363145718078:wvmn6ji7qsa"; 
        var googleSearchFrameWidth = 50; 
        var googleSearchFrameborder = 0; 
        var googleSearchDomain = "google.com";
        var googleSearchPath = "/cse";
    }
}


// RANDOM IMAGES
function showImg(img, imgList) {
    if (img.src.substring(img.src.length-9,img.src.length)=='blank.gif') {
        now = new Date();
        var i = now.getSeconds() % imgList.length;
        img.src=img.src.replace('img/blank.gif',imgList[i][0]);
        img.alt=imgList[i][1];
        img.title=imgList[i][1];
    }
}

// SWAP GOOD AND BAD NEWS
function goodBad(chosen) {
    var good = chosen=='good';
    document.getElementById('good').className = good ? "block" : "hidden";
    document.getElementById('bad').className = good ? "hidden" : "block";
    //var links = getElementsByClassName(document, "div", "links")[0];
    //links.className = good ? "hidden" : "block";
}

// QUIZ
function answerQuiz(img, isCorrect, background) {
    if (isCorrect) {
        img.src="../img/quiz-button-green-on-"+ background +".jpg";    
    } else {    
        img.src="../img/quiz-button-red-on-"+ background +".jpg";    
    }
}

function answerAll() {
    // set false answers to gray
     for (i=0; i<answersfalse.length; i++) {
        img = document.getElementById(answersfalse[i][0]);
        img.src = img.src="../img/quiz-button-gray-on-"+ answersfalse[i][1] +".jpg";
    }
    // set true answers to green
    for (i=0; i<answerstrue.length; i++) {
        img = document.getElementById(answerstrue[i][0]);
        img.src = img.src="../img/quiz-button-green-on-"+ answerstrue[i][1] +".jpg";
    }
}

function resetQuiz() {
    // set false answers to gray
     for (i=0; i<answersfalse.length; i++) {
        img = document.getElementById(answersfalse[i][0]);
        img.src = img.src="../img/quiz-button-gray-on-"+ answersfalse[i][1] +".jpg";
    }
    // set true answers to gray
    for (i=0; i<answerstrue.length; i++) {
        img = document.getElementById(answerstrue[i][0]);
        img.src = img.src="../img/quiz-button-gray-on-"+ answerstrue[i][1] +".jpg";
    }
}

function preloadButtons() {
    if (document.images) {
      pic1= new Image(27,25); 
      pic1.src="quiz-button-green-on-white.jpg"; 
      pic2= new Image(27,25); 
      pic2.src="quiz-button-green-on-green.jpg"; 
      pic3= new Image(27,25); 
      pic3.src="quiz-button-red-on-white.jpg"; 
      pic4= new Image(27,25); 
      pic4.src="quiz-button-red-on-green.jpg";

    }
}

// GALLERY
var newwindow;
function showGallery(url) {
    newwindow=window.open (url, null, config='height=580, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
	if (window.focus) {newwindow.focus();}
}

/*function gallery() {
    if (lang=='en') return 'Photo Gallery';
    else if (lang=='fr') return 'Photo Gallery';
    else if (lang=='es') return 'Photo Gallery';
    else if (lang=='zh') return 'Photo Gallery';
    else if (lang=='ar') return 'Photo Gallery';
    else if (lang=='it') return 'Photo Gallery';
}*/

function showPhoto(i) {
   // show forward and back buttons if were previously hidden
   if (current==0)  document.getElementById('back').className="block";
   if (current==photos.length-1)  document.getElementById('forward').className="block";
   
   // hide forward and back buttons if at beginning or end of list
   if (i==0)  document.getElementById('back').className="hidden";
   if (i==photos.length-1)  document.getElementById('forward').className="hidden";
   
   // highlight selected number and un-highlight previous
   document.getElementById('link'+current).className="";
   document.getElementById('link'+i).className="selected";
   
   // swap image
   var img = document.getElementById('image');
   img.src=path+photos[i];
   
   // set pop-up info
   //img.setAttribute("nicetitle",alttext[i]);
   
   // show caption
   var caption = document.getElementById('caption');
   caption.innerHTML = alttext[i];
   
   current=i;
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return new Array(myWidth, myHeight);
}

function resizeGallery() {
    if (navigator.userAgent.indexOf('MSIE 5.2') != -1	&&	navigator.userAgent.indexOf('Mac') != -1) {
        // Internet Explorer for Mac has very poor javascript support
    } else {
        var size= getWindowSize();
        iWidth = 500 - size[0];
        iHeight = 540 - size[1];
        window.resizeBy(iWidth, iHeight);
        self.focus();
     }
}

// NEWS
var req;
var lang;
function getNews(language) {
    lang=language;
    url="http://www.fao.org/newsroom/"+lang+"/rss.xml";
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
try {
    req.onreadystatechange = processRequest;
    req.open("GET", url, true);
    req.send(null);
} catch (e) { 
//debugger;
//alert(e);
}
}

function processRequest() {
    // only if req shows "complete"
    if (req.readyState == 4) {
    //debugger;
        // only if "OK"
        if (req.status == 200) {
	        response = req.responseXML.documentElement;
	        createText(document.getElementById('news1'),response.getElementsByTagName('item')[0]);
	        createText(document.getElementById('news2'),response.getElementsByTagName('item')[1]);
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function createText(div, newsItem) {
   title = document.createElement('div');
   title.className='newsTitle'; 
   titleNode = document.createTextNode(newsItem.getElementsByTagName('title')[0].firstChild.data);
   title.appendChild(titleNode);
   
   para = document.createElement('p');
   para.innerHTML = newsItem.getElementsByTagName('description')[0].firstChild.data+' ';
   
   link = document.createElement('a');
   link.setAttribute('href',newsItem.getElementsByTagName('link')[0].firstChild.data);
   link.setAttribute('target','_blank');
   linkText = document.createTextNode('[ '+more()+' ]');
   link.appendChild(linkText);
   para.appendChild(link);
   
   div.appendChild(title);
   div.appendChild(para);
}

function more() {
    if (lang=='en') return 'more';
    else if (lang=='fr') return 'plus d\'information';
    else if (lang=='es') return 'más información';
    else if (lang=='zh') return '更多';
    else if (lang=='ar') return 'للمزيد';
    else if (lang=='it') return 'di più...';
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}