/**
 * 
 */

$().ready(function(){
	

	$('.border').live({
		mouseenter: function(){
			$(this).css('border','1px solid #E2E2E2');
			var label = $(this).children('.item-label');
			label.show();
				label.animate({
					'top' : '108px'
				}, 'low');
			
		},
		mouseleave: function(){
			$(this).css('border','1px solid #FFF');
			var label = $(this).children('.item-label');
			if ( label.position().top > 108 ) {
				label.hide();
			}
			label.animate({
				'top' : '300px'
			}, 'low');
		}
	});
	
	$('.border-available').live({
		mouseenter: function(){
			$(this).css('border','1px solid #CCC');
		},
		mouseleave: function(){
			$(this).css('border','1px solid #F3F4EE');
		}
	});
	
	$('.item').live('click', function(){
		var $this = $(this)
		var id = $(this).attr('id').replace('item','');
		var block = $(this).parents('.item-row');
		$('.item-detail').slideUp('slow',function(){
			$(this).remove(); 
			$.ajax({
				'url' : '/index.php/site/item',
				'method' : 'POST',
				'data' : { 'id' : id },
				'success' : function(data){
					block.after(data);
					$("html,body").animate(
						{ scrollTop: $this.offset().top },
						1000,
						function(){ $('.item-detail').slideDown()
					});
					$.initHint({
						selector: $('.item-detail')
					});
				}
			})
		})
		return false;
	});
	
	$( '.close', '.item-detail' ).live( 'click', function(){
		$(this).parents('.item-detail').slideUp('slow')
	});
	
	$('.filters > .unselected').hover(
		function(){
			$(this).find('ul.unactive').slideDown();
		},
		function(){
			$(this).find('ul.unactive').slideUp();
		}
	);
	
	$('.filters .unselected a').click(function(){
		
		if ( $(this).hasClass('hide') ) {
			
			$(this).parents('ul').removeClass('unactive');
			$(this).parents('li').siblings('li').slideUp();
			$(this).removeClass('hide').addClass('active');
			var category = $('.filters .left a.active').text();
			var volume = $('.filters .center a.active').text();
			var price = $('.filters .right a.active').text();
			$('.catalog-wrapper').slideUp('low', function(){
				$.ajax({
					'url' : '/index.php/site/filter',
					'type' : 'POST',
					'data' : {'category' : category, 'volume' : volume, 'price' : price},
					beforeSend : function(){
						$('.item-row').remove();
						$('.ajax-loader-wrapper').show();
					},
					success : function(data){
						$('.ajax-loader-wrapper').hide();
						$('.catalog-wrapper').html(data);
						$('.catalog-wrapper').slideDown('low');
					}
				});
			})
		} else if ($(this).hasClass('active')) {
			
			$(this).parents('ul').addClass('unactive');
			$(this).parents('ul').slideUp();
			$(this).parents('li').siblings('li').show();
			$(this).removeClass('active').addClass('hide');
			var category = $('.filters .left a.active').text();
			var volume = $('.filters .center a.active').text();
			var price = $('.filters .right a.active').text();
			$('.catalog-wrapper').slideUp('low', function(){
				$.ajax({
					'url' : '/index.php/site/filter',
					'type' : 'POST',
					'data' : {'category' : category, 'volume' : volume, 'price' : price},
					beforeSend : function(){
						$('.item-row').remove();
						$('.ajax-loader-wrapper').show();
					},
					success : function(data){
						$('.ajax-loader-wrapper').hide();
						$('.catalog-wrapper').html(data);
						$('.catalog-wrapper').slideDown('low');
					}
				});
			});
			
		}
		return false;

	});

	
})
