﻿if (!window.GJHDigital_JSON_Silverlight)
	GJHDigital_JSON_Silverlight = {};

GJHDigital_JSON_Silverlight.Page = function() 
{
}
var host;
GJHDigital_JSON_Silverlight.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		host = rootElement;
		this.page = "data.aspx";
		this.Row = 0;
        this.txtRows = rootElement.findName("txtRows");
        
		this.downloadBooks();
		rootElement.findName("txtAuthor").Text = "LOADING...";
		this.btnLeft = rootElement.findName("btnLeft");
		this.btnRight = rootElement.findName("btnRight");
        this.btnLeft.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.btnLeftButtonMouseUp));
        this.btnRight.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.btnRightButtonMouseUp));
	},
	btnLeftButtonMouseUp :function(sender, eventArgs)
	{
	    this.Row -=1;
	    this.downloadBooks();
	},
	btnRightButtonMouseUp :function(sender, eventArgs)
	{
	    this.Row +=1;
	    this.downloadBooks();
	},
	downloadBooks: function(sender, eventArgs)
	{
	    var downloader = this.control.createObject("downloader");
	    downloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadBooksCompleted));	    
	    var url = this.page + "?id=0&q=SelectAll&Unique="+Math.random();
	    downloader.open("GET", url);
	    downloader.send();	    
	},
	downloadBooksCompleted: function(sender, eventArgs)
	{
	    var dtBooks = eval( "(" + sender.getResponseText("") + ")" );
	    var rows = dtBooks.row.length - 1;
	    var x = "";

        var r = this.Row; // the row number.
        
        try
        {
            // assign JSON response to silverlight textblocks
	        sender.findName("txtAuthor").Text = "Author: " + dtBooks.row[r].author;
	        sender.findName("txtTitle").Text = "Book Title: " + dtBooks.row[r].title;
	        sender.findName("txtYear").Text = "Year: " + dtBooks.row[r].year;
    	    this.txtRows.Text = "Row: " + String(this.Row + 1);
	        
	        // writes out all the titles
	        for(i=0; i<rows; i++)
	        {
	            x += dtBooks.row[i].author + " - " + dtBooks.row[i].title + " - <b style='color:red;'>" + dtBooks.row[i].year + "</b><br>";
	        }
	        // assign the JSON response to html <div>
	        document.getElementById("errorLocation").innerHTML = "<b>All the records</b><br>" + x;
	    }
	    catch(e)
	    {
	        alert("No more rows.");
	    }
	    
	}
}