// Allgemein -------------------------------------------------------------------
var is_ie = (navigator.userAgent.indexOf("Explorer") > -1);
var is_moz = (navigator.userAgent.indexOf("Firefox") > -1);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);

var add_fadein_opacity = 0;
var add_fadeout_opacity = 100;

var ajax_url = '/index.php?mode=ajax&func=save_comment';
var ratings = new sack(ajax_url);

function uri() {
        var url;
        url = trim (document.search.query.value);
	url = '/' + encodeURI(url.replace(/ |&/g, "-"));
	location.href = url.replace(/[\-]{2,}/,'-');
        return false;
}


function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function validemail(email) {
    var strReg = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+\.+([a-zA-Z0-9]{2,4})+$";
    var regex = new RegExp(strReg);
    return(regex.test(email));
}
// Kommentar Funktionen --------------------------------------------------------

var domain_id = 0;
var comment_rating = 0;
var comment_text = '';
var comment_user_name = '';
var comment_user_email = '';
var comment_user_website = '';

var post_show = 'normal';
var rate_fadein_opacity = 0;
var rate_fadeout_opacity = 100;
var is_being_rated = false;

// Post Ratings Fade In Text
function rade_fadein_text() {
    if(rate_fadein_opacity < 100) {
        rate_fadein_opacity += 10;
        if(is_opera)  {
            rate_fadein_opacity = 100;
        } else if(is_ie) {
            document.getElementById(ratings.element).filters.alpha.opacity = rate_fadein_opacity;
        } else if(is_moz) {
            document.getElementById(ratings.element).style.MozOpacity = 1;
            rate_fadein_opacity = 100;
        }
        setTimeout("rade_fadein_text()", 100);
    } else {
        document.getElementById(ratings.element).innerHTML = ratings.response;
        rate_fadein_opacity = 100;
        is_being_rated = false;
    }
}


// When User Mouse Over Ratings
function current_rating(position) {
    for(i = 1; i <= 5; i++) {
        if (i <= position) {
            document.getElementById('irating' + i).className = 'ratinga' + i;
        }
        else
            document.getElementById('irating' + i).className = 'rating' + i;
        if (i == position) {
            document.getElementById('ratingdesc').innerHTML = '';
            document.getElementById('ratingdesc').innerHTML = ratingtext[i];
        }

    }
}

// When User Mouse Out Ratings
function ratings_off() {
    if (comment_rating == 0) {
        for(i = 1; i <= 5; i++) {
            document.getElementById('irating' + i).className = 'rating' + i;
            document.getElementById('ratingdesc').innerHTML = '';
            document.getElementById('ratingdesc').innerHTML = ratingtext[9];
        }
    } else {
        current_rating(comment_rating);
    }


}

// When User Mouse Out Ratings
function set_rating(value) {
    comment_rating = value;
    current_rating(value);

}

// Check Form

// Process Post Ratings
function save_comment() {
    if(rate_fadeout_opacity > 0) {
            rate_fadeout_opacity -= 10;
            if(is_opera) {
                rate_fadein_opacity = 0;
            } else if(is_ie) {
                document.getElementById('commentform').filters.alpha.opacity = rate_fadeout_opacity;
            } else if(is_moz) {
                document.getElementById('commentform').style.MozOpacity = (rate_fadeout_opacity/100);
            }
            setTimeout("save_comment()", 30);
        } else {
            document.getElementById('commentform').style.display = 'none';
            document.getElementById('commenterror').style.display = 'none';
            rate_fadeout_opacity = 0;
            ratings.setVar("domainid", encodeURIComponent(document.kommentar.domainid.value));
            ratings.setVar("token", encodeURIComponent(document.kommentar.token.value));
            ratings.setVar("text", encodeURIComponent(document.kommentar.kommentar_text.value));
            ratings.setVar("user_name", encodeURIComponent(document.kommentar.user_name.value));
            ratings.setVar("user_email", encodeURIComponent(document.kommentar.user_email.value));
            ratings.setVar("user_website", encodeURIComponent(document.kommentar.user_website.value));
            ratings.setVar("rating", comment_rating);
            ratings.method = 'GET';
            ratings.element = 'new-comment';
            ratings.onCompletion = rade_fadein_text;
            ratings.runAJAX();
            rate_fadein_opacity = 0;
            rate_fadeout_opacity = 100;
        }
}

function check_rating_form(form, box) {
    var count = 0;
    while (element = document.forms[form].elements[count])
    {
        if (element.type != 'hidden' && element.name != '')
           document.getElementById('label_kommentar_' + element.name).style.backgroundColor = '#FFFFFF';

        count++;
    }
    if(comment_rating > 0)
    {
        document.getElementById('label_kommentar_bewertung').style.backgroundColor = '#FFFFFF';
    }
    count = 0;
    while (element = document.forms[form].elements[count])
    {
        if ((element.value == '' || element.value == 'Geben Sie hier Ihren Kommentar ein.')
            && element.name != ''){
            if (element.name != 'user_website' || element.name == 'kommentar_text') {
                hint(element.name, '#E28C01', box, form);
                return false;
                break;
            }
        }
        if (element.name == 'kommentar_text' && element.value.length < 50) {
            hint(element.name, '#E28C01', box, form);
            return false;
            break;
        }
        
        if (element.name == 'user_email') {
            if (!validemail(element.value)) {
                hint(element.name, '#E28C01', box, form);
                return false;
                break;
            }
        }

        count++;
    }
    if(comment_rating < 1 ) {
        hint('', '#FF3E41', box, form);
        return false;
    }

    // Alles Ok, Kommentar speichern
    save_comment();
}

function hint(feld, flag, box, form)
{
    if (feld) {
        document.forms[form].elements[feld].focus();
    }
    document.getElementById(box).style.display='block';

    var text = '';
    var hit = 0;
    var label = 'label_kommentar_';
    if(feld == 'kommentar_text')
    {
        if (document.forms[form].elements[feld].value.length < 50 
            && document.forms[form].elements[feld].value.substring(0, 35) != comment_messages['Geben Sie hier Ihren Kommentar ein']) {
            text = '<p>'+ comment_messages['Ihr Kommentar muss mindestens 50 Zeichen enthalten'] +'</p>';
        } else {
            text = '<p>'+ comment_messages['Bitte geben Sie einen Kommentar an'] +'</p>';
        }
        
        label = label + feld;
        hit = 1;

    }
    if(feld == 'user_name')
    {
        text = '<p>'+ comment_messages['Bitte geben Sie einen Namen an'] +'</p>';
        label = label + feld;
        hit = 1;
    }
    if(feld == 'user_email')
    {
        text = '<p>'+ comment_messages['Bitte geben Sie eine korrekte E-Mail Adresse an'] +'</p>';
        label = label + feld;
        hit = 1;
    }
    if(comment_rating < 1 && hit == 0)
    {
        text = '<p>'+ comment_messages['Bitte bewerten Sie diese Webseite'] +'</p>';
        label = 'label_kommentar_bewertung';
    }

    document.getElementById(label).style.backgroundColor = flag;
    document.getElementById(box).innerHTML = text;
}

(function($){
    $.fn.showdelay = function(){
        var delay = 0;
        return this.each(function(){
            if ($(this).css('display') != 'block') {
                $(this).hide()
                $(this).delay(delay).fadeIn(1200);
                $(this).focus();
                delay += 700;
            }
        });
    };
})(jQuery);

function show_all_comments() {
    $('#morecomments').css('display','none');
    $(".hreview").showdelay();

}

function show_all_description() {
    $('#langebeschreibunganzeigen').css('display','none');
    $(".langebeschreibung").showdelay();

}

// Sack AJAX Framework ---------------------------------------------------------
/* Simple AJAX Code-Kit (SACK) */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence, see documentation or authors website for more details */

function sack(file){
	this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true;
		}
	};

	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}

	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}

	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}

	this.runResponse = function(){
		eval(this.response);
	}

	this.runAJAX = function(urlstring){
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){
			alert(this.AjaxFailedAlert);
		} else {
			if (urlstring){
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring;
				} else {
					this.URLString = urlstring;
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime();
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			if (this.element) { this.elementObj = document.getElementById(this.element); }
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestFile + "&" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
					} catch (e) {}
				}

				this.xmlhttp.send(this.URLString);
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.runResponse(); }
							if (self.elementObj) {
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							self.URLString = "";
						break;
					}
				};
			}
		}
	};
this.createAJAX();
}

// Searchads Function ----------------------------------------------------------
function show_search_ads(ad_count, ad_width, ad_height, ad_thumbs, div) {

    for (var i = 0; i < document.getElementsByTagName('meta').length; i++) {
       if (document.getElementsByTagName('meta')[i].getAttribute('name') == 'keywords') {
           ad_keys = document.getElementsByTagName('meta')[i].getAttribute('content');
       }
    }
    var ad_url = '/index.php?mode=ajax&func=get_search_ads';

        $.getScript(ad_url+
            "&ad_loc=" + escape(window.location)+
            "&ad_keys=" + escape(ad_keys)+
            "&ad_count=" + ad_count+
            "&ad_div=" + div+
            "&ad_width=" + ad_width+
            "&ad_height=" + ad_height+
            "&ad_thumbs=" + ad_thumbs);

}
