if(!$chk(ARIProto)){ var ARIProto = new Class({}); var ARI = new ARIProto(); }
ARIProto.implement({

	addButtonsJS: function(){
		$(document.body).getElements('a.submit').addEvent('click',function(el){
			this.getParent('form').fireEvent('submit');			
		});
	},	
	
	addProductJS: function(){
		//var container = $(document.body).getElement('.keyvisualTabContents');
		this.productTabs = $('productTabs');
		if(!$chk(this.productTabs)) return false;
		this.productTabsContainer = this.productTabs.getElement('.productTabContents');
		this.productTabsTabs = this.productTabs.getElements('.productTabContent');
		this.productTabsTabLinks = this.productTabs.getElements('.productTabLink');
		
		this.productTabsTabActivate(this.productTabs.getElement('.productTabLinks .productTabLink'), true);

		this.productTabsTabLinks.each(function(button){
			button.addEvent('click',function(e){				
				if(!button.hasClass('productTabLinkJS')) e.stop();
				ARI.productTabsTabActivate(this);
			});
		});
			},

	productTabsTabActivate: function(button){
		button.blur();
		// Buttons
		this.productTabsTabLinks.removeClass('active');
		button.addClass('active');

		target = this.productTabs.getElement('.'+button.get("rel"));
		if($chk(target)){
			if(target.hasClass('active')) return false;
			// Current
			current = this.productTabsContainer.getElement('.active');		
			if($chk(current)) current.removeClass('active').setStyle('display','none');
			// Target
			this.productTabsTabs.setStyle('display','none');
			//target.addClass('active').setStyles({'position':'absolute','display':'block'});
			target.addClass('active').setStyle('display','block');
			//targetHeight = target.getCoordinates().height;
			//this.productTabsContainer.setStyle('height',targetHeight);
		}

		// Load content?
		if(button.hasClass('productTabLinkAjax') && $chk(target)){
			var src = button.get('href');
			//var src = target.get('src');
			if($chk(src) && src!='#'){
				var ajaxLoaderContainer = new Element('p',{'class':'ajaxLoader'}).inject(target,'top');
				var ajaxLoader = new Element('img',{'src':'fileadmin/templates/img/elements/ajax-loader.gif'}).inject(ajaxLoaderContainer,'top');
				//if($chk(ajaxLoader)){ ajaxLoader.fade('in',{'duration':'3000'}); }

				target.set('load', {
					evalScripts: true,
					onSuccess: function(){
						ARI.productTabsTabInlineForm(src, target);
						button.set('href','#').set('target','');
					}
				}).load(src);
			}
		}
	},
	
	productTabsTabInlineForm: function(action, tab){
		var form = tab.getElement('form');
		if(!$chk(form)) return false;
		var rel = form.get('rel');
		if(!$chk(rel)) return false;
		target = $(form.get('rel'));
		if(!$chk(target)) return false;
		form.set('send',{
			url: action,
			method: 'post',
			target: rel,
			onRequest: function(){
				target = $(form.get('rel'));
				target.setStyle('opacity',0.5);
			},
			onSuccess: function(data){
				target = $(form.get('rel'));
				newElement = new Element('span').set('html',data).setStyle('display','none').inject(target,'after');
				target.destroy();
				newElement.setStyle('display','block');
				ARI.productTabsTabInlineForm(action, tab);
			}
		});
		form.set('action','javascript:;');
		form.set('onsubmit','this.send();return false;');
		form.addEvent('submit',function(e){
			form.send();
		});
	},



	addProductJS_productTabs2: function(){
		//var container = $(document.body).getElement('.keyvisualTabContents');
		this.productTabs2 = $('productTabs2');
		if(!$chk(this.productTabs2)) return false;
		this.productTabs2Container = this.productTabs2.getElement('.productTab2Contents');
		this.productTabs2Tabs = this.productTabs2.getElements('.productTab2Content');
		this.productTabs2TabLinks = this.productTabs2.getElements('.productTab2Link');
		
		this.productTabs2TabActivate(this.productTabs2.getElement('.productTab2Links .productTab2Link'), true);

		this.productTabs2TabLinks.each(function(button){
			button.addEvent('click',function(e){				
				if(!button.hasClass('productTab2LinkJS')) e.stop();
				ARI.productTabs2TabActivate(this);
			});
		});
	},
	productTabs2TabActivate: function(button){
		button.blur();
		// Buttons
		this.productTabs2TabLinks.removeClass('active');
		button.addClass('active');

		target = this.productTabs2.getElement('.'+button.get("rel"));
		if($chk(target)){
			if(target.hasClass('active')) return false;
			// Current
			current = this.productTabs2Container.getElement('.active');		
			if($chk(current)) current.removeClass('active').setStyle('display','none');
			// Target
			this.productTabs2Tabs.setStyle('display','none');
			//target.addClass('active').setStyles({'position':'absolute','display':'block'});
			target.addClass('active').setStyle('display','block');
			//targetHeight = target.getCoordinates().height;
			//this.productTabsContainer.setStyle('height',targetHeight);
		}

		// Load content?
		if(button.hasClass('productTab2LinkAjax') && $chk(target)){
			var src = button.get('href');
			//var src = target.get('src');
			if($chk(src) && src!='#'){
				var ajaxLoaderContainer = new Element('p',{'class':'ajaxLoader'}).inject(target,'top');
				var ajaxLoader = new Element('img',{'src':'fileadmin/templates/img/elements/ajax-loader.gif'}).inject(ajaxLoaderContainer,'top');
				//if($chk(ajaxLoader)){ ajaxLoader.fade('in',{'duration':'3000'}); }

				target.set('load', {
					evalScripts: true,
					onSuccess: function(){
						ARI.productTabsTabInlineForm(src, target);
						button.set('href','#').set('target','');
					}
				}).load(src);
			}
		}
	},



	
	addProductGalleryJS: function(){
		this.productGallery = $('productGallery');
		if(!$chk(this.productGallery)) return false;
		this.productGalleryThumbs = $('productGalleryThumbs');
		this.productGalleryThumbsContent = $('productGalleryThumbsContent');
		this.productGalleryThumbsContentSize = $('productGalleryThumbsContentSize');
		this.productGalleryMagnifier = $('productGallery').getElement('.magnifier');

		this.productGalleryThumbs.getElements('a').each(function(el){
			el.addEvent('click', function(e){ e.stop(); ARI.productGalleryShowImage(el); });
		});
		$('productGalleryImage').setStyle('display','block');
		this.productGalleryShowImage(this.productGalleryThumbs.getElement('a'));

		$('productGalleryBack').addEvent('click', function(e){ e.stop(); ARI.productGallerySlideBack(); });
		$('productGalleryNext').addEvent('click', function(e){ e.stop(); ARI.productGallerySlideNext(); });
	},
	productGalleryShowImage: function(el){
		var big = el.getElement("img").get('rel');
		var original = el.get('href');
		if(!$chk(big) && $chk(original)) big = original;
		if($chk(big)){
			var oldImages = $('productGalleryImage').getElements('img');
			var newImage = new Element('img',{
				src:big,
				rel:original,
				opacity:0
			});
			if($chk(oldImages[0])){
				newImage.inject(oldImages.getLast(), 'after');
				newImage.fade('in');
				oldImages.fade('out');
				(function(){ oldImages.destroy(); }).delay(200);
			} else {
				newImage.inject($('productGalleryImage'), 'top');
				newImage.setStyle('opacity',1);
			}

			ARI.productGalleryFitImage(newImage);

		}
		if($chk(original)){
			ARI.productGalleryMagnifier.fade('in');
			ARI.productGalleryMagnifier.addEvent('click', ARI.productGalleryZoomImage.bindWithEvent());
		} else {
			ARI.productGalleryMagnifier.fade('out');
			ARI.productGalleryMagnifier.removeEvent('click');
		}
	},
	
	productGalleryFitImage: function(el){
		var size1 = $('productGalleryImage').getSize();		
		var size2 = el.getSize();
		if(!size2.x){
			(function(){ ARI.productGalleryFitImage(el); }).delay(100);
			return false;
		}
		
//		alert(size1.x +','+size1.y +' / '+size2.x +','+size2.y);
		if(size2.x > size1.x || size2.y > size1.y){
			if(size2.x > size2.y){ // Portrait
				var size2x = size1.x;
				var size2y = Math.round((size2.x/size2.y) * size2x);
			} else { // Landscape or quadratic
				var size2y = size1.y;
				var size2x = Math.round((size2.y/size2.x) * size2y);
			}
			// Resize and center
			el.setStyles({
				'position':'absolute',
				'width': size2x,
				'height': size2y,
				'left': Math.round((size1.x - size2x)/2),
				'top': Math.round((size1.y - size2y)/2)
			});
		} else {
			// Center
			el.setStyles({
				'position':'absolute',
				'left': Math.round((size1.x - size2.x)/2),
				'top': Math.round((size1.y - size2.y)/2)
			});
		}
	},
	
	productGalleryZoomImage: function(){
		var original = $('productGalleryImage').getElement('img').get('rel');
		if(!$chk(original)) return false;
		ARI.i = 0;
		ARI.slimboxShow = -1;
		ARI.slimbox = [];
		ARI.productGalleryThumbs.getElements('a').each(function(el){
			ARI.slimbox.extend([[el.get('href'),el.get('title')]]);
			if(el.get('href') == original) ARI.slimboxShow = ARI.i;
			ARI.i++;
		});
		if(ARI.slimboxShow >= 0){
			Slimbox.open(ARI.slimbox, ARI.slimboxShow, {
				loop: true,
				counterText: "{x}/{y}",
				overlayOpacity: 0.2,
				overlayFadeDuration: 300,
				resizeDuration: 300,
				resizeTransition: Fx.Transitions.Quad,
				imageFadeDuration: 300,
				captionAnimationDuration: 100
			});
		} else {
			Slimbox.open(original);
		}
		
	},
	productGallerySlideBack: function(el){
		$('productGalleryBack').blur();
		ARI.foundScrollTarget = false;
		ARI.productGalleryThumbs.getElements("img").each(function(el){
			var c = el.getCoordinates(ARI.productGalleryThumbs);
			if(c.left < 0) ARI.foundScrollTarget = el;
		});
		if(ARI.foundScrollTarget != false){
			var space = ARI.productGalleryThumbs.getStyle('width').toInt();
			var c = ARI.foundScrollTarget.getCoordinates(ARI.productGalleryThumbsContent);
			c.right = c.left+c.width;
			var target = space-c.right;
			if(target > 0) target = 0;
			new Fx.Tween(ARI.productGalleryThumbsContent, {transition: Fx.Transitions.Quart.easeOut}).start('left',ARI.productGalleryThumbsContent.getStyle('left'), target);
		}
	},
	productGallerySlideNext: function(el){
		$('productGalleryNext').blur();
		ARI.foundScrollTarget = false;
		ARI.productGalleryThumbs.getElements("a").each(function(el){
			if(ARI.foundScrollTarget != false) return false;
			var c = el.getCoordinates(ARI.productGalleryThumbs);
			c.right = c.left+c.width;
			var space = ARI.productGalleryThumbs.getStyle('width').toInt();
			if(c.right-space > 0){
				ARI.foundScrollTarget = true;
				var target = ARI.productGalleryThumbsContent.getStyle('left').toInt() - c.left;				
				var maxtarget = -(ARI.productGalleryThumbsContentSize.getSize().x - space);
				if(target < maxtarget) target = maxtarget;
				new Fx.Tween(ARI.productGalleryThumbsContent, {transition: Fx.Transitions.Quart.easeOut}).start('left',ARI.productGalleryThumbsContent.getStyle('left'), target);
			}
		});		
	},
	
	
	internationalPopup: function(name,url){
		if(!$chk('internationalPopup')) return false;
		$('internationalPopup').setStyle('display','block');
		
		var target = $('internationalPopupContent');
		target.empty();
		new Element('h1').set('html',name).inject('internationalPopupContent');
		target.set('load', {
			evalScripts: true,
			onRequest: function(){
				var ajaxLoaderContainer = new Element('p',{'class':'ajaxLoader'}).inject(target,'bottom');
				var ajaxLoader = new Element('img',{'src':'fileadmin/templates/img/elements/ajax-loader.gif'}).setStyle('opacity',0).inject(ajaxLoaderContainer,'top');
				// (function(){ if($chk(ajaxLoader)){ ajaxLoader.fade('in',{'duration':'3000'}); } }).delay(500);
			},
			onSuccess: function(){}
		}).load(url);
	},
	internationalPopupClose: function(){
		if(!$chk('internationalPopup')) return false;
		$('internationalPopup').setStyle('display','none');
	}
		
});

window.addEvent("domready", function(){
	ARI.addButtonsJS();
	ARI.addProductJS();
	ARI.addProductJS_productTabs2();
	ARI.addProductGalleryJS();
	
	// Fix for IE bug (no display of that div)
	$$('.madeingermany').setStyle('width','91px');
});
