var sliding_gallery = new Class({
	initialize:function(id, options){
		if (/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) return;
		this.options = {
			tab_margin_left: '0px',
			tab_margin_right: '0px',
			event:'click'
		}
		$extend(this.options,options);
		this.gallery_outer = $(id);
		this.gallery_fullwidth = $(id).getElement('div');
		this.tabs = $$('#' + id + ' .gallery_content');
				
		this.tab_items = [];
		this.tabs_dim = 0;
		this.tabs_dim += this.gallery_fullwidth.getStyle('margin-left').toInt();
		this.tabs_dim += this.gallery_fullwidth.getStyle('margin-right').toInt();
		
		this.tabs.each(function(el,i){
			var w = el.getStyle('width').toInt();
			this.tabs_dim += (w > 0) ? w : 200;
			this.tabs_dim += el.getStyle('border-right-width').toInt();
			this.tabs_dim += el.getStyle('border-left-width').toInt();
			this.tabs_dim += el.getStyle('margin-left').toInt();
			this.tabs_dim += el.getStyle('margin-right').toInt();
			this.tabs_dim += el.getStyle('padding-left').toInt();
			this.tabs_dim += el.getStyle('padding-right').toInt();

			this.tab_items[i] = new tab_item(el,this);
		}.bind(this));
		
		this.gallery_fullwidth.setStyle('width', this.tabs_dim+'px'); 

		this.tab_fx = new Fx.Scroll(this.gallery_outer,{
			duration:0,
			link:'cancel',
			onComplete:function(){
				this.gallery_outer.addEvent('mousemove',this.mouse_move.bind(this));
				var myCookie1  = Cookie.write(id + 'position', this.gallery_outer.getScroll().x, {duration: 1});
			}.bind(this)
		});
		
		this.gallery_outer.addEvent('mouseenter',function(e){
			e = new Event(e).stop();

			this.tab_fx.start(this.get_position(e));

		}.bind(this));
		
		this.gallery_outer.addEvent('mouseleave',function(){
			this.gallery_outer.removeEvents('mousemove');
			var myCookie1  = Cookie.write(id + 'position', this.gallery_outer.getScroll().x, {duration: 1});
		}.bind(this));
		
		var myCookie = Cookie.read(id + 'position');
		if($chk(myCookie) && myCookie.toInt() > 0) {
			new Fx.Scroll(this.gallery_outer,{link:'cancel',duration:0}).start(myCookie.toInt(),0);
		}
		else
		{
			new Fx.Scroll(this.gallery_outer,{link:'cancel',duration:0}).toElement(this.tab_items[0].item);
		}

	},
	mouse_move:function(e){
		e = new Event(e).stop();
		this.tab_fx.cancel();
		this.gallery_outer.scrollLeft = this.get_position(e);

	},
	get_position:function(e){
		var pos = this.gallery_outer.getPosition();
		var scroll = this.gallery_outer.getScroll();
		var scrollPos = Window.getScroll()['x']+e.client['x']-(pos['x']+scroll['x']);
		var position = scrollPos*(this.gallery_fullwidth.getStyle('width').toInt())/this.gallery_outer.getStyle('width').toInt() - scrollPos;
		return position;
	}
});

var tab_item = new Class({
	initialize:function(item,tab_obj){
		this.item = item;
		this.tab_obj = tab_obj;

		this.item.addEvent(this.tab_obj.options.event,this.on_event.bind(this));
	},
	on_event:function(e){
		//e = new Event(e).stop();
	
	}
});

