// Retrieves summaries and adds a small widget at the end of <body>
// --fsav, 2009.02.23

(function(){

	CSS_URL = 'http://shortenize.appspot.com/static/embed_style.css';
	JQUERY_URL = 'http://shortenize.appspot.com/static/jquery.js';

	/* Bookmarklet code:
javascript:function%20loadScript(scriptURL)%20{%20var%20scriptElem%20=%20document.createElement('SCRIPT');%20scriptElem.setAttribute('language',%20'JavaScript');%20scriptElem.setAttribute('src',%20scriptURL);%20document.body.appendChild(scriptElem);}%20loadScript('http://shortenize.appspot.com/static/embed.js');
	*/

	//////////////////////////////////////////////////////////////////////////
	// Load CSS

	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = CSS_URL;
	cssNode.media = 'screen';
	headID.appendChild(cssNode);

	//////////////////////////////////////////////////////////////////////////
	// Jquery loading

	// Add jQuery
	var GM_JQ = document.createElement('script');
	GM_JQ.src = JQUERY_URL;
	GM_JQ.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(GM_JQ);

	// Check if jQuery's loaded
	function GM_wait() {
		if(typeof window.jQuery == 'undefined') {
			window.setTimeout(GM_wait,100);
		} else {
			$ = window.jQuery; letsJQuery();
		}
	}
	GM_wait();

	//////////////////////////////////////////////////////////////////////////
	// Actual functionality

	var READ_API_URL = "http://shortenize.appspot.com/read";
	var WRITE_API_URL = "http://shortenize.appspot.com/write";
	var INITIAL_DIV_HTML = "<div class='summary_menu'><span class='summary_paginator'></span> - <a href='#' class='new_summary_link'>New summary</a></div><div class='summary_content'></div>";
	var NEW_SUMMARY_FORM_HTML = "<textarea style='width: 100%;' rows='5'></textarea><br/><input type='button' value='submit'/>";

	var summaries = null;
	var currentSummary = -1;

	var widgetDiv = null;

	function getQueryUrl() {
		return escape(window.location);
	}

	function getSummaries() {
		$.getJSON(READ_API_URL + "?_url=" + getQueryUrl() + "&_jsonp_callback=?",
			function(json) {
				summaries = json;
				updateDiv();
			});
	}

	function writeSummary() {
		$.getJSON(WRITE_API_URL
			+ "?_url=" + getQueryUrl()
			+ "&_jsonp_callback=?"
			+ "&_content=" + escape(widgetDiv.find("textarea").val()),
			function(json) {
				getSummaries();
			});
	}

	function displayNewSummaryForm() {
		var contentDiv = widgetDiv.find('.summary_content');
		contentDiv.html(NEW_SUMMARY_FORM_HTML);
		contentDiv.find('input').click(writeSummary);
	}

	function constructDiv() {
		widgetDiv = document.createElement("div");
		widgetDiv = $(widgetDiv);
		widgetDiv.attr('class','summary_container');
		widgetDiv.append(INITIAL_DIV_HTML);
		widgetDiv.find('.new_summary_link').click(displayNewSummaryForm);
	}

	function previousSummary() {
		currentSummary--;
		if(currentSummary < 0) currentSummary = 0;
		widgetDiv.find('.summary_content').html(summaries[currentSummary].content);
		updatePaginator();
	}

	function nextSummary() {
		currentSummary++;
		if(currentSummary >= summaries.length) currentSummary = summaries.length-1;
		widgetDiv.find('.summary_content').html(summaries[currentSummary].content);
		updatePaginator();
	}
	
	function updatePaginator() {
		var pspan = widgetDiv.find('.summary_paginator');
		if(summaries.length == 0){
			pspan.html("(No summaries)");
		}else{
			var html;
			if(currentSummary == 0){
				html = "<< Previous - Summary "
						+ (currentSummary+1) + " of " + summaries.length;
				if(summaries.length > 1){
					html += " - <a href='#' class='next_link'>Next >></a>";
				}else{
					html += "- Next >>";
				}
			}else if(currentSummary >= summaries.length - 1){
				html = "<a href='#' class='previous_link'><< Previous</a> - Summary "
						+ (currentSummary+1) + " of " + summaries.length + " - Next >>";
			}else{
				html = "<a href='#' class='previous_link'><< Previous</a> - Summary "
						+ (currentSummary+1) + " of " + summaries.length
						+ " - <a href='#' class='next_link'>Next >></a>";
			}

			pspan.html(html);

			pspan.find('.previous_link').click(previousSummary);
			pspan.find('.next_link').click(nextSummary);
		}
	}
	
	function updateDiv(){
		currentSummary = 0;
		updatePaginator();
		if(summaries.length > 0) {
			widgetDiv.find('.summary_content').html(summaries[0].content);
		}else{
			widgetDiv.find('.summary_content').html("No summary written yet.");
		}
	}

	//////////////////////////////////////////////////////////////////////////
	// Go

	// All your GM code must be inside this function
	function letsJQuery() {
		constructDiv();
		$(document.body).append(widgetDiv);
		getSummaries();
	}

})();


