var POKE = { };
POKE.app = {

	init : function () {
		this.bind_events();
		this.display_messages();
		if($('#links').get(0)){
			this.display_links_to_this_page('bakertweet', $('#links'));
		}
	},
	
	display_links_to_this_page : function (search_term, ul_element) {
			$('#loader').show();
			var feed = new google.feeds.Feed("http://blogsearch.google.com/blogsearch_feeds?q=" + search_term);
			
			feed.setNumEntries(50);
			feed.load(function(result) {
				if (!result.error) {
					for (var i = 0; i < result.feed.entries.length; i++) {
						var entry = result.feed.entries[i];
						var author = "<em> by "+entry.author+"</em>";
						var link = "<a href='"+entry.link+"'>"+entry.title+"</a>";
						if(entry.author && entry.author != 'unknown') {
							link = link + author;
						}
						ul_element.append("<li>"+link+"</li>");
					}
				}
				$('#loader').hide();
			});

	},
	
	bind_events : function () {
		$('#edit_bread_form ul li .remove').live('click', POKE.app.remove_bread);
	},
	
	update_management_form : function (total, initial) {
		var current_total = parseInt($('#id_form-TOTAL_FORMS').val());
		var new_total = current_total+total;
		$('#id_form-TOTAL_FORMS').val(new_total);		
		var current_initial = parseInt($('#id_form-INITIAL_FORMS').val());
		var new_initial = current_initial+initial;
		$('#id_form-INITIAL_FORMS').val(new_initial);		
		return new_total;
	},
	
	add_bread : function () {
		// new id
		var new_id = POKE.app.update_management_form(+1, 0)-1;
		if(new_id > POKE.app.num_items-1){
			POKE.app.create_message("I'm sorry, you can only have "+POKE.app.num_items+" messages.");
			return true;
		}
		// find first.
		var first = $('#edit_bread_form ul li:first');
		// shameless redirect to same page if no forms are left to clone!
		if(!first.get(0)){ window.location.href = window.location.href; }
		// clone it
		var clone = first.clone();
		// remove image
		clone.find('img.thumbnail').remove();
		// update numbers and values
		clone.find('input, textarea').each(function() {
			var c_id = $(this).attr('id');
			var c_name = $(this).attr('name');
			$(this).attr('id', c_id.replace('0', new_id));
			$(this).attr('name', c_name.replace('0', new_id));
			$(this).val('');
		});
		// add it to the list
		$('#edit_bread_form ul').append(clone);
		return false;
	},
	
	remove_bread : function (e) {
		var field = $(e.target).closest('.field_wrapper');
		var id = field.find('.id_wrapper input').val();
		if(id) {
			$.post("/kitchen/bread/remove/", { pk : id }, function(data, status) {
				POKE.app.remove_element(field);
				POKE.app.update_management_form(-1, -1);
			}, "json");
		}else{
			POKE.app.remove_element(field);
			POKE.app.update_management_form(-1, 0);
		}
		return false;
	},
	
	remove_element : function (el) {
		$(el).slideUp(500, function(){
			$(this).remove();
			POKE.app.reassign_form_indexes();
		});
	},
	
	reassign_form_indexes : function () {
		var i = 0;
		$('#edit_bread_form .field_wrapper').each(function() {
		    $(this).find('input, textarea').each(function() {
				var c_id = $(this).attr('id');
				var c_name = $(this).attr('name');
				$(this).attr('id', c_id.replace(/\d+/, i));
				$(this).attr('name', c_name.replace(/\d+/, i));
		    }); 
		    i++;
		});
	},
	
	// display messages
	_timeout_id : null,
	display_messages : function () {
		$('#messages').show();
		if($('#messages li').length){
			$('#messages').slideDown(300);
			POKE.app._timeout_id = setTimeout(POKE.app.clear_messages, 2000);
		}
	},
	
	clear_messages : function () {
		$('#messages').slideUp(900, function(){
			$('#messages ul').empty();
			$('#messages').hide();
		});
	},
	
	create_message : function (msg) {
		if(!msg) {return false; }
		$('#messages ul').append('<li><p>'+msg+'</p></li>');
		clearTimeout(POKE.app._timeout_id);
		POKE.app.display_messages();
	}
	
	
};

$(document).ready(function(){
	POKE.app.init();
});
