function aivao_catch_a(options){
	$("a").click(function(e){
		if($.trim($(this).attr("href")) == "#"){
		 	e.preventDefault();
		}
	});
}

function aivao_needles(needle){
	
	$(function() {
		$("#content").highlight(needle);
	});
}

function aivao_toggleevents(options){
	if(options["mode"] == "show"){
		$("#events_pane").find(".hideevent").show();
		$("#events_pane").find(".related_hd").hide();
		$("#events_pane").find(".toggle").html("<a href=\"#\" onclick=\"aivao_toggleevents({mode:'hide'})\">« "+aivao_translations["show related events only"]+"</a>");
	}
	if(options["mode"] == "hide"){
		$("#events_pane").find(".hideevent").hide();
		$("#events_pane").find(".related_hd").show();
		$("#events_pane").find(".toggle").html("<a href=\"#\" onclick=\"aivao_toggleevents({mode:'show'})\">» "+aivao_translations["show all events"]+"</a>");
	}
}

$(function() {
	aivao_catch_a({});
	
	$("#tweet").tweet({
        username: "andreasbick",
        avatar_size: 42,
        count: 3,
        loading_text: "loading tweets..."

  });

	
	$(".bookmarks").find(".bookmark").hover(
		function () {
			$(this).find("img").css({opacity:1});
	  	}, 
	  	function () {
			$(this).find("img").css({opacity:0.3});
	  	}
	);
	
	$(".subnavi_contacts").find("li").each(function(){
		$(this).hoverIntent({    
			 sensitivity: 3,
			 interval: 50,  
			 over: function(){
				$(this).find(".detail").fadeIn(300);
			},
			 timeout: 300,   
			 out: function(){
				$(this).find(".detail").fadeOut(300);
			}
		});
	});

	
	$('ul.sf-menu').superfish({ 
		delay:       400, 
		pathClass:     'sf-current',
		animation:   {opacity:'show'},
		dropShadows: false   
		
	});
	
	$(".searchinput").click(function(event){
	  	event.preventDefault();
	 	 if($.inArray(event.currentTarget.value+":", aivao_translate['search']) || $.inArray(event.currentTarget.value.toLowerCase()+":", aivao_translate['search'])){
			event.preventDefault();
			event.currentTarget.value = "";
		}
	});

	//$("#news_pane, #events_pane").jScrollPane();
	
	
	
	$("#events_pane").find(".item").hover(
		function () {
			//$(this).find("img").css({opacity:1});
			
	  	}, 
	  	function () {
			//$(this).find("img").css({opacity:0.25});
			
	  	}
	);
	
	$(".marginbox").find(".bookmark").hover(
		function () {
			$(this).find("img").css({opacity:1});
	  	}, 
	  	function () {
			$(this).find("img").css({opacity:0.3});
	  	}
	);
	
	/*
	$("a.link_pdf_list").each(function (){ 
		var link = $(this);
		$.post("/cms/front/ajax/filesize.php", {file:$(this).attr("href")}, function(data) {
			//alert(data["filesize"]);
			link.append(data["filesize"]);
		}, "json");
		
	});
	*/
	$("img.img_wide").each(function (){ 
		$(this).wrap("<div class=\"border_wide\"></div>");
	});
	/*
	$("a").each(function (){ 
		if($(this).attr("href").indexOf("[") > -1 && $(this).attr("href").indexOf("]") > -1){
			
			var link = $(this);
			
			$.post("/cms/front/ajax/loclinks.php", {href:link.attr("href"),location_id:location_id,language_id:language_id}, function(data) {
				//alert(data["href"]);
				link.attr({"href":data["href"]});

			}, "json");
		}
		
	});
	*/
});

function aivao_refreshpop(options){
	if(!$("#pop").is(":visible")){
		aivao_show_pop();
		var posi =  $(window).scrollTop()+80;
		
		$("#pop > .box").css({ 
			"top" : posi+"px"
		});
		$("#pop > .box").fadeIn(500);
	}
}

function aivao_show_pop(){
	$("#pop_bg").css({"opacity":"0.5"});
	$("#pop_bg").show();
	$("#pop").show();
}

function aivao_close_pop(options){
	
	$("#pop > .box").fadeOut(300, function(){
		$("#pop").fadeOut(300, function(){
			
		});
		$("#pop_bg").hide();
		$("#pop > .box > .content").empty();
	});
	
}

function aivao_datestring(options){
	var day = String(options["date"].getDate());
	if(day.length < 2){
		day = "0"+day;
	}
	var month = String(options["date"].getMonth()+1);
	if(month.length < 2){
		month = "0"+month;
	}
	str = day+"."+month+"."+options["date"].getFullYear();
	return str;
}
function aivao_timestring(options){
	var hours = String(options["date"].getHours());
	if(hours.length < 2){
		hours = "0"+hours;
	}
	var minutes = String(options["date"].getMinutes());
	if(minutes.length < 2){
		minutes = "0"+minutes;
	}
	var seconds = String(options["date"].getSeconds());
	if(seconds.length < 2){
		seconds = "0"+seconds;
	}
	str = hours+":"+minutes+":"+seconds;
	return str;
}



//-------------------------------- XXXXXXXXXXXX

function aivao_alert(options){
	var h = 152;
	$("#alert .content .ok").hide();
	$("#alert .content .cancel").hide();
	if(options["okbutton"]){
		$("#alert .content .ok").show();
	}
	if(options["cancelbutton"]){
		$("#alert .content .cancel").show();
	}
	
		$("#alert .content .ok").click(
			function() {
				if(options["okfunction"]){
					options["okfunction"].apply();
				}
			}
		);
	
	
		$("#alert .content .cancel").click(
			function() {
				options["cancelfunction"].apply();
			}
		);
	
	$("#alert .content .feedback").html(options["feedback"]);
	var posi =  Math.round($(window).scrollTop()+($(window).height()/2)-(h/2));
	if(posi < 10){
		posi = 10;
	}
	$("#alert .content").css({ 
		"top" : posi+"px"
	});
	$("#alert").fadeIn(100);
}


//-------------------------------- XXXXXXXXXXXX

function aivao_close_alert(){
	
	$("#alert").fadeOut(100, function(){
		$("#alert .content .feedback").html("");
	});
	
}

function my_checkempty(mandatory) {

	for(n=0;n<mandatory.length;n++){
		if($("#"+mandatory[n]).val() == ""){
			return false
		}
	}
	 return true					
}

function aivao_checkmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length-1
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	    return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}



function aivao_box(options){
	aivao_alert({okbutton:false,feedback:aivao_translations["one moment please"]+"..."});
	$("#pop > .box > .content").empty();
	
	$.post("/cms/front/ajax/"+options["box"]+".php", options, function(data) {
		
		$("#pop > .box > .content").html(data["html"]);
		aivao_refreshpop(options);
		aivao_catch_a({});
		$("#pop > .box").find("input, textarea, select").focus(
			function(){
				$(this).removeClass("highlighted");
			}
		);
		aivao_close_alert();
	}, "json");
}


//-------------------------------- XXXXXXXXXXXX

function aivao_checkbox(options){
	var grant = true;
	var mandas_filled = true;
	$("#pop > .box").find("input, select, textarea").each(function(){
		if($(this).attr("required") && ($.trim($(this).val()) == "" || $.trim($(this).val().toLowerCase()) == aivao_translations['please choose']+":")){
			$(this).addClass("highlighted");
			mandas_filled = false;
		}
	});
	if(!mandas_filled){
		aivao_alert({okbutton:true,cancelbutton:false,feedback:"<b>"+aivao_translations['fill in highlighted']+".</b>"});
		grant = false;
	}
	
	
	if(grant){
		
		
		options["formvars"] = "";
		$("#pop > .box").find("input, textarea, select").each(function(){
			if($(this).attr("type") != "checkbox"){
				options["formvars"] += $(this).attr("name")+"*#*"+$(this).val()+"._.";
			}
		});
		var checkboxes = [];
		var checkboxes_keys = [];
		$("#pop > .box").find(":checkbox").each(function(){		
			if($.inArray($(this).attr("name"), checkboxes_keys) == -1){
				//alert($(this).attr("name"));
				checkboxes_keys.push($(this).attr("name"));
				checkboxes[$(this).attr("name")] = [];
			}
			if($(this).is(":checked")){
				checkboxes[$(this).attr("name")].push($(this).attr("value"));
			}
		});
		for(n=0;n<checkboxes_keys.length;n++){
			options["formvars"] += checkboxes_keys[n]+"*#*"+checkboxes[checkboxes_keys[n]].join(".:.")+"._.";
		}
		options["formvars"] = options["formvars"].substr(0,(options["formvars"].length-3));
		//alert(options["formvars"]);
		aivao_alert({okbutton:false,cancelbutton:false,feedback:aivao_translations["one moment please"]+"..."});
		$.post("/cms/front/ajax/check_"+options["box"]+".php", options, function(data) {
			//alert(data["feedback"]);
			if(data["grant"] == 0){
				aivao_alert({okbutton:true,cancelbutton:false,feedback:data["feedback"]});
				if(data["capchagrant"] == 0){
					$(".capcha .img").html("<img src=\"/cms/php/class/capcha/image.php?guid="+data["guid"]+"&cb="+Math.random()+"\" />");
				}
				for(n=0;n<data["highlighted"].length;n++){
					$("input[name="+data["highlighted"][n]+"]").addClass("highlighted");
				}
				grant = false;
			}
			if(grant){
				if(options["box"] == "form"){
					aivao_alert({okbutton:true,cancelbutton:false,feedback:data["feedback"]});
					//$('html, body').animate({scrollTop:0}, 0);
					aivao_close_pop({});
				}				
			}
		}, "json");

	}
}

//-------------------------------- XXXXXXXXXXXX
