// api_json_reviews_widget.js

if (!LibraryThing)
	{
	var LibraryThing = {};
	}

if (!LibraryThing.LTFLreviewsWidget)
	{
	LibraryThing.LTFLreviewsWidget = {};
	}

if (!LibraryThing.LTFLreviewsWidget.settings)
	{
	LibraryThing.LTFLreviewsWidget.settings = {};
	}

	
/// GLOBALS

LibraryThing.LTFLreviewsWidget.dochead = document.getElementsByTagName('head')[0];

/// FUNCTIONS

// Make a title and author link
LibraryThing.LTFLreviewsWidget.makeTitleandauthorlink = function(book)
{
var titleandauthorlink = document.createElement("div");
titleandauthorlink.setAttribute('class','LTFL_titleandauthor');

if(book.title == '')
	{
	book.title = 'No title';
	}
	
if (book.title.length > 100)
	{
	book.title = book.title.replace(/^(.{100,125}[a-z] )(.*)/, "$1 ...");
	}

if(book.opac_link)
	{
	var titlelink = document.createElement("a");
	titlelink.setAttribute('class','LTFL_title');

	if( LibraryThing.LTFLreviewsWidget.settings.newwindow )
	{
		titlelink.setAttribute('target', '_blank');
	}
	if( LibraryThing.LTFLreviewsWidget.settings.server )
	{
		href = LibraryThing.LTFLreviewsWidget.settings.server + book.opac_link;
	}
	else 
	{
		href = book.opac_link;
	}
	titlelink.setAttribute('href', href);
	titlelink.innerHTML = book.title;
	titleandauthorlink.appendChild(titlelink);
	}
return titleandauthorlink;
}

LibraryThing.LTFLreviewsWidget.loadStylesheet = function(url)
{
var CSS = document.createElement('link');
CSS.type = "text/css";
CSS.rel = "stylesheet";
CSS.href = url;
LibraryThing.LTFLreviewsWidget.dochead.appendChild(CSS);
};

LibraryThing.LTFLreviewsWidget.showmore = function(uniqid)
{
document.getElementById(uniqid).className = 'LTFL_showall';
}

LibraryThing.LTFLreviewsWidget.hidepartofstring = function(book)
{
var booktext = book.text;

var hidespan_start = '<span id="' + book.uniqid + '"><span class="LTFL_more">&hellip; (<a href="javascript:LibraryThing.LTFLreviewsWidget.showmore(\'' + book.uniqid + '\')">show more</a>)</span><span class="LTFL_hidden">';
var hidespan_end = '</span>';

if (booktext.length > 270)
	{
	booktext = booktext.replace(/^(.{240,270}[a-z] )(.*)/, "$1 " + hidespan_start + "$2" + hidespan_end);
	}
return booktext;
}

LibraryThing.LTFLreviewsWidget.makeBookreview = function(book)
{
if (book.text)
	{
	var bookreview = document.createElement('div');
	bookreview.setAttribute('class','LTFL_bookreview');

	if (book.text.length > 270)
		{
		book.text = LibraryThing.LTFLreviewsWidget.hidepartofstring(book);
		}
	bookreview.innerHTML = book.text;

	return bookreview;
	}
else
	{
	return null;
	}
}

LibraryThing.LTFLreviewsWidget.makeUserid = function(book)
{
if (book.userid)
	{
	var useridsection = document.createElement('div');
	useridsection.setAttribute('class','LTFL_userid');

	var useridURL = 'http://www.librarything.com/forlibraries/user/' + book.userid;
	// replace @ with %40 to make IE happy: it was showing the href for the text of the link
	// if there was an '@' in the useridURL.  Somehow related to the issue reported here:
	// http://www.hedgerwow.com/360/bugs/ie-anchor-bug.html.  Adding *just* the space before the href
	// didn't seem to be enough
	useridURL = useridURL.replace('@', '%40');
	var useridlink = document.createElement('a');
	useridlink.appendChild(document.createTextNode(book.userid));
	useridlink.setAttribute('href', ' '+useridURL );

	useridsection.appendChild(document.createTextNode('Review by '));
	useridsection.appendChild(useridlink);

	return useridsection;
	}
else
	{
	return null;
	}
}

LibraryThing.LTFLreviewsWidget.makeRatingimage = function(book)
{
if (book.rating)
	{
	var ratingimage = document.createElement("img");
	ratingimage.setAttribute('class','LTFL_rating');
	var url = 'http://www.librarything.com/pics/ss' + (book.rating) + '.gif';
	ratingimage.setAttribute('src', url);
	var num_stars = book.rating / 2;
	ratingimage.setAttribute('title', num_stars + ' stars');
	return ratingimage;
	}
else
	{
	return null;
	}
}

// The main callback
LibraryThing.LTFLreviewsWidget.makeBasicwidget = function(settings, ltdata)
{
if(settings.usernum)
	{
	var widgetdivname = 'LTFL_reviewwidget_' + settings.usernum;
	}
else
	{
	var widgetdivname = 'LTFL_reviewwidget_library_' + settings.library;	
	}
	
var LTFL_reviewwidget = document.getElementById(widgetdivname);

// Load the stylesheet
if (ltdata.css != '')
	{
	LibraryThing.LTFLreviewsWidget.loadStylesheet(ltdata.css);
	}
	
for (i in ltdata.reviews)
	{
	var book = ltdata.reviews[i];

	if (book.text != '')
		{
		// create item div
		var itemdiv = document.createElement("div");
		itemdiv.setAttribute('class', 'LTFL_item');
	
		if(book.rating > 0)
			{
			itemdiv.appendChild(LibraryThing.LTFLreviewsWidget.makeRatingimage(book));
			}

		// create title and author
		var titleandauthor = LibraryThing.LTFLreviewsWidget.makeTitleandauthorlink(book);
		titleandauthor.id = book.lsr_id;
		itemdiv.appendChild(titleandauthor);
		
		if(typeof book.userid != 'undefined')
			{
			itemdiv.appendChild(LibraryThing.LTFLreviewsWidget.makeUserid(book));
			}

		if(book.text)
			{
			itemdiv.appendChild(LibraryThing.LTFLreviewsWidget.makeBookreview(book));
			}

		LTFL_reviewwidget.appendChild(itemdiv);
		}		
	}

if (ltdata.reviews.length == 0)
	{
	var itemdiv = document.createElement("div");
	itemdiv.setAttribute('class', 'LTFL_item');
	itemdiv.innerHTML = 'No reviews.';
	LTFL_reviewwidget.appendChild(itemdiv);
	}

}


var widgetResults = {"reviews":{"879626":{"lsr_id":"879626","editedstamp":"1259260342","stamp":"1259260342","title":"The summer I turned pretty \/ Jenny Han.","userid":"leahbooklover","opac_link":"http:\/\/ipac.librarypoint.org\/ipac20\/ipac.jsp?index=ISBNE&term=1416968237","text":"One word! AWESOME!","rating":"10","uniqid":"4b108989dc4a2"},"874181":{"lsr_id":"874181","editedstamp":"1258527589","stamp":"1258527589","title":"Along for the ride : a novel \/ by Sarah Dessen.","userid":"xoPaige","opac_link":"http:\/\/ipac.librarypoint.org\/ipac20\/ipac.jsp?index=ISBNE&term=0670011940","text":"This was my \"Dessen\" book, and I couldn't get enough of it! Totally great for any age. After I read it, my mother insisted on reading it as well. Also a great summer book :] Can't wait to read more from Sarah Dessen! ","rating":"10","uniqid":"4b108989e116f"},"873521":{"lsr_id":"873521","editedstamp":"1258404759","stamp":"1258404759","title":"Vampire kisses \/ Ellen Schreiber.","userid":"swimchick3324","opac_link":"http:\/\/ipac.librarypoint.org\/ipac20\/ipac.jsp?index=ISBNE&term=0060093358","text":"THIS IS AN AWSOME BOOK!!!!!! not as good as twilight but who knew vampire books are interssting? this book is all about Raven and how she is an outcast until a mysterious family moves into the abandoned old house that hasn't been touched for many years. Knowing something is up Raven has to know more...<br><br>this seris will take you on a journey of romance and the abnormal. READ IT...IF YOU DARE!","rating":"10","uniqid":"4b108989edcc5"},"873516":{"lsr_id":"873516","editedstamp":"1258388123","stamp":"1258388123","title":"The road \/ Cormac McCarthy.","userid":"lcriscuolo","opac_link":"http:\/\/ipac.librarypoint.org\/ipac20\/ipac.jsp?index=ISBNE&term=0307265439","text":"I do not see what everybody else seems to see in this book. I know it's gotten great reviews, been an Oprah pick, etc., but I found \"The Road\" to be extremely depressing and pointless.  I get the message that even in the midst of terrible circumstances you can choose to be good or evil. I get that love for his son kept the man going and doing whatever he needed to do. I just did not see any great literary merit in the writing, and the whole story was just one downer after another. Gave me nightmares too. ","rating":"2","uniqid":"4b108989f2e8e"},"873515":{"lsr_id":"873515","editedstamp":"1258384733","stamp":"1258384733","title":"The coldest winter ever.","userid":"lcriscuolo","opac_link":"http:\/\/ipac.librarypoint.org\/ipac20\/ipac.jsp?index=ISBNE&term=9781416521693","text":"I wasn't expecting to enjoy this book, but it was excellent. Not much happens that is happy in the book, but it still avoids being depressing. The characters and their points of view are fascinating, even though they're mostly not attitudes I would want to have myself. You can see where they're coming from. I would have like to see the main character grow a little more throughout the book. She has so many opportunities to do so and life lessons are right in her face, but, like many people in similar situations, she has to learn the hard way and takes a very very long time to wise up. I think it's more realistic the way it Souljah wrote it. ","rating":"9","uniqid":"4b10898a03e52"}},"css":"http:\/\/ltfl.librarything.com\/forlibraries\/widgetcss\/style01.css"};
LibraryThing.LTFLreviewsWidget.settings.usernum = 0;
LibraryThing.LTFLreviewsWidget.settings.library = 171;
LibraryThing.LTFLreviewsWidget.settings.limit = 5;
LibraryThing.LTFLreviewsWidget.settings.category = "";
LibraryThing.LTFLreviewsWidget.settings.newwindow = 0;
LibraryThing.LTFLreviewsWidget.settings.server = "";

LibraryThing.LTFLreviewsWidget.makeBasicwidget(LibraryThing.LTFLreviewsWidget.settings, widgetResults);


/*
	Array
(
    [reviews] => Array
        (
            [879626] => Array
                (
                    [lsr_id] => 879626
                    [editedstamp] => 1259260342
                    [stamp] => 1259260342
                    [title] => The summer I turned pretty / Jenny Han.
                    [userid] => leahbooklover
                    [opac_link] => http://ipac.librarypoint.org/ipac20/ipac.jsp?index=ISBNE&term=1416968237
                    [text] => One word! AWESOME!
                    [rating] => 10
                    [uniqid] => 4b108989dc4a2
                )

            [874181] => Array
                (
                    [lsr_id] => 874181
                    [editedstamp] => 1258527589
                    [stamp] => 1258527589
                    [title] => Along for the ride : a novel / by Sarah Dessen.
                    [userid] => xoPaige
                    [opac_link] => http://ipac.librarypoint.org/ipac20/ipac.jsp?index=ISBNE&term=0670011940
                    [text] => This was my "Dessen" book, and I couldn't get enough of it! Totally great for any age. After I read it, my mother insisted on reading it as well. Also a great summer book :] Can't wait to read more from Sarah Dessen! 
                    [rating] => 10
                    [uniqid] => 4b108989e116f
                )

            [873521] => Array
                (
                    [lsr_id] => 873521
                    [editedstamp] => 1258404759
                    [stamp] => 1258404759
                    [title] => Vampire kisses / Ellen Schreiber.
                    [userid] => swimchick3324
                    [opac_link] => http://ipac.librarypoint.org/ipac20/ipac.jsp?index=ISBNE&term=0060093358
                    [text] => THIS IS AN AWSOME BOOK!!!!!! not as good as twilight but who knew vampire books are interssting? this book is all about Raven and how she is an outcast until a mysterious family moves into the abandoned old house that hasn't been touched for many years. Knowing something is up Raven has to know more...<br><br>this seris will take you on a journey of romance and the abnormal. READ IT...IF YOU DARE!
                    [rating] => 10
                    [uniqid] => 4b108989edcc5
                )

            [873516] => Array
                (
                    [lsr_id] => 873516
                    [editedstamp] => 1258388123
                    [stamp] => 1258388123
                    [title] => The road / Cormac McCarthy.
                    [userid] => lcriscuolo
                    [opac_link] => http://ipac.librarypoint.org/ipac20/ipac.jsp?index=ISBNE&term=0307265439
                    [text] => I do not see what everybody else seems to see in this book. I know it's gotten great reviews, been an Oprah pick, etc., but I found "The Road" to be extremely depressing and pointless.  I get the message that even in the midst of terrible circumstances you can choose to be good or evil. I get that love for his son kept the man going and doing whatever he needed to do. I just did not see any great literary merit in the writing, and the whole story was just one downer after another. Gave me nightmares too. 
                    [rating] => 2
                    [uniqid] => 4b108989f2e8e
                )

            [873515] => Array
                (
                    [lsr_id] => 873515
                    [editedstamp] => 1258384733
                    [stamp] => 1258384733
                    [title] => The coldest winter ever.
                    [userid] => lcriscuolo
                    [opac_link] => http://ipac.librarypoint.org/ipac20/ipac.jsp?index=ISBNE&term=9781416521693
                    [text] => I wasn't expecting to enjoy this book, but it was excellent. Not much happens that is happy in the book, but it still avoids being depressing. The characters and their points of view are fascinating, even though they're mostly not attitudes I would want to have myself. You can see where they're coming from. I would have like to see the main character grow a little more throughout the book. She has so many opportunities to do so and life lessons are right in her face, but, like many people in similar situations, she has to learn the hard way and takes a very very long time to wise up. I think it's more realistic the way it Souljah wrote it. 
                    [rating] => 9
                    [uniqid] => 4b10898a03e52
                )

        )

    [css] => http://ltfl.librarything.com/forlibraries/widgetcss/style01.css
)

*/
	