function initCarousel(id) {
	$('#' + id).jcarousel({
		auto: 0.01,
		scroll: 1,
		wrap: 'circular',
		animation: 3000,
		initCallback: function(carousel) {
			carousel.clip.hover(function() {
				carousel.stopAuto();
			}, function() {
				if (carousel.options.animation != 500) {
					carousel.startAuto();
				}
			});
			carousel.buttonNext.unbind('click').bind('click', function() {
				carousel.stopAuto();
				carousel.options.animation = 500;
				carousel.next();
			});
			carousel.buttonPrev.unbind('click').bind('click', function() {
				carousel.stopAuto();
				carousel.options.animation = 500;
				carousel.prev();
			});
			carousel.list.find('a').hover(function() {
				var $self = $(this);
				$self.find('img').animate({
					height: 174, 
					width: 232
				});
			}, function() {
				var $self = $(this);
				$self.find('img').animate({
					height: 168, 
					width: 224
				});
			});
		}
	});
}

function initGallery() {
	$('#carousel').find('a').bind('mouseover', function() {
		var $container = $('#big_image');
		var $self = $(this);

		var $caption = $container.find('span');
		var $image = $container.find('img');

		$container.stop().hide().css({
			height: '150px', 
			width: '200px'
		});
		$caption.hide();
		$caption.html($self.attr('alt'));
		$image.attr('src', $self.attr('_href'));
		$image.parent().attr('href', $self.attr('href'));
		var left = $self.position().left + $self.parent().parent().position().left;
		var top = $self.position().top;
		$container.css({
			left: left + 49, 
			top: top - 1
		});

		$container.show();
		$container.stop().animate({
			height: '270px', 
			width: '360px', 
			left: left - 35, 
			top: top - 60
		});
		if ($caption.html()) {
			$caption.show();
		}
	});
	$('#big_image').bind('mouseleave mouseout', function() {
		$(this).stop().hide();
	});
}

function initSlider() {
	$('#slider').aviaSlider({
		autorotationSpeed: 2,
		blockSize: {
			height: 80,
			width: 80
		},
		display: 'diagonaltop',
		switchMovement: false,
		transition: 'slide'
	});
}

var x = x || {};

x.extend = function(first, second){
	for (var key in second){
		first[key] = second[key];
	}
};  

x.Filter = function(o) {
	this._options = {
		containerId: 'filter',
		itemsContainerId: 'items'
	};
	x.extend(this._options, o);

	this._currentPriceInterval = null;
	this._currentSquareInterval = null;
	this._dom = {};
	this._pricesIntervals = [];
	this._squaresIntervals = [];

	this._dom.$container = $('#' + this._options.containerId);
	this._dom.$items = $('#' + this._options.itemsContainerId);

	var self = this;
	this._dom.$container.find('a[price]').each(function() {
		var $self = $(this);
		var price = self._parseInterval($self.attr('price'));
		if (price != null) {
			self._pricesIntervals.push(price);
			$self.bind('click', function() {
				self._selectPrice(this);
				return false;
			});
		}
	});
	this._dom.$container.find('a[square]').each(function() {
		var $self = $(this);
		var square = self._parseInterval($self.attr('square'));
		if (square != null) {
			self._squaresIntervals.push(square);
			$self.bind('click', function() {
				self._selectSquare(this);
				return false;
			});
		}
	});
	this._dom.$container.find('.reset').click(function() {
		self._reset();
		return false;
	});
}

x.Filter.prototype = {
	_activateFilter: function() {
		this._dom.$container.find('a.price, a.square').each(function() {
			$(this).addClass('active');
		});
	},
	_assembleInterval: function(interval) {
		var result = '';
		if (interval != null) {
			if (interval.minValue == false) {
				result = '<' + interval.maxValue;
			} else if (interval.maxValue == false) {
				result = '>' + interval.minValue;
			} else {
				result = interval.minValue + '-' + interval.maxValue;
			}
		}
		return result;
	},
	_deactivateFilter: function() {
		this._dom.$container.find('a.price, a.square').each(function() {
			$(this).removeClass('active');
		});
	},
	_checkItem: function(price, priceInterval, square, squareInterval) {
		var result = null;
		if ((price != null) && (priceInterval != null)) {
			if (
				((priceInterval.minValue == false) && (price <= priceInterval.maxValue))
				|| ((priceInterval.maxValue == false) && (price >= priceInterval.minValue))
				|| ((price >= priceInterval.minValue) && (price <= priceInterval.maxValue))
				) {
				result = true;
			} else {
				result = false;
			}
		}
		if ((result == null) || result) {
			if ((square != null) && (squareInterval != null)) {
				if (
					((squareInterval.minValue == false) && (square <= squareInterval.maxValue))
					|| ((squareInterval.maxValue == false) && (square >= squareInterval.minValue))
					|| ((square >= squareInterval.minValue) && (square <= squareInterval.maxValue))
					) {
					result = true;
				} else {
					result = false;
				}
			}
		}
		return (result != null) ? result : false;
	},
	_getPriceInterval: function(value) {
		for (var key in this._pricesIntervals) {
			var price = this._pricesIntervals[key];
			if ((price.minValue == false) && (value <= price.maxValue)) {
				return price;
			} else if ((price.maxValue == false) && (value >= price.minValue)) {
				return price;
			} else if ((value >= price.minValue) && (value <= price.maxValue)) {
				return price;
			}
		}
		return null;
	},
	_getSquareInterval: function(value) {
		for (var key in this._squaresIntervals) {
			var square = this._squaresIntervals[key];
			if ((square.minValue == false) && (value <= square.maxValue)) {
				return square;
			} else if ((square.maxValue == false) && (value >= square.minValue)) {
				return square;
			} else if ((value >= square.minValue) && (value <= square.maxValue)) {
				return square;
			}
		}
		return null;
	},
	_parseInterval: function(param) {
		var regExp = /^([0-9]*)(\-|\<|\>)([0-9]+)$/;
		var matches = param.match(regExp);
		if (matches != null) {
			if (matches[2] == '-') {
				return this._setInterval(parseInt(matches[1]), parseInt(matches[3]));
			} else if (matches[2] == '<') {
				return this._setInterval(false, parseInt(matches[3]));
			} else if (matches[2] == '>') {
				return this._setInterval(parseInt(matches[3]), false);
			}
		}
		return null;
	},
	_reset: function() {
		this._currentPriceInterval = null;
		this._currentSquareInterval = null;
		this._dom.$items.find('*[price]').each(function() {
			$(this).show();
		});
		this._dom.$container.find('a[price], a[square]').each(function() {
			$(this).removeClass('disabled').removeClass('selected');
		});
		this._deactivateFilter();
	},
	_selectPrice: function(element) {
		var $element = $(element);
		var priceInterval = this._parseInterval($element.attr('price'));
		if (priceInterval != null) {
			var self = this;
			var squaresIntervals = [];
			this._dom.$items.find('*[price]').each(function() {
				var $item = $(this);
				var price = Math.round(parseFloat($item.attr('price')));
				var square = Math.round(parseFloat($item.attr('square')));
				var squareInterval = null;
				if (self._checkItem(price, priceInterval, square, self._currentSquareInterval)) {
					$item.show();
					if (self._currentSquareInterval != null) {
						squareInterval = self._currentSquareInterval;
					} else {
						squareInterval = self._getSquareInterval(square);
					}
				} else {
					$item.hide();
				}
				if (squareInterval != null) {
					var exists = false;
					for (var key in squaresIntervals) {
						if ((squaresIntervals[key].minValue == squareInterval.minValue) && (squaresIntervals[key].maxValue == squareInterval.maxValue)) {
							exists = true;
						}
					}
					if (!exists) {
						squaresIntervals.push(squareInterval);
					}
				}
			});
			this._currentPriceInterval = priceInterval;
			$element.removeClass('disabled');
			this._dom.$container.find('a[price]').addClass('disabled');
			this._dom.$container.find('a[price="' + self._assembleInterval(priceInterval) + '"]').removeClass('disabled');
			this._dom.$container.find('a[square]').addClass('disabled');
			if (!squaresIntervals.length && (this._currentSquareInterval != null)) {
				squaresIntervals.push(this._currentSquareInterval);
			}
			if (squaresIntervals.length) {
				for (var key in squaresIntervals) {
					this._dom.$container.find('a[square="' + self._assembleInterval(squaresIntervals[key]) + '"]').removeClass('disabled');
				}
			}
			this._setSelectedIntervals();
			this._activateFilter();
		}
		return true;
	},
	_selectSquare: function(element) {
		var $element = $(element);
		var squareInterval = this._parseInterval($element.attr('square'));
		if (squareInterval != null) {
			var self = this;
			var pricesIntervals = [];
			this._dom.$items.find('*[square]').each(function() {
				var $item = $(this);
				var price = Math.round(parseFloat($item.attr('price')));
				var priceInterval = null;
				var square = Math.round(parseFloat($item.attr('square')));
				if (self._checkItem(price, self._currentPriceInterval, square, squareInterval)) {
					$item.show();
					if (self._currentPriceInterval != null) {
						priceInterval = self._currentPriceInterval;
					} else {
						priceInterval = self._getPriceInterval(price);
					}
				} else {
					$item.hide();
				}
				if (priceInterval != null) {
					var exists = false;
					for (var key in pricesIntervals) {
						if ((pricesIntervals[key].minValue == priceInterval.minValue) && (pricesIntervals[key].maxValue == priceInterval.maxValue)) {
							exists = true;
						}
					}
					if (!exists) {
						pricesIntervals.push(priceInterval);
					}
				}
			});
			this._currentSquareInterval = squareInterval;
			$element.removeClass('disabled');
			this._dom.$container.find('a[square]').addClass('disabled');
			this._dom.$container.find('a[square="' + self._assembleInterval(squareInterval) + '"]').removeClass('disabled');
			this._dom.$container.find('a[price]').addClass('disabled');
			if (!pricesIntervals.length && (this._currentPriceInterval != null)) {
				pricesIntervals.push(this._currentPriceInterval);
			}
			if (pricesIntervals.length) {
				for (var key in pricesIntervals) {
					this._dom.$container.find('a[price="' + self._assembleInterval(pricesIntervals[key]) + '"]').removeClass('disabled');
				}
			}
			this._setSelectedIntervals();
			this._activateFilter();
		}
		return true;
	},
	_setInterval: function(minValue, maxValue) {
		var result = {};
		result.minValue = minValue;
		result.maxValue = maxValue;
		return result;
	},
	_setSelectedIntervals: function() {
		this._dom.$container.find('a').removeClass('selected');
		if (this._currentPriceInterval != null) {
			this._dom.$container.find('a[price="' + this._assembleInterval(this._currentPriceInterval) + '"]').addClass('selected');
		}
		if (this._currentSquareInterval != null) {
			this._dom.$container.find('a[square="' + this._assembleInterval(this._currentSquareInterval) + '"]').addClass('selected');
		}
		return true;
	}
}
