$(function() {
	
	$("#filter_clubname,#filter_clubcity").keyup(csearch.typing);	
	$("#filter_region").change(csearch.restart);
	$('#prevButton').click(csearch.prevPage);
	$('#nextButton').click(csearch.nextPage);
	$('#viewAllButton').click(csearch.viewAll);
	csearch.init();
});

var profiles = {};

var csearch = {

	timeout: 0,
	total: 0,
	filterTotal: 0,
	curPage: 1,
	perPage: 12,
	perPageDefault: 12,
	
	init: function() {
		csearch.total = profiles.data.length;
		csearch.filterTotal = csearch.total;
	},
	
	typing: function() {
		clearTimeout(csearch.timeout);
		csearch.timeout = setTimeout('csearch.restart()',1500);
	},
	
	restart: function() {
		csearch.curPage = 1;
		csearch.start();
	},
	
	nextPage: function() {
		if(csearch.curPage * csearch.perPage < csearch.filterTotal){
			csearch.curPage++;
			csearch.start();
		}
		return false;
	},
	
	prevPage: function() {
		if((csearch.curPage - 1) > 0){
			csearch.curPage--;
			csearch.start();
		}
		return false;
	},
	
	setPage: function(pageNo){
		if(pageNo > 0 && (pageNo * csearch.perPage > csearch.filterTotal)){
			csearch.curPage = pageNo;
			csearch.start();
		}
		return false;
	},
	
	viewAll: function() {
		csearch.curPage = 1;
		if(csearch.perPage == csearch.perPageDefault){
			csearch.perPage = csearch.filterTotal;
			csearch.start();
		}else{
			csearch.perPage = csearch.perPageDefault;
			csearch.start();
		}
		return false;
	},
	
	start: function() {
		clearTimeout(csearch.timeout);
		//$('#profiles-holder').slideUp();
		
		var name = $("#filter_clubname")[0];
		var city = $("#filter_clubcity")[0];
		var region = $("#filter_region")[0];
		
		var params = {};
		
		if(name.value != name.defaultValue) params.clubname = name.value;
		if(city.value != city.defaultValue) params.clubcity = city.value;
		if(region.value != region.defaultValue) params.region = region.value;		
		
		csearch.process(params);
	},
	
	process: function(params) {
		this.filterTotal = 0;
		profileArray = profiles.data;
		var startAt = (csearch.curPage - 1) * csearch.perPage;
		var endAt = (csearch.curPage) * csearch.perPage;
		for(i = 0; i < profileArray.length; i++){			
			if(csearch.isMatch(profileArray[i], params)){
				csearch.filterTotal += 1;
				if(csearch.filterTotal > startAt && csearch.filterTotal <= endAt){
					$('#club-profile-'+profileArray[i].id).show();
				}else{
					$('#club-profile-'+profileArray[i].id).hide();
				}
			}else{
				$('#club-profile-'+profileArray[i].id).hide();
			}
		}
		csearch.finish();
	},
	
	isMatch: function(profile, params){
		var match = false;
		if(params.clubname){
			if(profile.profileName){
				if(profile.profileName.toUpperCase().indexOf(params.clubname.toUpperCase()) >= 0){
					match = true;
				}else{
					match = false; // Prevent matching an unmatched parameter, not necessary but consistent with below
				}
			}else{
				match = false; // Prevent matching an unmatched parameter, not necessary but consistent with below
			}
		}
		if(params.clubcity){
			if(profile.city){
				if(profile.city.toUpperCase().indexOf(params.clubcity.toUpperCase()) >= 0){
					if(!params.clubname){ //Prevent matching an unmatched name
						match = true;
					}
				}else{
					match = false; // Prevent matching an unmatched city
				}
			}else{
				match = false; // Prevent matching an unmatched city
			}
		}
		if(params.region){
			if(profile.region){
				if(profile.region.toUpperCase().indexOf(params.region.toUpperCase()) >= 0){
					if(!params.clubname && !params.clubcity){ // Prevent matching an unmatched name or city
						match = true;
					}
				}else{
					match = false; // Prevent matching an unmatched region
				}
			}else{
				match = false;  // Prevent matching an unmatched region
			}
		}
		if(!params.region && !params.clubcity && !params.clubname){
			match = true;
		}
		return match;
	},
	
	finish: function() {
		var startAt = (csearch.curPage - 1) * csearch.perPage + 1;
		var endAt = (csearch.curPage) * csearch.perPage;
		if(csearch.filterTotal == 0){
			startAt = 0;
			endAt = 0;
		}else{
			if(endAt > csearch.filterTotal){
				endAt = csearch.filterTotal;
			}
		}
		$('#filter-results').html(csearch.filterTotal.toString());
		$('#filter-start-at').html(startAt.toString());
		$('#filter-end-at').html(endAt.toString());
			//$("#profiles-holder").slideDown();
	}
}

function cregion(region) {
	var check = $("#filter_region option[value="+region+"]");
	if(check.length > 0) {
		check.attr({ selected: true });
		csearch.start();
	}
}