
function stripycowAdsense(){
	var ad_setting_string = "";
	var templateID;
	var outputArray;
	var outputID;
	var outputPos = 0;
	var maxOutput;
	var callbackcode;
	var adtype = "afs";

	this.$ = function(id){
		return document.getElementById(id);
	}

	this.setType = function(atype){
		adtype = atype;
		if (adtype == "afc"){
			ad_setting_string = "google_ad_output = 'js';";
		}

	}

	this.setCallback = function(callback){
		callbackcode = callback
	}

	this.select = function(element, str){
		var Rx= RegExp('\\b'+str+'\\b');
		var who, i= 0, A= [], tem, temp;
		var G= element.getElementsByTagName('*');
		while(G[i]){
			tem= G[i++];
			temp=tem.className|| '';
			if(Rx.test(temp)) A.push(tem);
		}
		return A;
	}

	this.setTemplateID = function(id){ templateID = id; }

	this.setOutputArray = function(arr){ outputArray = arr; }
	this.setOutputID = function(id){ outputID = id; }
	this.setOutputMax = function(count){ maxOutput = count - 1; }

	this.setKeyword = function(keyword){ 
		if (adtype == "afs"){
			ad_setting_string += "\n" + "google_afs_query = '" + keyword + "';";
		}else{
			ad_setting_string += "\n" + "google_kw_type = 'broad';";
			ad_setting_string += "\n" + "google_kw = '" + keyword + "';";

		}
	}

	this.setGoogleSetting = function(holder, value){
		ad_setting_string += "\n" + holder + " = '" + value + "';";
	}

	this.createSettingsScript = function(){
        document.write("<script type='text/Javascript'>" + ad_setting_string + "</script>");
        document.write('<script language="JavaScript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');

		if (adtype == "afc"){
			ad_setting_string = "google_ad_output = 'js';";
		}
	}

	this.getAFCAdsense = function(){

		stripycowAdsenseObj.createSettingsScript();
		document.write('<script language="javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
	}

	this.getAFSAdsense = function(){
		stripycowAdsenseObj.createSettingsScript();
		document.write('<script language="javascript" src="http://www.google.com/afsonline/show_afs_ads.js"></script>');
	}

	this.receiveAFCAdsense = function(adsense){
        stripycowAdsenseObj.insertAdsense(adsense);
	}

    this.insertAdsense = function(adsense){
        leave = false;
        for (var i = 0; i < adsense.length; i++){
            e = adsense[i];
            //Wir haben alle Container gefuellt
            if (leave == true){
                if (callbackcode){
                    eval(callbackcode);
                    callbackcode = "";
                }
                return false;
            }

            cloned = stripycowAdsenseObj.$(templateID).cloneNode(true);
            cloned.id = "";

            stripycowAdsenseObj.select(cloned, templateID + "-visible_url")[0].innerHTML = e.line1;
            stripycowAdsenseObj.select(cloned, templateID + "-url")[0].href = e.url;

            stripycowAdsenseObj.select(cloned, templateID + "-line2")[0].innerHTML = e.line2;
            if (e.line3){
                stripycowAdsenseObj.select(cloned, templateID + "-line3")[0].innerHTML = e.line3;
            }else{
                stripycowAdsenseObj.select(cloned, templateID + "-line3")[0].innerHTML = "";
            }

            stripycowAdsenseObj.select(cloned, templateID + "-visible_url")[1].innerHTML = e.visible_url;
            stripycowAdsenseObj.select(cloned, templateID + "-url2")[0].href = e.url;
            cloned.style.display = "";


            //Wir haben ein Array an Targets bekommen
            try{
                if (outputArray[0]){
                    stripycowAdsenseObj.$(outputArray[outputPos]).appendChild(cloned);
                    outputPos++;
                    if (outputPos == outputArray.length){
                        leave = true;
                    }
                }else{
                    stripycowAdsenseObj.$(outputID).appendChild(cloned);
                }
            }catch(e){
                if (maxOutput >= i){
                    stripycowAdsenseObj.$(outputID).appendChild(cloned);
                }
            }

        }

        if (callbackcode){
            eval(callbackcode);
            callbackcode = "";
        }

        //Wir loeschen das Template da wir es nicht mehr brauchen
        //stripycowAdsenseObj.afterFinsishInserting();
    }

	this.receiveAFSAdsense = function(adsense){
        stripycowAdsenseObj.insertAdsense(adsense);

	}

	this.afterFinsishInserting = function(){
		//Wir brauchen das Template nicht mehr
		stripycowAdsenseObj.$(templateID).remove();
	}

}




function google_ad_request_done (google_ads) { stripycowAdsenseObj.receiveAFCAdsense(google_ads);  }
function google_afs_request_done(google_ads) { stripycowAdsenseObj.receiveAFSAdsense(google_ads); }
