
var jajax_form = {
	old_errors:new Array(),
	
	submit : function(obj)
	{
		
		while(!obj.find('form').attr('method'))
		{
			obj=obj.parent();
		}

		form=obj.find('form');
		url=form.attr('action');
		formname=form.attr('name');
		
		//alert(url);
				
		html = $.ajax({
	    url: url,
	    global: false,
	    type: 'POST',
	    data: this.post(formname),
	    dataType: "html",
	    async:false
		  }).responseText;
		this.check_error(html,obj);
		//alert(html);
	
	},
	
	post : function (formname)
	{
	 var elemente= new Array();
	 elemente['text']=1;
	 elemente['password']=1;
	 elemente['hidden']=1;
	 elemente['textarea']=1;
	 elemente['checkbox']=2;
	 elemente['select-one']=1;
	 elemente['select-multiple']=3;
	 elemente['radio']=4;
	 
		mytest='';
		count='';
		num=0;
		i=0;
		while(obj=document.forms[formname].elements[i++])
		{
			if (elemente[obj.type]==1)
			{
				if (mytest!='') mytest+='&';
				
				value=obj.value;
				
				if ($(obj).attr('default')&&value==$(obj).attr('default'))
					value='';
				
				mytest=mytest+obj.name+'='+escape(value.replace(/\+/ig,'<-%-p-l-u-s-%->'));
			}

			if (elemente[obj.type]==2)
			{
				if (mytest!='') mytest+='&';
				if (obj.checked)	myvalue=obj.value; else myvalue='';
				mytest=mytest+obj.name+'='+escape(myvalue.replace(/\+/ig,'<-%-p-l-u-s-%->'));
			}

			if (elemente[obj.type]==3)
				for(ix=0;ix<=obj.options.length-1;ix++)
					if (obj.options[ix].selected)
					{
						if (mytest!='') mytest+='&';
						mytest=mytest+obj.name.replace(/\[\]/,'['+ix+']')+'='+escape(obj.options[ix].value.replace(/\+/ig,'<-%-p-l-u-s-%->'));		
					}
			
			if (elemente[obj.type]==4)
				if (obj.checked) 
				{
					if (mytest!='') mytest+='&';
					mytest=mytest+obj.name+'='+escape(obj.value.replace(/\+/ig,'<-%-p-l-u-s-%->'));
				}
			
		}
		return mytest.replace(/\%3C\-\%25\-p\-l\-u\-s\-\%25\-\%3E/ig,'%2B').replace(/\%u20AC/ig,'%80').replace(/\%u2013/ig,'-');
	},
	
	check_error : function(errors,obj)
	{
		//alert(errors);
		teile=errors.split('|');
		for(key in this.old_errors)
		if(this.old_errors[key])
		{
			if (document.getElementById(key))
			{
				if (this.old_errors[key]!=1)
					document.getElementById(key).className=this.old_errors[key];
				else
					document.getElementById(key).className='no_error';
			}
			this.old_errors[key]=0;
		}
		
		if (teile[0]!='ok'){
			
			
			for(i in teile)
			if (teile[i])
			{
				if (teile[i] == 'errortext')
				{
					document.getElementById('error').innerHTML = teile[(i*1)+1];
				}
				
				if (document.getElementById(teile[i]))
				{
					this.old_errors[teile[i]]=document.getElementById(teile[i]).className;
					if (!this.old_errors[teile[i]]) this.old_errors[teile[i]]=1;
					document.getElementById(teile[i]).className+=' error';
				}
			}
		}

		if (teile[0]=='ok')
		{
			if (teile[1])
			{
				teile[1]=teile[1].replace(/&amp;/g,'&');
				location.href=teile[1];
			}
			if (teile[2])
			{
				teile[2]=teile[2].replace(/&amp;/g,'&');
				eval(teile[2]);
			}
		}
	},
	
	un_check_error:function()
	{
		for(key in this.old_errors)
		if(this.old_errors[key])
		{
			if (document.getElementById(key))
			{
				if (this.old_errors[key]!=1)
					document.getElementById(key).className=this.old_errors[key];
				else
					document.getElementById(key).className='no_error';
			}
			this.old_errors[key]=0;
		}
	}
	
	
	
}

var jbildergalerie = {
		bilder: 0,
		currentImg : 0,
		
		bildklick : function(obj)
		{
			$('#bildgross').attr('src',obj.attr('bildgross'));
			this.currentImg = obj.attr('bildnr')*1;
			this.bildnavigation();
		},
		
		bildnavigation : function (){
			navigation = $('.bildnavigation');
			navigation.find('span').html(this.currentImg+" von "+this.bilder);
			navigation.find('a:first').attr('href','javascript:jbildergalerie.naviklick(-1)');
			navigation.find('a:last').attr('href','javascript:jbildergalerie.naviklick(1)');
			this.bildBorder();
		},
		
		naviklick : function (richtung){
			bildnum = this.currentImg+(richtung*1);
			if(bildnum<=0) bildnum=this.bilder;
			if(bildnum>this.bilder) bildnum=1;
			
			$($('.thumb').get(bildnum-1)).click();
		},
		
		bildBorder : function(){
			currentBild = this.currentImg;
			$('.miniGalerie .miniPic').each(function(){
				$(this).removeClass('rahmen');
				actPic = $(this).find('img');
				actPic.removeClass('rahmen');
				bildNr = actPic.attr('bildnr');
				//console.log(bildNr + '_' + currentBild);
				if(bildNr == currentBild){
					actPic.addClass('rahmen');
					$(this).addClass('rahmen');
				}
			});
		}
}

var jGalerieView = {
	click : function(obj)
	{
		
		url=obj.attr('ajaxlink').split('?');
		
		html = $.ajax({
		     url: url[0],
		     global: false,
		     type: 'GET',
		     data: url[1],
		     dataType: "html",
		     async:false
			   }
				).responseText;
		//$('#overlay').show();
		//galeriePos = $(window).height();
		$('body').append('<div id="overlay"></div>');
		$('body').append('<div id="galerieView">'+html+'</div>');
		var verticalPos = $(window).scrollTop()+($(window).height()-$('#galerieView').height())/2;
		
		if (verticalPos<$(window).scrollTop())
		verticalPos=$(window).scrollTop();
		
		var horizontalPos= ($(window).width()-$('#galerieView').width())/2;
		if (horizontalPos<0)
			horizontalPos=0;
		
		$('#galerieView').css('top',verticalPos+'px');
		$('#galerieView').css('left',horizontalPos+'px');
		
		jload.bildergalerie();

		return false;
	},

	close : function()
	{
		$('#overlay').hide();
		$('#galerieView').remove();
		$('#overlay').remove();
	}

}





var jschlagwort = {

	suche : function(obj){
		location.href="index.php?inhalt=suche&suche="+obj.html()+$('#schlagwortsuche').attr('url');
	}
	
}

var jdanke = {

	show : function()
	{
		$('#form_empfehlen').hide();
		$('#danke_empfehlen').show();
	},
	
	close : function()
	{
		tb_remove();
	}
}
	        
var jveranstaltung = {
		danke : function()
		{
			//alert("danke");
			$('.anmeldung').hide();
			$('.anmeldung_danke').show();
		}
}

var jload = {
	load : function()
	{
		this.loadBackgroundImages();
		
		this.extendjquery();
		
		this.ajaxform();
		this.inputdefault();
		this.galerieView();
		this.stellen_detail();
		this.downloadsList();
		this.lifecycle();
	},

//	loadBackgroundImages : function(){
//		$(".bodybackground img").each(function(){
//			$(this).attr("src",$(this).attr("loadSrc"));
//		});
//	},
	
	loadBackgroundImages : function(){
		var bgImages = $(".bodybackground img");
		bgImages.first().attr("src",bgImages.first().attr("loadSrc"));
		for(i = 0; i < bgImages.length-1; i++){
			$(bgImages.get(i)).load(this.loadImage($(bgImages.get(i+1)), i));
		}
	},
	
	loadImage : function(image, i){
		image.attr("src",$(image).attr("loadSrc"));
	},
	
	lifecycle : function()
	{
		$($('.itlifecycle ul').get(0)).cycle({
					speed:  500,
					timeout: 0,
					startingSlide:0,
					timeout: 3000,
					before:function(){
						$(this).css('background-color','transparent');
					}
		});
		
		$('.itlifecycle map area').click(function(){
			
			$($('.itlifecycle ul').get(0)).cycle($(this).attr('id').replace('map','')*1);
			$($('.itlifecycle ul').get(0)).cycle('pause');
			return false;
		});
		
	},

	downloadsList : function()
	{
		$('.downloadsList li').bind('click',function(){
			if ($(this).find('a').attr('href'))
			{
				location.href=$(this).find('a').attr('href');
			}
		})
	},

	stellen_detail : function()
	{
		$('tr.stellen_detail').bind('click',function(){
			if ($(this).attr('href'))
			{
				location.href=$(this).attr('href');
			}
		})
	},
	
	galerieView : function(){
		$('.galerieItem a').live('click', function(){return jGalerieView.click($(this))});
	},
	
	bildergalerie : function(){
		$('#galerieView .close').click(function(){jGalerieView.close()});
		$('.thumb').click(function(){jbildergalerie.bildklick($(this))});
		jbildergalerie.bilder=$('.thumb').length;
		//random = Math.round(Math.random()*jbildergalerie.bilder)-1;
		$($('.thumb').get(0)).click();
	},

	extendjquery : function()
	{
		jQuery.fn.outerHTML = function(s)
		{
			return (s)
			? this.before(s).remove()
			: jQuery("<p>").append(this.eq(0).clone()).html();
		};
	},

	thickbox : function() //wird aus thickbox.js aufgerufen.
	{
			jload.inputdefault();
			jload.ajaxform();
			jload.closewindow();
	},
	
	closewindow : function()
	{
		$('#danke_close span').bind('click',function(){jdanke.close();})
	},
	
	
	
	ajaxform : function()
	{
		
		if ($('.form').html())
		{
			$('.form .button').live('click',function()
			{
				jajax_form.submit($(this));
				return false;
			});			
		}
		
	},
	
	
	
	inputdefault : function(obj)
	{
		
		if (!obj)
			obj=$('body');
		
		
		obj.find('input').each(function(){
			
			if ($(this).attr('default'))
			{
				
				$(this).bind('blur',function()
				{
					if ($(this).val()=='')
						$(this).val($(this).attr('default'));
					
					
				});
				
				$(this).bind('focus',function()
				{
					if ($(this).val()==$(this).attr('default'))
						$(this).val('');
				});
				
				if ($(this).val()=='')
					$(this).val($(this).attr('default'));
				
				
			}
		});
		
		obj.find('textarea').each(function()
		{
			
			if ($(this).attr('default'))
			{
				
				$(this).bind('blur',function()
				{
					if ($(this).val()=='')
						$(this).val($(this).attr('default'));
				});
				
				$(this).bind('focus',function()
				{
					if ($(this).val()==$(this).attr('default'))
						$(this).val('');
				});
				
				if ($(this).val()=='')
					$(this).val($(this).attr('default'));
			}
		});
	}
	

}




$(document).ready(function()
{
	jload.load();
	
});

$(function(){
		if ($('.bannerPaging li.activeSlide a').attr('href'))
			index=$('.bannerPaging li.activeSlide a').html()*1-1;
		else 
			index=0;
		
		bannerslider=10000;
		if ($.browser.msie&&$.browser.version*1<=8.0)
			bannerslider=0;

		$('.bodybackground').cycle({
					speed:  3000,
					timeout: 0,
					startingSlide:index
		});
		
		$('#bannerSlider').cycle({
			speed:  3000, 
   		timeout: bannerslider, 
    	pager:  '.bannerPaging',
			pagerAnchorBuilder: function(idx, slide) {
			
				// return sel string for existing anchor
				return '.bannerPaging li:eq(' + (idx) + ') a';
			},
			startingSlide:index,
			before:function(obj,obj2,opt,flag){
				id=$(obj2).attr('id').replace('banner','');
				document.cookie = "background=" + id;
				$('.bodybackground').cycle(id-1);
				$('.bodybackground img').css('width','100%');
				$('.bodybackground img').css('height','auto');
			}
		});
		
		
		
		$('.productSlider ul').cycle({ 
			prev:   '.prev', 
			next:   '.next',  
			timeout: 0 
		});
	
		
});

