//  TV Listings right-column module
PEOPLE.TVModule = {
    init : function() { // let's get started
        PEOPLE.TVModule.inserttimezones();
        PEOPLE.TVModule.adddayofweek();
		PEOPLE.TVModule.getPTGrids();
        PEOPLE.TVModule.toggleGrid();
	},
	inserttimezones : function() { // create time zones HTML and insert into DOM
		var html = '<div id="select-tz">'
					+ '<ul>'
						+ '<li><span>All Time Zones:</span></li>'
						+ '<li id="EST" class="active toggle-grid"><a href="#" title="View Times for Eastern Standard Time Zone">EST</a></li>'
						+ '<li id="CST" class="toggle-grid"><a href="#" title="View Times for Central Standard Time Zone">CST</a></li>'
						+ '<li id="MST" class="toggle-grid"><a href="#" title="View Times for Mountain Standard Time Zone">MST</a></li>'
						+ '<li id="PST" class="toggle-grid"><a href="#" title="View Times for Pacific Standard Time Zone">PST</a></li>'
					+ '</ul>'
				+ '</div>'
				+ '<div id="ptg"></div>'; // empty DIV for actual grid
		$('#tvgrid').html(html);
	},
	getPTGrids : function() { // requests grids and injects into containers
        var ptg = $('#ptg');

        ptg.append('<div id="eastern" class="grid default"></div><div id="central" class="grid"></div><div id="mountain" class="grid"></div><div id="pacific" class="grid"></div><div id="cached-grid" class="grid"></div>');

        //eastern
        $.ajax({
            url: 'http://api.zap2it.com/tvlistings/webservices/primetimeGrid?rty=html&aid=people&zip=12345&stnlt=10003,10991,10212,10098,51306,10057,10986',
            success: function(data){
                cacheAndRender(data, '#eastern');
            }
        });
        //central
        $.ajax({
            url: 'http://api.zap2it.com/tvlistings/webservices/primetimeGrid?rty=html&aid=people&zip=60611&stnlt=17080,17079,13461,17084,51337,10057,10986',
            success: function(data){
                cacheAndRender(data, '#central');
            }
        });
        //mountain
        $.ajax({
            url: 'http://api.zap2it.com/tvlistings/webservices/primetimeGrid?rty=html&aid=people&zip=79821&stnlt=33571,29073,17221,32766,51338,31555,10987',
            success: function(data){
                cacheAndRender(data, '#mountain');
            }
        });
        //pacific
        $.ajax({
            url: 'http://api.zap2it.com/tvlistings/webservices/primetimeGrid?rty=html&aid=people&zip=98101&stnlt=10004,10099,12351,12376,51336,31555,10987',
            success: function(data){
                cacheAndRender(data, '#pacific');
            }
        });

        function cacheAndRender(data, id) { // get just the table, not the javascript and strip escape characters
            var cachedGrid = $('#cached-grid'),
                theTable,
                replace;

            cachedGrid.html(data);
            theTable = $('#cached-grid table');
            replace = theTable.html().replace(/\\'/g, "'");
            theTable.html(replace);
            $(id).html(theTable);
            cachedGrid.html('');
        }

        PEOPLE.TVModule.addcid('ptg'); // add "cid" variable to all links
	},
    toggleGrid : function() {
        $('.toggle-grid').click(function(e){
            var $this = $(this),
                id = $this.attr('id'),
                toggleGrid = $('.toggle-grid'),
                grid = $('.grid'),
                eastern = $('#eastern'),
                central = $('#central'),
                pacific = $('#pacific'),
                mountain = $('#mountain');

            toggleGrid.removeClass('active');
            $this.addClass('active');

            grid.hide();

            switch(id) {
                case 'EST':
                    eastern.show();
                    break;
                case 'CST':
                    central.show();
                    break;
                case 'MST':
                    mountain.show();
                    break;
                case 'PST':
                    pacific.show();
            }

            e.preventDefault();
        });
    },
	addcid : function(id) { // adds cid=tvlmodule to links in specified ID
		if (!id || $('#'+id).length < 1) {return;}
		var cid = 'cid=tvlmodule';
		var As = $('#'+id).find('a');
		for (i = 0; i < As.length; i++) {
			var a = As[i];
			if (a.href.indexOf('cid=') > 0) {
				continue;
			} else if (a.href.indexOf('?') > 0) {
				a.href = a.href + '&' + cid;
			} else {
				a.href = a.href + '?' + cid;
			}
		}
	},
	adddayofweek : function() {
		if ($('#ptgrid').length < 1) {return;}
		var p = $('#ptgrid'),
			d = new Date(),
			w = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
			h = '<span id="dayofweek">'+w[d.getDay()]+'</span>';
		p.append(h);
	}
};
$(document).ready(function(){
    PEOPLE.TVModule.init();
});
