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

/*
	Implements the dataSource methods for productList.  
*/

var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
	_rowData: [
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },    
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" }],   
    
     //_rowData: [], 
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        var actual_count = 0;
        for(i = 0; i < this._rowData.length; i++){
            if(this._rowData[i].tweet != ""){
                actual_count = actual_count + 1;
            }
        }
        //return this._rowData.length;
        return actual_count;
    },
    setRowData: function(tweets){
        if(tweets != null){
            this._rowData = tweets;
        }
        document.getElementById("list").object.reloadData();     
    },
    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.userTweet.innerText != "") {
        if (this._rowData[rowIndex].tweet != "") {
            var cleanupTweet = this._rowData[rowIndex].tweet;
            var cleanupTweet = cleanupTweet.replace(/&quot;/g, "\"");
            var cleanupTweet = cleanupTweet.replace(/&amp;/g, "\&");
            var cleanupTweet = cleanupTweet.replace(/&lt;/g, "\<");
            var cleanupTweet = cleanupTweet.replace(/&gt;/g, "\>");
                        
            templateElements.userTweet.innerText = cleanupTweet;
            templateElements.userImg.src = this._rowData[rowIndex].icon;
            templateElements.userName.innerText = "@"+this._rowData[rowIndex].username;
            templateElements.time_text.innerText = this._rowData[rowIndex].time;
        }
        else {
            templateElements.userImg.style.border = "0px";
            templateElements.userImg.style.display = "none";
            templateElements.userTweet.innerText = "";
        }

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        
        var handler = function() {
            //console.log("index: "+rowIndex);
            //console.log(self._rowData[rowIndex].tweet);
            var username = self._rowData[rowIndex].username;
            var status_id = self._rowData[rowIndex].status_id;
            document.location = "http://www.twitter.com/"+username+"/statuses/"+status_id;
                   
        };
        
        rowElement.onclick = handler;

    }
};

var detailController = {
    // This object acts as a controller for the detail UI.
    
    setPark: function(park) {
        this._park = park;
        this._representedObject = park.username;

        var detailTweet = document.getElementById('detailTitle');
        detailTweet.innerHTML = this._park.tweet;
    }
    
};

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

}

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

function foo(obj)
{

    var parks = [
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" },
    { status_id: "Acadia", location: "Maine, USA", icon: "Images/09e8dd39cb708c0e891e9077272b076de79fc7db_normal.jpg", username: "user", tweet: "Loading Tweet ...",time:"" }];
    
    
    //var parks_array = [];
    var results_length = obj.results.length;
    
    for (i=0;i<6;i++)
    {	
        try {
            var icon = obj.results[i].profile_image_url;
            var user = obj.results[i].from_user;
            var tweet = obj.results[i].text;
            var status_id = obj.results[i].id;
            var tweet_time = obj.results[i].created_at;
            //console.log(tweet_time);
            //console.log(obj.results.length);
            //var parks;
            parks[i].time = tweet_time;
            parks[i].icon = icon;
            parks[i].username = user;
            parks[i].tweet = tweet;
            parks[i].status_id = status_id;
        }
        catch(exception){ parks[i].time = ""; parks[i].icon = ""; parks[i].username = ""; parks[i].tweet = ""; parks[i].status_id = "";}
        
    }
    listController.setRowData(parks);
}

//Format of Statuses:
//http://twitter.com/username/statuses/status_id

function showTweets(event)
{
    var tweets_type = new Array(
                        "njtransitfail",
                        "NJ+Transit+Bus",
                        "NJ+Transit+Buses",
                        "NJ+Transit+Commute",
                        "NJ+Transit+Delay",
                        "NJ+Transit+Train",
                        "NJ+Transit+Trains",
                        "NJ+Transit+Late",
                        "NJ+Transit+Morning",
                        "NJ+Transit+Stuck",
                        "NJ+Transit+Better",
                        "NJ+Transit+fail",
                        "NJ+Transit+fuck",
                        "NJ+Transit+Ride",
                        "NJ+Transit+Damn",
                        "NJ+Transit+Shit",
                        "NJ+Transit+Hate",
                        "NJ+Transit+Love",
                        "NJ+Transit+NYC",
                        "NJ+Transit+Traffic",
                        "NJ+Transit+Work",
                        "NJ+Transit+Waiting");
    
    var randomnumber=Math.floor(Math.random()*tweets_type.length);
    var random_keyword = tweets_type[randomnumber];
    var peeps_name = new Array();
    
    var twitterJSON = document.createElement("script"); 
    twitterJSON.type="text/javascript" 
    //here's the search URL
    //Pulls tweets within 25 mile radius of 3 Times Square, New York, NY
    twitterJSON.src="http://search.twitter.com/search.json?geocode=40.756683%2C-73.986395%2C100mi&callback=foo&q="+random_keyword;
    document.getElementsByTagName("head")[0].appendChild(twitterJSON);

}


function goNJTransit(event)
{
    document.location = "http://www.silucho.com/njtransit.tweets";
}


function goLIRR(event)
{
    document.location = "http://www.silucho.com/lirr.tweets";
}


function showCharter(event)
{
    document.location = "http://www.silucho.com/charterbus.tweets";
}


function switchTransit(event)
{
    // Insert Code Here
    // Values you provide
var popupValue = document.getElementById("popup_switch");	// replace with ID of popup

// Popup code
popupValue = popupValue.value;

switch(popupValue) 
{
    case "MTA NYC Transit":
        document.location = "http://www.silucho.com/mtanyc.tweets";
        break;        

    case "NJ Transit":
        document.location = "http://www.silucho.com/njtransit.tweets";
        break;
    case "LIRR":
        document.location = "http://www.silucho.com/lirr.tweets";
        break;
    case "SEPTA":
        document.location = "http://www.silucho.com/septa.tweets";
        break;
    case "BART":
        document.location = "http://www.silucho.com/bart.tweets";
        break;        
    case "Charter Buses":
        document.location = "http://www.silucho.com/charterbus.tweets";
        break;        

}

}

