var favorites = [];
var comparison = [];
$(function() {
	if((document.baseURI || document.URL).indexOf('catalog/') != -1) {
		$.getJSON('/catalog/favorites/', function(json) {
			favorites = json;
			$('#favorites-link').html('Избранное (' + favorites.length +')');
		});
		$.getJSON('/catalog/comparison/', function(json) {
			comparison = json;
			$('#comparison-link').html('Список сравнения (' + comparison.length +')');
		});
	}

	$('.ttip').ttip();
	$('#search_query').focus(function() {
		if ($(this).attr('value') == 'Поиск по сайту...') {
			$(this).attr('value', '');
		}
	});
	$('#search_query').blur(function() {
		if (!$(this).attr('value') || $(this).attr('value') == '') {
			$(this).attr('value', 'Поиск по сайту...');
		}
	});
	$('.toolbar .icons a').click(function() {
		$('#catalog_block').removeClass();
		$('#catalog_block').addClass(this.id);
		$('.toolbar .icons a').removeClass('active');
		$(this).addClass('active');
		$('.toolbar .order a').each(function() {
			var dtype = $('.toolbar .icons a.active').attr('id').replace('catalog-', '');
			$(this).attr('href', $(this).attr('href').replace('display=thumbs', 'display=' + dtype));
			$(this).attr('href', $(this).attr('href').replace('display=small-list', 'display=' + dtype));
			$(this).attr('href', $(this).attr('href').replace('display=large-list', 'display=' + dtype));
		});
		$.get('/catalog/config/', {catalog_display: this.id.replace('catalog-', ''), cache: Math.random()});
		return false;
	});
	$('.catalog-image').each(function(index) {
		var src = this.src;
		if ($.browser.opera && $.browser.version < 9.5) {
			this.src = '/app/resources/default/images/s.gif?cache=' + Math.random();
		} else {
			this.src = '/app/resources/default/images/s.gif';
		}
		$(this).css('background-image', 'url(' + src + ')');
		$(this).addClass('jq');
		return;
	});
	$('#catalog_block dd.icons a').click(function() {
		$.getJSON(this.href, { cache: Math.random() }, function(json) {
			if (json.result) {
				$('#' + json.id).removeClass();
				$('#' + json.id).addClass(json.result.className);
				$('#' + json.id + ' span').html(json.result.text);
				$('#' + json.id).attr('title', $('#' + json.id + ' span').html());
				if (json.group == 'favorites') {
					favorites = json.favorites;
					$('#favorites-link').html('Избранное (' + favorites.length + ')');
				} else {
					comparison = json.comparison;
					$('#comparison-link').html('Список сравнения (' + comparison.length + ')');
				}
			}
		});
		return false;
	});
	$('.toggle').click(function() {
		var level = ($(this).attr('level')) ? $(this).attr('level') : 1;
		var start = $(this);
		for(var i=0; i<level; i++) {
			start.parent().toggleClass('open');
			start = start.parent();
		}
		$(this).toggleClass('open');
		return false;
	});
	$('.dtoggle').click(function() {
		$('.dtoggle + .opened').slideUp('normal');
		if ($(this).attr('class') == 'dtoggle opened') {
			$('.dtoggle + .opened').removeClass('opened');
			$('a.opened').removeClass('opened');
			return false;
		}
		$('.dtoggle + .opened').removeClass('opened');
		$('a.opened').removeClass('opened');
		$(this).parent().find('div').addClass('opened');
		$(this).parent().find('div').slideDown('normal');
		$(this).addClass('opened');
		return false;
	});
});

