// JavaScript Document
antid=""

$(document).ready(function(){
	//Speiseplan
	tableHeightFix();
	//Speiseplan Switch
	$('ul#weekChange').click(function(){
		$('#weekChange li').css('display','block');
		$('#weekChange li').click(function(){
		
			$('#weekChange li').removeClass('aktiv').css('display','none');
			$(this).addClass('aktiv');
			$('#weekChange li').unbind('click');
		
			$.ajax({
				url: DBName + "/(speisseplan)?OpenAgent&" + $('#weekChange li.aktiv').html(),
  				cache: false,
  				processData: false,
  				async: true,
  				success: function(html){
  	  				$(".speisePlan tbody").html(html)
  	  				tableHeightFix();
  	  			}
  			});
  			
  	  		
  		
		return false;
	})
	return false;
}) 
	
	//CiSweb Login
	
	$('#CiSwebLOGIN').click(function(){
			showLogin($(this));
		})
		
	$('#showGBentry').click(function(){
			showGB();
		})
		
	$('#gb_send').click(function(){
			sendGB();
	});
	
	$('.gbant').click(function(){
			antGB($(this).attr("id"));
	});
	$('.gbdel').click(function(){
			delGB($(this).attr("id"));
	});
	$('.gbfrei').click(function(){
			freiGB($(this).attr("id"));
	});
		
	$('#gbanswer_send').click(function(){
			antwGB(antid);
	});
	
	$('.lavaLamp li[id$="' + navid +'"]').addClass("current")
		
	$('#closeLogin, #LoginBG, .closeGB').click(function(){
			$('#LoginBG, #loginBox, #gbook_entry, #gbook_answer').fadeOut()
		})				   
	
						   
	//Navigations-Animation einbinden					   
	$(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 })});
	
	//Ecken an Inhaltselemente anf\u00FCgen
	
	$('.infobox').append('<div class="eck_tr"></div><div class="eck_tl"></div><div class="eck_br"></div><div class="eck_bl"></div>');


	//GalleryView
	
	$('#photos').galleryView({
    panel_width: 620,
    panel_height: 270,
    transition_speed: 1500,
    transition_interval: 5000,
    nav_theme: 'light',
    border: 'none',
    pause_on_hover: true
});
})

function showLogin(a){
		$('#LoginBG').fadeTo(200,0.7)
		$('#loginBox').fadeIn()
		//a.attr('href','cisweb4_Biesok.nsf/allView.xsp')
		return false;
}

function showGB(){
		$('#LoginBG').fadeTo(200,0.7)
		$('#gbook_entry').fadeIn();
		return false;
}

function antGB(id){
		$('#LoginBG').fadeTo(200,0.7)
		$('#gbook_answer').fadeIn();
		antid=id
		return false;
}

function sendGB(){
	if($('#gbname').val()==""){
		alert("Bitte geben Sie Ihren Name ein!")
		return false;
	}
	
	if($('#gbemail').val()==""){
		alert("Bitte geben Sie Ihre Email ein!")
		return false;
	}
	
	if($('#gbsubject').val()==""){
		alert("Bitte geben Sie einen Betreff ein!")
		return false;
	}
	
	if($('#gbeintrag').val()==""){
		alert("Bitte geben Sie einen Text ein!")
		return false;
	}else{
		if(EMail($('#gbeintrag').val())){
			alert("Bitte geben Sie einen Text ein!")
			return false;
		}
	}
	$('#gbook_entry').fadeOut();
	
	$.ajax({
		url: DBName + "/savegb?OpenAgent&" + $('#gbname').val() + "&" + $('#gbemail').val() + "&" + $('#gbeintrag').val() + "&" + $('#gbsubject').val() ,
  		cache: false,
  		processData: false,
  		success: function(html){
  	  			$('#gb').html("Vielen Dank f\u00FCr Ihren Eintrag, er muss nur noch freigeschaltet werden!")
  		}
  	});
  	  		
}

function EMail(s)
{
  var a = false;
  var res = false;
  if(typeof(RegExp) == 'function')
    {
    var b = new RegExp('abc');
    if(b.test('abc') == true){a = true;}
    }

  if(a == true)
    {
    reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                     '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                     '(\\.)([a-zA-Z]{2,4})$');
    res = (reg.test(s));
    }
  else
    {
    res = (s.search('@') >= 1 &&
           s.lastIndexOf('.') > s.search('@') &&
           s.lastIndexOf('.') >= s.length-5)
    }
  return(res);
 }
 
 function delGB(id){
 	$.ajax({
		url: DBName + "/delgb?OpenAgent&" + id + "&d",
  		cache: false,
  		processData: false,
  		success: function(html){
  	    		location.reload()
  	  		
  	  	}
  	});
 }
  	
 function freiGB(id){
 	$.ajax({
		url: DBName + "/delgb?OpenAgent&" + id + "&f",
  		cache: false,
  		processData: false,
  		success: function(html){
  				location.reload()
  	  	}
  	});	
 }
 
 function antwGB(id){
 	$.ajax({
		url: DBName + "/delgb?OpenAgent&" + id + "&a&" + $('#gbanswereintrag').val(),
  		cache: false,
  		processData: false,
  		success: function(html){
  				location.reload()
  	  	}
  	});	
 }
 
 function tableHeightFix(){
  	var tabelle = $('table.speisePlan tbody');
 	tabelle.find('tr').each(function(){
 		 maxHeight_h1 = new Array();
 		 maxHeight_p = new Array();
 	$(this).find('td').each(function(){
 		maxHeight_h1.push($(this).find('h1').innerHeight());
 		maxHeight_p.push($(this).find('p.eng').innerHeight());
 		});
 		maxHeight_h1.sort(Numsort).reverse();	maxHeight_p.sort(Numsort).reverse();
 		$(this).find('h1').css('height',maxHeight_h1[0])
 		$(this).find('td p.eng').css('height',maxHeight_p[0])			
 	})
 	
 
  };
  
  //Array-Sortierfunktion f\u00FCr Zahlen
  function Numsort(a,b){
  return a-b;
  }