﻿/*
Author: Karlos Collazo karlosc@libertypr.com
Date: Mar 2011
Description: This .js makes possible the creation of dynamic objects and elements for the Programming Guide (IPG)
*/

//Create Dynamics IDs
var child = 1;
var parentID;

/* ---------------------------------------- General Functions ------------------------------------------ */
/*
Function by: Ariel Flesler
Decription:
This function detects and scroll screen to specific position
*/
function isScrolledIntoView(elem) {
    var docViewTop = $("#browse_listing_results").scrollTop();
    var docViewBottom = docViewTop + $("#browse_listing_results").height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

function isScrolledIntoViewSearch(elem) {
    var docViewTop = $("#search_listing_results").scrollTop();
    var docViewBottom = docViewTop + $("#search_listing_results").height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

/*
Function by: 
David Gouch <http://individed.com>
Description: 
This function get the string to "Camel Case"

*/
String.prototype.toTitleCase = function () {
    return this.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function (match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" &&
        	match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1)
            return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1)
            return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 ||
        	title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
            return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

/*
Function by:
Carmelo Collazo ccollazo@libertypr.com
Description:
This function closes the overlay content when show enabled
*/
function Close_Popup() {
    $('#popup').fadeOut('fast');
    $('#popup').css('z-index', '-500');
    $('#window').fadeOut('fast');
    $('#clsBtn').fadeOut('fast');
}

function hideDetail(elem) {
    //console.log("hideDetail()", elem);
    var parent = elem.parentNode;
    var localChild = elem.childNodes[0];

    $(localChild).fadeOut("fast");

}

/* ---------------------------------------- Browse Container ------------------------------------------- */
//Dynamic Division creation
function createDiv(div_id_parent, first, last) {
    var beginwith = first;
    var endwith = last

    $(document).ready(function () {
        $(".browse_listing_container").mask("<br />Loading...");

        $.getJSON('../television/getBrowseData.aspx', { start: first, end: last }, function (data) {
            var divParent = document.getElementById(div_id_parent);

            var month = new Array(12);
            month[0] = "January";
            month[1] = "February";
            month[2] = "March";
            month[3] = "April";
            month[4] = "May";
            month[5] = "June";
            month[6] = "July";
            month[7] = "August";
            month[8] = "September";
            month[9] = "October";
            month[10] = "November";
            month[11] = "December";

            var day = new Array(7);
            day[0] = "Sun";
            day[1] = "Mon";
            day[2] = "Tue";
            day[3] = "Wed";
            day[4] = "Thu";
            day[5] = "Fri";
            day[6] = "Sat";

            $.each(data.posts, function (i, post) {
                child++;
                var divChild = document.createElement("div");
                var dateStr = post.schedule;
                dateStr = dateStr.substr(0, 2) + '-' + dateStr.substr(2, 2) + '-' + dateStr.substr(4, 4)
                var dateObj = new Date(dateStr);

                var curDate = day[dateObj.getDay()] + ', ' + month[dateObj.getMonth()] + ' ' + dateObj.getDate()

                divChild.id = "myDiv";
                divChild.setAttribute('id', post.id); //div_id_parent + child);
                divChild.style.margin = "0px auto";
                divChild.className = "browse_listing_blocks2";
                divChild.innerHTML = "<div id='title' class='browse_listing_title_result'>" + post.title + "</div>    " +
                                     "<div id='schedule' class='browse_listing_schedule_result'>" + curDate + "</div>";

                divParent.appendChild(divChild);

                $(".browse_listing_container").unmask();
            });

            $("div.browse_listing_blocks2").click(function (event) {

                console.log("div.browse_listing_blocks2", this);

                if ($("#browse_listing_result_details").is(":hidden")) {
                    console.log("    browse_listing_blocks2 hidden()");
                    $(event.currentTarget).append($("#browse_listing_result_details"));
                    //$("#browse_listing_result_details").insertAfter(event.currentTarget);
                    $("#browse_listing_result_details").slideDown("fast", function () {
                        showDetail(this);
                    });
                    currentListing = this;
                }
                else {
                    if (this == currentListing) {
                        console.log("    browse_listing_blocks2 shown() this == currentListing");

                        console.log("Inside Listing Block 2");
                        //If currently visible hide detail child 
                        hideDetail($("#browse_listing_result_details_child")[0]);
                        $("#browse_listing_result_details_child").slideUp();
                        tempListing = undefined;

                        //Hide Parent Result
                        hideDetail($("#browse_listing_result_details")[0]);
                        $("#browse_listing_result_details").slideUp();
                        currentListing = undefined;
                    }
                    else {
                        console.log("    browse_listing_blocks2 shown() this != currentListing", $("#browse_listing_result_details_child"));
                        //If currently visible hide detail child 
                        hideDetail($("#browse_listing_result_details_child")[0]);
                        $("#browse_listing_result_details_child").slideUp();

                        //Hide Parent Result and show nodes on next division
                        hideDetail($("#browse_listing_result_details")[0]);
                        $("#browse_listing_result_details").slideUp(function () {
                            //$("#browse_listing_result_details").insertAfter(event.currentTarget);
                            $(event.currentTarget).append($("#browse_listing_result_details"));
                            $("#browse_listing_result_details").slideDown("fast", function () {
                                showDetail(this);
                            });
                        });

                        currentListing = this;
                    }
                }

                if (!event) var event = window.event;
                event.cancelBubble = true;
                if (event.stopPropagation) event.stopPropagation();
            });
        });

    });
}


function showDetail(elem) {
    var parent = elem.parentNode;
    var detail = elem.childNodes[0];
    var detailChild = 1;

    console.log("Parent:", parent);

    //console.log(isScrolledIntoView($("#browse_listing_result_details")));
    if (!isScrolledIntoView($("#browse_listing_result_details"))) {
        $("#browse_listing_results").scrollTo(parent, "fast");
    }

    //Move our #browse_listing_result_details_child elsewhere before clearing the resultParent
    $("#browse_listing_result_details_child").hide();
    document.getElementById("browse_main_container").appendChild(document.getElementById("browse_listing_result_details_child"));
    detail.innerHTML = "";

    //Retrieve data for current node
    var resultdivID = $(parent).attr("id");
    parentID = resultdivID;
    console.log("showDetail()", resultdivID, elem);

    $.getJSON('../television/getElementData.aspx', { id: resultdivID }, function (detaildata) {
        var divDetailParent = detail;

        var month = new Array(12);
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";

        var day = new Array(7);
        day[0] = "Sun";
        day[1] = "Mon";
        day[2] = "Tue";
        day[3] = "Wed";
        day[4] = "Thu";
        day[5] = "Fri";
        day[6] = "Sat";

        $.each(detaildata.posts, function (detaili, detailpost) {
            detailChild++;
            var divDetailChild = document.createElement("div");

            //***Fix to Day & Time***
            var dateStr = detailpost.schedule;
            dateStr = dateStr.substr(0, 2) + '-' + dateStr.substr(2, 2) + '-' + dateStr.substr(4, 4);
            var dateObj = new Date(dateStr);
            var curDate = new Date();
            var result = dateObj.getDate() - curDate.getDate();
            var datetopresent;
            var timeStr = detailpost.time;
            var tHour = timeStr.substr(0, 2);
            var tMin = timeStr.substr(2, 2);
            var myTime;
            var title;

            if (result == 0) {
                datetopresent = "Today";
            }
            else if (result == 1) {
                datetopresent = "Tomorrow";
            }
            else {
                datetopresent = day[dateObj.getDay()] + ', ' + month[dateObj.getMonth()] + ' ' + dateObj.getDate()
            }

            if (timeStr >= 1300) {
                tHour = tHour - 12
                myTime = tHour + ':' + tMin + ' PM'
            }
            else {
                if (timeStr >= 1200) {
                    myTime = tHour + ':' + tMin + ' PM'
                }
                else if (timeStr == 0000) {
                    tHour = 12
                    myTime = tHour + ':' + tMin + ' AM'
                }
                else {
                    myTime = tHour + ':' + tMin + ' AM'
                }
            }

            if (detailpost.episode == '') {
                title = detailpost.title;
            }
            else {
                title = detailpost.episode
            }

            divDetailChild.id = "myDiv";
            divDetailChild.setAttribute('id', detailpost.id + detailpost.channelid); //divDetailParent.id + detailChild); //div_id_parent + child);
            divDetailChild.style.margin = "0px auto";
            divDetailChild.className = "browse_listing_title_result_detail_parent";
            divDetailChild.innerHTML = "<div id='title' class='browse_listing_title_result'>" + title + "</div>" +
                                       "<div id='schedule' class='browse_listing_schedule_result'>" + datetopresent + " " + myTime + "</div>";

            divDetailParent.appendChild(divDetailChild);
            //console.log("inside");
        });
        $(detail).fadeIn("fast");

        $(".browse_listing_title_result_detail_parent").click(function (event) {
            console.log("browse_listing_title_result_detail_parent click()");

            if ($("#browse_listing_result_details_child").is(":hidden")) {
                console.log("   browse_listing_result_details_child hidden()");

                $(event.currentTarget).append($("#browse_listing_result_details_child"));
                $("#browse_listing_result_details_child").slideDown("fast", function () {
                    showDetailChild($("#browse_listing_result_details_child")[0]);
                });
                currentlistingChild = this;
                tempListing = parent;

            }
            else {
                console.log("Shown: ", event.currentTarget);
                console.log("This Object: ", this);
                console.log("Parent: ", parent);

                if (this == currentlistingChild) {
                    console.log("Parent: ", parent);
                    hideDetail($("#browse_listing_result_details_child")[0]);
                    $("#browse_listing_result_details_child").slideUp();
                    tempListing = undefined;
                }
                else {
                    hideDetail($("#browse_listing_result_details_child")[0]);
                    $("#browse_listing_result_details_child").slideUp();
                    tempListing = undefined;

                    $(event.currentTarget).append($("#browse_listing_result_details_child"));
                    $("#browse_listing_result_details_child").slideDown("fast", function () {
                        showDetailChild($("#browse_listing_result_details_child")[0]);
                    });
                    currentlistingChild = this;
                    tempListing = parent;
                }
            }

            if (!event) var event = window.event;
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
        });

        $("#browse_listing_result_details_child").click(function (event) {
            if (!event) var event = window.event;
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();

        });
    });
}

function showDetailChild(args) {
    var parent = args.parentNode;
    var child = args.childNodes[0];

    if (!isScrolledIntoView($("#browse_listing_result_details_child"))) {
        $("#browse_listing_title_result").scrollTo(parent, "fast");
    }

    console.log("Parent ID: " + parent.id);

    $.getJSON('../television/getElementData.aspx', { id: parentID }, function (data) {
        console.log(" Test", data);

        $.each(data.posts, function (i, json) {
            var loopchildid = json.id + json.channelid;
            var localDuration;
            var fullInfoID = 'PID' + json.id + 'CHID' + json.channelid;
            console.log(loopchildid);
            console.log('TOKEN', fullInfoID);

            if (loopchildid == parent.id) {

                //Prepare Duration
                localDuration = json.duration;

                if (localDuration < 100) {
                    localDuration = localDuration.substring(2, 4) + " min";
                }

                else {
                    localDuration = localDuration.substring(0, 2) + ":" + localDuration.substring(2, 4) + " hr";
                }

                //Check for closed captioned
                cc = json.closedcap;
                console.log('CC HERE', json.closedcap)

                if (cc == "Closed Captioned") {

                    cc = " <img src='../images/television/closed_caption_icon.png'> ";
                }

                HD = json.definition;
                console.log('HD Here', HD)

                if (HD == "HD") {

                    HD = " <img src='../images/television/hdtv_icon.png'> ";
                }

                //Audience = json.audience
                Audience = "";

                //Audience = " <img src='../images/television/pg_icon.png'> ";

                //Channel Info
                var ChannelName = json.channelnm;
                var ChannelID = json.channelid;
                var TopString = ChannelID + " " + ChannelName.toTitleCase() + " | Duration: " + localDuration + " | ";

                //Program Detail               
                var detail = json.description;

                //Creates the inner HTMLs for child details
                child.innerHTML = "<div id='title' class='browse_listing_schedule_topinformation'>" + TopString +
                                    " " + HD + " " + Audience + " " + cc + "</div>" +

                                    "<div id='schedule' class='browse_listing_schedule_bottominformation'>" + detail + "</div>" +
                                    "<div id='InfoButton' class='browse_listing_info_button'>" +
                                    "<a>" +
                                    "<img src='../images/television/more_details_button.png'></a>" +
                                    "</div>";

                //Opens Overlay          
                $("#InfoButton").click(function () {
                    $('#window').load('getFullInfo.aspx', fullInfoID, function () {
                        $('#popup').css('z-index', '10');
                        $('#popup').css('visibility', 'visible');
                        $('#popup').fadeIn('fast');
                        $('#window').css('visibility', 'visible');
                        $('#window').fadeIn('fast');
                        $('#clsBtn').css('visibility', 'visible');
                        $('#clsBtn').fadeIn('fast');
                    });
                });

                //Closes Open Overlay
                $("#popup").click(function () {
                    Close_Popup();
                });

                //Closes Open Overlay
                $("#clsBtn").click(function () {
                    Close_Popup();
                });
            }
        });
    });
    $(child).fadeIn("fast");
}

/* ---------------------------------------- Search Container Functions ------------------------------------------ */
function showResults(div_id_parent, string) {
    console.log('Hit Show');
    $(document).ready(function () {
        $(".search_result_main").mask("<br />Loading...");

        $.getJSON('../television/getSearchData.aspx', { str: string }, function (data) {
            var divParent = document.getElementById(div_id_parent);

            var month = new Array(12);
            month[0] = "January";
            month[1] = "February";
            month[2] = "March";
            month[3] = "April";
            month[4] = "May";
            month[5] = "June";
            month[6] = "July";
            month[7] = "August";
            month[8] = "September";
            month[9] = "October";
            month[10] = "November";
            month[11] = "December";

            var day = new Array(7);
            day[0] = "Sun";
            day[1] = "Mon";
            day[2] = "Tue";
            day[3] = "Wed";
            day[4] = "Thu";
            day[5] = "Fri";
            day[6] = "Sat";

            $.each(data.posts, function (i, post) {
                child++;
                var divChild = document.createElement("div");
                var dateStr = post.schedule;
                dateStr = dateStr.substr(0, 2) + '-' + dateStr.substr(2, 2) + '-' + dateStr.substr(4, 4)
                var dateObj = new Date(dateStr);

                var curDate = day[dateObj.getDay()] + ', ' + month[dateObj.getMonth()] + ' ' + dateObj.getDate()

                divChild.id = "myDiv";
                divChild.setAttribute('id', post.id); //div_id_parent + child);
                divChild.style.margin = "0px auto";
                divChild.className = "search_listing_blocks2";
                divChild.innerHTML = "<div id='title' class='search_listing_title_result'>" + post.title + "</div>    " +
                                     "<div id='schedule' class='search_listing_schedule_result'>" + curDate + "</div>";

                divParent.appendChild(divChild);

                $(".search_result_main").unmask();
            });

            $("div.search_listing_blocks2").click(function (event) {
                if ($("#search_listing_result_details").is(":hidden")) {
                    $(event.currentTarget).append($("#search_listing_result_details"));
                    //$("#browse_listing_result_details").insertAfter(event.currentTarget);
                    $("#search_listing_result_details").slideDown("fast", function () {
                        showSearchDetail(this);
                    });
                    currentListing = this;
                }
                else {
                    if (this == currentListing) {
                        //If currently visible hide detail child 
                        hideDetail($("#search_listing_result_details_child")[0]);
                        $("#search_listing_result_details_child").slideUp();
                        tempListing = undefined;

                        //Hide Parent Result
                        hideDetail($("#search_listing_result_details")[0]);
                        $("#search_listing_result_details").slideUp();
                        currentListing = undefined;
                    }
                    else {
                        //If currently visible hide detail child 
                        hideDetail($("#search_listing_result_details_child")[0]);
                        $("#search_listing_result_details_child").slideUp();

                        //Hide Parent Result and show nodes on next division
                        hideDetail($("#search_listing_result_details")[0]);
                        $("#search_listing_result_details").slideUp(function () {
                            //$("#browse_listing_result_details").insertAfter(event.currentTarget);
                            $(event.currentTarget).append($("#search_listing_result_details"));
                            $("#search_listing_result_details").slideDown("fast", function () {
                                showSearchDetail(this);
                            });
                        });

                        currentListing = this;
                    }
                }

                if (!event) var event = window.event;
                event.cancelBubble = true;
                if (event.stopPropagation) event.stopPropagation();
            });
        });

    });
}

function showSearchDetail(elem) {
    console.log('Hit Search Detail');

    var parent = elem.parentNode;
    var detail = elem.childNodes[0];
    var detailChild = 1;

    //console.log("Parent:",parent);

    //console.log(isScrolledIntoView($("#browse_listing_result_details")));
    if (!isScrolledIntoViewSearch($("#search_listing_result_details"))) {
        $("#search_listing_results").scrollTo(parent, "fast");
    }

    //Move our #browse_listing_result_details_child elsewhere before clearing the resultParent
    $("#search_listing_result_details_child").hide();
    document.getElementById("main_container").appendChild(document.getElementById("search_listing_result_details_child"));
    detail.innerHTML = "";

    //Retrieve data for current node
    var resultdivID = $(parent).attr("id");
    parentID = resultdivID;
    //console.log("showDetail()", resultdivID, elem);

    $.getJSON('../television/getElementData.aspx', { id: resultdivID }, function (detaildata) {
        var divDetailParent = detail;

        var month = new Array(12);
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";

        var day = new Array(7);
        day[0] = "Sun";
        day[1] = "Mon";
        day[2] = "Tue";
        day[3] = "Wed";
        day[4] = "Thu";
        day[5] = "Fri";
        day[6] = "Sat";

        $.each(detaildata.posts, function (detaili, detailpost) {
            detailChild++;
            var divDetailChild = document.createElement("div");

            //***Fix to Day & Time***
            var dateStr = detailpost.schedule;
            dateStr = dateStr.substr(0, 2) + '-' + dateStr.substr(2, 2) + '-' + dateStr.substr(4, 4);
            var dateObj = new Date(dateStr);
            var curDate = new Date();
            var result = dateObj.getDate() - curDate.getDate();
            var datetopresent;
            var timeStr = detailpost.time;
            var tHour = timeStr.substr(0, 2);
            var tMin = timeStr.substr(2, 2);
            var myTime;
            var title;

            if (result == 0) {
                datetopresent = "Today";
            }
            else if (result == 1) {
                datetopresent = "Tomorrow";
            }
            else {
                datetopresent = day[dateObj.getDay()] + ', ' + month[dateObj.getMonth()] + ' ' + dateObj.getDate()
            }

            if (timeStr >= 1300) {
                tHour = tHour - 12
                myTime = tHour + ':' + tMin + ' PM'
            }
            else {
                if (timeStr >= 1200) {
                    myTime = tHour + ':' + tMin + ' PM'
                }
                else if (timeStr == 0000) {
                    tHour = 12
                    myTime = tHour + ':' + tMin + ' AM'
                }
                else {
                    myTime = tHour + ':' + tMin + ' AM'
                }
            }

            if (detailpost.episode == '') {
                title = detailpost.title;
            }
            else {
                title = detailpost.episode
            }

            divDetailChild.id = "myDiv";
            divDetailChild.setAttribute('id', detailpost.id + detailpost.channelid); //divDetailParent.id + detailChild); //div_id_parent + child);
            divDetailChild.style.margin = "0px auto";
            divDetailChild.className = "search_listing_title_result_detail_parent";
            divDetailChild.innerHTML = "<div id='title' class='search_listing_title_result'>" + title + "</div>" +
                                       "<div id='schedule' class='search_listing_schedule_result'>" + datetopresent + " " + myTime + "</div>";

            divDetailParent.appendChild(divDetailChild);
            //console.log("inside");
        });
        $(detail).fadeIn("fast");

        $(".search_listing_title_result_detail_parent").click(function (event) {
            if ($("#search_listing_result_details_child").is(":hidden")) {
                $(event.currentTarget).append($("#search_listing_result_details_child"));
                $("#search_listing_result_details_child").slideDown("fast", function () {
                    showSearchDetailChild($("#search_listing_result_details_child")[0]);
                });
                currentlistingChild = this;
                tempListing = parent;

            }
            else {
                console.log("Shown: ", event.currentTarget);
                console.log("This Object: ", this);
                console.log("Parent: ", parent);

                if (this == currentlistingChild) {
                    console.log("Parent: ", parent);
                    hideDetail($("#search_listing_result_details_child")[0]);
                    $("#search_listing_result_details_child").slideUp();
                    tempListing = undefined;
                }
                else {
                    hideDetail($("#search_listing_result_details_child")[0]);
                    $("#search_listing_result_details_child").slideUp();
                    tempListing = undefined;

                    $(event.currentTarget).append($("#search_listing_result_details_child"));
                    $("#search_listing_result_details_child").slideDown("fast", function () {
                        showSearchDetailChild($("#search_listing_result_details_child")[0]);
                    });
                    currentlistingChild = this;
                    tempListing = parent;
                }
            }

            if (!event) var event = window.event;
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
        });

        $("#search_listing_result_details_child").click(function (event) {
            if (!event) var event = window.event;
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();

        });
    });
}

function showSearchDetailChild(args) {
    console.log('Hit Child Search Detail');
    var parent = args.parentNode;
    var child = args.childNodes[0];

    if (!isScrolledIntoViewSearch($("#search_listing_result_details_child"))) {
        $("#search_listing_title_result").scrollTo(parent, "fast");
    }

    $.getJSON('../television/getElementData.aspx', { id: parentID }, function (data) {
        $.each(data.posts, function (i, json) {
            var loopchildid = json.id + json.channelid;
            var localDuration;
            console.log(loopchildid);

            if (loopchildid == parent.id) {
                localDuration = json.duration;

                if (localDuration < 100) {
                    localDuration = localDuration.substring(2, 4) + " min";
                }

                else {
                    localDuration = localDuration.substring(0, 2) + ":" + localDuration.substring(2, 4) + " hr";
                }

                //Channel Info
                var ChannelName = json.channelnm;
                var ChannelID = json.channelid;
                var TopString = ChannelID + " " + ChannelName.toTitleCase() + " | Duration: " + localDuration + " | ";


                //Program Detail               
                var detail = json.description;
                var fullInfoID = 'PID' + json.id + 'CHID' + json.channelid;

                //Check for closed captioned
                cc = json.closedcap;
                console.log('CC HERE', json.closedcap)

                if (cc == "Closed Captioned") {

                    cc = " <img src='../images/television/closed_caption_icon.png'> ";
                }

                HD = json.definition;
                console.log('HD Here', HD)

                if (HD == "HD") {

                    HD = " <img src='../images/television/hdtv_icon.png'> ";
                }

                //Audience = json.audience
                Audience = "";

                //Audience = " <img src='../images/television/pg_icon.png'> ";

                //Creates the inner HTMLs for child details
                child.innerHTML = "<div id='title' class='search_listing_schedule_topinformation'>" + TopString +
                                    " " + HD + " " + Audience + " " + cc + "</div>" +

                                    "<div id='schedule' class='search_listing_schedule_bottominformation'>" + detail + "</div>" +
                                    "<div id='InfoButton' class='search_listing_info_button'>" +
                                    "<div>" +
                                    "<img src='../images/television/more_details_button.png'></div>" +
                                    "</div>";

                //Opens Overlay          
                $("#InfoButton").click(function () {
                    $('#window').load('getFullInfo.aspx', fullInfoID, function () {
                        $('#popup').css('z-index', '10');
                        $('#popup').css('visibility', 'visible');
                        $('#popup').fadeIn('fast');
                        $('#window').css('visibility', 'visible');
                        $('#window').fadeIn('fast');
                        $('#clsBtn').css('visibility', 'visible');
                        $('#clsBtn').fadeIn('fast');
                    });
                });

                //Closes Open Overlay
                $("#popup").click(function () {
                    Close_Popup();
                });

                //Closes Open Overlay
                $("#clsBtn").click(function () {
                    Close_Popup();
                });
            }
        });
    });
    $(child).fadeIn("fast");
}

/* ---------------------------------------- TV-Listing Container ------------------------------------------- */
function createTVListing(parentDiv) {
    $(document).ready(function () {
        $(".tvlisting_result_container").mask("<br />Loading...");

        $.getJSON('../television/getChannel.aspx', function (data) {
            //console.log('Hit TV Listing .getJSON');
            var divParent = document.getElementById(parentDiv);

            $.each(data.posts, function (i, post) {
                //console.log('Hit TV Listing .each');             
                var divChild = document.createElement("div");

                divChild.id = "myDiv";
                divChild.setAttribute('id', post.channelID);
                divChild.style.margin = "0px auto";
                divChild.className = "tvlisting_result_line";
                divChild.innerHTML = "<div class='tvlisting_result_channel'>" +
                                     "<div id='name' class='tvlisting_result_channel_name'>" + post.channelName + "</div>    " +
                                     "<div id='location' class='tvlisting_result_channel_location'>" + post.channelLoc + "</div>" +
                                     "</div>";

                divParent.appendChild(divChild);

                $.getJSON('../television/getGuideSchedule.aspx', { channelID: post.channelID, scheduleID: '032511', StartTime: '1600', EndTime: '1900' },
                function (dataChild) {
                    //console.log('Hit get programming'); 
                    var totalSize = 0;
                    var left = 100;

                    $.each(dataChild.posts, function (i, postChild) {
                        var divChildp = document.createElement("div");
                        var divSize = postChild.size;
                        var tempSize = 0;

                        totalSize = (totalSize * 1) + (divSize * 1)

                        if (totalSize >= 85) {
                            tempSize = 0;
                        }
                        else {
                            tempSize = divSize
                            left = (left * 1) - (tempSize * 1)
                        }

                        //console.log('TotalSize:',totalSize,'DivSize:',divSize,'TempSize:',tempSize); 

                        divChildp.id = "myDiv";
                        divChildp.setAttribute('id', "pID" + postChild.programID + "chID" + postChild.channelID);
                        divChildp.style.margin = "0px auto";
                        divChildp.style.width = tempSize + "%";
                        divChildp.className = "tvlisting_result_blocks";
                        divChildp.innerHTML = "<div class='tvlisting_result_blocks_child'>" +
                                                "<div id='name' class='tvlisting_result_channel_name'>" + postChild.programName + "</div>    " +
                        //"<div id='location' class='tvlisting_result_channel_location'>" +""+ "</div>"+ //postChild.episodeName 
                                                "</div>";

                        divChild.appendChild(divChildp);

                    }); //End Div get each 

                    $("div.tvlisting_result_blocks").click(function (event) {
                        //console.log('Target Hit: ', this); 
                        if ($("#tv_listing_result_details").is(":hidden")) {
                            $('#tvlisting_result_line').append($("#tv_listing_result_details"));
                            $("#tv_listing_result_details").slideDown("fast", function () {
                                //showSearchDetail(this);
                            });
                            currentListing = this;
                        }
                        else {
                            if (this == currentListing) {
                                //Hide Parent Result
                                hideDetail($("#tv_listing_result_details")[0]);
                                $("#tv_listing_result_details").slideUp();
                                currentListing = undefined;
                            }
                            else {
                                //Hide Parent Result and show nodes on next division
                                hideDetail($("#tv_listing_result_details")[0]);
                                $("#tv_listing_result_details").slideUp(function () {
                                    $(event.currentTarget).append($("#tv_listing_result_details"));
                                    $("#tv_listing_result_details").slideDown("fast", function () {
                                        //showSearchDetail(this);
                                    });
                                });

                                currentListing = this;
                            }
                        }

                        if (!event) var event = window.event;
                        event.cancelBubble = true;
                        if (event.stopPropagation) event.stopPropagation();

                    }); //End event click

                }); //End Div get jSON

            }); //End  Main page each    

            //console.log('Finished Loading..'); 
            $(".tvlisting_result_container").unmask();
            //console.log('Unmasked!'); 

        }); //End Main page get jSON     

    });
}
