/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return parks.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitle) {
            templateElements.rowTitle.innerText = parks[rowIndex].name;
        }

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handler = function() {
            var park = parks[rowIndex];
            detailController.setPark(park);
            var browser = document.getElementById('browser').object;
            // The Browser's goForward method is used to make the browser push down to a new level.  Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById('detailLevel'), park.name);
        };
        rowElement.onclick = handler;
    }
};

var detailController = {
    // This object acts as a controller for the detail UI.
    
    setPark: function(park) {
        this._park = park;
        this._representedObject = park.name;
        
        // When the park is set, this controller also updates the DOM for the detail page appropriately.  As you customize the design for the detail page, you will want to extend this code to make sure that the correct information is populated into the detail UI.
        var detailTitle = document.getElementById('detailTitle');
        detailTitle.innerHTML = this._park.name;
        var detailLocation = document.getElementById('detailLocation');
        detailLocation.innerHTML = this._park.postedby;
        var detailDate = document.getElementById('detailDate');
        detailDate.innerHTML = this._park.dt;
        var rating = document.getElementById('detailRating');
        rating.innerHTML = this._park.rating;
        //detailDescription.innerHTML = "The scenery in " + this._park.name + " is amazing this time of year!";
        
        var detailHTML = document.getElementById('HTMLtext');
        //detailHTML.innerHTML = "this is a <i>test</i> Bye for now";
        detailHTML.innerHTML = this._park.joke;
        
    }
    
};

// Sample data.  Some applications may have static data like this, but most will want to use information fetched remotely via XMLHttpRequest.

var parks= [];

//parks1.push({ name: "Hani", location: "London, UK" });	


//Added by Hani
// Values you provide
var feedURL = "http://www.theabouabed.com/iphone.aspx";	// The feed to fetch

var onloadHandler = function() { xmlLoaded(xmlRequest); };	// The function to call when the feed is loaded; currently calls the XMLHttpRequest load snippet

// XMLHttpRequest setup code
var xmlRequest = new XMLHttpRequest();
xmlRequest.onload = onloadHandler;
xmlRequest.open("GET", feedURL);
xmlRequest.setRequestHeader("Cache-Control", "no-cache");
xmlRequest.send(null);


// Called when an XMLHttpRequest loads a feed; works with the XMLHttpRequest setup snippet
function xmlLoaded(xmlRequest) 
{
	if (xmlRequest.status == 200) {
		// Parse and interpret results
		// XML results found in xmlRequest.responseXML
		// Text results found in xmlRequest.responseText
        //alert("Test");
        //var parks = [ xmlRequest.responseText];
        var i;
        var j;
        var tmp = xmlRequest.responseText;
        var tmp1;
        var title;
        var dt;
        var joke;
        var postedby;
        var rating;
        
        i = tmp.indexOf("$5$");
        
        while(i != -1)
        {
            tmp1 = tmp.substring(0,i);
            j = tmp1.indexOf("$1$");
            title = tmp1.substring(0,j);
            
            tmp1 = tmp1.substring(j+3);
            j = tmp1.indexOf("$2$");
            dt = tmp1.substring(0,j);
            
            tmp1 = tmp1.substring(j+3);
            j = tmp1.indexOf("$3$");
            joke = tmp1.substring(0,j);
            
            tmp1 = tmp1.substring(j+3);
            j = tmp1.indexOf("$4$");
            postedby = tmp1.substring(0,j);
            
            tmp1 = tmp1.substring(j+3);
            rating = tmp1;
            
            parks.push({ name: title, joke: joke, postedby: postedby, dt: dt, rating: rating });
        
            tmp = tmp.substring(i+3);    
            i = tmp.indexOf("$5$");
        }
         document.getElementById("list").object.reloadData();      	
	}
	else {
		alert("Error fetching data: HTTP status " + xmlRequest.status);
	}
}

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    dashcode.setupParts();
}
