var BhunzPersonal = new Class({
	
	lang : 'de',
	current : 0,
	scrollFaktor : 25,
	scrollTimer : null,
	scrollDelay : 30,
	contactopen :false,
	impressumopen : false,
	isPhone : false,
	ajaxContentOpen : false,
	initialize : function(lang)
	{
		this.lang = lang;
		
		$$('#lang-'+lang+' span')[0].dispose();
		
		this.setContactForm();
		this.setImpressum();
		this.setUpAccordions();
		this.setPositions();
		this.setEvents();
		this.setDynStyles();
		
		this.checkScrollbars(0);


		if(Browser.Platform.other || Browser.Platform.ipod)this.isPhone = true;

		$$('.hasSWF').each(function(item){
			new Element('div',{ 'class' : 'flashvid' }).inject(item);
		});
	},
	
	fixTouchDevices : function()
	{
		

	},
	
	setImpressum : function()
	{
		$('impressumlink').addEvent('click',function(e){
			e.stop();
			bhunz.impressumopen = true;
			$('impressum').set({
				styles : {
					opacity : 0,
					display : 'block'
				},
				tween : {
					onComplete : function(){
						$$('.content').getLast().setStyle('height',this.subject.getSize().y);
						bhunz.checkScrollbars(bhunz.current, true);
					}
				}
			}).tween('opacity',1);
		});

		$('impressum-close').addEvent('click',function(e){
			if(e)e.stop();
			$$('.content').getLast().tween('top',0); 
			$('impressum').set({
					tween : {
						onComplete : function(){
							this.subject.setStyles({ display : 'none' });
							$$('.content').getLast().setStyle('height',252);
							bhunz.checkScrollbars(bhunz.current);
							bhunz.impressumopen = false;
						}
					}
			}).tween('opacity',0);
		});		
	},
	
	setContactForm : function()
	{
		$('formlink').addEvent('click',function(e){
			e.stop();
			bhunz.contactopen = true;
			$('contactform').set({
				styles : {
					opacity : 0,
					display : 'block'
				},
				tween : {
					onComplete : function(){
						$$('.content').getLast().setStyle('height',this.subject.getSize().y);
						bhunz.checkScrollbars(bhunz.current, true);
						
					}
				}
			}).tween('opacity',1);
		});
		
		$('cancelform').addEvent('click',function(e){
			if(e)e.stop();
			$$('.content').getLast().tween('top',0); 
			$('contactform').set({
					tween : {
						onComplete : function(){
							this.subject.setStyles({ display : 'none' });
							$$('.content').getLast().setStyle('height',252);
							bhunz.checkScrollbars(bhunz.current);		
							bhunz.contactopen = false;
						}
					}
			}).tween('opacity',0);
		});
		
		$('submitform').addEvent('click',function(e){
			e.stop();
			
			$$('form')[0].set('send',{onComplete : function(a){ 

		    	if(a.match(/Fehlermeldung/))
				{
					$('contact-error').set({
						
						styles : {
							display : 'block',
							opacity : 0,
							top : $$('.content').getLast().getStyle('top').toInt() * -1
						},
						tween : {
							onComplete : function(){
								this.subject.set({
									tween : {
										onComplete : function(){
											this.subject.setStyle('display','none');	
										}
									}
								}).tween.delay(2000,this.subject,['opacity',0]);
							}
						}
						
				   }).tween('opacity',1);
				}
				else
				{
					$$('.content').getLast().tween('top',0);
					$('contact-confirm').set({ 
						styles : {
							display : 'block',
							opacity : 0
						},
						tween : {
							onComplete : function(){
								bhunz.checkScrollbars(bhunz.current);
								$('contactform').setStyle('display','none');
								this.subject.set('tween',{
												 onComplete : function(){ 
													this.subject.setStyle('display','none');
													bhunz.contactopen = false;
													}
												 }).tween.delay(2000,this.subject,['opacity',0]);
							}
						}
					}).tween('opacity',1);
				}

		 	}}).send()
			

		});
		
	},
		
	verifyForm : function()
	{
		
		var ok = true;
		$$('dd[text*=*]').getElements('input, textarea').each(function(item){
			if(item.get('value') == '')ok = false;
		},this);
		return ok;
	},
	
	setUpAccordions : function()
	{
		$$('.accbox .accitem').each(function(item,i){
				
				item.getElements('h2 a')[0].addEvents({
					click : function(e)
					{
						if(e)e.stop();
						var info = this.retrieve('info');
						
						
						bhunz.stopScroll();  					
						bhunz.startScroll( -1, 20 );
						if(this.hasClass('open'))
						{
							info.item.set('tween',{
										  onComplete : function()
										  {
											bhunz.stopScroll(); 
											bhunz.checkScrollbars(bhunz.current, true);
										  }
										  }).tween('height',15);
							this.removeClass('open');
							info.item.getElements('h2 a')[0].set('tween',{ onComplete : function(){ this.subject.setStyle('color',''); }}).tween('color','#9AAC3B');							
						}
						else
						{
							this.getParents('.accbox').getElements('a.open')[0].fireEvent('click');
							this.addClass('open');
							info.item.set('tween',{
										  onComplete : function()
										  {
											bhunz.stopScroll();  
											bhunz.checkScrollbars(bhunz.current, true);
											
											}
										  }).tween('height',info.height);
							info.item.getElements('h2 a')[0].tween('color','#000');
						}
						
					}
				}).store('info',{
					height : item.getSize().y,
					item : item
				});
				
				item.setStyle('height',13).set('tween',{
					onComplete : function()
					{
						bhunz.checkScrollbars(bhunz.current,true);	
					}
				});
		});
	},
	
	testUri : function(){
		var uri = window.location.hash.substr(1);
		try{
			$$('#navi a[href*='+uri+']')[0].fireEvent('click');
		}catch(e){}
	},

	setDynStyles : function()
	{
		$$('.arrow').setStyles({
			display : 'block',
			visibility : 'visible',
			opacity : 0.01
		});

		$$('#langbox a').each(function(item,i){
			item.setStyles({ 
				   display : 'block',
				   opacity : '0'}).tween.delay(300 + i * 300,item,['opacity',1]);
		});

	},

	checkScrollbars : function(next,dontScroll)
	{

		var nextHeight = $$( '.item ' +'.'+ this.lang )[next].getSize().y;
		$('scrollbar').tween('opacity',nextHeight <= 252?0:1);
		if(!dontScroll || nextHeight <= 252)$$('.item .'+this.lang)[this.current].tween('top',0);
		
	},

	setEvents : function()
	{
		
		$$('.ajaxbox a[href*=ajax]').addEvent('click',function(e){
			e.stop();
			new Request({
						url : this.get('href').substr(6)+'.html',
						onComplete: function(a){
							bhunz.ajaxContentOpen = true;
							$$('.content')[bhunz.current].tween('top',0);
							var el = new Element('div',{
								id : 'ajaxcontent',
								html : a,
								styles : {
									opacity : 0,
									display : 'block'
								},
								tween : {
									onComplete : function(){
										$$('.content')[bhunz.current].setStyle('height',this.subject.getSize().y);
										
										bhunz.checkScrollbars(bhunz.current, true);
									}
								}
							}).inject($$('.content')[bhunz.current]).tween('opacity',1);							
					
							el.getElements('a[href*=close]').addEvent('click',function(e){
								if(e)e.stop();
								$$('.content')[bhunz.current].tween('top',0); 
								$('ajaxcontent').set({
										tween : {
											onComplete : function(){
												this.subject.dispose();
												$$('.content')[bhunz.current].setStyle('height','auto');
												bhunz.checkScrollbars(bhunz.current);
												bhunz.ajaxContentOpen = false;
											}
										}
								}).tween('opacity',0);
																					   });
						
						}
						}).get();
		
		});
		
		$$('#langbox a span').addEvents({
			mouseenter : function(){ this.tween('opacity',0.1) },
			mouseleave : function(){ this.tween('opacity',1) },
			click : function(){
				window.location = this.getParent().get('href');
			}
		});

		$$('#langbox a').addEvents({
			click : function(){
				window.location = this.get('href');
			}
		});


		$(window).addEvent('resize',function()
			{
				try{
					bhunz.setPositions();
				}catch(e){}
			});
					
		$$('.item').each(function(item,i){
			
			$$('#navi li a')[i].addEvents({
					click : function(e)
					{
						var item = this.retrieve('item'), items = $$('.item');
						if(bhunz.contactopen)$('cancelform').fireEvent('click');
						if(bhunz.impressumopen)$('impressum-close').fireEvent('click');
						if(bhunz.ajaxContentOpen)$$('#ajaxcontent a[href*=close]')[0].fireEvent('click');
						if(item == bhunz.current)return;

					
						bhunz.checkScrollbars(item);
						
						$$('#navi a.active').setStyle('opacity',0.8).removeClass('active');
						this.addClass('active').setStyle('opacity',1);
						
						var oldx = $('inner').getStyle('left').toInt();
						var newx = item * -970 + 54;
						
						bhunz.stopScroll();  
						
						$('inner').set('tween',{ 
							transition : 'pow:out',
							duration : (Math.abs( oldx - newx )).round(),
							onComplete : function(){
								
								if(!bhunz.isPhone)
								{
									$$('.flashvid').each(function(item){
										item.set('html','');
									});
								}
								if($$('.item .visual')[bhunz.current].hasClass('hasSWF'))
								{
									if(!bhunz.isPhone)
									{
										new Swiff(
											$$('.item .visual img')[bhunz.current].get('src').replace('jpg','swf'),
											{
												width : 448,
												height : 252
											}
										).inject($$('.item .visual')[bhunz.current].getElements('.flashvid')[0]);
									}
									if(Browser.Engine.name == 'trident')
									{
										document.title = 'Barbara Hunz Personalmanagement GmbH';
									}
								}
								
							}
						}).tween('left',  newx );
						
						bhunz.current = item;

						$('prev').setStyle('display',bhunz.current == 0?'none':'block');
						$('next').setStyle('display',bhunz.current == items.length-1?'none':'block');
					},
					
					mouseenter : function()
					{
						if(!this.hasClass('active'))this.tween('opacity',1);
					},
					mouseleave : function()
					{
						if(!this.hasClass('active'))this.tween('opacity',0.8);
					}			
					
				}).store('item',i).setStyle('opacity',0.8);
		});

		$$('.arrow').addEvents({
			click : function()
			{
				var n = bhunz.current + (this.get('id') == "next"?1:-1);
				if($$('.item')[n])
				{
					$$('#navi a')[n].fireEvent('click');
				}
			},
			mouseenter : function()
			{
				if((this.get('id') == 'prev' && bhunz.current > 0)
					||
					(this.get('id') == 'next' && bhunz.current < $$('.item').length-1)
				){
					this.tween('opacity',0.4);	

				}
			},
			mouseleave : function()
			{
				this.tween('opacity',0.01);	

			}
		});

		if(!this.isPhone)
		{
			$$('.content-box').addEvent('mousewheel',function(e){
				if(e.stop)e.stop();
				bhunz.scroll(e.wheel < 0?-1:1);

			});
		}
		
		$$('#scrollbar span').addEvents({
			
			mousedown : function(e)
			{
				e.stop();
				if(bhunz.isPhone)
				{
					bhunz.scroll(this.get('id') == 'sb0'?1:-1);					
				}
				else
				{
					bhunz.startScroll(this.get('id') == 'sb0'?1:-1);
				}

			},
			mouseup : function(e)
			{
				if(e)e.stop();

				if(!bhunz.isPhone)
				{
					bhunz.stopScroll();
				}

			},
			mouseleave : function()
			{
				this.fireEvent('mouseup');
			}
			
		});
	},
	
	startScroll : function( dir, fak )
	{
		this.scrollFaktor = fak || 5;
		this.scrollTimer = bhunz.scroll.periodical(this.scrollDelay,this,dir);
	},
	
	scroll : function( dir )
	{
//		$$('.content-box')[ bhunz.current ].fireEvent('mousewheel',{ wheel : dir });
	
		var item = $$('.content-box .content' )[this.current],
		parent = $$('.content-box')[this.current],
		height = item.getSize().y,
		diff = 252 - height,
		pos = item.getPosition( parent ).y,
		step = (dir < 1?-1:1) * this.scrollFaktor,
		newpos = pos + step;
		if(height < 252)return false;
	
		newpos = newpos < diff?diff:newpos > 0?0:newpos;
		item.setStyle( 'top', newpos);		
	},
	
	stopScroll : function()
	{
		this.scrollFaktor = 25;
		$clear( this.scrollTimer );
	},
	
	setPositions : function()
	{
		var s = $(document).getSize();
		var l = $$('.item').length;
		$('inner').setStyle('width',l * 916);
		
		if(s.x < 1024)s.x = 1024;
		
		$('stage').setStyle('width',s.x);

		$$('.overlay').setStyles({
			width : (s.x - 916) * .5,
			display : 'block',
			opacity : .7
		});
		$('ol0').setStyle('left',(s.x - 916) * -0.5);
		
		$('ol1').setStyle('left',916);
	}	
});


$(window).addEvents({
	load : function()
	{
		var p = window.location.pathname
		bhunz = new BhunzPersonal(p.match(/english/)?'en':p.match(/francais/)?'fr':'de');
		bhunz.testUri();
	}
});

