var Site = {
	
	init: function(){

		Site.removeBorders();
		Site.initSubmit();
		Site.initVerify();
		Site.remooz();
		Site.hideSlidenav();
		if($('productsList')) Site.smoothScroll();
		
	},
	
	start: function(){
		Site.PreloadMouseOvers();	

		if($('newsSlider')) Site.newsSlider();
		if($('galleryBox')) Site.gallerySlide();
		if($('homeslider')) Site.homeSlide();
		if($('productsList')) Site.checkBrowserHeight();
		Site.slidenav();
	},

	PreloadMouseOvers: function(){
		var images = [];
		
		$$('.over').each(function(el){
			if (el.get('src')) {
				src	= el.get('src');
				src = src.replace(/-active/, '');
				src = src.replace(/-inactive/, '');
				src = src.replace(/-over/, '');
				
				var extension = src.substring(src.lastIndexOf('.'), src.length)
				
				images.push(src.replace(extension, '-over' + extension));
			}
		});
		
		if (images.length > 0) {
			new Asset.images(images, {
				onComplete: function(){
					Site.InitMouseOvers();
				}
			});		
		}
	},
	
	InitMouseOvers: function(){
		var src;
		
		$$('.over').each(function(el){
			var clone = el.clone().injectAfter(el);
			
			el.dispose();
		});
		
		$$('.over').each(function(el){
			if (el.get('src')) {
				el.addEvent('mouseover', function(){
					src	= el.get('src');
					src = src.replace(/-active/, '');
					src = src.replace(/-inactive/, '');
					
					var extension = src.substring(src.lastIndexOf('.'), src.length)
					
					el.set('src', src.replace(extension, '-over' + extension));
				});
				
				el.addEvent('mouseout', function(){
					el.set('src', src);
				});     
			}                                              
		});
	},
	
	removeBorders: function(){
		$$('a').each(function(el){
			el.addEvent('focus', function(){
				el.blur();
			});
		});
	},
	
	initSubmit: function(){
		$$('.submit').each(function(el){
			var button 	= el.getElement('a');
			var form	= $(button.get('rel'));
			
			function submit(){
				el.getElements('a').each(function(button){
					button.remove();
				});
				
				el.setHTML('');
				
				var loader = $('loader').clone().injectInside(el).setStyle('display', 'block');
				
				form.submit();
			}
			
			form.addEvent('submit', function(e){
				e = new Event(e).stop();
				
				submit();
			});
			
			button.addEvent('click', function(e){
				e = new Event(e).stop();
				
				submit();
			});
		});
	},
	
	initVerify: function(){
		$$('a').each(function(el){
			if (el.get('rel') == 'confirm') {
				el.addEvent('click', function(e){
					if (confirm('Sind Sie sicher?') == false) {
						e = new Event(e).stop();
					}
				});
			}
		});
	},
	
	deleteObject: function(url, query, object){
		var request = new Request.HTML({
			method: 'post',
			url: site_url+url,
			update: $(object)
		}).send(query);
	},
	
	alternating: function(object){
		var i = 1;
		
		object.getElements('tr').each(function(el){
			if (el.hasClass('odd')) {
				el.removeClass('odd');
			} else if (el.hasClass('even')) {
				el.removeClass('even');
			}
			
			if (i == 1) {
				el.addClass('odd');
				
				i = 2;
			} else {
				el.addClass('even');
				
				i = 1;
			}
		});
	},
	
	hideNotification: function(){
		var slide = new Fx.Slide($('notification'));
		
		(function(){ slide.slideOut(); }).delay(2000);
	},
	
	remooz: function(){
	
		$$('.zoom').each(function(element) {
	 
	        // Constructor, takes the element and options as arguments
	        new ReMooz(element, {
	            centered: true, // Zoom the center of the screen
	            origin: element.getElement('img') // Take the image inside as origin for the zooming element
	        });
	 
	    });
	},
	
	newsSlider: function(){
		next   = 0;
		
		active = $('activeContainer').get('rel').toInt();
		
		if (active == 1) {
			$('previous').setStyle('visibility','hidden');
		}
		if (active == maxContainer) {
			$('next').setStyle('visibility','hidden');
		}
	
		var scroll = new Fx.Scroll('newsSlider', {
			wait: false,
			duration: 1000,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Circ.easeInOut,
			onComplete: function(){
				active = $('activeContainer').get('rel').toInt();
				if (active!=1) {
					$('previous').setStyle('visibility', 'visible');
				}
				if (active!=maxContainer) {
					$('next').setStyle('visibility', 'visible');
				}
			}
		});	
		
		$('previous').addEvent('click', function(event){
			event 	= new Event(event).stop();
			active 	= $('activeContainer').get('rel').toInt();

			if (active > 1) {
				nextItem   = --active;
				
				if (scroll.toElement('li_'+nextItem+'')) {
					$('previous').setStyle('visibility', 'hidden');
					$('next').setStyle('visibility', 'hidden');
					$('activeContainer').set('rel', nextItem);
				}
			}
		});

		$('next').addEvent('click', function(event){
			event 	= new Event(event).stop();
			active 	= $('activeContainer').get('rel').toInt();
			
			if (active < maxContainer) {
				if (active == maxContainer) {
					nextItem   = 1;
				} else {
					nextItem   = active+1;
				}
				
				$('newsList').getChildren().each(function(child){
					//alert(child.get('id'));
				});
				
				if (scroll.toElement('li_'+nextItem)) {
					$('previous').setStyle('visibility', 'hidden');
					$('next').setStyle('visibility', 'hidden');
					$('activeContainer').set('rel', nextItem);
				}
			}
		});
	},
	
	hideSlidenav: function() {
		if (!tramp && !other) {
			var slide = new Fx.Slide('slidenav'	).hide();
			$('othermenu').setStyle('display','none');
			$('trampmenu').setStyle('display','none');
		} else {
			if (other) {
				$('othermenu').setStyle('display','block');
				$('trampmenu').setStyle('display','none');
			} else if (tramp) {
				$('othermenu').setStyle('display','none');
				$('trampmenu').setStyle('display','block');
			}
		}
		
		
	},
	
	slidenav: function() {
		var slide = new Fx.Slide('slidenav', {duration: 300} );

		$('tramplink').addEvent('mouseenter', function(){
			
			if ($('othermenu').getStyle('display') == 'block') {
				slide.slideOut().chain(function() {
					$('othermenu').setStyle('display','none');
					$('trampmenu').setStyle('display','block');
					slide.slideIn();
				});
			} else if($('trampmenu').getStyle('display') != 'block') {
				$('trampmenu').setStyle('display','block');
				slide.slideIn();
			}
		});
		
		$('otherlink').addEvent('mouseenter', function(){
			
			if ($('trampmenu').getStyle('display') == 'block') {
				slide.slideOut().chain(function() {
					$('trampmenu').setStyle('display','none');
					$('othermenu').setStyle('display','block');
					slide.slideIn();
				});
			} else if($('othermenu').getStyle('display') != 'block') {
				$('othermenu').setStyle('display','block');
				slide.slideIn();
			}
		});
				
		$('navcontainer').addEvent('mouseleave', function(){
			
			if (tramp) {
				if ($('othermenu').getStyle('display') == 'block' || $('trampmenu').getStyle('display') != 'block') {
					slide.slideOut().chain(function() {
						$('trampmenu').setStyle('display','block');
						$('othermenu').setStyle('display','none');
						slide.slideIn();
					});
				}
			}
			if (other) {
				if ($('trampmenu').getStyle('display') == 'block' || $('othermenu').getStyle('display') != 'block') {
					slide.slideOut().chain(function() {
						$('othermenu').setStyle('display','block');
						$('trampmenu').setStyle('display','none');
						slide.slideIn();
					});
				}
			} 
			if (!tramp && !other) {
				slide.slideOut().chain(function() {
					$('trampmenu').setStyle('display','none');
					$('othermenu').setStyle('display','none');
				});
			}
		});
	
	},	
	
	gallerySlide: function() {
	
		active = $('activeContainer').get('rel').toInt();
		if(active==1) {
			$('galleryPrevious').setStyle('visibility','hidden');
		}
		if(active==maxContainer) {
			$('galleryNext').setStyle('visibility','hidden');
		}
	
		var scroll = new Fx.Scroll('galleryMask', {
			wait: true,
			duration: 1000,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Circ.easeInOut,
			onComplete: function(){
				active = $('activeContainer').get('rel').toInt();
				if (active!=1) {
					$('galleryPrevious').setStyle('visibility', 'visible');
				}
				if (active!=maxContainer) {
					$('galleryNext').setStyle('visibility', 'visible');
				}
			}
		});	
		
		$('galleryPrevious').addEvent('click', function(event){
			event 	= new Event(event).stop();
			active 	= $('activeContainer').get('rel').toInt();

			if (active > 1) {
				next   = --active;
				
				if (scroll.toElement('cont_'+next+'')) {
					$('galleryPrevious').setStyle('visibility', 'hidden');
					$('galleryNext').setStyle('visibility', 'hidden');
					$('activeContainer').set('rel', next);
				}
			}
		});

		$('galleryNext').addEvent('click', function(event){
			event 	= new Event(event).stop();
			active 	= $('activeContainer').get('rel').toInt();
			
			if (active < maxContainer) {
				if (active == maxContainer) {
					next   = 1;
				} else {
					next   = ++active;
				}
				
				if (scroll.toElement('cont_'+next+'')) {
					$('galleryPrevious').setStyle('visibility', 'hidden');
					$('galleryNext').setStyle('visibility', 'hidden');
					$('activeContainer').set('rel', next);
				}
			}
		});
	
	},
	
	showHome : function() {
		$('homeslider').setStyle('visibility','visible');
	},
	
	homeSlide : function() {
		var mySlide = new BySlideMenu('landingMenu', {containerWidth: 'full', elementWidth: 495, compressSize: 125, defaultIndex: 1});
		Site.showHome();
	},
	
	homeTip : function() {
	
	},
	
	checkBrowserHeight : function() {
		var w = 0;
		var h = 0;
	
		//IE
		if(!window.innerWidth) {
			//strict mode
			if(!(document.documentElement.clientWidth == 0)) {
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}
			//quirks mode
			else {
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		} else {
			w = window.innerWidth;
			h = window.innerHeight;
		}
		
		if(h<675) {
		
			$('goDown').setStyle('display','block');
			$('goDown').addEvent('click', function(f) {
				$('goDown').setStyle('display','none');
			});
		}
		
	},
	
	smoothScroll : function() {
	
		var mySmoothScroll = new Fx.SmoothScroll({
			links: '.downside',
			wheelStops: false,
			transition: Fx.Transitions.Circ.easeInOut,
			duration: 2000
		});
	
	}
	
}

window.addEvent('domready', Site.init);
window.addEvent('load', Site.start);