Element.implement({
	dragCenter: function(vis,nodrag){
		if (nodrag==null) nodrag = false;
		if (!nodrag) {
			if (this.drag==null) this.drag = this.makeDraggable();
		}
		else if (this.drag) {
			this.drag.detach();
			this.drag = null;
		}
		this.setPosition({
			x: (window.getSize().x/2 - this.getSize().x/2), 
			y: window.getScroll().y + 170
		});
		if (vis) this.setStyle('visibility', 'visible');
	}
});

var bucciStore = new Class({

	Implements: [Options],
	
	options: {
		ajax: false,							/* Mode determins how the content pages are loaded: either via ajax or as complete html page */
		loadcontent: '/Cat',
		target: 'list',
		nav: '#mainnav li a, #footer a'
	},

	/* Class Constructor */
    initialize: function(options){
		this.setOptions(options);
		this.initurl = this.options.loadcontent;
		this.target = $(this.options.target);
		var c = this;
		if (this.options.ajax) {
			$$(this.options.nav).addEvent('click', function(e){
				e.preventDefault();
				c.setFilter(this.href);			
			});		
			this.setFilter();
		}
    },

	
	/* Sets a content Filter or resets it if false and shows the result of the loaded kml file */
	setFilter: function(url) {
		this.target.load(url!=null?url:this.initurl).fade('in');
	},
	
	/* Adds the function for a packshot image */
	addPackshotEvent: function(e,urls,id) {
		if (urls.length>1) {
			e.addEvent('mouseover', function(e){
				this.key=1;
				var toggler = function() {
					this.src = urls[this.key][0];			
					this.key>=urls.length-1?this.key=1:this.key++;				
				}.bind(this);
				toggler();
				if (!this.timer) this.timer = toggler.periodical(500, this); //other users online check
			});
			e.addEvent('mouseout', function(e){
				clearInterval (this.timer);
				this.timer = null;
				this.key = 0;
				this.src = urls[this.key][0];			
			});		
		}
		e.addEvent('click', function(e){		
			this.fireEvent('mouseout',this);
			var e = new Element('div',{'class': 'packshotBack', styles: {'width': this.getStyle('width'), 'height': this.getStyle('height')}}).load('/Ajax/'+id).fade('in');
			e.inject(this,'before');			
		});
	},
	
	closeItem: function(ev,el) {
		ev.preventDefault();
		el.getParent('div').fade('out').destroy();
		return false;
	},

	addCart: function(ev, form) {
		if (ev != null) ev.stop();
		if (form==null) {
			$('cart').load('/AddCart');			
		}
		var req = new Request.HTML(
			{
			url: '/AddCart', 
			method: "post", 
			evalScripts: false,			
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$('cart').set('text', '');
				$('cart').adopt(responseTree);
				$('cart').setStyle('display','block');
				if (responseJavaScript) eval(responseJavaScript);
				/* Reload Einkaufszettel if shown */
				if ($('notify').getStyle('visibility') == 'visible') {
					this.checkOut(null,'List');
				}
			}.bind(this)
		});
		req.send(form);
		return false;
	},
	
	checkOut: function(ev, func, sub) {
		if (ev != null) ev.preventDefault();
		if ($('notify').getStyle('visibility') == 'hidden') $('notify').dragCenter(true);	
		var req = new Request.HTML(
			{
			url: '/CheckOut/'+func+(sub!=null?'/'+sub:''), 
			method: "post", 
			evalScripts: false,			
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$('notify').set('text', '');
				$('notify').adopt(responseTree);
				if (responseJavaScript) eval(responseJavaScript);
			}
		});
		req.send();		
	},
	
	closeNotify: function(ev) {
		ev.preventDefault();
		$('notify').empty().setStyle('visibility','hidden');		
	},
	
	payment: function(ev, form) {
		
	}
	
});
