// Referrer- und Countercode Ausgabe
function webhits( opts ){
	if (typeof(opts) == 'object'){
		if (typeof(opts.ref_stat) != 'undefined' && (opts.ref_stat == 1 || opts.ref_stat == 'T' || opts.ref_stat == 'Y')){
			webhitsReferrercode(opts);
		}
		webhitsCountercode(opts);
	}else{
		document.write('<p>Fehler bei der Einbindung des WebHits-Codes. Ihre &uuml;bergebenen Optionen m&uuml;ssen als Object &uuml;bergeben werden.</p>');
	}
}

function webhitsSetCookie(name, wert, expires){
	if (typeof(expires) == 'undefined'){
		d = (new Date);
		d.setTime(( new Date().getTime() + 1000*3600*24*365*10 ));
		expires = d.toGMTString();
	}
	var cook = name + "=" + unescape(wert);
	cook += "; expires=" + expires;
	cook += "; path=/";
	document.cookie = cook;
	alert("Sie haben einem anonymen Nutzungsprofil wiedersprochen. Es werden keine Daten von Ihnen erfasst.");
}

function webhitsDeleteCookie(name) {
	var cook = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
	cook += "path=/";
	document.cookie = cook;
	alert("Sie stimmen einem anonymen Nutzungsprofil zu. Ihre Daten werden anonymisiert und verarbeitet.");
}

function webhitsGetCookie(name) {
	var i=0;
	var suche = name + "=";
	while (i<document.cookie.length) {
		if (document.cookie.substring(i, i + suche.length) == suche) {
			var ende = document.cookie.indexOf(";", i+ suche.length);
			ende = ( ende > -1 ) ? ende : document.cookie.length;
			var cook = document.cookie.substring(i + suche.length, ende);
			return unescape(cook);
		}
		i++;
	}
	return "";
}

function webhitsPrivacy(){
	document.write('<p id="webhitsPrivacy">');
	document.write('<strong>Hinweise zum Datenschutz – Verwendung einer Webanalyse Software:</strong><br />');
	document.write('<input type="button" onclick="webhitsDeleteCookie(\'webhits_track\');" value="Ja" /><span>, von meinen Daten darf ein anonymes Nutzungsprofil erstellt werden.</span><br />');
	document.write('<input type="button" onclick="webhitsSetCookie(\'webhits_track\', \'no\' );" value="Nein*" /><span>, von mir ab sofort kein anonymes Nutzungsprofil mehr erfassen.</span><br />');
	document.write('<small>* erfordert Cookies</small>');
	document.write('</p>');
}

function webhitsReferrercode(opts){
	if (typeof(opts.df) != 'undefined'){
		if( webhitsGetCookie('webhits_track') != 'no' ){
			var strInRef = false;
			if (typeof(opts.ref_ignore) != 'undefined') {
				webhitsOptions = opts.ref_ignore.split(',');
				for (var i=0; i < webhitsOptions.length; i++){
					var val = trim(webhitsOptions[i]);
					if ((document.referrer.length > 0) && (document.images) && (document.referrer.toLowerCase().indexOf(val) >= 0)){
						strInRef = true;
					}
				}
			}

			if ((document.referrer.length > 0) && (document.images) && (strInRef == false)){
				refstat = new Image(1,1);
				refstat.src = "http://www.webhits.de/cgi/refstat?df="+opts.df+"&amp;ref="+escape(document.referrer);
			}
		}
	}else{
		alert('Fehler im Referrercode! Die Counter-ID fehlt in der Variablendefinition.');
	}
}

function getCounterURL( opts ){
	params = getCounterParams(opts);
	if (typeof(opts.https) != 'undefined' && (opts.https == "1" || opts.https == "T" || opts.https == "Y")){
		protokoll = 'https';
	}else{
		protokoll = 'http';
	}
	whURL = protokoll+'://www.webhits.de/cgi/Count.cgi?df='+opts.df+'.dat&'+params.join('&');
	return whURL;
}

function webhitsCountercode(opts, returnHTML){
	if (typeof(opts.df) != 'undefined'){
		if( webhitsGetCookie('webhits_track') == 'no' ){
			opts.incr = '0';
		}
	
		if (typeof(opts.width) == 'undefined'){ opts.width = 'auto'; }
		if (typeof(opts.height) == 'undefined'){ opts.height = 'auto'; }
		
		if (typeof(opts.text) != 'undefined' && (opts.text == "1" || opts.text == "T" || opts.text == "Y")){
			whCode = webhitsCounterAsText(opts, true);
		}else if (typeof(opts.flash) != 'undefined' && (opts.flash == "1" || opts.flash == "T" || opts.flash == "Y")){
			whCode = webhitsCounterAsFlash(opts, true);
		}else{
			if (typeof(opts.sh) != 'undefined' && (opts.sh == "0" || opts.sh == "N" || opts.sh == "F")){
				whCode = '<IMG ALT="Counter" SRC="'+getCounterURL( opts )+'" WIDTH="1" HEIGHT="1" />';
			}else{
				whCode = '<IMG ALT="Counter" SRC="'+getCounterURL( opts )+'" STYLE="width:'+opts.width+';height:'+opts.height+'" />';
			}
		}
	}else{
		whCode = '<p>Fehler im Countercode! Die Counter-ID fehlt in der Variablendefinition.';
	}
	if( typeof( returnHTML ) != 'undefined'){
		return whCode;
	}else{
		document.write( whCode );
	}
}

function webhitsCounterAsText(opts, returnHTML){
	opts.dd = 'javascript';
	whCode = '<SCRIPT LANGUAGE="JavaScript1.2" SRC="'+getCounterURL( opts )+'" TYPE="text/javascript"></SCRIPT>';
	whCode += '<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">';
	whCode += 'if (typeof(WH_COUNTER) != "undefined") {';
	whCode += '	document.write(WH_COUNTER);';
	whCode += '}';
	whCode += '</SCRIPT>';

	if( typeof( returnHTML ) != 'undefined'){
		return whCode;
	}else{
		eval( whCode );
	}
}

function webhitsCounterAsFlash(opts, returnHTML){
	if (typeof(opts.counterJs) != 'undefined' && opts.counterJs != ''){
		opts.dd = 'javascript';
		whCode = '<SCRIPT LANGUAGE="JavaScript1.2" SRC="'+getCounterURL( opts )+'" TYPE="text/javascript"></SCRIPT>';
		whCode += '<SCRIPT LANGUAGE="JavaScript1.2" SRC="'+opts.counterJs+'" TYPE="text/javascript"></SCRIPT>';
		whCode += '<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">';
		whCode += 'document.write(WHTable);';
		whCode += '</SCRIPT><NOSCRIPT><A HREF="http://www.webhits.de/" TARGET="_blank">Counter by WebHits</A></NOSCRIPT>';
	
		if( typeof( returnHTML ) != 'undefined'){
			return whCode;
		}else{
			eval( whCode );
		}
	}else{
		alert('Fehler im Flashcounter! Bitte definieren Sie die Variable counterJs mit dem Pfad zur counter.js.');
	}
}

function getCounterParams(opts){
	var params = new Array();
	if (typeof(opts.display)	!= 'undefined') { params[1]		= 'display='+opts.display;	}
	if (typeof(opts.timezone)	!= 'undefined') { params[2]		= 'timezone='+opts.timezone;}
	if (typeof(opts.tformat)	!= 'undefined') { params[3]		= 'tformat='+opts.tformat; 	}
	if (typeof(opts.dformat)	!= 'undefined') { params[4]		= 'dformat='+opts.dformat; 	}
	if (typeof(opts.ignore)		!= 'undefined') { params[5]		= 'ignore='+opts.ignore; 	}
	if (typeof(opts.ft) 		!= 'undefined') { params[6]		= 'ft='+opts.ft; 			}
	if (typeof(opts.frgb)		!= 'undefined') { params[7] 	= 'frgb='+opts.frgb; 		}
	if (typeof(opts.tr) 		!= 'undefined') { params[8]		= 'tr='+opts.tr; 			}
	if (typeof(opts.md) 		!= 'undefined') { params[9]		= 'md='+opts.md; 			}
	if (typeof(opts.pad) 		!= 'undefined') { params[10] 	= 'pad='+opts.pad;			}
	if (typeof(opts.dd) 		!= 'undefined') { params[11]	= 'dd='+opts.dd; 			}
	if (typeof(opts.comma)		!= 'undefined') { params[12] 	= 'comma='+opts.comma; 		}
	if (typeof(opts.srgb) 		!= 'undefined') { params[13] 	= 'srgb='+opts.srgb; 		}
	if (typeof(opts.prgb) 		!= 'undefined') { params[14] 	= 'prgb='+opts.prgb; 		}
	if (typeof(opts.chcolor)	!= 'undefined') { params[15]	= 'chcolor='+opts.chcolor; 	}
	if (typeof(opts.sh) 		!= 'undefined') { params[16]	= 'sh='+opts.sh; 			}
	if (typeof(opts.incr)	 	!= 'undefined') { params[17] 	= 'incr='+opts.incr; 		}
	if (typeof(opts.negate) 	!= 'undefined') { params[18]	= 'negate='+opts.negate; 	}
	if (typeof(opts.degrees)	!= 'undefined') { params[19]	= 'degrees='+opts.degrees; 	}
	if (typeof(opts.rotate)		!= 'undefined') { params[20]	= 'rotate='+opts.rotate; 	}

	return array_values(params);
}

function array_values (input) {
    var tmp_arr = [], cnt = 0;
    var key = ''; 
    for ( key in input ){
        tmp_arr[cnt] = input[key];
        cnt++;
    } 
    return tmp_arr;
}
function trim (str, charlist) {
    str += '';
    
    if (!charlist) {
        // default list        
		whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {            str = str.substring(i);
            break;
        }
    }
        l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}