//////////////////////
// GLOBAL VARIABLES //
//////////////////////

var Books = new Object();
//Books = {}

// Books.baseurl = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=";
Books.current_page=1;
Books.target_offset=0;
Books.current_offset=0;
Books.full_page=870;
Books.is_moving=false;
Books.direction="";
Books.acceleration=4;
Books.default_shelf="work-related";
Books.actual_shelf="";

///////////////
// FUNCTIONS //
///////////////

Books.Print = function(shelf)
{
	var tmp = new Array();
	for (i=0; i<Books[shelf].length ;i++ )
	{
		//curl = "<a href='"+Books.baseurl+Books[shelf][i].isbn+"' target='_blank'>";
		curl = "<a href='"+Books[shelf][i].goodreads_url+"' target='_blank'>";
		tmp.push("<div class='book'>\n<div class='cover'>");
		tmp.push(curl);
		tmp.push("<img src='"+Books[shelf][i].cover+"' class='photo' /></a>");
		tmp.push("</div>\n<div class='info'>");
		tmp.push("<span class='author'>"+Books[shelf][i].author+"</span>\n");
		tmp.push("<span class='title'>"+curl+Books[shelf][i].title+"</a></span>\n");
		tmp.push("<span class='subtitle'>"+curl+Books[shelf][i].subtitle+"</a></span>\n");
		tmp.push("</div>\n</div>");
	}

	document.getElementById("books").innerHTML=tmp.join("");
	Books.current_page=1;
	document.getElementById("books").style.left="0px"; // second time for safety
	Books.Enum(true);
}

Books.Enum = function(isnew)
{
	highnum = 3 * Books.current_page;
	lownum = highnum -2;
	totalnum = Books[Books.actual_shelf].length;

	if (highnum >= totalnum) // we're on the last page
	{
		highnum = totalnum;
		document.getElementById("bs_right").className="right_in";
	} else {
		document.getElementById("bs_right").className="right_ac";
	}

	if (highnum == lownum) // we're on the last page and there is only a single item on it
	{
		enumtext="<span class='crange'>"+highnum+"</span>";
	} else {
		enumtext="<span class='crange'>"+lownum+" - "+highnum+"</span>";
	}

	enumtext+="<span class='cof'> of "+totalnum+"</span>";

	document.getElementById("counter").innerHTML=enumtext;

	if (lownum == 1) // we're on the first page
	{
		document.getElementById("bs_left").className="left_in";
	} else {
		document.getElementById("bs_left").className="left_ac";
	}

	Pulse.Do(document.getElementById("counter").firstChild, "color", "ff79fa", "0f96d8", 15);

	if (isnew)
	{
		Pulse.Do(document.getElementById("counter").childNodes[1], "color", "b0b0b0", "788083", 15);
	}

	if (Books.current_page < 1 || highnum < 0 || lownum < 0) // Houston, we've got a problem
	{
		Books.Fetch(Books.actual_shelf);
	}
}

Books.Scroll = function(direction){
	 if (!Books.is_moving)
     {
		Books.direction=direction;
		Books.current_offset=Number(document.getElementById("books").style.left.slice(0,-2));

		if (Books.direction=="right") {
			Books.target_offset = Books.current_offset - Books.full_page - 1;

			upper_limit = 0 - ((Math.ceil(Books[Books.actual_shelf].length / 3) - 1) * Books.full_page);
			if (Books.current_offset > upper_limit)
			{
				Books.current_page++;
				Books.Enum(false);
				Books.anim_timer=window.setInterval(Books.Move,20);
			}
		} else if (Books.direction=="left") {
			Books.target_offset = Books.current_offset + Books.full_page + 1;

			if (Books.current_offset < 0)
			{
				Books.current_page--;
				Books.Enum(false);
				Books.anim_timer=window.setInterval(Books.Move,20);
			}
		}
	 }
}

Books.Move = function(){
	Books.is_moving=true;
	
	handle = document.getElementById("books");
	Books.current_offset=Number(handle.style.left.replace("px",""));
	
	if (Books.direction=="right")
	{
		Books.current_offset = Math.floor(Books.current_offset - ((Books.current_offset - Books.target_offset) / Books.acceleration));
		if (Books.current_offset > Books.target_offset)
		{
			handle.style.left = Books.current_offset + 'px';
		} else {
			Books.ClearInt();
		}

	} else if (Books.direction=="left")
	{
		Books.current_offset = Math.ceil(Books.current_offset + ((Books.target_offset - Books.current_offset) / Books.acceleration));
		if (Books.current_offset < Books.target_offset)
		{
			handle.style.left = Books.current_offset + 'px';
		} else {
			Books.ClearInt();
		}
	}
}

Books.ClearInt = function(){
	window.clearInterval(Books.anim_timer);
	Books.is_moving=false;
}

Books.Fetch = function(shelf)
{
	if (shelf != Books.actual_shelf)
	{
		if (Books.actual_shelf != "")
		{
			document.getElementById("sl_"+Books.actual_shelf).className="";
		}
		document.getElementById("sl_"+shelf).className="active";

		//document.getElementById("books").innerHTML="";
		document.getElementById("books").style.left="0px";

		Books.actual_shelf = shelf;

		if (Books[shelf])
		{
			Books.Print(shelf);
		} else {
			Status(document.getElementById("books"), "loading", "[XHR] Sending request (Books.Fetch)");
			Ajax.Request("get","mode=books&query="+shelf,"FetchBooks", document.getElementById("books"));
		}
	}
}

///////////////////////////
// AJAX CALLBACK METHODS //
///////////////////////////

var Page = new Object();

Page.ParseBooks = function() 
{
	statusdiv = document.getElementById("books");
	if(Ajax.CheckReadyState(Ajax.request, statusdiv))
	{
		Ajax.channel_open=true;
		//response = Ajax.request.responseXML.documentElement; // for XML
		restring = Ajax.request.responseText; // for JSON

		Status(statusdiv, "ok", "[XHR] Response received - parsing data (Page.Parsebooks)");
		
		try
		{
			var reobj = JSON.parse(restring);
		} catch (e)	{
			Status(statusdiv, "error", "[JSON parser] "+e+" (Page.Parsebooks)");
		}

		if (reobj.errors.length==0)
		{
			Books[reobj.query]=reobj.response;
			Books.Print(reobj.query);
		} else {
			Status(statusdiv, "error", "[XHR] "+reobj.errors+" (Page.Parsebooks)");
		}
	}
}


//////////
// INIT //
//////////

function Init()
{
	//console.Output(true);
	Books.Fetch(Books.default_shelf);
}
