// 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":{"880278":{"lsr_id":"880278","editedstamp":"1259453428","stamp":"1259453428","title":"Pretty little liars \/ Sara Shepard.","userid":"adults_reviews@spl.org","opac_link":"http:\/\/catalog.spl.org\/ipac20\/ipac.jsp?index=ISBNEX&term=006088732X","text":"it was a realy good book i liked it because it was good normal girls just like me but they have bared secrets that with the help of someone who goes by the name of A might just let thouse secrets slip if they dont lisen to them.This adictingly good book will pull at your amoshons and leve you shoked.<br><br>Review by: Cabo, West Seattle Branch","rating":"8","uniqid":"4b168d846c9c2"},"880277":{"lsr_id":"880277","editedstamp":"1259453350","stamp":"1259453350","title":"As simple as snow \/ Gregory Galloway.","userid":"adults_reviews@spl.org","opac_link":"http:\/\/catalog.spl.org\/ipac20\/ipac.jsp?index=ISBNEX&term=0425207803","text":"This new girl Anna is peculiar and different from who she appears to be. She send notes and letters, with codes and strange understandings. But when she disappears a week after Valentines day, all the letters will unravel Anna's past, and possibly her future.<br><br>Review by: Maggie, West Seattle Branch","rating":"10","uniqid":"4b168d850bac2"},"880276":{"lsr_id":"880276","editedstamp":"1259453280","stamp":"1259453280","title":"Spanking Shakespeare \/ Jake Wizner.","userid":"adults_reviews@spl.org","opac_link":"http:\/\/catalog.spl.org\/ipac20\/ipac.jsp?index=ISBNEX&term=0375840850","text":"Normally when people say they can't stop laughing when they read a book, I don't believe them. It wasn't until I read Spanking Shakespeare that I knew it was even possible to literally laugh-out-loud while reading. The quirky title may give some insight on the type of book it is, but it's actually much more. First of all it's about a boy in High School who had the misfortune of being named Shakespeare Shapiro. Shakespeare has a way of being brutally honest with everyone but himself. Although the comedy in this book can sometimes make you cringe, our protagonist gives a look at what its like to be a teenage boy who doesn't know what he wants in life. A great read that both captures what its like to be a teenager and is down right hysterical.<br><br>Review by: Emily, Central Library","rating":"10","uniqid":"4b168d854b245"},"876444":{"lsr_id":"876444","editedstamp":"1258865279","stamp":"1258865279","title":"Housecat : how to keep your indoor cat sane and sound \/ by Christine Church.","userid":"Gail","opac_link":"http:\/\/catalog.spl.org\/ipac20\/ipac.jsp?index=ISBNEX&term=0876051425","text":"This book is a disappointment if you really need information on how to keep an indoor cat healthy and happy.   Some new owners may need to know it is a good idea  to have a litter box that is big enough, but this book has no information on how to determine what that is.  The book tells us to have toys.  We are not given information on what sorts of things cats find intriguing or what  they need to stay challenged and healthy.   <br><br> I do not agree that it is necessary to wash out and refill a cat's water bowl three (3) times a day.  <br><br>However, sometimes even one good idea is worth reading a book if that idea is helpful. ","rating":"4","uniqid":"4b168d8587abc"},"873519":{"lsr_id":"873519","editedstamp":"1258401566","stamp":"1258401566","title":"White gold wielder \/ Stephen R. Donaldson.","userid":"isaac b sunderland","opac_link":"http:\/\/catalog.spl.org\/ipac20\/ipac.jsp?index=ISBNEX&term=0345348702","text":"This has been one on the best books i have ever read.","rating":"10","uniqid":"4b168d85a4719"}},"css":"http:\/\/ltfl.librarything.com\/forlibraries\/widgetcss\/style01.css"};
LibraryThing.LTFLreviewsWidget.settings.usernum = 0;
LibraryThing.LTFLreviewsWidget.settings.library = 6;
LibraryThing.LTFLreviewsWidget.settings.limit = 5;
LibraryThing.LTFLreviewsWidget.settings.category = "Teens";
LibraryThing.LTFLreviewsWidget.settings.newwindow = 0;
LibraryThing.LTFLreviewsWidget.settings.server = "";

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


/*
	Array
(
    [reviews] => Array
        (
            [880278] => Array
                (
                    [lsr_id] => 880278
                    [editedstamp] => 1259453428
                    [stamp] => 1259453428
                    [title] => Pretty little liars / Sara Shepard.
                    [userid] => adults_reviews@spl.org
                    [opac_link] => http://catalog.spl.org/ipac20/ipac.jsp?index=ISBNEX&term=006088732X
                    [text] => it was a realy good book i liked it because it was good normal girls just like me but they have bared secrets that with the help of someone who goes by the name of A might just let thouse secrets slip if they dont lisen to them.This adictingly good book will pull at your amoshons and leve you shoked.<br><br>Review by: Cabo, West Seattle Branch
                    [rating] => 8
                    [uniqid] => 4b168d846c9c2
                )

            [880277] => Array
                (
                    [lsr_id] => 880277
                    [editedstamp] => 1259453350
                    [stamp] => 1259453350
                    [title] => As simple as snow / Gregory Galloway.
                    [userid] => adults_reviews@spl.org
                    [opac_link] => http://catalog.spl.org/ipac20/ipac.jsp?index=ISBNEX&term=0425207803
                    [text] => This new girl Anna is peculiar and different from who she appears to be. She send notes and letters, with codes and strange understandings. But when she disappears a week after Valentines day, all the letters will unravel Anna's past, and possibly her future.<br><br>Review by: Maggie, West Seattle Branch
                    [rating] => 10
                    [uniqid] => 4b168d850bac2
                )

            [880276] => Array
                (
                    [lsr_id] => 880276
                    [editedstamp] => 1259453280
                    [stamp] => 1259453280
                    [title] => Spanking Shakespeare / Jake Wizner.
                    [userid] => adults_reviews@spl.org
                    [opac_link] => http://catalog.spl.org/ipac20/ipac.jsp?index=ISBNEX&term=0375840850
                    [text] => Normally when people say they can't stop laughing when they read a book, I don't believe them. It wasn't until I read Spanking Shakespeare that I knew it was even possible to literally laugh-out-loud while reading. The quirky title may give some insight on the type of book it is, but it's actually much more. First of all it's about a boy in High School who had the misfortune of being named Shakespeare Shapiro. Shakespeare has a way of being brutally honest with everyone but himself. Although the comedy in this book can sometimes make you cringe, our protagonist gives a look at what its like to be a teenage boy who doesn't know what he wants in life. A great read that both captures what its like to be a teenager and is down right hysterical.<br><br>Review by: Emily, Central Library
                    [rating] => 10
                    [uniqid] => 4b168d854b245
                )

            [876444] => Array
                (
                    [lsr_id] => 876444
                    [editedstamp] => 1258865279
                    [stamp] => 1258865279
                    [title] => Housecat : how to keep your indoor cat sane and sound / by Christine Church.
                    [userid] => Gail
                    [opac_link] => http://catalog.spl.org/ipac20/ipac.jsp?index=ISBNEX&term=0876051425
                    [text] => This book is a disappointment if you really need information on how to keep an indoor cat healthy and happy.   Some new owners may need to know it is a good idea  to have a litter box that is big enough, but this book has no information on how to determine what that is.  The book tells us to have toys.  We are not given information on what sorts of things cats find intriguing or what  they need to stay challenged and healthy.   <br><br> I do not agree that it is necessary to wash out and refill a cat's water bowl three (3) times a day.  <br><br>However, sometimes even one good idea is worth reading a book if that idea is helpful. 
                    [rating] => 4
                    [uniqid] => 4b168d8587abc
                )

            [873519] => Array
                (
                    [lsr_id] => 873519
                    [editedstamp] => 1258401566
                    [stamp] => 1258401566
                    [title] => White gold wielder / Stephen R. Donaldson.
                    [userid] => isaac b sunderland
                    [opac_link] => http://catalog.spl.org/ipac20/ipac.jsp?index=ISBNEX&term=0345348702
                    [text] => This has been one on the best books i have ever read.
                    [rating] => 10
                    [uniqid] => 4b168d85a4719
                )

        )

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

*/
	