function TVListings()
{
    TVListings.__instance = this;
}

TVListings.GetInstance = function()
{
	return TVListings.__instance;
}

TVListings.Fetch = function(brand, startdate, enddate, showtitle, showcount, container) {
    var head = document.getElementsByTagName("head").item(0);
    var jsonScript = document.createElement("script");
    var jsonURL = "http://tvlistings.ctvdigital.net/listings/fetch?brand=" + brand + "&startdate=" + startdate + "&enddate=" + enddate + "&showtitle=" + showtitle + "&showcount=" + showcount + "&container=" + container + "&format=json";
    jsonScript.setAttribute("type", "text/javascript");
    jsonScript.setAttribute("src", jsonURL);
    head.appendChild(jsonScript);
}

TVListings.FetchShows = function(brand, container) {
    var head = document.getElementsByTagName("head").item(0);
    var jsonScript = document.createElement("script");
    var jsonURL = "http://tvlistings.ctvdigital.net/showlistings/getshows?brand=" + brand + "&container=" + container + "&format=json";
    jsonScript.setAttribute("type", "text/javascript");
    jsonScript.setAttribute("src", jsonURL);
    head.appendChild(jsonScript);
}

TVListings.FetchByLetter = function(letter, container) {
    var head = document.getElementsByTagName("head").item(0);
    var jsonScript = document.createElement("script");
    var jsonURL = "http://tvlistings.ctvdigital.net/showlistings/getshows?brand=all&letter=" + letter + "&container=" + container + "&format=json";
    jsonScript.setAttribute("type", "text/javascript");
    jsonScript.setAttribute("src", jsonURL);
    head.appendChild(jsonScript);
}

function GetMonthDayYear(jsonDateString) {

    var re = new RegExp("\/Date[(]([0-9]+)");
    pad = function(val, len) {
        val = String(val);
        len = len || 2;
        while (val.length < len) val = "0" + val;
        return val;
    };
    
    var myArray = jsonDateString.match(re);
    if (myArray == null) {
        return "";
    }
    else {
        var dDate = new Date();
        var millisecondsSinceEpoch = parseInt(myArray[1]);
        var AXSTime = 0;
        dDate.setTime(millisecondsSinceEpoch + AXSTime);
        //dDate.setTime(millisecondsSinceEpoch);
        return pad(dDate.getMonth() + 1, 2) + "/" + pad(dDate.getDate(), 2) + "/" + dDate.getFullYear();
        }
    }

function GetAMPMShowTime(jsonDateString) {
    
    var re = new RegExp("\/Date[(]([0-9]+)");

    pad = function(val, len) {
        val = String(val);
        len = len || 2;
        while (val.length < len) val = "0" + val;
        return val;
        };
        
    var myArray = jsonDateString.match(re);
        if (myArray == null) {
            return "";
        }
        else {
            var dDate = new Date();
            var millisecondsSinceEpoch = parseInt(myArray[1]);
            var AXSTime = 0;
            dDate.setTime(millisecondsSinceEpoch + AXSTime);
            //dDate.setTime(millisecondsSinceEpoch);
            return (dDate.getHours() % 12 || 12) + ":" + pad(dDate.getMinutes()) + " " + (dDate.getHours() < 12 ? "AM" : "PM");
        }
    }
	
	TVListings.Render = function(jsonData, container) { 
                            var tvltype = typeof (jsonData);
                            var lastDate = "";
                            if (tvltype != "undefined") {
                              for (var i=0; i < 1; i++) {
                                if(jsonData.TelevisionListingData[0].prog_title && jsonData.TelevisionListingData[1].prog_title){
                                   /* html += "<dt>" + GetAMPMShowTime(jsonData.TelevisionListingData[i].web_datetime) + "</dt>" + "<dd>" + jsonData.TelevisionListingData[i].parent_title + "</dd>";*/
								   setNow(jsonData.TelevisionListingData[0].parent_title);
								   setNext(jsonData.TelevisionListingData[1].parent_title + " - " + GetAMPMShowTime(jsonData.TelevisionListingData[1].web_datetime)+ " ET");
									
                                }                                  
                              }
                            }

                           
                            
                            }	

                            var pNow=null;
							var pNext=null;
							
							function setNow(data){
								pNow=data;
							}
							function setNext(data){
								pNext=data;
							}
							
							function playingNow()
							{
								document.write(pNow);
							}
							function playingNext()
							{
								document.write(pNext);
								
							}
							
							//0-based month (0 = January, 1 = February, etc.) var startDate = new Date();
                            var startDate = new Date();

                            var endDate = new Date();
                            endDate.setDate(endDate.getDate()+1);
							
                            TVListings.Fetch('muchmusic', startDate.getTime() - 3600000, endDate.getTime(), '', '2');