var popeaterMooEffects = {};
popeaterMooEffects.searchEvents = function() {
	var aObjArr = $$('#searches ul li a');
	var txt = $$('#searches form input#asylum-search').get('value');
	aObjArr.each(function(el) {
		el.addEvent('click', function(event) {
			var li = el.getParent('li'), liClass = li.get('class'), inpValCur = txt;
			li.getParent('ul').getChildren('li').each(function(elm) {
				elm.removeClass('current');				
			});
			li.addClass('current');
			$$('#searches form input[type=text]').each(function(elm) {
				var inpVal = elm.get('value');
				if(inpVal != txt && inpVal != '') {
					inpValCur = inpVal;
				}
				elm.set('value', txt);
			});
			$$('#searches form').each(function(elm) {
				var formId = elm.get('id');
				if(liClass.contains(formId)) {
					elm.removeClass('hide');
					$(elm).getElement('input[type=text]').set('value', inpValCur);
				}else {
					elm.addClass('hide');
				}
			});
			return false;
		});
	});
   	var formObjArr = $$('#searches form');
   	var clientVal = $$('#searches input[id=formUsrAgent]').get('value');
   	var cobrandVal = $$('#searches input[id=formCobrand]').get('value');
  	formObjArr.each(function(el) {
   		var inpObj = $(el).getElement('input[type=text]');
		el.addEvent('submit', function(e) {
   			if((cobrandVal == "aolde" || cobrandVal == "alice") && (clientVal == 1)) {
   				if (el.get('id') == 'search-web') {
   					actionURL = "http://suche.aolsvc.de/aol/search?";
   				}
   				else if (el.get('id') == 'search-img') {
   					actionURL = "http://suche.aolsvc.de/aol/image?";
   				} else {
   					actionURL = el.get('action');
   				}
   			} else {
   				actionURL = el.get('action');
   			}
			el.set('action', actionURL  + '/query/' + inpObj.get('value'));
		});

    		inpObj.addEvents({
			focus: function(event) {
				if(this.value == txt){
					this.value = '';
				}else{
					this.value = this.value
					this.className = 'searchTxt';
				}				
			},
			blur: function(event) {
				if(this.value == '') {
					this.value = txt;
				}
			}		
    		});
   	});
}

popeaterMooEffects.searchEventsFt = function() {
	var aObjArr = $$('#searchesFt ul li a');
	var txt = $$('#searchesFt form input#news-search').get('value');
	aObjArr.each(function(el) {
		el.addEvent('click', function(event) {
			var li = el.getParent('li'), liClass = li.get('class'), inpValCur = txt;
			li.getParent('ul').getChildren('li').each(function(elm) {
				elm.removeClass('current');				
			});
			li.addClass('current');
			$$('#searchesFt form input[type=text]').each(function(elm) {			
				var inpVal = elm.get('value');
				if(inpVal != txt && inpVal != '') {
					inpValCur = inpVal;
				}
				elm.set('value', txt);
			});
			$$('#searchesFt form').each(function(elm) {
				var formId = elm.get('id');
				if(liClass.contains(formId)) {
					elm.removeClass('hide');
					$(elm).getElement('input[type=text]').set('value', inpValCur);
				}else {
					elm.addClass('hide');
				}
			});
			return false;
		});
	});				
   	var formObjArr = $$('#searchesFt form');
   	var clientValFt = $$('#searchesFt input[id=formUsrAgentFt]').get('value');
    	var cobrandValFt = $$('#searchesFt input[id=formCobrandFt]').get('value');
   	formObjArr.each(function(el) {
   		var inpObj = $(el).getElement('input[type=text]');
		el.addEvent('submit', function(e) {
   			if((cobrandValFt == "aolde" || cobrandValFt == "alice") && (clientValFt == 1)) {
   				if (el.get('id') == 'search-web') {
   					actionURL = "http://suche.aolsvc.de/aol/search?";
   				}
   				else if (el.get('id') == 'search-img') {
   					actionURL = "http://suche.aolsvc.de/aol/image?";
   				} else {
   					actionURL = el.get('action');
   				}
   			} else {
   				actionURL = el.get('action');
   			}
			el.set('action', actionURL  + '/query/' + inpObj.get('value'));
		});
		if(el.get('id') == 'search-sl') {
   			el.addEvent('submit', function(e) {
				el.set('action', el.get('action') + inpObj.get('value'));
			});
		}
    		inpObj.addEvents({
			focus: function(event) {
				if(this.value == txt){
					this.value = '';
				}else{
					this.value = this.value
					this.className = 'searchTxt';
				}				
			},
			blur: function(event) {
				if(this.value == '') {
					this.value = txt;
				}
			}		
    		});
   	});
}
// window add event hijacked - attach to the document	
document.addEvent('domready', function() {
	popeaterMooEffects.searchEvents();
	popeaterMooEffects.searchEventsFt();
});

// Google AFC standard JS function
function google_ad_request_done(google_ads) {
  if (google_ads.length>=1) {
    try {
      google_ads.reverse();
    } catch(e) {}
    if (typeof google_ad_targets != "undefined") {
      for (google_ad_spot in google_ad_targets) {
        var spot_obj = false;
        try {
          spot_obj = document.getElementById(google_ad_spot);
        } catch(e) {}
        if (spot_obj != false && spot_obj != null) {
          var ad_count = google_ad_targets[google_ad_spot];
          if (typeof google_ad_header_text == "undefined") {
            google_ad_header_text = "Sponsored Links";
          }
          var ad_markup = "<div class='gad_content'><div class='gad_header'>" + google_ad_header_text;
          ad_markup += "</div>";
          for (var i = 0; i<=ad_count-1;i++) {
            if (google_ads.length >=1) {    
              var google_ad = google_ads.pop();
              var ad_url = google_ad.url;
              var ad_visible_url = google_ad.visible_url;
              var ad_line1 = google_ad.line1;
              var ad_line2 = google_ad.line2;
              var ad_line3 = google_ad.line3;
              ad_markup += "<div class=\"gad\" id=\"gad_" + google_ad_spot;
              ad_markup += "_" + (i+1) + "\"><p><a href=\"" + ad_url + "\" onmouseout=\"window.status=''\"";
              ad_markup += " onmouseover=\"window.status='go to " + ad_visible_url + "';return true;\">";
              ad_markup += "<span class=\"gad_title\">" + ad_line1 + "</span><br/><span class=\"gad_body\">";
              ad_markup += ad_line2 + "" + ad_line3 + "</span><br/><span class=\"gad_link\">";
              ad_markup += ad_visible_url + "</span></p></div>";
            }
          }
          ad_markup += "</div>";
          spot_obj.innerHTML = ad_markup;
        }
      }
    }   
  }
  return true;
}

//String trim JS function
String.prototype.trim = function () {
	var extraSpace = /^\s+(.*?)\s+$/; // white space trailing, leading and any number chars
	return this.replace(extraSpace, "$1");
}

// Blogsmith standard JS function for e-mail form and contact form
function inputValuesTest(loc) {
	var validName = 0;
	var validEmail = 0;
	var validComments = 0;
	var validEmail2 = 0;
	var email = document.getElementById("AuthorEmail").value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if((document.getElementById("AuthorName").value).trim() != "") {
		validName = 1;
	}
	if (filter.test(email)) {
		validEmail = 1;
	}
	if((document.getElementById("txtComments").value).trim() != "") {
		validComments = 1;
	}
	if (loc == 1) {
		var forward_email = document.getElementById("ForwardEmail").value;
		if (filter.test(forward_email)) {
			validEmail2 = 1;
		}
	}
	if((loc == 2) && (validName && validEmail && validComments)) { return true; } 
	else if ((loc == 1) && (validName && validEmail && validComments && validEmail2)) { return true; }
	else {
		var errorMessage = "Fehlerhafte Eingabe:<br /> ";
		if(validName == 0) { errorMessage += "Du musst einen Namen angeben <br />"; }
		if(validEmail == 0) { errorMessage += "Die Emailadresse ist ungültig <br />"; }
		if((loc == 1) && validEmail2 == 0) { errorMessage += "Weiterleiten e-mail Adresse ist nicht gültig! <br />"; }
		if(validComments == 0) { errorMessage += "Du musst einen Kommentar einfügen <br />"; }
		document.getElementById('formerrors').innerHTML = '<div class = "displayErr">' +errorMessage+ '</div>';
		return false;
	}
}

var popeaterDE = new Object();
var popeaterDE = {
	pageEvents: {
		addLoadEvent: function(obj, evType, fn, useCapture) {
			if (obj.addEventListener) {
				obj.addEventListener(evType, fn, useCapture);
				return true;
			} else if (obj.attachEvent) {
				var r = obj.attachEvent("on"+evType, fn);
				return r;
			} else {
				alert("Handler could not be attached");
			}
		},
		currentCategory: function(current_cat) {
			if(!current_cat) current_cat = "home";
			document.getElementById("cat-"+current_cat).style.fontWeight = "bold";
		},
		galleyMouseevents: function (elID, tgName) {
			var gObj=document.getElementById(elID).getElementsByTagName(tgName);
			for (var i=0; i<gObj.length; i++) {
				gnode = gObj[i];
				gnode.onmouseover = function () {
					this.style.borderColor = "#E80079";
				}
				gnode.onmouseout = function () {
					this.style.borderColor = "#FFFFFF";
				}
			}
		}
	},
	hat: {
		showHat: function(aliceHat,portalHat,clientHat,cbCookie,usrAgent,topcont) {
			if((cbCookie == "alice" || cbCookie == "aolde") && (usrAgent == 1)) {
				clientHat.style.display = "block";
				aliceHat.style.display = "none";
				portalHat.style.display = "none";
				topcont.style.backgroundPosition = "0px 104px";
			}
			else if((cbCookie == "alice") && (usrAgent != 1)) {
				clientHat.style.display = "none";
				aliceHat.style.display = "block";
				portalHat.style.display = "none";
			}
			else if((cbCookie == "aolde") && (usrAgent != 1))  {
				clientHat.style.display = "none";
				aliceHat.style.display = "none";
				portalHat.style.display = "block";
			}
			else {
				clientHat.style.display = "none";
				aliceHat.style.display = "none";
				portalHat.style.display = "block";
			}
		}
	},
	footer: {
		showFooter: function(plftr,alftr,ftrDesc,ua,cbc) {
			if((cbc == "alice" || cbc == "aolde") && (ua == 1)) {
				plftr.style.display = "none";
				alftr.style.display = "none";
				ftrDesc.style.display = "none";
			}
			else if((cbc == "alice") && (ua != 1)) {
				plftr.style.display = "none";
				alftr.style.display = "block";
				ftrDesc.style.display = "block";
			}
			else if((cbc == "aolde") && (ua != 1))  {
				plftr.style.display = "block";
				alftr.style.display = "none";
				ftrDesc.style.display = "block";
			}
			else {
				plftr.style.display = "block";
				alftr.style.display = "none";
				ftrDesc.style.display = "block";
			}
		}
	},
	search: {
		encodeParam: function(param) {
			if(encodeURIComponent)
			return encodeURIComponent(param);
			return escape(param);
		}
	}
} // @end obj
popeaterDE.pageEvents.addLoadEvent (this, "load", function(){
popeaterDE.pageEvents.currentCategory(current_category)}, false);

popeaterDE.pageEvents.addLoadEvent (this, "load", function(){
popeaterDE.pageEvents.galleyMouseevents("featuredgallery", "DIV")}, false);

// This will be converted to mootools soon...
function jq_stuff() {
	$jq("#cat-first > li:last-child").css("background", "none");
	$jq("#cat-second > li:last-child").css("background", "none");
	$jq("#mostRead li").each(function(i){$jq(this).addClass("article"+(i+1));})
	$jq(".icq-wit").hover(function(){ $jq(this).attr("alt", "Per ICQ versenden"); });
	//$jq(".adholder").prev().children(':first-child').addClass("adborder");
	$jq(".hub").children('ul').children(':last-child').prev().children(':first-child').addClass("tagborder");
	
	$jq("#afs_top,#afs_bottom").children(":last-child").children().each(function() {
		$jq(this).hover (function(){
			$jq(this).children(":first-child").find("h2").css("text-decoration","underline");
		},function(){
			$jq(this).children(":first-child").find("h2").css("text-decoration","none");
		});
	});
	$jq(".sltext").find("a").hover(
		function() {
			$jq(this).css("text-decoration","underline");
		},function() {
			$jq(this).css("text-decoration","none");
		}
	);
	$jq("#afs_top ul li:first-child,#afs_bottom ul li:first-child").css("padding-top","0px");
	$jq(".rssFeeds > li:last-child").css("border-bottom","none");
}