function callback(item) {
	window.location = item.url;
}

$('#viewPreviewTxt').html('<img src="/images/ajax-loader.gif" alt="loading preview">');

jQuery(function() {
 //Search box text removal and auto suggest
 var searchBox = $("input#searchbox"); 
 searchBox.jsonSuggest(searchBooks.webSites, {onSelect:callback, maxResults:6});
 var searchBoxDefault = "Search";
 searchBox.focus(function(){  
     if($(this).attr("value") == searchBoxDefault) $(this).attr("value", "");  
 });  
 searchBox.blur(function(){  
     if($(this).attr("value") == "") $(this).attr("value", searchBoxDefault);  
 });
});

$(document).ready(function(){
	
	
	//var text = $('#featured-book .description').text();
	//var first =  $('<span>'+ text.charAt(0) + '</span>').addClass("capital");
	//$('#featured-book .description').text(text.substring(1)).prepend(first);
	
	
	
	
	// ====================================
	// = News item hover for the homepage =
	// ====================================
	
	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 0, // number = milliseconds for onMouseOver polling interval    
	     over: growNews, // function = onMouseOver callback (REQUIRED)    
	     timeout: 500, // number = milliseconds delay before onMouseOut    
	     out: shrinkNews // function = onMouseOut callback (REQUIRED)    
	};
	//$('.big-news').each(function(i)
	//{
	//	$(this).hoverIntent(config)
	//});
	
	function growNews()
	{
		$(this).find('.big-news-flyout').show('fast');
	}
	
	function shrinkNews()
	{
		$(this).find('.big-news-flyout').hide('fast');
	}
	
	
	// ==========================================================
	// = Toggle effect for the sliding in and out of the basket =
	// ==========================================================
	

	
      $("#footer-menu-link").html(open_basket_text);

	  
	  if($.browser.msie && $.browser.version == "6.0")
      {
      }
      else
      {
          $("#footer-menu-link").toggle(function () {



                      $("#footer-menu").slideDown("fast");
            	      $("#footer-menu-link").html("<span>Close</span> your basket");                  

                    return false;

          },
    	  function(){


          		$("#footer-menu").slideUp("fast");
          		$("#footer-menu-link").html(open_basket_text);


    	  });
      }
          
    

    var options =  {
        closeClass: 'media-close',
        closeHTML: '<a href="#">Close</a>'
    }


    $('#open-dialogue').click(function(){
       $('#mediaInfo .content').modal(options); 
    });

	//var test = new Whatever();
	
	$('#searchbox').focus(function(){
	   $(this).attr({value:''});
	});
	
	
}); 

if($('body.books').length > 0)
{
    google.load("books", "0");
    google.setOnLoadCallback(initialize);
    
}    
    

var opened = false;
var timeout;

function bookFound()
{
    $('#viewPreviewTxt').empty();
    $('#viewPreviewTxt').html('<a id="viewPreviewLink" class="expand" href="#">Expand</a><span id="previewMessage"> to view a preview of this book.</span>');
    
    $('#book-preview .expand').toggle(function() 
    {   
        //console.log(document.getElementById('google-book-preview'));
        $('#google-book-preview').slideDown('fast');
        var viewer = new google.books.DefaultViewer(document.getElementById('google-book-preview'));
        viewer.load('ISBN:' + jsISBN, null, setLoaded);
        $('#viewPreviewLink').text('Close');
        $('#previewMessage').text(' this preview');
        timeout = setTimeout(function(){checkifLoaded()}, 4000);
        
    },
    function()
    {   
        $('#google-book-preview').slideUp('fast');
        $('#viewPreviewLink').text('Expand');
        $('#previewMessage').text(' to view a preview of this book.');
    }
    
    );
}

function checkifLoaded()
{
    //console.log('checking if loaded');
    
    if(opened)
    {
        clearTimeout(timeout);
        
    }else
    {
         var viewer = new google.books.DefaultViewer(document.getElementById('google-book-preview'));
         viewer.load('ISBN:' + jsISBN, null, setLoaded);
         timeout = setTimeout(function(){checkifLoaded()}, 4000);
    }
    
}

function setLoaded()
{
    opened = true;
    //console.log('has been opened set to true, does it appear?');
}

function notFound()
{
    $('#viewPreviewTxt').html('<span id="previewMessage" class="no-book">We are sorry but there is currently no preview available for this book.</span>');
}

function initialize()
{
    //viewer.load('ISBN:' + jsISBN, notFound, bookFound);
   
   $.getScript("http://books.google.com/books?bibkeys=ISBN:"+ jsISBN + "&jscmd=viewapi&callback=testGoogleResult");
   
    
}

function testGoogleResult(data)
{
    
    for (var test in data)
    {
       if(data[test].embeddable)
       {
          bookFound()
          break; 
       }else
       {
          notFound()
          break;
       }
    }
    
}
