/**
* Profile search
*/
function ProfileSearch(){
	
	Box.call(this);
}

ProfileSearch.prototype = new Box;
ProfileSearch.prototype.constructor = ProfileSearch;
ProfileSearch.prototype.template = "#template_profile_search";
ProfileSearch.prototype.text = "";
ProfileSearch.prototype.icon = "boxicons/search.png";
ProfileSearch.prototype.title = "Citizen Search";
ProfileSearch.prototype.starting = 1;
ProfileSearch.prototype.number = 10;
ProfileSearch.prototype.busy = false;

ProfileSearch.prototype.setText = function(text){
	
	this.text = text;
	return this;
}

ProfileSearch.prototype.setBusy = function(busy){

	this.busy = busy;
		
	if (busy){
		
		$(this.containerId).find(".searching").show();
		$(this.containerId).find(".profile_search_name").attr("disabled","yes");
	} else {

		$(this.containerId).find(".searching").hide();		
		$(this.containerId).find(".profile_search_name").removeAttr("disabled");		
	}
}

ProfileSearch.prototype.addContent = function(){
	
	var obj = this;
	$(this.containerId).find(".profile_search_go").click(function(event){

		event.preventDefault();
		if (obj.busy)
			return;
		obj.starting = 1;
		obj.setBusy(true);		
		obj.searchName = $(obj.containerId).find(".profile_search_name").val();
		$(obj.containerId).find(".profile_search_profiles").slideUp("fast");
		$.getJSON("interface/json.php",{action: "profileSearch", name: obj.searchName, starting: obj.starting, number: obj.number},obj.jqueryResultsCallbackFunc(obj));
	});	
	
	$(this.containerId).find(".next").click(function(event){
		
		event.preventDefault();
		if (obj.busy)
			return;		
		obj.setBusy(true);
		$(obj.containerId).find(".profile_search_name").val(obj.searchName);						
		obj.starting += obj.number;
		$(obj.containerId).find(".profile_search_profiles").slideUp("fast");			
		$.getJSON("interface/json.php",{action: "profileSearch", name: obj.searchName, starting: obj.starting, number: obj.number},obj.jqueryResultsCallbackFunc(obj));		
	});
	
	$(this.containerId).find(".prev").click(function(event){

		event.preventDefault();
		if (obj.busy)
			return;		
		obj.setBusy(true);		
		$(obj.containerId).find(".profile_search_name").val(obj.searchName);		
		obj.starting -= obj.number;
		$(obj.containerId).find(".profile_search_profiles").slideUp("fast");			
		$.getJSON("interface/json.php",{action: "profileSearch", name: obj.searchName, starting: obj.starting, number: obj.number},obj.jqueryResultsCallbackFunc(obj));		
	});	
	
	this.loaded();
}

ProfileSearch.prototype.addScore = function (scoreData){
		
	var score = $("#template_player_scores_score").clone().attr("id","")
	.addClass("scoreboard_position_" + scoreData.scoreboardPosition);
	score.find(".score_rank").html(scoreData.position);
	score.find(".score_image").attr("src","images/avatars/playerpics" + scoreData.picture + ".png");
	score.find(".score_name").html(scoreData.userName);
	score.find(".score_link").attr("href","?action=profile&userId=" + scoreData.userId);			   
	score.find(".score_countryName").html(scoreData.countryName);
	score.find(".score_score").html("");
	score.find(".score_countryFlag").attr("src", "images/flags/" + scoreData.country + ".gif");
	if (scoreData.userId == userId){
		
		score.find(".highlightable").addClass("highlighted");
	}
	
		score.appendTo(this.containerId + " .profile_search_profiles");
		score.css("display","");				
}

ProfileSearch.prototype.jqueryResultsCallbackFunc = function(obj){
	
	return function(data){
		
		$(obj.containerId).find(".profile_search_results").hide();
		$(obj.containerId).find(".profile_search_noresults").hide();
		$(obj.containerId).find(".profile_search_profiles").empty();
		$(obj.containerId).find(".prev").hide();
		$(obj.containerId).find(".next").hide();
		if (data.matches == 0){
			
			$(obj.containerId).find(".profile_search_noresults").show();			
		} else {
			
			$(obj.containerId).find(".profile_search_results").show();
			$(obj.containerId).find(".starting_result").html(obj.starting);
			$(obj.containerId).find(".ending_result").html((obj.starting+obj.number)-1 <= data.matches ? (obj.starting+obj.number)-1 : data.matches);
			$(obj.containerId).find(".total_result").html(data.matches);
			
			if (obj.starting != 1)
				$(obj.containerId).find(".prev").show();
				
			if (obj.starting+obj.number < data.matches)
				$(obj.containerId).find(".next").show();
				
			//Show the results	
			for (var i = 0; i < data.profile.length; i++){
						
				data.profile[i].scoreboardPosition = i;
				obj.addScore(data.profile[i]);
			}
			
			//Add click functionality
			$(obj.containerId).find(".score_link").click(function(event){
			
				event.preventDefault();
				var newProfile = new Profile;
				newProfile.setOwner(obj).setUserId(piece($(this).attr("href"),"userId"));
				obj.addChild(newProfile);
			});			
			
			$(obj.containerId).find(".profile_search_profiles").stop(true,true).slideDown("slow");
		}
		obj.setBusy(false);							
		
	}
}

