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 header div
                    var headerDiv = new Element ('div', {
                                		'id' : 'wide_afs_header_div_' + j,
                                		'class' : 'google_wide_unit_header'
                                	}).injectInside(wide_id);
                    var id_headerDiv = 'wide_afs_header_div_' + j;
                    
                    //build anchor
                    var wideAdsHeader = new Element ('a', {
                                        'id' : 'wide_afs_header_a_' + j,
                                        'href' : 'http://services.google.com/feedback/online_hws_feedback',
                        				'class' : 'none',
                                		'styles' : {
                        					'color' : '#FFFFFF',
                        				},
                        				'target' : '_blank'
                                    }).injectInside(id_headerDiv);
                    var id_afs_header = 'wide_afs_header_a_' + j;
                    $(id_afs_header).appendText(txt_google_ads);
                }
                
                // render a wide ad
                //build div
                var afs = new Element ('div', {
                            		'id' : 'wide_afs_' + j,
                            		'class' : 'google_wide_unit_content',
                            		'events' : {
                    					'mouseout' : function(){
                    					       this.className = 'google_wide_unit_content';
                    					},
                    					'mouseover' : function(){
                    					    this.className = 'google_wide_unit_contentH';
                    					}
                    				}
                            	}).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;
                    					}
                    				},
                    				'styles' : {
                                        'font-weight' : 'bold'
                    				},
                    				'target' : '_blank'
                                }).injectInside(id_wide);
                var id_a1 = 'wide_afs_a1_' + j;
                $(id_a1).appendText(google_ads[i].line1.unescapeHtml());
                
                //build div desc
                var div_desc = new Element ('div', {
                                    'id' : 'wide_afs_div_desc_' + j,
                                    'styles' : {
                                        'width' : '100%',
                                        'margin-left' : '5px'
                                    }
                                }).injectInside(id_wide);
                var id_div_desc = 'wide_afs_div_desc_' + j;
                $(id_div_desc).appendText(google_ads[i].line2.unescapeHtml());
                
                //build div lien
                var div_lien = new Element ('div', {
                                    'id' : 'wide_afs_div_lien_' + j,
                                    'class' : 'googleLink',
                                    'styles' : {
                                        'width' : '100%'
                                    }
                                }).injectInside(id_wide);
                var id_div_lien = 'wide_afs_div_lien_' + j;
                
                //build anchor 2
                var a = new Element ('a', {
                                    'id' : 'wide_afs_a2_' + j,
                                    '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_div_lien);
                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 header div
                var headerDiv = new Element ('div', {
                            		'id' : 'wide_afs_header_div_' + j,
                            		'class' : 'google_narrow_unit_header'
                            	}).injectInside('narrow_ad_unit');
                var id_headerDiv = 'wide_afs_header_div_' + j;
                
                //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(id_headerDiv);
                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_narrow_lastH';
                    					}
                    				}
                                }).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='';
                					    this.style.color = '#FFFFFF';
                					},
                					'mouseover' : function(){
                					    window.status = 'afficher la page ' + visible_url;
                					    this.style.color = '#000000';
                					    return true;
                					}
                				},
                				'styles' : {
                				    'color' : '#FFFFFF',
                				    'font-size' : '12px',
                				    'font-weight' : 'bold'
                				},
                    			'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';
}