﻿clist = new ListBrowser({
	hashSection: null,
	url: __root + 'People/Browse',
	holderSelector: "#search_results_place"
});

clist.heights = [];

clist.defaults['Page.rpp'] = 50;
clist.defaults['Page.sort'] = 3;
clist.defaults.q = '';
clist.masterFilter.q = '';
clist.defaults.CompIndex = '00';

clist.li = 0;

clist.getOverlayContent = function() {
	var img = jQuery('<img alt="Loading.."/>');
	img.attr('src', __root + 'Content/images/ajax-loader.gif');
	img.css({
		'left': '150px',
		'top': '170px',
		position: 'relative'
	});
	return img;
};

clist.renew = function() {

	var th = clist;
	if (th.beforeRenew)
		th.beforeRenew();

	th.getState();
	var prm = { };
	for (var i in th.state) {
		prm[i] = th.state[i];
	}
	var s = jQuery.param(prm);
	if (s) prm['stateLink'] = s;
	th.showOverlay();

	prm.loggedin = th.li;
	prm.ci = th.ci;

	jQuery(th.holderSelector).load(th.url, prm, function(resp) {
		th.hideOverlay();
		if (th.onPageLoad)
			th.onPageLoad(resp);
		th.afterPageLoad(resp);
	});

};

clist.afterPageLoad = function() {
	jQuery('.search_option_container').each(function() {
		var jopt = jQuery(this);
		if (jopt.find("a:contains('See all')").length > 0) {
			var ids = '';
			jopt.find("input[type='checkbox']").each(function() {
				if (this.checked)
					ids += ',' + this.value;
			});
			var id = jopt.attr('id').replace('srch_opt_cntnr', '');
			jQuery.get(__root + 'Bars/GetOption/' + id, { values: ids }, function(resp) {
				jopt.replaceWith(resp);
			});
		}
	});

};

clist.doSrch = function() {
	if (clist.inProcess)
		return;
	clist.applyFilter();
	clist.show();
};

clist.clear_q = function() {
	clist.masterFilter.q = '';
	clist.show();
};


clist.beforeRenew = function() {
	clist.inProcess = true;
	jQuery('.bars-sort-order').removeClass('active');
	jQuery('#sordr' + Math.abs(this.sort)).addClass('active');
	this.jageSlider.slider('values', 0, jQuery('#MinAge').val());
	this.jageSlider.slider('values', 1, jQuery('#MaxAge').val());

	var n = jQuery('#MinHeight').val();
	for (var i = 0; i < this.heights.length; i++)
		if (n == this.heights[i].id)
			this.jheightSlider.slider('value', i);

	clist.showAge();
	clist.showHeight();
	clist.inProcess = false;
};

clist.removeUser = function(id) {
	var th = this;
	if (confirm('Remove this item from search results?')) {
		jQuery.get(__root + "People/Run/RemoveFromResults", { UserID: id }, function(resp) {
			th.doSrch();
		});
	}
};

clist.showAge = function(a, b) {
	clist.inProcess = true;
	if (a != 0 && !a) a = this.jageSlider.slider('values', 0);
	if (b != 0 && !b) b = this.jageSlider.slider('values', 1);
	var s = a == b ? this.ageNames[a] : 'From ' + this.ageNames[a] + ' to ' + this.ageNames[b];
	jQuery("#ages").html(s);
	jQuery('#MinAge').val(a);
	jQuery('#MaxAge').val(b);
	clist.inProcess = false;
};

clist.showHeight = function(i) {
	clist.inProcess = true;
	if (i != 0 && !i) i = this.jheightSlider.slider('values', 0);
	jQuery('#height').html(this.heights[i].val);
	jQuery('#MinHeight').val(this.heights[i].id);
	clist.inProcess = false;
};

clist.beforeChangePage = function() {
	window.scrollTo(0, 0);
};

clist.enableSliders = function(minAge, maxAge) {
	var th = this;

	this.jageSlider = jQuery("#slider-range");
	this.jageSlider.slider({
		range: true,
		min: 0,
		max: th.ageNames.length - 1,
		values: [minAge, maxAge],
		slide: function(event, ui) {
			th.showAge(ui.values[0], ui.values[1]);
		},
		change: function() {
			th.doSrch();
		}
	});
	this.showAge();

	this.jheightSlider = jQuery("#slider-range-min");
	this.jheightSlider.slider({
		range: "min",
		value: 0,
		min: 0,
		max: th.heights.length - 1,
		slide: function(event, ui) {
			th.showHeight(ui.value);
		},
		change: function() {
			th.doSrch();
		}
	});
	this.showHeight(0);
};


function onCloseSelPopup(opt_id, itms_class) {
    var ids = '';
    jQuery.each(jQuery(".p_" + itms_class), function () {
        if (this.checked)
            ids += ',' + this.id;
    });
    jQuery.get(__root + 'People/GetOption/' + opt_id, { values: ids }, function (resp) {
        jQuery('#srch_opt_cntnr' + opt_id).replaceWith(resp);
        clist.doSrch();
    });
}
