/**
    * Adds GoogleAdsense AFC & AFS to the DOM 
    * by calling the function getTheAds() -> args set in addGoogle_Adds.php 
    *
    * @vars String
    * @name adPleace,type,width,fl,href,hrefAnker,line1,line2,line3,counttestline
    *
    * @vars Integer
    * @name google_adnum,google_num_ads
    *
    * @vars Array
    * @name google_ads,gAds
    *
    * @return void
*/
var $j = jQuery.noConflict();
var google_adnum = 0;
function getTheAds(google_ads,adPleace,type,width,fl) {$j(function(){
    var gAds =[];
    var wrap;
    //wordwrap if ad-with is limited
    (width != 'auto') ? wrap = '<br/>' : wrap = '';
    //round corner position when width is limited 
    (width != 'auto') ? cornerWidth = 'style="left:'+(width - 5)+'px;"' : cornerWidth = '';
    //number of received ads
    google_num_ads = google_ads.length;
    //return when there are no ads
	if (google_num_ads <= 0) return;
    
    //define lines and anchors for each ad
	for(i in google_ads) {
    	href        = '<a target="_blank"  class="adsense"  rel="nofollow" href="' + google_ads[i].url + '">';
    	hrefAnker   = google_ads[i].line1;
    	line1       = google_ads[i].line2;
    	line2       = google_ads[i].visible_url;
    	line3       = google_ads[i].line3;
    	counttestline = google_ads[i].url;
    	if(line3){text3 = line3;}else{text3 ='';}
        
        //building html-code for each ad-item
        if (type == 'afc') {
            gAds[i] = '<li class="ad_'+i+'"><span class="head">'+href+'<b>'+hrefAnker+'</b></span></a>'+wrap+'<span class="text">'+line1+''+text3+'</span>'+wrap+'<span class="link">'+href+''+line2+'</span></a></li>';
        }else if (type == 'afs') {
            gAds[i] = '<li class="ad_'+i+'"><span class="head">'+href+''+hrefAnker+'</span></a>'+wrap+'<span class="text">'+line1+''+text3+'</span>'+wrap+'<span class="link">'+href+''+line2+'</span></a></li>';
        }
    }
    
    //building the html-DOM for the ads and append it
    if (type == 'afc') {
        adHeadline = '<h4><a href="https://www.google.com/adsense/support/bin/request.py?contact=abg_afcz&gl=de&hideleadgen=1">Anzeigen</a></h4>';
        $j('#'+adPleace).html('');
        $j('#'+adPleace).replaceWith('<div class="adsense '+fl+'" style="width:'+width+'px;">'+adHeadline+'<div class="clt"></div><div class="crt" '+cornerWidth+'></div><ul id='+adPleace+'>');
        //append the ad-items
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        if (is_chrome) {
            for(i in gAds) {
                i = +i;
                if (typeof(i) == 'number') {
                    $j('.adsense ul#'+adPleace).append(gAds[i]);
                }
            }
        } else {
            for(i in gAds) {
                $j('.adsense ul#'+adPleace).append(gAds[i]);
            }   
        }
        $j('#'+adPleace).after('<div class="clb"></div><div class="crb" '+cornerWidth+'></div></ul></div>');
    }else if (type == 'afs') {
        adHeadline = '<h4>Anzeigen</h4>';
        $j('#'+adPleace).html('');
        $j('#'+adPleace).replaceWith('<div class="adsense">'+adHeadline+'<div class="clt"></div><div class="crt"></div><ul id='+adPleace+'>');
        //appends the first ads of afs right in place of the function calling
        for(var i=0;i<google_num_ads/2;i++) {
            $j('.adsense ul#'+adPleace).append(gAds[i]);
        }
        $j('#'+adPleace).after('<div class="clb"></div><div class="crb"></div></ul></div>');
        
        //appends the left ads on a div with the id="'+adPleace+'_2"
        if (google_num_ads >= google_num_ads/2) {
            $j('#'+adPleace+'_2').html('');
            $j('#'+adPleace+'_2').replaceWith('<div class="adsense">'+adHeadline+'<div class="clt"></div><div class="crt"></div><ul id='+adPleace+'_2>');
            for(var i=google_num_ads/2;i<google_num_ads;i++) {
                $j('.adsense ul#'+adPleace+'_2').append(gAds[i]);
            }
            $j('#'+adPleace+'_2').after('<div class="clb"></div><div class="crb"></div></ul></div>');
        }
    }
})
google_adnum = google_adnum + google_ads.length;

return;
};
