
function change_tab(vt)
{
	if (vt == 'cars')
	{
		$('div#cars').css('display', 'block');
		$('div#bikes').css('display', 'none');

		$('li#tcars').attr('class', 'tabon');
		$('li#tbikes').attr('class', 'taboff');
	}
	else
	{
		if (vt == 'bikes')
		{
			$('div#cars').css('display', 'none');
			$('div#bikes').css('display', 'block');

			$('li#tcars').attr('class', 'taboff');
			$('li#tbikes').attr('class', 'tabon');
		}
		else
		{
			$('div#cars').css('display', 'block');
			$('div#bikes').css('display', 'none');

			$('li#tcars').attr('class', 'tabon');
			$('li#tbikes').attr('class', 'taboff');
		}
	}
}

function all_checkbox()
{
	$("#all").click(function() {
		$("input[@type='checkbox']").not("#all").each(
			function() {
				this.checked = !this.checked;
			});
	});
}

function Filters(cookieDomain, ajax_url, params, logged) {
	this.cookieDomain = cookieDomain;
	this.OPENED = 1;
	this.CLOSED = 2;
	this.LESS = 3;
	this.MORE = 4;
	this.HIDDEN = 5;
	this.params = params;
	this.url = ajax_url;
	this.enter = 13;
	this.logged = logged;
}

Filters.prototype.init = function() {
	this.prepareR();
	this.prepareMore();
	this.prepareClose();
	this.prepareToggle();
	this.prepareAddFilter();
	this.prepareSearch();
	this.prepareCatchEnter();
	var self = this;
	$("input[name='filter']").click(function(){
		self.query();
	});
}

Filters.prototype.prepareR = function() {
	switch (this.params['r'])
	{
		case 'search_min_params':
			this.params['r'] = 'search';
			break;
		case 'browse_min_params':
		case 'browse_other':
			this.params['r'] = 'browse';
			break;
	}
}

Filters.prototype.prepareCatchEnter = function() {
	var self = this;
	$("#filters_details").find("input[type='text']").keypress(function(e) {
		if (e.which == self.enter) {
			self.query();
		}
	});
}


Filters.prototype.changeClass = function(element, classToChange, newClass) {
	$(element).removeClass(classToChange);
	$(element).addClass(newClass);
}

Filters.prototype.prepareMore = function(){
	var self = this;
	$("ul > .see_more").each(function(){
		$(this).click(function(){
			self.changeClass($(this).parent().parent(), "filters_content", "filters_content_hidden");
			self.changeClass($(this).parent().parent().next(), "filters_content_hidden", "filters_content");
			self.saveState($(this).parent().parent().parent().parent(), self.MORE);
			return false;
		});
	});
	
	$("ul > .see_less").each(function(){
		$(this).click(function(){
			self.changeClass($(this).parent().parent().prev(), "filters_content_hidden", "filters_content");
			self.changeClass($(this).parent().parent(), "filters_content", "filters_content_hidden");
			self.saveState($(this).parent().parent().parent().parent(), self.LESS);
			return false;
		});
	});
}

Filters.prototype.prepareClose = function() {
	var self = this;
	$(".filter_close").each(function() {
		var objectId = $(this).parent().parent().parent().attr("id"); 
		if ($.cookie(objectId) == self.CLOSED){
			$(this).parent().parent().parent().fadeOut("slow");
		}
		else {
			$(this).parent().parent().parent().fadeIn("slow");
		}
		$(this).click(function(){
				$(this).parent().parent().parent().fadeOut("slow");
				self.saveState($(this).parent().parent().parent(), self.CLOSED);
			});
	});
	
}

Filters.prototype.prepareToggle = function() {
	var self = this;
	$(".headtoggle > h3 > .filter_arrow").each(function(){
		$(this).click(function(){
			if ($(this).parent().parent().next().css("display") == "none") {
				self.headerFadeIn($(this).parent().parent().next(), $(this).children("img")); 
				self.saveState($(this).parent().parent().parent(), self.OPENED);
			} 
			else {
				self.headerFadeOut($(this).parent().parent().next(), $(this).children("img"));
				self.saveState($(this).parent().parent().parent(), self.HIDDEN);
			}
		});
	});
	$(".headtoggle > h3 > .filter_text").each(function(){
		$(this).click(function(){
			if ($(this).parent().parent().next().css("display") == "none") {
				self.headerFadeIn($(this).parent().parent().next(), $(this).prev().children("img")); 
				self.saveState($(this).parent().parent().parent(), self.OPENED);
			} 
			else {
				self.headerFadeOut($(this).parent().parent().next(), $(this).prev().children("img"));
				self.saveState($(this).parent().parent().parent(), self.HIDDEN);
			}
		});
	});
}

Filters.prototype.headerFadeIn = function(element, replaceImage) {
	element.fadeIn("slow");
	$(replaceImage).replaceWith($("<img/>").attr("src", global_path + "/images/arr1.gif"));
}

Filters.prototype.headerFadeOut = function(element, replaceImage) {
	element.fadeOut("slow");
	replaceImage.replaceWith($("<img/>").attr("src", global_path + "/images/arr1l.gif"));
}

Filters.prototype.saveState = function(object, state) {
	var objectId = $(object).attr("id");
	$.cookie(objectId, state, { path: '/', domain: this.cookieDomain });	
}

Filters.prototype.prepareAddFilter = function() {
	var self = this;
	$('.filter_add').each(function(){
		$(this).click(function(){
			var addObject = $(this).attr("id").substring(4);
			if ($("#" + addObject).css("display") == "none"){
				$("#" + addObject).fadeIn("slow");
				self.saveState($("#" + addObject), self.OPENED);
			}
		});
	});
}

Filters.prototype.prepareSearch = function() {
	var self = this;
	$('.rolling_filter_list_li').each(function(){
		self.filterClick(this);
	});
	$('.rolling_filter_list_li_c').each(function(){
		self.filterClick(this);
	});
}

Filters.prototype.filterClick = function(element) {
	var self = this;
	$(element).click(function(){
		$(this).toggleClass('rolling_filter_list_li_c').toggleClass('rolling_filter_list_li');
		if ($(element).parent().parent().hasClass("filters_content_all")){
			$(element).parent().parent().prev().find("#" + $(this).attr("id")).each(function(){
				$(this).toggleClass('rolling_filter_list_li_c').toggleClass('rolling_filter_list_li');		
			});
		}
		else{
			$(element).parent().parent().next().find("#" + $(this).attr("id")).each(function(){
				$(this).toggleClass('rolling_filter_list_li_c').toggleClass('rolling_filter_list_li');		
			});
		}
		self.query();
	});
}

Filters.prototype.getSelected = function(){
	var self = this;
	var params = new Object();
	$('.filters_content_all').find('.rolling_filter_list_li_c').each(function() {
		var param = self.getParamName($(this).parent().parent().parent().parent());
		if(param != false) {
			if (!self.isset(params, param)) {
				params[param] = $(this).attr("id");
			}
			else {
				params[param] += "," + $(this).attr("id");
			}
		}
	});
	return params;
}

Filters.prototype.getParamName = function(object){
	var paramId = $(object).attr("id");
	switch(paramId) {
		case "filters_types":
			return "t";
		case "filters_colours":
			return "c";
		case "transmission_types":
			return "tr";
		case "fuel_types":
			return "f";
		case "features_cars":
		case "features_bikes":
			return "ft";
		default:
			return false;
		
	}
}

Filters.prototype.isset = function(object, name) {
	return (object[name] == undefined) ? false : true;
}

Filters.prototype.getRanges = function() {
	var ranges = new Object();
	$("#filters_details").find("input[type='text']").each(function(){
		if ($(this).val() != 0) {
			ranges[$(this).attr('name')] = $(this).val();
		}
	});
	return ranges;
}

Filters.prototype.mergeObjects = function(first, second) {
	for (prop in second) {
		first[prop] = second[prop];
	}
	return first;
}

Filters.prototype.query = function(){
	var self 			= this;
	self.addLoadingImage();
	var selectedItems 	= this.getSelected();
	var ranges 			= this.getRanges();
	var merged 			= this.mergeObjects(selectedItems, this.params);
	merged 				= this.mergeObjects(merged, ranges);
	//TODO: sprawdzic czmu jest wysylane selected items a nie merged??
	$.post(self.url, selectedItems, function(data){
		self.replaceContent(data)
	});
}

Filters.prototype.addLoadingImage = function() {
	var loadingImage = $("<img></img").attr('src', 'images/ajax-filter.gif');
	$('#search_results').html(loadingImage);
	var top = ($(window).scrollTop() > $('#search_results').offset().top) ? $(window).scrollTop() + $(window).height_opera95_fixed() / 2 : $('#search_results').offset().top + $(window).height_opera95_fixed() / 4;
	loadingImage.css('position', 'absolute').css('left', '50%')
		.css('top', top)
		.css('margin-left', -loadingImage.width()/2).css('margin-top', -loadingImage.height()/2)
		.css('width', loadingImage.width()).css('height', loadingImage.height());
		
}

Filters.prototype.replaceContent = function(data) {
	var self = this;
	var response = $.parseJSON(data);
	$('#search_results').html(response['data']);
	$('#info_bar').replaceWith(response['bar']);
	email = new Email('#email_alert', this.logged);
	email.rebind(this);
}

function Email(object, logged) {
	this.object = object;
	this.logged = logged;
}

Email.prototype.rebind = function(filters) {
	$(this.object).unbind('click');
	alerts = new SearchAlerts('#create_search_alert', filters);
	if (this.logged == true) {
		$(this.object).click(function(){alerts.create();});
	}
	else {
		$(this.object).click(function(){$(this).parent().next().toggle()});
	}
}

