/* 
 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.
 */


// This object implements the dataSource methods for the list.
var categoryController = {
	
	// Sample data for the content of the list. 
	// Your application may also fetch this data remotely via XMLHttpRequest.
	_rowData: null,
	
	// The List calls this method to find out how many rows should be in the list.
	numberOfRows: function() {
		if(this._rowData==null)
		{
			return 1;
		} else {
			return this._rowData.length;
		}
	},
	
	loadCategoryData : function() {

		// load the list for this here...
		// categoryObject is a node...
				// Values you provide
		var feedURL = "http://www.antsmarching.org/iphonefiles/main.php";

		// XMLHttpRequest setup code
		var xmlRequest = new XMLHttpRequest();
		xmlRequest.onreadystatechange= function() 
		{
			var result= null;
        
			switch(xmlRequest.readyState)
			{
				case 4:
					if(xmlRequest.status==200)
					{
						// parse it...
						    //	dumpResponseXML(responseXML);			
						categoryController._rowData= new Array();
    
						var nodelist= xmlRequest.responseXML.getElementsByTagName('year');
						for(var ii= 0; ii<nodelist.length; ii++)
						{
							categoryController._rowData.push(new YearResultItem(
								nodelist[ii].getAttribute('y')));
						}
						
						// now tell us to reload!
						var list= document.getElementById('list2');						
						list.object.reloadData();
					}
					break;
                
                case 3:
					break;
		
                default:
					break;
			}
		}

		// voodoo.
		
		xmlRequest.open("GET", feedURL);
        xmlRequest.send("");
	},
	
	// The List calls this method once for every row.
	prepareRow: function(rowElement, rowIndex, templateElements) {
		var title= "";

		if(this._rowData!=null)
		{
			//title= this._rowData[rowIndex].y;
			if (templateElements.label1) {
				templateElements.label1.innerText = this._rowData[rowIndex].y;
			}
			// Assign a click event handler for the row.
			var self = this;
			rowElement.onclick = function(event) {
				// Do something interesting
				var resort = self._rowData[rowIndex];

				tourController.setRepresentedObject(resort);
				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('tourLevel'), resort.y+" Tours");
			}			
		} else {
			title= "Loading..."
		}

		// templateElements contains references to all elements that have an id in the template row.
		// Ex: set the value of an element with id="label".
		if (templateElements.label) {
			templateElements.label.innerText = title;
		}
	}
};

// This object implements the dataSource methods for the list.
var tourController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    /*
    setRepresentedObject: function(representedObject) {
        this._representedObject = representedObject;
        return this._representedObject;
        } // a ResultItem.
       */ 
    _rowData: null,
 		
			// Values you provide
    setRepresentedObject: function(categoryObject)
	{
        var tour = categoryObject.y;
		var feedURL = "http://www.antsmarching.org/iphonefiles/year.php?year="+tour;

		// XMLHttpRequest setup code
		var xmlRequest = new XMLHttpRequest();
		xmlRequest.onreadystatechange= function() 
		{
			var result= null;
        
			switch(xmlRequest.readyState)
			{
				case 4:
					if(xmlRequest.status==200)
					{
						// parse it...
						    //	dumpResponseXML(responseXML);			
						tourController._rowData= new Array();
    
						var nodelist= xmlRequest.responseXML.getElementsByTagName('tour');

						for(var ii= 0; ii<nodelist.length; ii++)
						{
							tourController._rowData.push(new TourResultItem(
								nodelist[ii].getAttribute('id'),
								nodelist[ii].getAttribute('name')));
						}
						
						// now tell us to reload!
						var list= document.getElementById('list3');						
						list.object.reloadData();
					}
					break;
                
                case 3:
					break;
		
                default:
					break;
			}
		}

		xmlRequest.open("GET", feedURL);
		//xmlRequest.setRequestHeader("Content-type", "text/xml; charset=utf-8");
		//xmlRequest.setRequestHeader("SOAPAction", "SOAP Request header info here...");
		//xmlRequest.send(buildXMLRequest('cat', 'XXXX', '', '', ''));
        xmlRequest.send();
	},

    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        if(this._rowData==null)
            return 1;
        else return this._rowData.length;
    },
    
// The List calls this method once for every row.
	prepareRow: function(rowElement, rowIndex, templateElements) {
		var title= "";

		if(this._rowData!=null)
		{
			//title= this._rowData[rowIndex].y;
			if (templateElements.label2) {
				templateElements.label2.innerText = this._rowData[rowIndex].name;
			}
			// Assign a click event handler for the row.
			var self = this;
			rowElement.onclick = function(event) {
				// Do something interesting
				var resort = self._rowData[rowIndex];

				listController.setRepresentedObject(resort);
				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('listLevel'), resort.name);
			}			
		} else {
			title= "Loading..."
		}

		// templateElements contains references to all elements that have an id in the template row.
		// Ex: set the value of an element with id="label".
		if (templateElements.label) {
			templateElements.label.innerText = title;
		}
	}
};



var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    /*
    setRepresentedObject: function(representedObject) {
        this._representedObject = representedObject;
        return this._representedObject;
        } // a ResultItem.
       */ 
    _rowData: null,
 		
			// Values you provide
    setRepresentedObject: function(categoryObject)
	{
        var tour = categoryObject.id;
		var feedURL = "http://www.antsmarching.org/iphonefiles/tourindex.php?tourid="+tour;

		// XMLHttpRequest setup code
		var xmlRequest = new XMLHttpRequest();
		xmlRequest.onreadystatechange= function() 
		{
			var result= null;
        
			switch(xmlRequest.readyState)
			{
				case 4:
					if(xmlRequest.status==200)
					{
						// parse it...
						    //	dumpResponseXML(responseXML);			
						listController._rowData= new Array();
    
						var nodelist= xmlRequest.responseXML.getElementsByTagName('show');

						for(var ii= 0; ii<nodelist.length; ii++)
						{
							listController._rowData.push(new ResultItem(
								nodelist[ii].getAttribute('id'),
								nodelist[ii].getAttribute('showdate'),
                                nodelist[ii].getAttribute('set'),
                                nodelist[ii].getAttribute('showinfo'),
                                nodelist[ii].getAttribute('venue')));
						}
						
						// now tell us to reload!
						var list= document.getElementById('list');						
						list.object.reloadData();
					}
					break;
                
                case 3:
					break;
		
                default:
					break;
			}
		}

		xmlRequest.open("GET", feedURL);
		//xmlRequest.setRequestHeader("Content-type", "text/xml; charset=utf-8");
		//xmlRequest.setRequestHeader("SOAPAction", "SOAP Request header info here...");
		//xmlRequest.send(buildXMLRequest('cat', 'XXXX', '', '', ''));
        xmlRequest.send();
	},

    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        if(this._rowData==null)
            return 1;
        else return this._rowData.length;
    },
    
prepareRow: function(rowElement, rowIndex, templateElements) {
		
		if(this._rowData != null)
		{
			// 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 = this._rowData[rowIndex].showdate;//+"\n"+this._rowData[rowIndex].venue;
                templateElements.text.innerText = this._rowData[rowIndex].venue;
			}

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handler = function() {
            var resort = self._rowData[rowIndex];
			//categoryController.setRepresentedObject(resort);
detailController.setRepresentedObject(resort);
            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'), resort.showdate);
/*			
            
            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'), resort);
*/
        };
        rowElement.onclick = handler;
		} else {
			if (templateElements.rowTitle) {
				templateElements.rowTitle.innerText = "Loading...";
			}
		}
    }
};


var detailController = {
    // This object acts as a controller for the detail UI.
    
    setRepresentedObject: function(representedObject) {
        this._representedObject = representedObject; // a ResultItem.
		
		// load the list for this here...
		// categoryObject is a node...
		// Values you provide
		var feedURL = "http://www.antsmarching.org/iphonefiles/tourindex.php";

		// XMLHttpRequest setup code
		var xmlRequest = new XMLHttpRequest();
		xmlRequest.onreadystatechange= function() 
		{
			var result= null;
        
			switch(xmlRequest.readyState)
			{
				case 4:
					if(xmlRequest.status==200)
					{
						// parse it...
						    //	dumpResponseXML(responseXML);			
						detailController._rowData= new Array();
    
						var nodelist= xmlRequest.responseXML.getElementsByTagName('show');
						for(var ii= 0; ii<nodelist.length; ii++)
						{
							detailController._rowData.push(new ResultItem(
								nodelist[ii].getAttribute('id'),
								nodelist[ii].getAttribute('showdate'),
                                nodelist[ii].getAttribute('set'),
                                nodelist[ii].getAttribute('showinfo'),
                                nodelist[ii].getAttribute('venue')));
						}
						
						// now tell us to reload!
						// we don't have to reload, we just have to change out our fields...
//						var list= document.getElementById('categoryList');						
//						list.object.reloadData();
        // When the represented object 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.
						r= detailController._representedObject;
						var showdate= r.showdate;
                        var setlist = r.set;
                        var showinfo = r.showinfo;
								
						var detailText = document.getElementById('detailTitle');
						detailText.innerHTML = showinfo;
                        
                        var detailLocation = document.getElementById('detailLocation');
						detailLocation.innerHTML = showdate;

						var setlistText = document.getElementById('detailDescription');
						setlistText.innerHTML = setlist;

					}
					break;
                
                case 3:
					break;
		
                default:
					break;
			}
		}
				
		xmlRequest.open("GET", feedURL);
        xmlRequest.send("");

    }
    
};


var homeController = {
    // 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 links.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.label3) {
            templateElements.label3.innerText = links[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 link = links[rowIndex];
            //detailController.setResort(resort);
            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.
            if (link.location == 'podcast')
            {
                window.open('http://www.antsmarching.org/iphone/podcast');
            }
            if (link.location == 'setlist')
            {
                window.open('http://www.antsmarching.org/iphone/setlist');
            }
            if (link.location == 'yearList')
            {
                browser.goForward(document.getElementById(link.location), link.name);
            } 
            if (link.location == 'news')
            {
                window.open('http://www.antsmarching.org/iphone/news');
            }
            if (link.location == 'forum')
            {
                window.open('http://www.antsmarching.org/forum');
            }
        };
        rowElement.onclick = handler;
    }
};


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

function ResultItem(id, showdate, set, showinfo, venue)
{
    this.id= id;
    this.set= set;
    this.showdate= showdate;
    this.showinfo= showinfo;
    this.venue= venue;
    
    return this;
}

function YearResultItem(y)
{
    this.y= y;
    
    return this;
}

function TourResultItem(id, name)
{
    this.id= id;
    this.name = name;
    
    return this;
}

function myClickHandler(event)
{
    var id = detailController._representedObject.id;
    window.open('http://www.antsmarching.org/tour/ViewShow.php?ShowID='+id);
}


function TourClick(event)
{
    var browser = document.getElementById('browser').object;
    browser.goForward(document.getElementById('yearList'), "Tour Central");
    categoryController.loadCategoryData();
}
var links = [
    { name: "News", location: "news" },
    { name: "Tour Central", location: "yearList" }, 
    { name: "Live Setlist", location: "setlist" }, 
    { name: "Ants Podcast", location: "podcast" },
    { name: "Forum", location: "forum" }
];

