$(function() {
	$('.nav-td .level-0').find('ul').hide();

	var links		= $('.nav-td a');
	var albumlinks	= $('.nav-td a.album');
	var carousel	= $('#carousel');

	var albumdata	= {};
	var lastindex	= 3;

	links.click(function() {
		if ($(this).html() == 'Образцы стекла') {
			location.href = '/glass';
			return false;
		}

		if ($(this).html() == 'Пожаробезопасное стекло MGPyrobel') {
			location.href = '/glass/mgpyrobel';
			return false;
		}
		
		$(this).parent().parent().find('li').removeClass('selected');
		$(this).parent().addClass('selected');

		//Если произошел клик по категории
		if ($(this).parent().find('ul').length > 0) {
			var ul		= $(this).parent().find('ul:first');
			var display	= ul.css('display');

			//Если в категории только один альбом
			if ($('li', ul).length == 1 && ul.hasClass('level-1')) {
				$(this).parent().parent().find('li:not(#' + $(this).parent().attr('id') + ')').find('ul').hide();
				$('a', ul).click();
			} else {
				if (display == 'none') {
					$(this).parent().parent().find('li:not(#' + $(this).parent().attr('id') + ')').find('ul').hide();

					ul.show();
				} else {
					ul.hide();
					ul.parent().removeClass('selected');
					ul.parent().find('ul').hide();
				}
			}
		}
	});

	albumlinks.click(function() {
		var id = $(this).attr('class').split('-');
		id = id[1];
		
		var params = {
			model: $.json.serialize({
				ctx: {
					cmd: 'gallery.ShowAlbum'
				},
				data: {
					id: id
				}
			})
		};

		$.getJSON('/commands', params, function(data, textStatus) {
			albumdata = data.data;
			lastindex = 3;

			var total = albumdata.photos.length;

			var html = '<table>'
					+ '<tr>'
					+ '	<td class="image">'
					+ '		<img src="' + albumdata.photos[0].url + '" height="380">';

			html += '		<div class="description">' + albumdata.album.name + '<br />' + albumdata.album.description + '<br />' + albumdata.photos[0].description + '</div>';

			html += '	</td>'
					+ '</tr>'
					+ '<tr>'
					+ '	<td class="previews">'
					+ '		<table>'
					+ '		<tr>';

			if (total > 4)
				html += '			<td class="leftbtn"><img src="/v/i/left.gif" class="button buttonleft" width="31" height="31"></td>';
			else
				html += '			<td class="leftbtn"></td>';
			
			html += '			<td>';

			for (var i = 0; i < albumdata.photos.length; i++) {
				html += '<img src="' + albumdata.photos[i].coverUrl + '" ';
				
				if (i == 0)
					html += ' class="current" ';

				if (i > 3)
					html += ' class="hidden" ';

				html += ' align="center">';
			}

			html += '			</td>';

			if (total > 4)
				html += '			<td class="rightbtn"><img src="/v/i/right.gif" class="button buttonright" width="31" height="31"></td>';
			else
				html += '			<td class="rightbtn"></td>';

			html += '		</tr>'
					+ '		</table>'
					+ '	</td>'
					+ '</tr>'
					+ '</table>';

			carousel.html(html);

			$('.previews', carousel).find('img:not(.button)').hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			}).click(function() {
				var index	= $('.previews img', carousel).index(this);
				if (albumdata.photos.length > 4)
					index -= 1;
				
				var src		= albumdata.photos[index].url;

				var description = '';
				if (albumdata.photos[index].description)
					description = albumdata.photos[index].description;

				var html	= '		<img src="' + src + '" height="380">'
							+ '		<div class="description">' + albumdata.album.name + '<br />' + albumdata.album.description + '<br />' + description + '</div>';

				$('.image').empty();
				$('.image').html(html);

				$(this).parent().find('img').removeClass('current');
				$(this).addClass('current');
			});

			$('.buttonright').click(function() {
				lastindex += 1;

				if (lastindex == albumdata.photos.length - 1)
					$(this).hide();

				$('.previews', carousel).find('img:not(.button):visible:first').hide();
				$('.previews', carousel).find('img:not(.button):eq(' + lastindex + ')').show();

				$('.buttonleft').show();
			});

			$('.buttonleft').click(function() {
				lastindex -= 1;

				if (lastindex == 3)
					$(this).hide();

				$('.previews', carousel).find('img:not(.button):visible:last').hide();
				$('.previews', carousel).find('img:not(.button):eq(' + (lastindex - 3) + ')').show();

				$('.buttonright').show();
			});
		});
	});

	
	$('.nav-td').show();

});
