/*
	MOBILE CLASS
	by Daniel Trattner @ frey-wille
*/
var baseUrl = "http://m.frey-wille.com";


/* CONSTANTS */
var MC = mobileconstants = {
	ORIENTATION:{
		PORTRAIT:'portrait',
		LANDSCAPE:'landscape'
	}
}

/* 
	class FW (MAIN CLASS) 
*/
var FW = new Class({
	debug:false,
	debugWindow:null,
	content:null,
	orientation:MC.ORIENTATION.PORTRAIT,
	debugLines:[],
	device:'',
	ismobile: false,
	touchsupport: false,
	slideshows:[],
	devicename:'unknown',
    initialize: function(){},
	init: function(istab,devicename,maxWidth){
		this.isTablet = istab;
		this.devicename = devicename;
		this.maxWidth = maxWidth;
		this.initDevice();
        this.initDebug();
		this.onOrientationChange();
		this.initSlideshows();
		
		new FWBOUTIQUES(this);
	},
	
	/* get device properties */
	initDevice: function(){
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		this.device = Browser.Device.name;
		this.ismobile = Browser.isMobile;
		this.touchsupport = Browser.Features.Touch;
		this.content = $('content');
		//set body orientationChange Event and call it right after
		document.body.addEvent('orientationchange',this.onOrientationChange.bind(this));
	},
	
	/* inits debug window */
	initDebug: function(){
		if(this.debug)
		{
			this.debugWindow = new Element('div',{
				styles:{
					'color':'#fff',
					'font-size':'10px',
					'position':'absolute',
					'left':'0px',
					'top':'0px',
					'z-index':'10000',
					'background':'#000',
					'width':'300px',
					'height':'130px',
					'overflow':'auto'
				}
			}).inject(document.body,'bottom');
			this.message('Debug On '+window.innerWidth+'x'+window.innerHeight+', '+this.devicename);
			if(this.touchsupport) {this.message('touch support detected');}
			else {this.message('no touch support detected');}
			
			if(this.ismobile) {this.message('device is mobile');}
			else {this.message('device is not mobile');}
			
			this.debugWindow.addEvent('touchmove',function(e){
				//this.message('move');
				this.debugWindow.setPosition({x:e.client.x-150,y:e.client.y-this.debugWindow.getSize().y/2});
			}.bind(this));
			this.debugWindow.addEvent('click',function(e){
				var h = this.debugWindow.getStyle('height')=="130px"?"20px":"130px";
				this.debugWindow.setStyle('height',h);
				this.message('singlelinemode');
			}.bind(this));
		}
	},
	
	/* check for slideshow (small and big) ids and start them */
	initSlideshows: function(){
		$$('#mobileslideshow').each(function(el){
			this.slideshows.push(new FWSLIDESHOW(this,el));
		}.bind(this));
		if($('slideshow_big_wrapper')){
			//this.bigslideshow = new FWBIGSLIDESHOW(this,$('slideshow_big_wrapper'));
		}
	},
	
	/* for debugging */
	message: function(m){
		if(this.debug)
		{
			var i;
			this.debugLines.push('['+this.device+']'+m+'<br/>');
			var anz = this.debugWindow.getStyle('height')=="130px"?10:1;
			var single = this.debugWindow.getStyle('height')=="130px"?false:true;
			
			if(this.debugLines.length > anz){
				var a = [];
				for(i=0;i<this.debugLines.length;i++){
					if(i > 0){
						a.push(this.debugLines[i]);
					}
				}
				this.debugLines = a;
			}
			if(single) {
				this.debugLines = [this.debugLines[this.debugLines.length-1]];
			}
			var s = '';
			for(i=0;i<this.debugLines.length;i++){
				s += this.debugLines[i];	
			}
			this.debugWindow.set('html',s);
			this.debugWindow.scroll++;
		}
	},
	
	/* ORIENTATION */
	onOrientationChange: function(){
		switch(window.orientation){
			case -90:
			case 90:
				this.orientation = MC.ORIENTATION.LANDSCAPE;
				break;
			case 0:
			case 180:
			default:
				this.orientation = MC.ORIENTATION.PORTRAIT;
				break;
		}
		this.message('orientation changed to '+this.orientation);
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		window.scrollTo(0, 0);
		
		// resize content div
		if($('footer'))
		{
			var footerHeight = $('footer').getSize().y;
			var h = this.height-126-footerHeight;
			this.content.setStyle('min-height',h+"px");
			//this.content.setStyle('width',this.maxWidth+"px");
			if($('lightbg')){
				$('lightbg').setStyle('min-height',h+"px");
			}
			if($('darkbg')){
				$('darkbg').setStyle('min-height',h+"px");
			}
		}
		
		if($('welt')){
			$('welt').setStyle('width',this.width/2);
		}
		
		// set homeMessage fontsize
		if($('homeMessage')){
			switch(this.orientation){
				case MC.ORIENTATION.LANDSCAPE:
					var s = Math.ceil((this.height)/20);
					break;
				case MC.ORIENTATION.PORTRAIT:	
				default:
					var s = Math.ceil((this.width)/20);
					break;
			}
			$('homeMessage').setStyle('font-size',s+'px');
		}
		//this.reloadBackground();
	},
	
	/* set right sized background */
	reloadBackground: function(){
		var req = new Request({url: 'img/bg/getBackground.php?maxwidth='+this.width,
			   method:'get',
				onSuccess: function(data){
					this.message('Background: '+data);
					$(document.body).setStyle('background-image','url('+data+')');
					$(document.body).setStyle('background-size','100%');
				}.bind(this),
				onFailure: function(){
					this.message('Background request failed');
				}.bind(this)
			}).send();
	}
});


/*
	class FWBIGSLIDESHOW
*/
var FWBIGSLIDESHOW = new Class({
	parent:null,
	duration:1000,
	container:null,
	next:null,
	previous:null,
	resizeImages:false,
	css3:true,
	images:[],
	initialize: function(parent,el){
		this.parent = parent;
		this.container = el;
		//this.url = document.location.href;
		// image container and image
		this.imgcontainer = new Element('div',{id:'slideshow_image'}).inject(this.container);
		var img = this.container.getFirst().inject(this.imgcontainer);
		
		this.resizeImage(img);
		
		// get current image to load
		var a = this.imgcontainer.getFirst().getAttribute('src');
		var b = a.split('/');
		var c = b[b.length-1].split('folder=');
		var d = c[1].split('&');
		this.currentLoad = a;
		this.dir="none";
		this.folder = d[0];
		
		// get current Image
		this.setCurrentImage();
		//this.parent.message('slideshow, folder='+this.folder+', currentImage='+this.currentImage);
		
		// verhindert, dass runtergescrollt werden kann.. daher deaktiviert
		// event.preventDefault in swipe.js entfernt, daher wieder aktiviert...
		//this.swipeEvent($('slideshow_bigarrows'));
		//this.pinchEvent($('slideshow_bigarrows'));
		//this.touchEvents($('slideshow_bigarrows'));
		
		// REqUEST the imagenames (right size)
		if(this.folder !== '')
		{
			var req = new Request({url: 'img/slideshow/get.json.php?folder='+this.folder,
			   method:'get',
				onSuccess: function(data){
					data = JSON.decode(data);
					if(data.pictures){
						this.images = data.pictures;
						this.onRequestFinished();
					}
					//this.parent.message('initSlideshow request successful');
				}.bind(this),
				onFailure: function(){
					this.parent.message('initSlideshow request failed');
				}.bind(this)
			}).send();
		}
	},
	resizeImage: function(img){
		this.updateSize();
		if(this.resizeImages){
			img.setAttribute('width',this.picsize.x+"px");
			img.setAttribute('height',this.picsize.y+"px");
			//img.setStyle('width',this.picsize.x+"px");
			//img.setStyle('height',this.picsize.y+"px");
		} else {
			this.imgcontainer.setStyle('margin-top',this.picmargin+"px");
		}
	},
	updateSize: function(){
		var w = this.imgcontainer.getFirst().getSize().x;
		var h = this.imgcontainer.getFirst().getSize().y;
		
		var contentheight = this.parent.height-$('header').getSize().y-$('footer').getSize().y;
		this.picmargin = contentheight%2-h%2;
		
		if(w>h){
			var ratio = Math.abs(w/h);
			this.picsize = {x:this.parent.width,y:this.parent.width*ratio};
		} else {
			var ratio = Math.abs(w/h);
			this.picsize = {x:contentheight*ratio,y:contentheight};
		}
	},
	
	swipeEvent: function(el){
		el.addEvent('swipe',function(e){
			if(!this.blockSlide)
			{
				this.getNextAndPrevious();
				switch(e.direction){
					case 'left':
						if(this.next){
							this.dir = "next";
							this.slideOut();
						}
						break;
					case 'right':
						if(this.previous){
							this.dir = "previous";
							this.slideOut();
						}
						break;
				}
			}
		}.bind(this));
	},
	zoom:1,
	pinchEvent: function(el){
		el.addEvent('touchstart',function(event){
			this.pinch = false;
			if (event.targetTouches.length == 2){ 
				this.pinch = true;
				
			}
		}.bind(this));
		
		el.addEvent('touchmove',function(event){
			if(this.pinch){
				event.preventDefault();
			}
			//this.parent.message('pinch');
			if (event.targetTouches.length == 2){ 
				this.pinch = true;
			}
			if(this.pinch){
				this.zoom = (event.scale > 1)?this.zoom+0.01:this.zoom-0.01;
				this.zoom = this.zoom>2?2:this.zoom;
				this.zoom = this.zoom<1?1:this.zoom;
				//this.parent.message('pinch');
				this.image.setAttribute('width',(this.imageSize.x*this.zoom)+"px");
				this.image.setAttribute('height',(this.imageSize.y*this.zoom)+"px");
				//this.parent.message(event.scale);
			}
		 }.bind(this));
		el.addEvent('touchend',function(event){
			this.pinch = false;
		}.bind(this));
		/*el.addEvent('pinch',function(event){
		 	this.parent.message(event.pinch);
			//this.image.setAttribute('width',event.scale*200);
			switch(event.pinch){
				case "in":
					var m = new Fx.Tween.CSS3(this.image, {
						duration: this.duration,
						transition: 'sine:out'
					});
					m.addEvent('complete', function(){ this.zoomed = true;}.bind(this));
					m.start('width',this.parent.width*2);
					break;
				case "out":
					var m = new Fx.Tween.CSS3(this.image, {
						duration: this.duration,
						transition: 'sine:out'
					});
					m.addEvent('complete', function(){ this.zoomed = false;}.bind(this));
					m.start('width',this.parent.width);
					break;
			}
		}.bind(this));
		*/
	},
	zoomed:false,
	touchEvents: function(el){
		el.addEvent('touch',function(event){
			//var m1 = new Fx.Tween.CSS3(this.image, {	duration: this.duration,transition: 'sine:out'});
			//var m2 = new Fx.Tween.CSS3(this.image, {	duration: this.duration,transition: 'sine:out'});
			switch(this.zoomed){				
				case true:
					this.zoomed = false;
					this.parent.message('zoomout');
					//m1.start('width',this.imageSize.x);
					//m2.start('height',this.imageSize.y);
					//m.start('left',0);
					this.image.setStyle('width','');
					this.image.setStyle('height','');
					this.container.setStyle('left','0px');
					this.container.setStyle('top','0px');
					break;
				case false:
					this.zoomed = true;
					var touch = event.changedTouches[0];
					
					var lmiddle = -((this.imageSize.x*1.5)-this.imageSize.x)/2;
					if(touch.pageX > this.imageSize.x/2){
						var l = lmiddle - (touch.pageX-this.imageSize.x/2)*1.5;
					} else {
						var l = lmiddle + (this.imageSize.x/2-touch.pageX)*1.5;
					}
					
					/*
					var tmiddle = 0;
					if(touch.pageX > this.imageSize.x/2){
						var l = tmiddle - (touch.pageX-this.imageSize.x/2)*1.5;
					} else {
						var l = tmiddle + (this.imageSize.x/2-touch.pageX)*1.5;
					}
					*/
					
					
					this.parent.message('zoomin l='+l);
					this.image.setStyle('width',(this.imageSize.x*1.5)+"px");
					this.image.setStyle('height',(this.imageSize.y*1.5)+"px");
					this.container.setStyle('left',l+'px');
					//this.container.setStyle('top',t+'px');
					break;
			}
		}.bind(this));
		/*
		el.addEvent('touchstart',function(event){
			this.pinch = false;
			if (event.targetTouches.length == 2){ 
				this.pinch = true;
			} else {
				var touch = event.touches[0];
				this.start = {x: touch.pageX, y: touch.pageY};
			}
		}.bind(this));
		
		el.addEvent('touchmove',function(event){
			if (event.targetTouches.length == 2) this.pinch = true;
			
			if(this.pinch){
				this.image.setAttribute('width',event.scale*200+"%");
				//this.parent.message(event.scale);
			} else { // next & previous
				var touch = event.touches[0];
				var end = {x: touch.pageX, y: touch.pageY};
				var diff = end.x - this.start.x;
				var diffy = Math.abs(end.y - this.start.y);
				if(diffy < 50) {event.preventDefault();}
				//this.parent.message(diff);
				//this.parent.message("SLIDE diff = "+diff);
				var direction = diff>0?'right':'left';
				direction = Math.abs(diff)>(this.parent.width/2)?direction:'';
				
				this.container.setPosition({x:diff,y:0});
				//this.container.setAttribute('left',diff+"px");
				
				if(!this.blockSlide)
				{
					this.getNextAndPrevious();
					switch(direction){
						case 'left':
							if(this.next){
								this.dir = "next";
								this.slideOut();
							}
							break;
						case 'right':
							if(this.previous){
								this.dir = "previous";
								this.slideOut();
							}
							break;
					}
				}
			}
			
		}.bind(this));
		
		el.addEvent('touchend',function(event){
			if(!this.pinch && !this.blockSlide){
				if(this.css3){
					var m = new Fx.Tween.CSS3(this.container, {
						duration: this.duration,
						transition: 'sine:out'
					});
					m.start('left',0);
				} else {
					var m = new Fx.Morph(this.container, {duration: this.duration, transition: Fx.Transitions.Sine.easeOut});
					m.start({left:0});
				}
			}
			this.pinch = false;
		}.bind(this));	
		
		*/
	},
	onRequestFinished: function(){
		var arrow,url;
		
		if($("slideshow_bigleft")){
			arrow = $("slideshow_bigleft");
			arrow.set('morph', {duration: this.duration/2, transition: Fx.Transitions.Linear});
			url = arrow.getFirst().getAttribute('href');
			var b = url.split('/');
			this.previous = b[b.length-1];
			arrow.getFirst().setAttribute('href','javascript:void(0)');
			arrow.addEvent('click',function(){
				if(!this.blockSlide)
				{
					this.dir = "previous";
					this.getNextAndPrevious();
					if(this.previous){
						this.slideOut();
					}
				}
			}.bind(this));
		}
		if($("slideshow_bigright")){
			arrow = $("slideshow_bigright");
			arrow.set('morph', {duration: this.duration/2, transition: Fx.Transitions.Linear});
			url = arrow.getFirst().getAttribute('href');
			var b = url.split('/');
			this.next = b[b.length-1];
			arrow.getFirst().setAttribute('href','javascript:void(0)');
			arrow.addEvent('click',function(){
				if(!this.blockSlide)
				{
					this.dir = "next";
					this.getNextAndPrevious();
					if(this.next){
						this.slideOut();
					}
				}
			}.bind(this));
		}
		this.tweenComplete();
	},
	setCurrentImage: function(){
		var e = this.currentLoad.split('image=');
		var f = e[e.length-1].split('&');
		this.currentImage = f[0];
	},
	getNextAndPrevious: function(){
		for(var i=0;i<this.images.length;i++){
			if(this.images[i] == this.currentImage){
				this.next = this.images[i+1]?this.images[i+1]:null;
				this.previous = this.images[i-1]?this.images[i-1]:null;
				break;
			}
		}
		if(!this.next){ $("slideshow_bigright").morph({opacity:0}); }
		else {$("slideshow_bigright").morph({opacity:1});}
		if(!this.previous){ $("slideshow_bigleft").morph({opacity:0}); }
		else {$("slideshow_bigleft").morph({opacity:1});}
		
	},
	getImagePath: function(image){
		return "img/slideshow/"+this.folder+"/pictures/"+image;
	},
	tweenComplete:function(){
		
			if(this.dir!=="none"){
				this.currentImage = this.dir=="next"?this.next:this.previous;
			}
		
			this.getNextAndPrevious();
			
			//this.parent.message('next='+this.next+', prev='+this.previous);
			
			//this.currentLoad = this.dir=="next"?this.next:(this.dir="none"?this.currentLoad:this.previous);
			
			if(this.currentImage)
			{		
				
				//var a = this.currentLoad.split('/');
				//this.setCurrentImage();
				var d = new Date();
				var width = this.parent.width;
				if(!this.parent.isTablet){
					width = Math.ceil(width*1.5);
				}
				//if(this.parent.isTablet){ width = Math.ceil(width*.66); }
				this.url = "img/slideshow/getImage.php?folder="+this.folder+"&image="+this.currentImage+"&maxwidth="+width+"&nocache="+d.getHours()+d.getMinutes()+d.getSeconds();
				
				//this.parent.message('loading '+this.url);
				this.image = Asset.image(this.url, {
					id: '',
					title: '',
					onLoad: function(){
						this.container.set('morph',null);
						//this.image.setAttribute('width','');
						//this.currentHeight = this.image.getAttribute('height');
						//this.image.setAttribute('height','');
						//this.updateSize();
						
						//this.resizeImage(this.image);
						this.imgcontainer.empty();
						this.image.inject(this.imgcontainer);
						this.imageSize = this.image.getSize();
						if(!this.parent.isTablet){
							this.image.setStyle('width',Math.ceil(this.image.getSize().x/1.5)+"px");
							this.image.setStyle('height',Math.ceil(this.image.getSize().y/1.5)+"px");
						}
						//this.parent.message('loading successfull');
						//this.currentImage = this.dir=="next"?this.next:this.previous;
						//this.getNextAndPrevious();
						
						//var from = this.dir=="next"?this.parent.width:-this.parent.width;
						//this.bigTweenOut.set('left',from);
						//this.bigTweenIn.set('left',from);
						//this.container.setStyle('left',0);
						//this.bigTweenIn.start('left', from ,0);
						this.slideIn();
						//this.parent.message('tween from '+from+' to 0');
					}.bind(this),
					onError: function(e){
						this.parent.message('error loading image');
					}.bind(this)
				});
			}
		
	},
	slideIn: function(){
		var from = (this.dir=="next" || this.dir=="none")?this.parent.width:-this.parent.width;
		this.container.setStyle('left',from+"px");
		this.container.setPosition({x:from,y:0});
		this.parent.message('container position = '+this.container.getPosition().x);
		if(this.css3){
			var m = new Fx.Tween.CSS3(this.container, {
				duration: this.duration,
				transition: 'sine:out'
			});
			m.addEvent('complete', function(){ this.blockSlide = false;}.bind(this));
			m.start('left',from,0);
			//this.parent.message('container move from '+from+' to 0');
		} else {
			var m = new Fx.Morph(this.container, {duration: this.duration, transition: Fx.Transitions.Sine.easeOut,onComplete:function(){ this.blockSlide = false; /*this.parent.message('unblocked');*/}.bind(this)});
			m.start({left:0});
		}
	},
	slideOut: function(){
		this.blockSlide = true;
		var to = this.dir=="next"?-this.parent.width:this.parent.width;
		this.container.setStyle('left',"0px");
		this.container.setPosition({x:to,y:0});
		this.parent.message('container position = '+this.container.getPosition().x);
		if(this.css3){
			var m = new Fx.Tween.CSS3(this.container, {
				duration: this.duration,
				transition: 'sine:out'
			});
			m.addEvent('complete', this.tweenComplete.bind(this));
			m.start('left',0,to);
			//this.parent.message('container move from '+from+' to 0');
		} else {
			var m = new Fx.Morph(this.container, {duration: this.duration, transition: Fx.Transitions.Sine.easeOut,onComplete:this.tweenComplete.bind(this)});
			m.start({left:to});
		}
	}
});
var FWSLIDESHOW = new Class({
	parent:null,
	folder:'',
	images:[],
	initialize: function(parent,el){
		this.parent = parent;
		var f = el.get('text');
		this.folder = f;
		el.empty();
		if(f !== '')
		{
			var req = new Request({url: 'img/slideshow/get.json.php?folder='+f,
			   method:'get',
				onSuccess: function(data){
					data = JSON.decode(data);
					if(data.pictures){
						this.buildSlideShow(data.pictures);
					}
					//this.parent.message('initSlideshow request successful');
				}.bind(this),
				onFailure: function(){
					this.parent.message('initSlideshow request failed');
				}.bind(this)
			}).send();
		}
	},
	buildSlideShow: function(pics){
		this.pics = pics;
		//this.parent.message('building slideshow');
		var s = $('mobileslideshow');
		s.addClass('slideshow');
		
		var wrapper = new Element('div').inject(s);
		wrapper.addClass('slideshow_miniwrapper');
		if(this.parent.isTablet){
			wrapper.setStyle('width','511px');
		} else {
			wrapper.setStyle('width','220px');
		}
		
		wrapper.addEvent('touchstart',function(event){
			if(this.slideshowcontainer.get('morph')){
				this.slideshowcontainer.get('morph').cancel();
			}
			var touch = event.touches[0];
			this.move = true;
			this.speed = 0;
			//this.lastX = 0;
			this.currentX = 0;
			this.timeStart = new Date().getTime();
			this.moveX = touch.pageX;
			this.moveFrom = this.slideshowcontainer.getPosition().x;
			//this.parent.message('touchstart '+this.moveX);
		}.bind(this));
		wrapper.addEvent('touchmove',function(event){
			event.preventDefault();
			var touch = event.touches[0];
			//if(!this.distancecount){
			//	this.lastX = this.currentX;
			//	this.distancecount = 10;
			//}
			//this.distancecount--;
			var to = this.moveFrom+touch.pageX-this.moveX;
			this.currentX = to;
			if(to <= 0 && to >= this.minx){
				this.slideshowcontainer.setStyle('left',to+"px");
			}
			//this.parent.message('touchmove '+touch.pageX);
		}.bind(this));
		wrapper.addEvent('touchend',function(event){
			this.move = false;
			this.timeEnd = new Date().getTime();	
			if(this.currentX-this.moveFrom > 0) { var dir = 0;}
			var dir = this.currentX-this.moveFrom>0?0:1;
			if(this.moveFrom>this.currentX){
				var diff = this.moveFrom-this.currentX;
			} else {
				var diff = this.currentX-this.moveFrom;
			}
			this.time = this.timeEnd-this.timeStart;
			var a = diff/this.time;
			a = a*2;
			
			//this.parent.message('touchend speed='+this.speed+', diff='+diff+', time='+this.time+', a='+a);
			//this.parent.message('touchend element='+event.target.getAttribute('src'));
			
			// time < 100 == KLICK
			if(this.time > 130)
			{
				//this.parent.message('this.time groesser 100');
				this.slideshowcontainer.set('morph', {duration: Math.abs(a)*600, transition: Fx.Transitions.Quad.easeOut});
				var to = dir?(this.currentX-a*diff):(this.currentX+a*diff);
				if(to > 0){ to = 0;}
				if(to < this.minx){to = this.minx;}
				this.slideshowcontainer.morph({left: to});
			} else {
				//this.parent.message('this.time kleiner 100');
				this.openBigSlider(event.target);
			}
		}.bind(this));
		
		var container = new Element('div').inject(wrapper);
		container.addClass('slideshow_minicontainer');
		
		this.slideshowcontainer = container;
		
		var arrd = new Element('div').inject(s);
		arrd.addClass('slideshow_miniarrow');
		var arr = new Element('img',{src:'img/pfeilsch_next.png'}).inject(arrd);
		if(this.parent.isTablet){
			arrd.setStyle('padding-top','40px');
			arr.setStyle('margin-top','40px');
		} else {
			arr.setStyle('margin-top','15px');
		}
		
		arr.addEvent('click',function(e){
			this.currentx -= this.abstand;
			if(this.currentx >= this.minx){
				this.slideshowcontainer.set('morph', {duration: 500, transition: Fx.Transitions.Sine.easeOut});
				this.slideshowcontainer.morph({left: this.currentx});
			}
		}.bind(this));
		var table = new Element('table',{cellpadding:5,cellspacing:0}).inject(container);
		var tb = new Element('tbody').inject(table);
		var tr = new Element('tr').inject(tb);
		var a,pic, thumb, td, img,src;
		for(var i=0;i<pics.length;i++){
			pic = pics[i];
			//thumb = pic.replace(/pictures/,'thumbs');
			td = new Element('td').inject(tr);
			if(this.parent.isTablet){
				src = "img/slideshow/getImage.php?folder="+this.folder+"&image="+pic+"&maxwidth=160";
				img = new Element('img',{src:src,styles:{'width':'160px'}}).inject(td);
			} else {
				src = "img/slideshow/getImage.php?folder="+this.folder+"&image="+pic+"&maxwidth=60";
				img = new Element('img',{src:src,styles:{'width':'60px'}}).inject(td);
			}
			/*
			img.addEvent('swipe',function(e){
				switch(e.direction){
					case 'left':
						this.currentx -= this.abstand;
						if(this.currentx >= this.minx){	this.slideshowcontainer.morph({left: this.currentx});}
						break;
					case 'right':
						this.currentx += this.currentx<0?this.abstand:0;
						if(this.currentx < 0){	this.slideshowcontainer.morph({left: this.currentx});}
						break;
				}
			}.bind(this));
			*/
			/*
			img.addEvent('click',function(e){
				if(!this.move){
					this.openBigSlider(e.target);
				}
			}.bind(this));
			*/
			this.images.push(img);
		}
		this.abstand = (this.images[1].getPosition().x-this.images[0].getPosition().x);
		this.minx = -((this.images.length-3)*this.abstand);
		this.currentx = 0;
	},
	openBigSlider: function(startEl){
		var src = startEl.getAttribute('src');
		var a = src.split('folder=');
		var b = a[a.length-1].split('&');
		var folder = b[0];
		var c = src.split('image=');
		var d = c[c.length-1].split('&');
		var pic = d[0];
		
		window.location = baseUrl+"/slide/"+folder+"/"+pic;
	},
	initializeBigSlider: function(){
			//this.parent.message('initializeBigSlider');
			var as = this.bigSliderAnim.getSize();
			this.bigSliderAnim.destroy();
			var c = new Element('div').inject(document.body,'bottom');
			c.setStyle('overflow','hidden');
			
			c.setStyle('width',as.x+"px");
			c.setStyle('height',as.y+"px");
			c.setPosition({x:0,y:0});
			this.bigSliderContainer = c;
			var s = new Element('div').inject(c);
			s.setStyle('position','absolute');
			s.setStyle('z-index',101);
			this.bigslideshowcontainer = s;
			this.bigslideshowcontainer.set('morph', {duration: 3000, transition: 'sine:out'});
			var img,src;
			var table = new Element('table',{cellpadding:0,cellspacing:0}).inject(s);
			var tb = new Element('tbody').inject(table);
			var tr = new Element('tr').inject(tb);
			var td;
			for(var i=0;i<this.images.length;i++){
				td = new Element('td').inject(tr);
				img = new Element('img').inject(td);
				src = this.images[i].getAttribute('src');
				src = src.replace(/thumbs/,'pictures');
				//this.parent.message('img: '+src);
				img.setAttribute('src',src);
				img.setStyle('width',(this.parent.width/2)+"px");
				img.setStyle('height',(this.parent.height/2)+"px");
				img.setStyle('float','left');
				img.addEvent('click',function(e){
					this.bigSliderContainer.destroy();
				}.bind(this));
				img.addEvent('swipe',function(e){
					//this.parent.message('bigswipe '+e.direction);
					switch(e.direction){
						case 'left':
							this.bigcurrentx -= this.bigabstand;
							if(this.bigcurrentx >= this.bigminx){	this.bigslideshowcontainer.morph({left: this.bigcurrentx});}
							break;
						case 'right':
							this.bigcurrentx += this.bigcurrentx<0?this.bigabstand:0;
							if(this.bigcurrentx <= 0){	this.bigslideshowcontainer.morph({left: this.bigcurrentx});}
							break;
					}
					//this.parent.message('bigswipe moved to '+this.bigcurrentx);
				}.bind(this));
				
			}
			this.bigabstand = this.parent.width/2;
			this.bigminx = -this.images.length*this.bigabstand;
			this.bigcurrentx = -this.startIndex*this.bigabstand;
			s.setPosition({x:this.bigcurrentx,y:0});
			this.bigInitialized = true;
	}
});
var FWBOUTIQUES = new Class({
	parent:null,
	links:[],
	elements:[],
	slides:[],
	mores:[],
	opened:-1,
	initialize: function(p){
		this.parent = p;
		
		// scan for accordeon items
		$$('.bfaccordeon').each(function(el,index){
			var a = el.getPrevious().getFirst();
			this.links.push(a);
			this.elements.push(el);
			a.setAttribute('href','javascript:void(0)');
			if(a.getParent().hasClass('opened')){
				this.opened = index;
			}
		}.bind(this));
		
		// create accordeon
		this.accordeon = new Fx.Accordion(this.links, this.elements, {
			display:this.links.length>1?this.opened:0,
			alwaysHide:true,
			opacity:false,
			onActive: function(toggler, element){
				toggler.getParent().removeClass('closed');
				toggler.getParent().addClass('opened');
			},
			onBackground: function(toggler, element){
				toggler.getParent().removeClass('opened');
				toggler.getParent().addClass('closed');
			}
		});
		
		// if accordeon active, replace more.. links
		if(this.links.length){
			$$('.bfmore').each(function(el,index){
				var a = el.getFirst();
				var url = a.getAttribute('href');
				//var tmp = url.split('?');
				var tmp = url.split('/');
				var newurl = baseUrl+'/inc/ajax/boutiquedetails.php?var1='+tmp[tmp.length-3]+'&var2='+tmp[tmp.length-2]+'&var3='+tmp[tmp.length-1];
				//var newurl = 'http://192.168.1.37/mobile/inc/ajax/boutiquedetails.php?var1='+tmp[tmp.length-3]+'&var2='+tmp[tmp.length-2]+'&var3='+tmp[tmp.length-1];
				this.mores.push(newurl);
				a.setAttribute('href','javascript:void(0)');
				a.setAttribute('id',index);
				a.addEvent('click',function(el){
					var url = this.mores[el.target.getAttribute('id')];
					var content = el.target.getParent().getParent().getParent();
					content.load(url);
				}.bind(this));
				
			}.bind(this));
		}
		
	}
});
var fw;
