/*
 * SimpleModal gb Style Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: gb.js 238 2010-03-11 05:56:57Z emartin24 $
 */
google.load("books", "0");
var viewer = null;
var gb = null;
var isbn = 0;
var pg = 0;
var gb = null;
var self = null;
function initialize() {
	$('span.gb').each(function (i) {
		if($(this).hasClass('loading')){
			$(this).removeClass('loading');
			if($(this).hasClass('gbook')){
				$(this).css('background-image', 'url(/skins/roundedblue/img/gbook.png)');
			}else if($(this).hasClass('none')){
				$(this).css('background-image', 'url(/skins/roundedblue/external.png)');
				$(this).css('padding-left', '0');
				$(this).css('padding-right', '13px');
			}
		}
	});		
}
google.setOnLoadCallback(initialize);
function successCallback() {
	if(pg){
		setTimeout(function (){
			if(!viewer.goToPage(pg)){
				alert("Unable to preview page " + pg);
			}
		} ,1000);
	}	
}
function goPage(){
	
}
function alertNotFound() {
  	alert("could not embed the book!");
	self.close();
}
function setLinks(blink,book){
	if(book.preview!="noview"){
			blink.addClass('gbook');
			if(!blink.hasClass('loading')){
				blink.css('background-image', 'url(/skins/roundedblue/img/book.png)');
			}
		}else {
			blink.addClass('none');
			blink.css('background-image', 'url(/skins/roundedblue/external.png)');
			blink.css('background-position', 'center right');
			blink.css('padding-right', '13px');
			blink.css('padding-left', '0');
		}
		blink.attr('href',book.info_url);
}
function parseEntries(booksInfo) {
	for (i in booksInfo) {
		var q = booksInfo[i].bib_key;
		var alpha = 65
		q = '#' + q.substring(5);
		if(!$(q).length){
			var s=q+String.fromCharCode(alpha).toLowerCase();
			while($(s).length && alpha<=90){
				setLinks($(s),booksInfo[i]);
				alpha++;
				s=q+String.fromCharCode(alpha).toLowerCase();
			}
		}else setLinks($(q),booksInfo[i]);
	}
	$("span.gbook").click(function (e) {
			e.preventDefault();	
			var trigger = $(this);
    			isbn = trigger.attr('id');
			if (isbn.charAt(isbn.length-1) != parseInt(isbn.charAt(isbn.length-1))){
				isbn = isbn.substring(0,isbn.length-1)
			}
			
			url = "/skins/roundedblue/data/proxy.php?url="+escape("isbndb.com/api/books.xml?access_key=EF8D6HMR&index1=isbn&value1=") + isbn;
			$.ajax({
       				type: "GET",
				url: url,
				dataType: "html",
				success: function(xml) {
					x=xml.indexOf("<Title>");
					y=xml.indexOf("</Title>");
					if(xml.substring(x+7, y).length){
						$('#gb-modal-title').html(xml.substring(x+7, y));
					}
				},
				error:function(x,e){
					if(x.status==0){
						alert('You are offline!!\n Please Check Your Network.');
					}else if(x.status==404){
						alert('Requested URL not found.');
					}else if(x.status==500){
						alert('Internel Server Error.');
					}else if(e=='parsererror'){
						alert('Error.\nParsing XML Request failed.');
					}else if(e=='timeout'){
						alert('Request Time out.');
					}else {
						alert('Unknow Error.\n'+x.responseText);
					}
				}
			});
			if(trigger.attr('name')){
				pg=trigger.attr('name');
			}
			$("#gb-modal-content").modal({
			overlayId: 'gb-overlay',
			containerId: 'gb-container',
			closeHTML: null,
			minHeight: 80,
			opacity: 65, 
			position: ['0',],
			overlayClose: true,
			onOpen: gb.open,
			onShow: gb.show,
			onClose: gb.close
		});
	});
};
jQuery(function ($) {
	gb = {
		container: null,
		init: function (){
			var queryString="http://books.google.com/books?jscmd=viewapi&bibkeys=";
			$('span.gb').each(function (i) {
				isbn = $(this).attr('id');
				if (isbn.charAt(isbn.length-1) != parseInt(isbn.charAt(isbn.length-1))){
					isbn = isbn.substring(0,isbn.length-1);				}
				queryString+="ISBN:" + isbn + ",";
				$(this).addClass('loading');
			});
			queryString = queryString.substring(0, queryString.length - 1)+"&callback=parseEntries";
			var scriptElement = document.createElement("script");
			scriptElement.setAttribute("id", "jsonScript");
			scriptElement.setAttribute("src", queryString);
			scriptElement.setAttribute("type", "text/javascript");
			 // make the request to Google booksearch
			document.documentElement.firstChild.appendChild(scriptElement);
		},
		open: function (d) {
			var self = this;
			self.container = d.container[0];
			d.overlay.fadeIn('slow', function () {
				$("#gb-modal-content", self.container).show();
				var title = $("#gb-modal-title", self.container);
				title.show();
				d.container.slideDown('slow', function () {
					setTimeout(function () {
						var h = 500 + title.height() + 20; // padding
						d.container.animate(
							{height: h}, 
							200,
							function () {
								$("div.close", self.container).show();
								$("#gb-modal-data", self.container).show();
								if($('.gb').css('display')!="none"){
									viewer = new google.books.DefaultViewer(document.getElementById('gb-modal-data'));
									viewer.load(isbn, alertNotFound, successCallback);
								}
							}
						);
					}, 300);
				});
			})
		},
		close: function (d) {
			self = this; // this = SimpleModal object
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				500,
				function () {
					$.modal.close();
				}
			);
		viewer = null;
		$('#gb-modal-title').html('Google Book Client');
		}
	};
	
	$(document).ready(function() {
        	gb.init();
        });
	$('span.gb').mouseover(function(){
		window.status=$(this).attr('href');
	});
	$('span.gb').click(function(){
		if(!$(this).hasClass('gbook')){
			window.location = $(this).attr('href');
		}
	});
});
