window.addEvent('load', function() {
	$$('a').setProperty('onfocus', 'blur()');
	$('overlay').setStyle('opacity', 0);
	$('images_product').setStyle('opacity', 0);
	$('overlay').setStyle('height', $('body').getStyle('height').toInt() + 50);
	var products = $$('.products');
	var products2 = $$('.products2');
	
	products.addEvent('click',function(){
		var id = this.getProperty('id').replace('product','');
		var myHTMLRequest = new Request.HTML({
			url:'products/getPhotos',
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$('overlay').setStyle('z-index', 10000);
				$('images_product').setStyle('z-index', 10000);
				$('images_product').set('html', responseHTML + responseJavaScript);
				change_photo();
				$('images_product').setStyle('top', window.getScroll().y + 10);
				$('overlay').tween('opacity', 0.4);
				$('images_product').tween('opacity', 1);
				overlay_close();
			}
		});
		myHTMLRequest.post({id:id});
		return false;
	});
	
	products2.addEvent('click',function() {
		var id = this.getProperty('id').replace('product2','');
		var myHTMLRequest = new Request.HTML({
			url:'products/getPhotos',
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				$('overlay').setStyle('z-index', 10000);
				$('images_product').setStyle('z-index', 10000);
				$('images_product').set('html', responseHTML + responseJavaScript);
				change_photo();
				$('images_product').setStyle('top', window.getScroll().y + 10);
				$('overlay').tween('opacity', 0.4);
				$('images_product').tween('opacity', 1);
				overlay_close();
			}
		});
		myHTMLRequest.post({id:id});
		return false;
	});
	$$('.menu-link').addEvent('mouseenter',function() {
		var span = this.getFirst('span');
		span.set('morph', {duration: 500,
			transition: Fx.Transitions.Cubic.easeOut,
			fps: 25});
		this.set('morph', {duration: 500,
			transition: Fx.Transitions.Cubic.easeOut,
			fps: 25});
		span.morph('.menu-link-span-enter');
		this.morph('.menu-link-enter');
	});
	
	$$('.menu-link').addEvent('mouseleave',function() {
		var span = this.getFirst('span');
		mouseleave_a .bind(this).delay(150);
		mouseleave_span.bind(span).delay(150);
	});
	
	mouseleave_a = function () {
		this.morph('.menu-link');
	};
	
	mouseleave_span = function () {
		this.morph('.menu-link-span');
	};
	
	if( $('perPage-form') ) {
		$('perPage-form').getFirst('select').addEvent('change', function(){
			$('perPage-form').submit();
		});
	}
	
	$('overlay').addEvent('click',function() {
		$('overlay').tween('opacity',0);
		$('images_product').tween('opacity',0);
		$('overlay').setStyle('z-index', '1');
		big_none.delay(500);
		$('images_product').setStyle('z-index', '1');
	});

	function overlay_close() {
		$('overlay-close').addEvent('click',function() {
			$('overlay').tween('opacity',0);
			$('images_product').tween('opacity',0);
			$('overlay').setStyle('z-index', '1');
			big_none.delay(500);
			$('images_product').setStyle('z-index', '1');
		});
	}
	
	function change_photo() {
		if ( $$('.small_images')[0] ) {
			$$('.small_images').setStyle('opacity', 0.5);
			$$('.small_images')[0].setStyle('opacity', 1);
			$$('.small_images').addEvent('click', function(){
				$$('.small_images').tween('opacity', 0.5);
				this.tween('opacity', 1);
				var src = this.getProperty('src').replace('/mini/', '/big/');
				$('big').tween('opacity', 0);
				change.delay(500, null, src)
				
			});
		}
	}
	
	function big_none() {
		$('big').setStyle('display', 'none');
		$('overlay-close').setStyle('display', 'none');
		$('sp').setStyle('display', 'none');
		$('bp').setStyle('display', 'none');
	}
	
	function change(src) {
		$('big').setProperty('src', src);
		$('big').tween('opacity', 1);
	}
	
});
