$(window).addEvents({
	
	'domready': function() {
		
		if ($('sample-menu'))
			yourMenu = new YourMenu();
		
	},
	
	'load': function() {
		
		preload('/images/catering/menu_top_bg_on.png');
		
		if (typeof(slideshow) != 'undefined') {
			var options = {};
			if ($('about-us'))
				options = { mode: 'slide-in' }
			new Slideshow('slideshow', slideshow, options);
		}
			
		if ($('gallery-nos'))
			new Gallery();
		
	}
	
});

var Slideshow = new Class({
	
	Extends: Options,
	
	options: {
		width: 236,
		height: 236,
		delay: 4000,
		mode: 'grow-center'
	},
	
	initialize: function(cont, sections, options) {
		this.cont = cont;
		this.sections = sections;
		this.setOptions(options);
		
		this.idx = [];
		this.preload = [];
		
		this.sections.each(function(section, key) {
			this.idx[key] = 0;
			this.preload[key] = true;
			preload(this.sections[section][this.idx[key] + 1]);
		}, this);
		
		this.rotate.periodical(this.options.delay, this);
	},
	
	rotate: function() {
		this.sections.each(function(section, key) {
			this.idx[key] = (this.idx[key] + 1) % this.sections[section].length;
			if (this.preload[key]) {
				preload(this.sections[section][this.idx[key] + 1]);
				if (this.idx[key] + 1 == this.sections[section].length - 1)
					this.preload[key] = false;
			}

			var cont = $(this.cont + '-' + section);
			var img = cont.getElement('img');
			cont.setStyle('background-image', 'url(' + img.src + ')');
			img.dispose();
			
			if (this.options.mode == 'slide-in') {
				var settingsInitial = {};
				var settingsFinal = {};
				var pos = 'margin-' + (key == 1 ? 'top' : 'left');
				settingsInitial[pos] = (key == 2 ? 1 : -1) * (key == 1 ? this.options.height : this.options.width);
				settingsFinal[pos] = 0;
				var delay = key * 750;
			}
			else {
				var settingsInitial = { opacity: 0 };
				var settingsFinal = { opacity: 1 };
				var delay = key * 750;
			}
			
			img = new Element('img', { src: this.sections[section][this.idx[key]] })
				.setStyles(settingsInitial)
				.inject(cont);
			img.fx = new Fx.Morph(img, { duration: 'long' });
			img.fx.start.delay(delay, img.fx, settingsFinal);
		}, this);
	}
	
});

var Gallery = new Class({
	
	Extends: Options,
	
	options: {},
	
	initialize: function(cont, sections, options) {
		$$('#gallery-nos a').addEvents({
			'mouseenter': this.showThumb.bindWithEvent(this),
			'mouseleave': this.hideThumb.bindWithEvent(this),
			'click': this.changePhoto.bindWithEvent(this)
		});
		
		$('gallery-nav-prev').addEvent('click', this.nav.bindWithEvent(this));
		$('gallery-nav-next').addEvent('click', this.nav.bindWithEvent(this));
		
		this.id = $('gallery-nos').getElement('a').id.replace(/no/, '');
		if (location.href.indexOf('#')) {
			var arr = location.href.match(/\d+$/);
			if (arr)
				this.changePhoto(arr[0]);
		}
	},
	
	showThumb: function(event) {
		var a = $(event.target);
		if (a.hasClass('no-photo')) return;

		a.pos = a.getPosition($('layout2').getElement('.top'));
		$('gallery-thumb').getElement('img').set('src', galleryFolder + a.id.replace(/no/, '') + '_n');
		$('gallery-thumb').setStyles({
			display: 'block',
			top: a.pos.y - 185,
			left: a.pos.x - 45
		});
	},
	
	hideThumb: function(event) {
		var a = $(event.target);
		if (a.hasClass('no-photo')) return;
		
		$('gallery-thumb').setStyle('display', 'none');
	},
	
	nav: function(event) {
		var a = $(event.target);
		if (a.hasClass('gallery-nav-off')) return;
		
		if (a.get('text').indexOf('PREV') != -1)
			this.changePhoto($('no' + this.id).getPrevious('a').id.replace(/no/, ''));
		else
			this.changePhoto($('no' + this.id).getNext('a').id.replace(/no/, ''));
			
		return false;
	},
	
	changePhoto: function() {
		var id = typeof(arguments[0]) == 'object' ? $(arguments[0].target).id.replace(/no/, '') : arguments[0];
		if ($('no' + id).hasClass('on')) return;
		
		$('no' + this.id).removeClass('on');
		$('no' + id).addClass('on');
		this.id = id;
		
		if ($('gallery-info' + id)) {
			$(document).getElement('.info').set('html', $('gallery-info' + id).get('html'));
			$(document).getElement('.prod-title').set('html', $('gallery-info' + id).getElement('strong').get('html'));
			if (typeof(yourMenu) != 'undefined' && $(document).getElement('.prod-title').getElement('a.your-menu-add')) {
				$(document).getElement('.prod-title').getElement('a.your-menu-add').addEvent('click', yourMenu.add.bindWithEvent(yourMenu));
			}
		}

		if ($('no' + this.id).getPrevious('a') != null)
			$('gallery-nav-prev').removeClass('gallery-nav-off');
		else
			$('gallery-nav-prev').addClass('gallery-nav-off');
		if ($('no' + this.id).getNext('a') != null)
			$('gallery-nav-next').removeClass('gallery-nav-off');
		else
			$('gallery-nav-next').addClass('gallery-nav-off');
			
		imgLoader.load('gallery-large', galleryFolder + id + '_l');
		
		location.href = $('no' + id).href;
		
		return false;
	}
	
});

var YourMenu = new Class({
	
	initialize: function() {
		$$('#sample-menu a.your-menu-add').addEvent('click', this.add.bindWithEvent(this));
		$$('#sample-menu a.your-menu-remove').addEvent('click', this.remove.bindWithEvent(this));
		
		this.req = new Request({ link: 'chain' });
	},
	
	add: function(event) {
		var a = $(event.target);
		this.req.options.url = '/catering/your-menu-add/' + a.rel;
		this.req.send();
		a.dispose();
		
		var count = $('your-menu').getElement('span');
		count.set('text', count.get('text').toInt() + 1);
		
		return false;
	},
	
	remove: function(event) {
		var a = $(event.target);
		this.req.options.url = '/catering/your-menu-remove/' + a.rel;
		this.req.send();
		
		a.getParent('.prod').dispose();
		if ($('layout2').getElement('.col2 .content').get('html').trim() == '')
			$('layout2').getElement('.col2 .content').set('html', 'no products found!<br><br><br>');
		else
			$('layout2').getElement('.col2 .content .prod').setStyle('margin-top', 0);
		
		var count = $('your-menu-count').getElement('strong');
		count.set('text', count.get('text').toInt() - 1);
		
		return false;
	}
	
});