$(window).addEvents({
	domready: function() {
		MenuHover.init();
	}
});

// menu support
var MenuHover = {};
MenuHover.current = null;

MenuHover.init = function() {
	$$('#menu a').addEvents({
		mouseenter: function(event) {
			var a = $(event.target);
			if (a.tagName != 'A') a = a.getParent('a');
			
			var idx = a.id.replace(/[^\d]+/, '');
			if (!idx || a.hasClass('bg' + idx)) return;
			
			$('menu-ln' + idx).set('morph', { duration: 0 }).morph('.bg' + idx);
			$('menu-lt' + idx).set('morph', { duration: 0 }).morph('.bg' + idx);
		},
		mouseleave: function(event) {
			var a = $(event.target);
			if (a.tagName != 'A') a = a.getParent('a');
			
			var idx = a.id.replace(/[^\d]+/, '');
			if (!idx || a.hasClass('bg' + idx)) return;
			
			$('menu-ln' + idx).set('morph', { duration: 'normal' }).morph('.bg0');
			$('menu-lt' + idx).set('morph', { duration: 'normal' }).morph('.bg0');
		}
	});
};

MenuHover.updateSelected = function(idx) {
	if (this.current == idx) return;
	
	if (this.current) {
		$('menu-ln' + this.current).removeClass('bg' + this.current);
		$('menu-lt' + this.current).removeClass('bg' + this.current);
		
		$('menu-ln' + this.current).set('morph', { duration: 'normal' }).morph('.bg0');
		$('menu-lt' + this.current).set('morph', { duration: 'normal' }).morph('.bg0');
		
		this.current = null;
	}
	
	if ($('menu-ln' + idx)) {
		this.current = idx;
		
		$('menu-ln' + this.current).set('morph', { duration: 0 }).morph('.bg' + this.current);
		$('menu-lt' + this.current).set('morph', { duration: 0 }).morph('.bg' + this.current);
		
		$('menu-ln' + this.current).addClass('bg' + this.current);
		$('menu-lt' + this.current).addClass('bg' + this.current);
	}
};

// print photo switch
var PrintPhotoNav = {};
PrintPhotoNav.init = function() {
	if ($('print-photos'))
		$('print-photos').getElements('img').addEvent('click', function() {
			var src = this.get('src');
			var large = $('print-photo-large').getElement('img');
			this.set('src', large.get('src').replace(/_l/, ''));
			
			ImgLoader.add({ wrapper: 'print-photo-large', src: src + '_l' });
		});
};

// photo navigation
var PhotographyNav = {};
PhotographyNav.film = null;
PhotographyNav.filmPos = null;
PhotographyNav.knob = null;
PhotographyNav.filmLeverage = null;
PhotographyNav.knobLeverage = null;

PhotographyNav.step = 0;
PhotographyNav.int = null;
PhotographyNav.scrolled = 0;

PhotographyNav.init = function() {
	this.scrolled = 0;
	this.knob = $('photography-slider').getElement('div');
	this.film = $('photography-film');
	
	this.film.addEvent('click', 
		function(event) {
			if (event.target.tagName == 'IMG') {
				PhotographyNav.step = 0;
				ImgLoader.add({ wrapper: 'photography-large', src: event.target.src + '_l' });
			}
		}
	);
	
	var s2 = this.film.getScrollSize().y;
	var s1 = 412;
	if (s2 > 412) {
		this.film.setStyle('height', 412);
		$('photography-slider').setStyle('height', 412);
	}
	else {
		this.film.setStyle('height', s2);
		this.knob.setStyle('height', s2);
		s1 = s2;
	}
	
	if (s1 < s2)
		this.knob.setStyle('height', Math.round(s1 * 100 / s2));
	else
		return;
	
	this.filmPos = this.film.getPosition();
	this.filmHeight = s1;
	this.filmLeverage = s2 - s1;
	this.knobLeverage = $('photography-slider').getSize().y - this.knob.getStyle('height').toInt();
	
	this.film.addEvents({
		mousemove: this.filmMouseMove.bind(this),
		mouseenter: function() {
			PhotographyNav.int = setInterval(PhotographyNav.filmWind.bind(PhotographyNav), 1);
		},
		mouseleave: function() {
			clearInterval(PhotographyNav.int);
			PhotographyNav.step = 0;
		}
	});
};

PhotographyNav.filmMouseMove = function(event) {
	var offset = event.page.y - this.filmPos.y + 1;
	
	if (offset <= 30)
		this.step = -6;
	else if (offset <= 60)
		this.step = -3;
	else if (offset <= 90)
		this.step = -1;
	else if (offset >= this.filmHeight - 30)
		this.step = 6;
	else if (offset >= this.filmHeight - 60)
		this.step = 3;
	else if (offset >= this.filmHeight - 90)
		this.step = 1;
	else
		this.step = 0;
};

PhotographyNav.filmWind = function() {
	if ((this.scrolled == 0 && this.step < 0) || (this.scrolled == this.filmLeverage && this.step > 0)) return;
	this.scrolled += this.step;
	
	if (this.scrolled < 0)
		this.scrolled = 0;
	else if(this.scrolled > this.filmLeverage)
		this.scrolled = this.filmLeverage;
		
	this.film.scrollTop = this.scrolled;
	this.knob.setStyle('margin-top', this.scrolled / this.filmLeverage * this.knobLeverage);
};

/* webdesign hover */
var WebdesignHover = {};

WebdesignHover.init = function() {
	$('webdesign').getElements('a').addEvents({
		mouseenter: function(event) {
			if (!event.target.rel) return;
			
			if (!$('webdesign-hover')) {
				var p = new Element('p', { id: 'webdesign-hover' });
				$('webdesign').grab(p);
			}
			
			var coords = event.target.rel.split('-');
			var img = new Element('img', { src: '/udata/webdesign/' + coords[0] });
			$('webdesign-hover').set('html', '');
			$('webdesign-hover').grab(img);
			
			var pos = event.target.getPosition();
			$('webdesign-hover').setStyles({ display: 'block', opacity: 0, width: coords[1], height: coords[2], left: pos.x + 150 });
			
			var sizeWindow = window.getSize();
			var scroll = window.getScroll();
			var top = pos.y;
			if (top + parseInt(coords[2], 10) > sizeWindow.y + scroll.y)
				top -= pos.y + parseInt(coords[2], 10) - (sizeWindow.y + scroll.y) + 20;
			$('webdesign-hover').setStyle('top', top);
			
			$('webdesign-hover').set('morph', {}).morph({ opacity: 1 });
		},
		mouseleave: function(event) {
			if (!event.target.rel) return;
			
			$('webdesign-hover').set('morph', { onComplete: function() {
				$('webdesign-hover').setStyles({ display: 'none', width: 'auto', height: 'auto' });
			}}).morph({ opacity: 0 });
		}
	});
}
