$(function() {
	var menu_active = $('.menu LI.active');
	if (menu_active.size() > 0)
	{
		var page_cont = menu_active.parent();
		var offset = menu_active.position().left;
		menu_active.css('background-position', '-'+offset+'px 4px').css('background-repeat', 'no-repeat');
	}

	$('.main-teasers .teaser').click(function(e) {
		window.location.href = $(this).find('.title A').attr('href');
	});

	$('.main-teasers .teaser').each(function(i, o) {
		var t = $(o);
		var pb = t.css('padding-bottom');
		pb = parseInt(pb);
		if (isNaN(pb)) {pb = 0;}
		var mh = t.find('.text').css('min-height');
		mh = parseInt(mh);
		if (isNaN(mh)) {mh = 0;}
		t.find('.text').css('min-height', (mh-pb)+'px');
	});

	$('.city-select .select').find('DIV, SPAN').click(function(e) {
		if ($('.city-select').hasClass('opened'))
		{
			$('.city-select').removeClass('opened');
		}
		else
		{
			$('.city-select').addClass('opened');
		}
		$('.city-select .options').slideToggle(function() {
			
		});
	});

	$('.brands A, .materials-menu A, .materials-side A').mouseover(function() {
		if ($(this).find('.gray IMG').size() > 0)
		{
			$(this).children('IMG').hide();
			$(this).find('.gray').show();
		}
	});
	$('.brands A, .materials-menu A, .materials-side A').bind('mouseleave', function() {
		if ($(this).find('.gray IMG').size() > 0)
		{
			$(this).children('IMG').show();
			$(this).find('.gray').hide();
		}
	});

	/*if ($('.main-illu .item').size() > 0)
	{
		var changeMainIllu = function() {
			var current = $('.main-illu .item.first');
			var next = current.next('.item');
			if (next.size() == 0) {next = $('.main-illu .item:eq(0)');}
			current.fadeOut();
			next.fadeIn(function() {
				current.removeClass('first');
				next.addClass('first');
			});
		}
		window.setInterval(changeMainIllu, 5000);
	}*/

	if ($('.gallery').size() > 0)
	{
		var current = 0;
		var nav = '';
		var gallery_items_count = $('.gallery .slider li').size();
		if (gallery_items_count <= 1)
		{
			$('.gallery-nav, .gallery .nav-left, .gallery .nav-right').hide();
		}
		else
		{
			for (i = 0; i < gallery_items_count; i++)
			{
				nav += '<a href="#" rel="page_'+i+'"';
				if (i == 0) {nav += ' class="active"';}
				nav += '></a>';
			}
			$('.gallery-nav').html(nav);

			$('.gallery-nav a').click(function(e) {
				e.preventDefault();
				var t = $(this);
				var num = t.attr('rel').substr(5);

				var next = $('.gallery .slider li:eq('+num+')');
				current = parseInt(num);

				var offset = 0;
				next.prevAll().each(function(i, o) {
					offset += $(o).innerWidth()+2;
				});
				offset = 15-offset;
				$('.gallery .slider ul').animate({marginLeft: offset+'px'});

				$('.gallery-nav a').removeClass('active');
				t.addClass('active');
			});

			$('.gallery .nav-right a').click(function(e) {
				e.preventDefault();
				var next = $('.gallery .slider li:eq('+(current+1)+')');
				if (next.size() == 0)
				{
					next = $('.gallery .slider li:eq(0)');
					current = -1;
				}
				++current;
				var offset = 0;
				next.prevAll().each(function(i, o) {
					offset += $(o).innerWidth()+2;
				});
				offset = 15-offset;
				$('.gallery .slider ul').animate({marginLeft: offset+'px'});

				$('.gallery-nav a').removeClass('active');
				$('.gallery-nav a:eq('+current+')').addClass('active');
			});

			$('.gallery .nav-left a').click(function(e) {
				e.preventDefault();
				var next = $('.gallery .slider li:eq('+(current-1)+')');
				if (next.size() == 0)
				{
					next = $('.gallery .slider li:last-child');
					current = $('.gallery .slider li').size();
				}
				--current;
				var offset = 0;
				next.prevAll().each(function(i, o) {
					offset += $(o).innerWidth()+2;
				});
				offset = 15-offset;
				$('.gallery .slider ul').animate({marginLeft: offset+'px'});

				$('.gallery-nav a').removeClass('active');
				$('.gallery-nav a:eq('+current+')').addClass('active');
			});
		}
	}

	$('.gallery li A').click(function(e) {
		e.preventDefault();

		var selected = $(this);

		var gallery = $(this).parents('.gallery');

		$('body').append('<div class="overlay" style="display: none"></div>');
		$('body').append('<div class="gallery-wnd" style="display: none;"><div class="wnd"><a href="#" class="close"></a><div class="slider"></div></div></div>');

		var wnd = $('.gallery-wnd .wnd');

		$('.gallery-wnd').css({top: gallery.offset().top+'px'});
		wnd.append('<div class="controls"><div class="prev"><a href="#"></a></div><div class="next"><a href="#"></a></div><div class="info"><span class="current"></span> θη <span class="total"></span></div></div>');
		wnd.find('.current').html(parseInt(selected.attr('rel').substr(5))+1);
		wnd.find('.total').html(gallery.find('li').size());

		wnd.find('.close').click(function(e) {
			e.preventDefault();
			$('.overlay').fadeOut();
			$('.gallery-wnd').fadeOut(function() {
				$('.overlay, .gallery-wnd').remove();
			});
		});

		wnd.find('.next a').click(function(e) { e.preventDefault(); });
		wnd.find('.prev a').click(function(e) { e.preventDefault(); });
		wnd.find('.next').click(function(e) {
			e.preventDefault();
			var t = $(this);
			var gallery = t.parents('.gallery-wnd');
			var current = gallery.find('.item.first');
			var next = current.next('.item');

			var new_number = gallery.find('.current').html();
			new_number = parseInt(new_number)+1;

			if (next.size() == 0)
			{
				next = gallery.find('.item:eq(0)');
				new_number = 1;
			}

			gallery.find('.current').html(new_number);

			current.fadeOut(function() {
				current.removeClass('first');
				next.fadeIn(function() {
					next.addClass('first');
				});
			});
		});
		wnd.find('.prev').click(function(e) {
			e.preventDefault();
			var t = $(this);
			var gallery = t.parents('.gallery-wnd');
			var current = gallery.find('.item.first');
			var next = current.prev('.item');

			var new_number = gallery.find('.current').html();
			new_number = parseInt(new_number)-1;

			if (next.size() == 0)
			{
				next = gallery.find('.item').last();
				new_number = gallery.find('.item').size();
			}

			gallery.find('.current').html(new_number);

			current.fadeOut(function() {
				current.removeClass('first');
				next.fadeIn(function() {
					next.addClass('first');
				});
			});
		});

		var items_code = '';
		gallery.find('li a').each(function(i, o) {
			items_code += '<div class="item'+('item_'+i == selected.attr('rel') ? ' first' : '')+'"><img src="'+$(o).attr('href')+'" alt="" /></div>';
		});

		wnd.find('.slider').append(items_code);

		$('.overlay').fadeIn();
		$('.gallery-wnd').fadeIn();
	});

	$('.materials .img').each(function(i, o) {
		var block = $(o);
		var img = $(o).find('IMG');
		var offset = (img.width()-block.innerWidth())/2;
		img.css('marginLeft', '-'+offset+'px');
		offset = (img.height()-block.innerHeight())/2;
		img.css('marginTop', '-'+offset+'px');
	});

	$('.gallery-embeded .next A').click(function(e) {e.preventDefault();})
	$('.gallery-embeded .next').click(function(e) {
		e.preventDefault();
		var t = $(this);
		var gallery = t.parents('.gallery-embeded');
		var current = gallery.find('.item.first');
		var next = current.next('.item');

		var new_number = gallery.find('.current').html();
		new_number = parseInt(new_number)+1;

		if (next.size() == 0)
		{
			next = gallery.find('.item:eq(0)');
			new_number = 1;
		}

		gallery.find('.current').html(new_number);

		current.fadeOut(function() {
			current.removeClass('first');
			next.fadeIn(function() {
				next.addClass('first');
			});
		});
	});
	$('.gallery-embeded .prev A').click(function(e) {e.preventDefault();})
	$('.gallery-embeded .prev').click(function(e) {
		e.preventDefault();
		var t = $(this);
		var gallery = t.parents('.gallery-embeded');
		var current = gallery.find('.item.first');
		var next = current.prev('.item');

		var new_number = gallery.find('.current').html();
		new_number = parseInt(new_number)-1;

		if (next.size() == 0)
		{
			next = gallery.find('.item').last();
			new_number = gallery.find('.item').size();
		}

		gallery.find('.current').html(new_number);

		//console.log(current);

		current.fadeOut(function() {
			current.removeClass('first');
			next.fadeIn(function() {
				next.addClass('first');
			});
		});
	});

	//$('.gallery-embeded .item A').fancybox();
	$('.gallery-embeded .item A').click(function(e) {
		e.preventDefault();

		var gallery = $(this).parents('.gallery-embeded');

		$('body').append('<div class="overlay" style="display: none"></div>');
		$('body').append('<div class="gallery-wnd" style="display: none;"><div class="wnd"><a href="#" class="close"></a><div class="slider"></div></div></div>');

		var wnd = $('.gallery-wnd .wnd');

		$('.gallery-wnd').css({top: gallery.offset().top+'px'});
		wnd.append('<div class="controls"><div class="prev"><a href="#"></a></div><div class="next"><a href="#"></a></div><div class="info"><span class="current"></span> θη <span class="total"></span></div></div>');
		wnd.find('.current').html(gallery.find('.current').html());
		wnd.find('.total').html(gallery.find('.total').html());

		wnd.find('.close').click(function(e) {
			e.preventDefault();
			$('.overlay').fadeOut();
			$('.gallery-wnd').fadeOut(function() {
				$('.overlay, .gallery-wnd').remove();
			});
		});

		wnd.find('.next a').click(function(e) { e.preventDefault(); });
		wnd.find('.prev a').click(function(e) { e.preventDefault(); });
		wnd.find('.next').click(function(e) {
			e.preventDefault();
			var t = $(this);
			var gallery = t.parents('.gallery-wnd');
			var current = gallery.find('.item.first');
			var next = current.next('.item');

			var new_number = gallery.find('.current').html();
			new_number = parseInt(new_number)+1;

			if (next.size() == 0)
			{
				next = gallery.find('.item:eq(0)');
				new_number = 1;
			}

			gallery.find('.current').html(new_number);

			current.fadeOut(function() {
				current.removeClass('first');
				next.fadeIn(function() {
					next.addClass('first');
				});
			});
		});
		wnd.find('.prev').click(function(e) {
			e.preventDefault();
			var t = $(this);
			var gallery = t.parents('.gallery-wnd');
			var current = gallery.find('.item.first');
			var next = current.prev('.item');

			var new_number = gallery.find('.current').html();
			new_number = parseInt(new_number)-1;

			if (next.size() == 0)
			{
				next = gallery.find('.item').last();
				new_number = gallery.find('.item').size();
			}

			gallery.find('.current').html(new_number);

			current.fadeOut(function() {
				current.removeClass('first');
				next.fadeIn(function() {
					next.addClass('first');
				});
			});
		});

		var items_code = '';
		gallery.find('.item a').each(function(i, o) {
			items_code += '<div class="item'+($(o).parent().hasClass('first') ? ' first' : '')+'"><img src="'+$(o).attr('href')+'" alt="" /></div>';
		});

		wnd.find('.slider').append(items_code);

		$('.overlay').fadeIn();
		$('.gallery-wnd').fadeIn();
	});

	$('.gallery-embeded .magnify').click(function(e) {e.preventDefault();});
	$('.gallery-embeded .magnify').click(function(e) {
		$('.gallery-embeded .item.first A').click();
	});

	$('.order-btn A').click(function(e) {
		e.preventDefault();
		$('body').append('<div class="overlay"></div>');
		$('.order-form').css({
			left: ($('.order-btn').offset().left-650+$('.order-btn').width())+'px',
			right: 'auto',
			top: $('.order-btn').offset().top+'px'
		});
		$('.order-form .catalog-id INPUT').val($('#catalog_item_id').val());
		$('.overlay').fadeIn();
		$('.order-form').fadeIn();
	});

	$('.order-form .close').click(function(e) {
		e.preventDefault();
		$('.overlay, .order-form').fadeOut(function() {
			$('.overlay').remove();
		});
	});
});
