String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
}

function google_afs_request_done(google_ads)
{
    /*
     * Verify that there are actually ads to display.
    */
    var google_num_ads = google_ads.length;
    if (google_num_ads <= 0)
    {
        return;
    }

    var narrowAds = "";   // narrow ad unit html text
    var narrowAds_header = ""; // header (contains annonces google and feedback link) for narrow ad unit

    var wide_ads = 0;
    for(i = 0; i < google_num_ads; i++)
    {
        if (google_ads[i].type == "text/wide")
            wide_ads++;
    }
    
    if (typeof(wide_split_no) == 'undefined') {
        wide_split_no = 2;
    }
    
    var j = 0;
    var k = Math.ceil(wide_ads/2);
    var wide1 = 0;
    var wide2 = 0;
    var narrow = 0;
    for(i = 0; i < google_num_ads; i++)
    {
        if (google_ads[i].type == "text/wide")
        {
            if (j < wide_split_no)
                wide_id = 'wide_ad_unit1';
            else
                wide_id = 'wide_ad_unit2';

            if ($(wide_id) !== null) {
                if (wide_id == 'wide_ad_unit1') wide1 = 1;
                else
                    if (wide_id == 'wide_ad_unit2') wide2 = 1;

                //put the header
                if (j == 0 || j == wide_split_no) {
                    //build anchor
                    var wideAdsHeader = new Element ('a', {
                                        'id' : 'wide_afs_header_a_' + j,
                                        'href' : 'http://services.google.com/feedback/online_hws_feedback',
                                		'styles' : {
                        					'text-decoration' : 'none'
                        				},
                        				'target' : '_blank'
                                    }).injectInside(wide_id);
                    var id_afs_header = 'wide_afs_header_a_' + j;
                    
                    //build h1
                    var headerH1 = new Element ('h1', {
                                        'id' : 'wide_afs_h1_' + j
                                    }).injectInside(id_afs_header);
                    var id_headerH1 = 'wide_afs_h1_' + j;
                    $(id_headerH1).appendText(txt_google_ads);
                }
                
                // render a wide ad
                //build div
                var afs = new Element ('div', {
                            		'id' : 'wide_afs_' + j,
                            		'class' : 'google_announce_wide',
                            		'events' : {
                    					'mouseout' : function(){
                    					       this.className = 'google_announce_wide';
                    					},
                    					'mouseover' : function(){
                    					    this.className = 'google_announce_wideH';
                    					}
                    				}
                            	}).injectInside(wide_id);
                var id_wide = 'wide_afs_' + j;
                
                //build anchor 1
                var visible_url = google_ads[i].visible_url.unescapeHtml();
                var a = new Element ('a', {
                                    'id' : 'wide_afs_a1_' + j,
                                    'class' : 'title',
                                    'href' : google_ads[i].url,
                            		'events' : {
                    					'mouseout' : function(){
                                            window.status='';
                    					},
                    					'mouseover' : function(){
                    					    window.status = 'afficher la page ' + visible_url;
                    					    return true;
                    					}
                    				},
                    				'target' : '_blank'
                                }).injectInside(id_wide);
                var id_a1 = 'wide_afs_a1_' + j;
                $(id_a1).appendText(google_ads[i].line1.unescapeHtml());
                
                //build p description
                var p_desc = new Element ('p', {
                                    'id' : 'wide_afs_p_desc_' + j,
                                    'class' : 'Description'
                                }).injectInside(id_wide);
                var id_p_desc = 'wide_afs_p_desc_' + j;
                $(id_p_desc).appendText(google_ads[i].line2.unescapeHtml());
                
                //build anchor 2
                var a = new Element ('a', {
                                    'id' : 'wide_afs_a2_' + j,
                                    'href' : google_ads[i].url,
                                    'class' : 'Lien',
                            		'events' : {
                    					'mouseout' : function(){
                                            window.status='';
                    					},
                    					'mouseover' : function(){
                    					    window.status = 'afficher la page ' + visible_url;
                    					    return true;
                    					}
                    				},
                    				'target' : '_blank'
                                }).injectInside(id_p_desc);
                var id_a2 = 'wide_afs_a2_' + j;
                $(id_a2).appendText(google_ads[i].visible_url);
            }
            j++;
        }
        else
        {
            // render a narrow ad
                        
            //put the header
            if (narrow == 0) {
                //build anchor
                var narrowAdsHeader = new Element ('a', {
                                    'id' : 'narrow_afs_header_a_' + j,
                                    'href' : 'http://services.google.com/feedback/online_hws_feedback',
                            		'styles' : {
                    					'text-decoration' : 'none'
                    				},
                    				'target' : '_blank'
                                }).injectInside('narrow_ad_unit_header');
                var id_afs_header = 'narrow_afs_header_a_' + j;
                
                //build span
                var headerSpan = new Element ('span', {
                                    'id' : 'narrow_afs_span_' + j,
                                    'styles' : {
                                        'font-weight' : 'bold'
                                    }
                                }).injectInside(id_afs_header);
                var id_headerH1 = 'narrow_afs_span_' + j;
                $(id_headerH1).appendText(txt_google_ads);
            }
                
            // build main div
            if (narrow == 0) {
                var main_div = new Element ('div' , {
                                    'id' : 'narrow_afs_' + i,
                            		'class' : 'google_announce_narrow_last',
                            		'events' : {
                    					'mouseout' : function(){
                                            this.className = 'google_announce_narrow_last';
                    					},
                    					'mouseover' : function(){
                    					    this.className = 'google_announce_narrowH';
                    					}
                    				}
                                }).injectInside('narrow_ad_unit');
            } else {
                var main_div = new Element ('div' , {
                                    'id' : 'narrow_afs_' + i,
                            		'class' : 'google_announce_narrow',
                            		'events' : {
                    					'mouseout' : function(){
                                            this.className = 'google_announce_narrow';
                    					},
                    					'mouseover' : function(){
                    					    this.className = 'google_announce_narrowH';
                    					}
                    				}
                                }).injectInside('narrow_ad_unit');
            }
            var id_main_div = 'narrow_afs_' + i;

            //build first anchor, ad title
            var visible_url = google_ads[i].visible_url.unescapeHtml();
            var ad_title = new Element ('a', {
                                'id' : 'narrow_afs_a1_' + i,
                                'class' : 'title',
                                'href' : google_ads[i].url,
                        		'events' : {
                					'mouseout' : function(){
                                        window.status='';
                					},
                					'mouseover' : function(){
                					    window.status = 'afficher la page ' + visible_url;
                					    return true;
                					}
                				},
                    			'target' : '_blank'
                            }).injectInside(id_main_div);
            var id_ad_title = 'narrow_afs_a1_' + i;
            $(id_ad_title).appendText(google_ads[i].line1.unescapeHtml());
            
            //build p description
            var p_desc = new Element ('p', {
                                'id' : 'narrow_afs_p_desc_' + i,
                                'class' : 'Description'
                            }).injectInside(id_main_div);
            var id_p_desc = 'narrow_afs_p_desc_' + i;
            $(id_p_desc).appendText(google_ads[i].line2.unescapeHtml());
                
            //build p lien
            var p_desc = new Element ('p', {
                                'id' : 'narrow_afs_p_lien_' + i,
                                'class' : 'Lien'
                            }).injectInside(id_main_div);
            var id_p_lien = 'narrow_afs_p_lien_' + i;
            
            //build anchor 2
            var a = new Element ('a', {
                                'id' : 'narrow_afs_a2_' + i,
                                'href' : google_ads[i].url,
                        		'events' : {
                					'mouseout' : function(){
                                        window.status='';
                					},
                					'mouseover' : function(){
                					    window.status = 'afficher la page ' + visible_url;
                					    return true;
                					}
                				},
                				'target' : '_blank'
                            }).injectInside(id_p_lien);
            var id_a2 = 'narrow_afs_a2_' + i;
            $(id_a2).appendText(google_ads[i].visible_url);
            
            narrow = 1;
        }
    }

    if (wide1 == 0 && $('wide_ad_div1') !== null)
        $('wide_ad_div1').style.display = 'none';
        
    if (wide2 == 0 && $('wide_ad_div2') !== null)
        $('wide_ad_div2').style.display = 'none';
        
    if (narrow == 0 && $('narrow_ad_div') !== null)
        $('narrow_ad_div').style.display = 'none';
}