function delete_cat(cat_id){
	$('#cat_'+cat_id).remove();
};

function catalog_filter(filter){
	if(filter != undefined){
		$('#categories_filter').val(filter);
	}
	else{
		$('#categories_filter').val('');
	}
	build_catalog();
}

function state_filter(filter){
	if(filter != undefined){
		$('#state_filter').val(filter);
	}
	else{
		$('#categories_filter').val('');
		$('#state_filter').val('');
		$('#catalog_category a').removeClass('selected_category');
		$('a#for_all').addClass('selected_filter');
		$('#category_caption').html(':');
	}
	build_catalog();
}

function build_catalog(){
	$('#catalog_content').prepend('<li class="loading_catalog"><p><img src="/img/loading_catalog.gif" />Загрузка каталога...</p></li>');
	var catalog_filter = $('#categories_filter').val();
	var state_filter = $('#state_filter').val();
	var present_price = '';
	$.ajax({
		type: 'POST',
		url: '/presents/ajax/show_catalog',
		data: 'show=catalog&catalog_filter='+catalog_filter+'&state_filter='+state_filter,
		cache: false,
		timeout: 10000,
		success: function(response){
			response = unescape(response);
			var response = response.split("|");
			var responseType = response[0];
			var catalogData = eval('('+response[1]+')');
			if(responseType=="success"){
				$('#catalog_content').empty();
				for (i = 0; i < catalogData.length; i++){
					if($('#state_filter').val() == 'by_price' && catalogData[i]['price'] != 0 ){
						present_price = '<span class="catalog_price">'+catalogData[i]['price']+'</span>';
					}
					$('#catalog_content').append('<li id="present_'+catalogData[i]['id']+'" class="clearfix"><a href="javascript:show_present('+catalogData[i]['id']+');">'+catalogData[i]['title']+'</a>'+present_price+'</li>');
				}
			}else if(responseType=="error"){
				
			}else{
				$('#catalog_content').html('<li>Ошибка загрузки каталога.</li>');
			}
		}
	});
}

$(document).ready(function(){
	$('#present_price').attr('disabled', $('#present_price_individual').is(':checked'));
	$('#present_price_individual').click(function(){
		$('#present_price').attr('disabled', $('#present_price_individual').is(':checked'));
	});
	$('#add_present_category').click(function(){
		if($('#present_category').attr("value") > 0){
			$('#categories_list').append('<li id="cat_'+$('#present_category').attr("value")+'"><a href="javascript:void(0)" onclick="javascript:delete_cat('+$('#present_category').attr("value")+');"><img src="/img/tag_blue_delete.png" title="Удалить" alt="Удалить" /></a>'+$('#present_category option:selected').attr("text")+'<input type="hidden" name="categories_id[]" value="'+$('#present_category').attr("value")+'" /></li>');
		}
	});
	$('#quote_hover').click(function(){
		$('#full_description').hide('normal');
		$('#present').fadeOut('normal', function(){
			$('#quote-box')
				.unbind('mouseover')
				.unbind('mouseout')
				.removeAttr('style')
				.animate({'width': '560px', 'marginRight': '100px'}, 'slow', function(){
					$(this).addClass('show_quote_box');
				});
		});
	});
	$('#catalog_state li').click(function(){
		$(this).each(function(){
			$('#catalog_state li').removeClass('selected-tab');
		});
		$(this).addClass('selected-tab');
	});
	$('#catalog_state li a').click(function(){
		$('#state_caption').html($(this).attr('title'));
	});
	$('#catalog_category a').click(function(){
		$(this).each(function(){
			$('#catalog_category a').removeClass('selected_category');
		});
		$(this).addClass('selected_category');
		$('#category_caption').html(' из набора &laquo;'+$(this).html()+'&raquo;:');
	});
	$('#categories_filter').val('');
	
	/*$("#calendar").datepicker({
		dateFormat: 'yy-mm-dd',
		onChangeMonthYear: function(year, month) {
			$.ajax({
				type: 'POST',
				url: '/presents/ajax/show_holiday',
				data: 'show=holidays_by_month&month='+month,
				cache: false,
				success: function(response){
					response = unescape(response);
					var response = response.split("|");
					var responseType = response[0];
					var holidays = eval('('+response[1]+')');
					if(responseType=="success"){
						for (i=0; i<holidays.length;i++){
							$("#calendar a").each(function(){
								if ($(this).text() == holidays[i]['day']){
									$(this).addClass('ui-state-active');
									$(this).attr('title', holidays[i]['title']);
								}
							});
						};
					}
				}
			});
		},
		onSelect: function(dateText) {
			$("#calendar_date").attr('value', dateText);
			$.ajax({
				type: 'POST',
				url: '/presents/ajax/show_holiday',
				data: 'show=holiday&date='+dateText,
				cache: false,
				success: function(response){
					response = unescape(response);
					var response = response.split("|");
					var responseType = response[0];
					var dateData = eval('('+response[1]+')');
					$('#holiday').empty();
					if(responseType=="success"){
						$('#holiday').html('<p>'+dateData+'</p>');
					}
				}
			});
			$.ajax({
				type: 'POST',
				url: '/presents/ajax/show_holiday',
				data: 'show=holidays&date='+dateText,
				cache: false,
				success: function(response){
					response = unescape(response);
					var response = response.split("|");
					var responseType = response[0];
					var holidays = eval('('+response[1]+')');
					if(responseType=="success"){
						for (i=0; i<holidays.length;i++){
							$("#calendar a").each(function(){
								if ($(this).text() == holidays[i]['day']){
									$(this).addClass('ui-state-active');
									$(this).attr('title', holidays[i]['title']);
								}
							});
						};
					}
				}
			});
		}
	});*/

});
