var wunit = $("wide_ad_unit");
var nunit = $("narrow_ad_unit");
google_afs_ad = '';
if (wunit) google_afs_ad += 'w8';
if (nunit) google_afs_ad += "n8";

var trim = function(s,l) {
	if($type(s)=='element'){
		if (s.offsetWidth>l) {
			s.tt=s.innerHTML.length-Math.ceil((s.offsetWidth-l)/30);
			s.innerHTML=trim(s.innerHTML,s.tt);
			return trim(s,l);
		} else return;
	} else {
		l=l||10;
		var c=((l-3)/2).toInt();
		return s.length>l?(s.substring(0,c)+'...'+s.substring(s.length-c)):s;
	}
};

function google_afs_request_done(google_ads) {
    var google_num_ads = google_ads.length;
    if (google_num_ads <= 0 || (!wunit && !nunit)) {
        return;
    }

    var highlight = function(w) {
        var p = new RegExp($(iid).value, "ig");
        return w.replace(p, function(m) {
            if (m) return '<b>' + m + "</b>"
        });
    }
    google_ads.each(function(ad) {
        var w = ad.type == 'text/wide';
        var unit = w ? wunit : nunit;
        if (!unit['ul']) {
            unit.ul = new Element('ul').injectTop(unit);
            if (!w) {
                new Element('h6').setHTML("赞助商链接").injectBefore(unit.ul);
            }
        }
        var title = new Element('div', {
            'styles':{
                'line-height':'16px',
                'font-size':'14px'
            }
        }).adopt(new Element('a', {
            'styles':{
                'color':'#0000FE'
            },
            'href':'http://' + ad.visible_url,
            'events': {
                'click': function(e) {
                    new Event(e).stop();
                    window.open(ad.url);
                }
            }
        }).setHTML(highlight(ad.line1)));
        var content = new Element('div', {
            'styles':{
                'line-height':'14px',
                'font-size':'12px',
                'margin':'3px 0'
            }
        }).setHTML(ad.line2 + (ad.line3 ? '<br>' + ad.line3 : ''));
        var link = new Element('div', {
            'styles':{
                'line-height':'14px',
                'font-size':'12px'
            }
        });
        var vu = new Element('span', {'styles':{'color':'#008000'}}).setHTML(ad.visible_url);
        link.adopt(vu);
        if (w) {
            link.appendText(" - ").adopt(new Element('a', {
				'href':ad.url,
				'styles':{
					'color':'#b2a9de',
					'text-decoration':'none'
				}
				}).setHTML('推广'));
        }
        new Element('li',{'styles':{'margin-bottom':'15px'}}).adopt(title, content, link).injectInside(unit.ul);
        if (!w)trim(vu,180);
    });
}
google_afs_ie = 'UTF-8';
google_afs_oe = 'UTF-8';
//google_afs_gl = 'cn';
google_afs_hl = 'zh-CN';
google_afs_client = 'pub-8883306226496766';
google_afs_channel = '4368670425';
google_afs_adsafe = 'high';
google_afs_query = $(iid).value;
