window.addEvents({
	'domready': function() {
		new FilmGallery('film', location.href.indexOf('/locations') != -1 ? { 'glider': false } : {});
		
		if ($('locations')) {
			var map = $('locations').getElement('.map');
			new GoogleMap(map, { points: [map.coords], center: true, zoom: 15 });
		}
		
		$$('#submenu strong').addEvent('click', function(event) {
			var sm = $('sm' + $(event.target).id.substr(1));
			if (!sm.fx) sm.fx = new Fx.Tween(sm, { link: 'cancel' });
			if ($('submenu').sm) {
				if (!$('submenu').sm.fx) {
					$('submenu').sm.fx = new Fx.Tween($('submenu').sm, { link: 'cancel' });
					$('submenu').sm.setStyle('height', $('submenu').sm.getSize().y);
				}
				$('submenu').sm.fx.start('height', 0);
			}
			if (!$('submenu').sm || $('submenu').sm != sm) {
				$('submenu').sm = sm;
				sm.fx.start('height', sm.getScrollSize().y);
			}
			else
				$('submenu').sm = null;
		});
	}
});

var FilmGallery = new Class({

	Extends: Options,
	options: {
		'glider': true
	},

	initialize: function(cont, options) {
		this.cont = $(cont);
		this.setOptions(options);
		this.scroller = this.cont.getElement('p.scroll');
			
		if (this.options.glider) {
			this.glider = this.cont.getElement('span');
			this.glider.setStyle('opacity', 0);
			this.gliderFx = new Fx.Morph(this.glider);
			var pos = this.scroller.getPosition();
			var size = this.scroller.getSize();
			this.gliderBoundaries = [pos.x.toInt(), pos.x.toInt() + size.x.toInt() - 180];
			this.scroller.getElements('img')
				.addEvents({
					'mouseenter': this.gliderStart.bind(this),
					'mouseleave': this.gliderStop.bind(this)
				});
		}
		else {
			this.scroller.getElements('img').addEvent('click', this.openLarge.bind(this));
			
			this.idx = 0;
			this.thumbs = this.scroller.getElements('img');
			this.thumbs.each(function(img) { preload(img.get('src').replace(/-t/, '')); });
			this.top = $('large').getElement('.left');
			this.bottom = $('large').getElement('.right');
			this.large = $('large');
			this.largeWidth = $('large').getSize().x;
			this.largePosX = $('large').getPosition().x;
			this.large.addEvents({
				'mousemove': this.largeMouseMove.bind(this),
				'mouseleave': this.largeMouseLeave.bind(this),
				'click': this.largeClick.bind(this)
			});
		}
		
		if (this.scroller.getScrollSize().x <= this.scroller.getSize().x) return;
		this.scrollMax = this.scroller.getScrollSize().x - this.scroller.getSize().x;
		this.cont.getElements('a.scroll')
			.setStyle('visibility', 'visible')
			.addEvents({
				'mouseenter': this.scrollStart.bind(this),
				'mouseleave': this.scrollStop.bind(this)
			});
	},
	
	scrollStart: function(event) {
		var target = $(event.target);
		this.scrollInc = target.hasClass('fwd') ? 1 : -1;
		
		if (this.scrollInt) $clear(this.scrollInt);
		this.scrollInt = this.scroll.periodical(10, this);
	},
	
	scrollStop: function(event) {
		if (this.scrollInt) $clear(this.scrollInt);
		this.scrollInc = 0;
	},
	
	scroll: function(event) {
		var sl = this.scroller.scrollLeft;
		if ((this.scrollInc == -1 && sl <= 0) || (this.scrollInc == 1 && sl >= this.scrollMax)) return;
		this.scroller.scrollLeft = sl + this.scrollInc;
	},
	
	gliderStart: function(event) {
		var target = $(event.target);
		
		this.glider
			.set('class', target.alt.length > 23 ? 'two' : '')
			.set('html', '<strong>' + target.alt.replace(/:/, ':</strong>'));
		
		var pos = target.getPosition();
		var x = pos.x < this.gliderBoundaries[0] ? this.gliderBoundaries[0] : (pos.x > this.gliderBoundaries[1] ? this.gliderBoundaries[1] : pos.x);
		x -= this.gliderBoundaries[0];
		
		this.gliderFx.cancel();
		this.gliderFx.start({ 'opacity': 1, 'margin-left': x });
	},
	
	gliderStop: function(event) {
		this.gliderFx.cancel();
		this.gliderFx.start({ 'opacity': 0, 'margin-left': 0 });
	},
	
	openLarge: function(event) {
		var target = $(event.target);
		this.idx = target.lang.toInt();
		imgLoader.loadImg('large', target.get('src').replace(/-t$/, ''));
		
		event.stop();
	},
	
	largeMouseMove: function(event) {
		this.inc = event.page.x - this.largePosX <= this.largeWidth / 2 ? -1 : 1;
		if ((this.inc == -1 && this.idx == 0) || (this.inc == 1 && this.idx == this.thumbs.length - 1)) {
			this.large.setStyle('cursor', 'auto');
			this.top.setStyle('display', 'none');
			this.bottom.setStyle('display', 'none');
		}
		else {
			this.large.setStyle('cursor', 'pointer');
			this.top.setStyle('display', this.inc == -1 ? 'block' : 'none');
			this.bottom.setStyle('display', this.inc == 1 ? 'block' : 'none');
		}
	},
	
	largeMouseLeave: function(event) {
		if ($(event.relatedTarget) && ($(event.relatedTarget).hasClass('left') || $(event.relatedTarget).hasClass('right'))) return;
		
		this.large.setStyle('cursor', 'auto');
		this.top.setStyle('display', 'none');
		this.bottom.setStyle('display', 'none');
	},
	
	largeClick: function(event) {
		if (this.large.getStyle('cursor') != 'pointer') return;
		
		this.idx += this.inc;
		if (this.idx == 0) this.top.setStyle('display', 'none');
		if (this.idx == this.thumbs.length - 1) this.bottom.setStyle('display', 'none');
		if (this.idx == 0 || this.idx == this.thumbs.length - 1)
			this.large.setStyle('cursor', 'auto');
		
		imgLoader.loadImg('large', this.thumbs[this.idx].get('src').replace(/-t$/, ''));
		document.body.focus();
	}

});

var Tenant = {}
Tenant.hookLoginEvents = function() {
	$('formlogin').getElements('input').addEvents({
		'keydown': function(event) { if (event.code == 13) Tenant.login() },
		'focus': function() {
			if (!this.valueDef)
				this.valueDef = this.value;
			if (this.valueDef == this.value)
				this.value = '';
		},
		'blur': function() { if (!this.value) this.value = this.valueDef }
	});
	$('formlogin').getElement('a[rel=login]').addEvent('click', Tenant.login.bind(Tenant));
	$('formlogin').getElement('a[rel=fpass]').addEvent('click', function() {
		$('user-area').set('html', '<p>Please wait...</p>');
		new Request.HTML({ update: 'user-area', url: '/tenant/forgotten-password' }).get();
	});
}
Tenant.hookFpassEvents = function() {
	$('formfpass').getElements('input').addEvents({
		'keydown': function(event) { if (event.code == 13) Tenant.fpass() },
		'focus': function() {
			if (!this.valueDef)
				this.valueDef = this.value;
			if (this.valueDef == this.value)
				this.value = '';
		},
		'blur': function() { if (!this.value) this.value = this.valueDef }
	});
	$('formfpass').getElement('a[rel=fpass]').addEvent('click', Tenant.fpass.bind(Tenant));
	$('formfpass').getElement('a[rel=back]').addEvent('click', function() {
		$('user-area').set('html', '<p>Please wait...</p>');
		new Request.HTML({ update: 'user-area', url: '/tenant' }).get();
	});
}
Tenant.login = function() {
	if (Tenant.doingLogin) return;
	Tenant.doingLogin = true;
	
	$('formlogin').getElement('a[rel=login]').set('html', 'please wait...');
	$('formlogin').set('send', { 'url': '/tenant/login', onComplete: Tenant.loginCallback.bind(Tenant) }).send();
}
Tenant.loginCallback = function(response) {
	Tenant.doingLogin = false;
	
	if (response.trim().match(/^ok$/)) {
		$('user-area').set('html', '<p>Please wait...</p>');
		new Request.HTML({ update: 'user-area', url: '/tenant' }).get();
	}
	else {
		if ($('formlogin'))
			$('formlogin').getElement('a[rel=login]').set('html', 'sign-in');
		alert(response);
	}
}
Tenant.fpass = function() {
	if (Tenant.doingFpass) return;
	Tenant.doingFpass = true;
	
	$('formfpass').getElement('a[rel=fpass]').set('html', 'please wait...');
	$('formfpass').set('send', { 'url': '/tenant/forgotten-password', onComplete: Tenant.fpassCallback.bind(Tenant) }).send();
}
Tenant.fpassCallback = function(response) {
	Tenant.doingFpass = false;
	
	if (response.trim().match(/^ok$/)) {
		alert('An e-mail with a randomly generated password has been sent to your e-mail address!');
		$('user-area').set('html', '<p>Please wait...</p>');
		new Request.HTML({ update: 'user-area', url: '/tenant' }).get();
	}
	else {
		if ($('formfpass'))
			$('formfpass').getElement('a[rel=fpass]').set('html', 'retrieve password');
		alert(response);
	}
}