var Gutter = {
	// variables
	results:{},
	display:{
		profile:"profile",
		tags:"tags",
		search:"search"
	},
	
	
	// service
	services:{
		server:"",
		path:"services/",
		collection:"collection.wsdl.php",
		browse:"browse.wsdl.php"
	},	
	
	//methods
	data:function(browse, public)
	{
		var service	= !browse ? this.services.collection : this.services.browse;
		service		= this.services.server + this.services.path + service;

		var post = {tags:true}
		if(public)
		{
			post.public = true;
		}
		if(Gutter.favorites){post['favorites'] = true;}

		$.ajax(
			{
				url:service,
				cache:false,
				data:post,
				dataType:"json",
				type:"POST",
				success:function(response)
				{
					Gutter.results	= response;
					Gutter.build();
				},
				error:function()
				{
					console.log("failed:" + service);
				}
			}
		);
	},
	
	build:function()
	{
		// find max count first
		var max		= 1;
		var total	= 0;
		for(tag in this.results)
		{
			max	= this.results[tag] > max && max < 15? this.results[tag] : max;
			total	+= this.results[tag];
		}
		
		for(tag in this.results)
		{
			var div	= $(document.createElement("div"));
			div.attr(
				{
					'class':"tag"				
				}
			)
			div.text(tag);
			div.css(
				{
					"font-size":(0.85 + (this.results[tag]/max)) + "em"
				}
			)

			$(div).bind(
				'click',
				function()
				{
					window.location = "?search=tags|" + $(this).text();
				}
			)
			if(this.results[tag] > 1)
			{
				$("#" + this.display.tags).append(div)
			}
		}
		
		var clear	= $(document.createElement("div"));
		clear.css(
			{
				clear:"both"
			}
		)
		$("#" + this.display.tags).append(clear);

		this.hilight();
	},

	hilight:function()
	{
		search	= this.search();
		
		if(search.tags)
		{
			$(".tag:contains(" + search.tags + ")").css({color:"#CB6828"});
		}
	},

	search:function()
	{
		var search	= {}
		if (location.search != ""){ 
			var x = location.search.substr(1).split("&") 
			for (var i=0; i<x.length; i++){ 
				var y = x[i].split("="); 

				if(y[1].indexOf("|") != -1)
				{
					var z = y[1].split("|");
					search[z[0]] = [z[1]];
				}
			} 
		}
		return search;
	}	
}