$(document).ready(function(){
	addAjax();
	addLabel();
	addFilter();
	$('#gallery-list a').lightBox({
		imageLoading: '/bitrix/templates/.default/images/lightbox/loading.gif',
		imageBtnClose: '/bitrix/templates/.default/images/lightbox/close.gif',
		imageBtnPrev: '/bitrix/templates/.default/images/lightbox/prev.gif',
		imageBtnNext: '/bitrix/templates/.default/images/lightbox/next.gif',
		txtImage: 'Фото',
		txtOf: 'из'
	});
});

function addFilter ()
{
	var $filterInputs = $('.filter :input');
	
	$filterInputs.change(function(){
		var $obj = $(this);
		var value = $obj.val();
		var id = $obj.attr('id');
		var match = id.match(/^filter\['(.+)'\]/);
		var filterName = '';
		if (match && match[1]) {
			filterName = match[1];
		}
		if (filterName == '' || value == '') {
			return false;
		}
		var $allColObjs = $('.small-table td[name ^= ' + filterName + '-]');
		var needUpdate = false;
		
		if ($obj.is(':checkbox') && $obj.is(':checked')) {
			$.cookie($obj.attr('name'), null);
		} else {
			$.cookie($obj.attr('name'), $obj.val());
		}
		if (filterName == 'interval' && value >= 0) {
			if (value == 0) {
				$allColObjs.each(function(){
					var $colObj = $(this);
					if (!$colObj.hasClass('show')) {
						$colObj.addClass('show');
						needUpdate = true;
					}
				});
			} else {
				$allColObjs.each(function(){
					var $colObj = $(this);
					var valMatch = $colObj.attr('name').match(/^interval-(\d+)$/);
					if (valMatch && valMatch[1]) {
						var objVal = parseInt(valMatch[1]);
						if (objVal > parseInt(value)) {
							$colObj.removeClass('show');
							needUpdate = true;
						} else {
							if (!$colObj.hasClass('show')) {
								$colObj.addClass('show');
								needUpdate = true;
							}
						}
					}
				});
			}
		} else {
			if (!$obj.is(':checkbox') && value == 0) {
				$allColObjs.each(function(){
					var $colObj = $(this);
					if (!$colObj.hasClass('show')) {
						$colObj.addClass('show');
						needUpdate = true;
					}
				});
			}
			if (!$obj.is(':checkbox') && value != 0) {
				var name = filterName + '-' + value;
				
				$allColObjs.each(function(){
					var $colObj = $(this);
					if ($colObj.attr('name') == name) {
						if (!$colObj.hasClass('show')) {
							$colObj.addClass('show');
							needUpdate = true;
						}
					} else {
						$colObj.removeClass('show');
						needUpdate = true;
					}
				});
			}
			if ($obj.is(':checkbox')) {
				var name = filterName + '-' + value;
				
				if ($obj.is(':checked')) {
					$allColObjs.each(function(){
						var $colObj = $(this);
						if ($colObj.attr('name') == name && !$colObj.hasClass('show')) {
							$colObj.addClass('show');
							needUpdate = true;
						}
					});
				} else {
					$allColObjs.each(function(){
						var $colObj = $(this);
						if ($colObj.attr('name') == name) {
							$colObj.removeClass('show');
							needUpdate = true;
						}
					});
				}
			}
		}
		if (needUpdate) {
			$('.small-table tr').each(function(){
				var $tr = $(this);
				var $tds = $tr.find('td');
				var show = true;
				
				$tds.each(function(){
					var $td = $(this);
					if (!$td.hasClass('show')) {
						show = false;
						return true;
					}
				});
				if (show) {
					$tr.show();
				} else {
					$tr.hide();
				}
			});
		}
	});
	$filterInputs.each(function(){
		var $obj = $(this);
		
		if ($obj.is(':checkbox') && $.cookie($obj.attr('name')) != null) {
			$obj.attr('checked', '');
		}
		if ($.cookie($obj.attr('name')) != null) {
			$obj.val($.cookie($obj.attr('name')));
			$obj.change();
		}
	});
}

function addLabel ()
{
	$('form .row input').each(function(){
		var $input = $(this);
		if ($input.val() != '') {
			$(this).parent().children('label').hide();
		}
	});
	$('form .row input, form .row label').focus(function(){
		$(this).parent().children('label').hide();
	}).blur(function(){
		var $input = $(this);
		if ($input.val() == '') {
			$input.parent().children('label').show();
		}
	});
}

function addAjax()
{
	var $ajaxLink = $('.body .ajax h1 a, .body .ajax span a');
	var match = window.location.hash.match(/^#(.+)$/);
	var id = 0;
	if (match && match[1]) {
		id = match[1];
	}
	$ajaxLink.each(function(){
		var $a = $(this);
		var $div = $a.parent().parent();
		var $text = $div.children('p, div.ajax-content');
		
		if ($div.attr('id') == id) {
			$a.addClass('open');
		}
		if (!$a.hasClass('open')) {
			$text.hide();
		}
		$a.bind('click', function() {
			if ($a.hasClass('open')) {
				$text.stop(true, true).hide('slow');
			} else {
				$text.stop(true, true).show('slow');
			}
			$a.toggleClass('open');
			//return false;
		});
	});
}
