Main = { 

	init: function() {
		
		Main.set();
		Search.init();
		Thumbs.init();
		
	},

	set : function() {
		$.url = location.href;
	}

}

Thumbs = {
	
	init: function() {
		this.addEvents();	
	},
	
	addEvents: function() {
		$("a.thumbs").click(function(evt) {
			
			evt.preventDefault();
			
			
			var el = this;
			var href = $(el).attr("href").replace("#","").split("/");
			var num = $(this).text();
			var mode = href[0];
			var quote_id = href[1];
			var qid = href[2];
			$.ajax({
			   type: "POST",
			   url: "/ajax/thumbs_"+mode+"/"+quote_id,
			   dataType: "json",
			   data: "qid="+qid,
			   success: function(response){
				   	if( response.error == 1) {
				   		alert(response.msg)
				   	} else {
				   		$(el).text(parseInt(num)+1);
				   		$(el).parents(".social").find("a.thumbs").unbind().click(function(evt) { evt.preventDefault(); });
				   	}
			   }
			 });
 		})
	}
}

Search = {
	
	init : function() {
		Search.input = $('#search input');
		Search.button = $('#search button');
		Search.message = "Buscar por..."; 
		Search.bind();
		Search.focus();
	},
	
	focus : function() {
		if($.url.indexOf('/search/') != -1) {
			Search.input.focus();
		}
	},
	
	bind : function() 
	{
		Search.input.focus(function(){
			var s = $(this);
			if(s.val() == Search.message)
				s.val('');
			//s.css('color','black');
		}).blur(function() {
			var s = $(this);
			if(s.val() == '')
				s.val(Search.message);
			//s.css('color','#999');
		}).keypress(function(e) {
			var key = e.keyCode || e.charCode;
			if(key == 13) // pressionou enter
				Search.run();
		});
		Search.button.click(function(){
			Search.run();
		})
	},
	
	run : function() 
	{
		
		var term = Search.input.val();
		if(!Search.validate(term))
			return;
		
		location.replace('/search/'+term);
		
	},
	
	validate : function(term) {
		if(term == '' || term == Search.message) {
			return false;
		}
		return true;
	}
}



$(Main.init);
