﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../_webservices/tvschedule.asmx"/>

function getCurrent() {
    TVSchedule.GetOnNow('54.1', nowFound, OnTVScheduleFailed);
    TVSchedule.GetOnNow('CREATE', nowFound, OnTVScheduleFailed);
    TVSchedule.GetOnNow('54.3', nowFound, OnTVScheduleFailed);
}

function nowFound(foundResult, userContext, methodName) {
    try {
        showAdded(foundResult[0].ser_title, foundResult[0].pgTitle);
    } catch (e) { }
    //alert("NowOn" + foundResult[0].ai_virt_chnl.replace(".", "_") + document.getElementById("NowOn" + foundResult[0].ai_virt_chnl.replace(/\s+$/,"")))
    try {    
    document.getElementById("NowOn" + foundResult[0].ai_virt_chnl.replace(/\s+$/, "")).innerHTML = "<div class='title'>" + toProperCase2(foundResult[0].ser_title) + " " + foundResult[0].vsn_rating + "</div>" + (foundResult[0].pgTitle == "" ? "" : "<div class='subtitle'>" + toProperCase2(foundResult[0].pgTitle) + "</div>") + (foundResult[0].pde_text.ltrim() == "" ? "" : "<div id='descrip_" + foundResult[0].ai_vsn_id + "' class='details' onclick='moreShowDescription(" + foundResult[0].ai_vsn_id + ")' ><a>[show description]</a></div>") + (foundResult[0].amazonSmallImg == "" ? "" : "<a href='" + foundResult[0].amazonURL + "'><img src='" + foundResult[0].amazonSmallImg + "' border='0' target='_blank' /></a><br>") + (foundResult[0].hasCat == 0 ? "" : "<a href='/main/AmazonStore/loadAmazonCatalog.aspx?vsnid=" + foundResult[0].ai_vsn_id + "'>Coming Soon=>See Related Items for Sale</a>");
    } catch (e) { }
}
function moreShowDescription(id) {
    if (document.getElementById("descrip_" + id).innerHTML.indexOf("[close]") < 1) {
        document.getElementById("descrip_" + id).innerHTML = "Loading Data ...";
        TVSchedule.getExpandedDescription(id, expandDescription, OnTVScheduleFailed);
    } else {
    lessShowDescription(id);
    }
}
function lessShowDescription(id) {
    document.getElementById("descrip_" + id).innerHTML = "<a>[show description]</a>";
}
function getNextOn() {
    TVSchedule.NextOn('54.1', nextFound, OnTVScheduleFailed);
    TVSchedule.NextOn('CREATE', nextFound, OnTVScheduleFailed);
    TVSchedule.NextOn('54.3', nextFound, OnTVScheduleFailed);
}
function expandDescription(results, userContext, methodName) {
    var id = results[0].id;
    var descriptionTxt = "";
    for(var z=0;z<results.length;z++){
        descriptionTxt+=results[z].pde_text + (results[z].pde_text.ltrim==""?"<br>":"");
    }
    document.getElementById("descrip_" + id).innerHTML = descriptionTxt + " <a>[close]</a>";
   
}
function nextFound(foundResult, userContext, methodName) {
    try {
        showAdded(foundResult[0].ser_title, foundResult[0].pgTitle);
    } catch (e) { }
    try {
        document.getElementById("NextOn" + foundResult[0].ai_virt_chnl.replace(/\s+$/, "")).innerHTML = "<div class='title'>" + toAMPM(foundResult[0].ai_air_time) + " " + toProperCase2(foundResult[0].ser_title) + " " + foundResult[0].vsn_rating + "</div>" + (foundResult[0].pgTitle == "" ? "" : "<div class='subtitle'>" + toProperCase2(foundResult[0].pgTitle) + "</div>") + (foundResult[0].pde_text.ltrim() == "" ? "" : "<div id='descrip_" + foundResult[0].ai_vsn_id + "' class='details' onclick='moreShowDescription(" + foundResult[0].ai_vsn_id + ")' ><a>[show description]</a></div>");
    } catch (e) { }
}
function getTonightOn() {
    sdte = new Date();
    edte = new Date();
    sdte.setHours(20,0,0,0)
    edte.setHours(22, 0, 0, 0)
    TVSchedule.GetSchedule('54.1', sdte.toLocaleString(), edte.toLocaleString(), tonightFound, OnTVScheduleFailed)
    TVSchedule.GetSchedule('CREATE', sdte.toLocaleString(), edte.toLocaleString(), tonightFound, OnTVScheduleFailed)
    TVSchedule.GetSchedule('54.3', sdte.toLocaleString(), edte.toLocaleString(), tonightFound, OnTVScheduleFailed)
}
function tonightFound(foundResult, userContext, methodName) {
    
    for (var z = 0; z < foundResult.length; z++) {
        addTonight(foundResult[z]);
    }
}
function addTonight(show) {
    try {
        showAdded(show.ser_title, show.pgTitle);
    } catch (e) { }
    try {
        document.getElementById("tonight" + show.ai_virt_chnl.replace(/\s+$/, "")).innerHTML += "<div class='title'>" + toAMPM(show.ai_air_time) + " " + toProperCase2(show.ser_title) + " " + show.vsn_rating + "</div>" + (show.pgTitle == "" ? "" : "<div class='subtitle'>" + toProperCase2(show.pgTitle) + "</div>") + (show.pde_text.ltrim() == "" ? "" : "<div id='descrip_" + show.ai_vsn_id + "' class='details' onclick='moreShowDescription(" + show.ai_vsn_id + ")' ><a>[show description]</a></div>") + (show.amazonSmallImg == "" ? "" : "<a href='" + show.amazonURL + "'>Buy Now!<br><img src='" + show.amazonSmallImg + "' border='0' target='_blank' /></a><br>") + (show.hasCat == 0 ? "" : "<a href='/main/AmazonStore/loadAmazonCatalog.aspx?vsnid=" + show.ai_vsn_id + "'>Coming Soon=>See Related Items for Sale</a>") + "<br>";
    } catch (e) { }
}
function toAMPM(tme) {
    var split = tme.split(":");
    var retstring = new String()
    dte = new Date();
    dte.setHours(split[0], split[1], split[2], 0);
    retstring = dte.toLocaleTimeString();
    return retstring.replace(":00 "," ");
}
function initTVSchedule() {
    getCurrent()
    getNextOn()
    getTonightOn()
}
function toProperCase(s) {
    return s.toLowerCase().replace(/^(.)|\s(.)/g,
          function($1) { return $1.toUpperCase(); });
}
function toProperCase2(S) {
    var original = S;
    original = original.toLowerCase();
    var o_split = original.split(' ');
    
    //this probably isn't a complete list of words that shouldn't be capitalized
    var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');
    for (i = 0; i < o_split.length; i++) {
        if (i == 0) {
            //always capitalize the first word
            
            o_split[i] = (o_split[i].substring(0, 1)).toUpperCase() + o_split[i].substring(1);
        }
        
        else if (special_words.indexOf(o_split[i]) < 0) {
            o_split[i] = (o_split[i].substring(0, 1)).toUpperCase() + o_split[i].substring(1);
        }
    }
    retval = o_split.join(' ');
    return retval.replace("Pbs", "PBS");
}
[ ].indexOf || (Array.prototype.indexOf = function(v) {
    for (var i = this.length; i-- && this[i] !== v; );
    return i;
});
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

function OnTVScheduleFailed(error, userContext, methodName) {
    //alert(error.get_message() + "\n" + userContext + "\n" + methodName);
}
