﻿// JScript File
function GetElementText( titleElements, name )
{
//    alert(name);
    var myText = "";
    var myElement = titleElements[0].getElementsByTagName(name)[0]; //[0].firstChild.nodeValue;
    if (myElement.firstChild){
        //alert(myElement.firstChild.nodeValue);
        myText = myElement.firstChild.nodeValue;
    }
    return myText;
}

function getTitleListView(titleID, cpt, dt, grpID, sby) {
    //alert("getTitleListView" + "titleID="+titleID + "&cpt=" + cpt + "&device=" + dt + "&grpID=" + grpID );    

    var myInner = "<img src=\"theme/std2/img/ajax-loader.gif\" style=\"padding:9px 0px 9px 0px;\" />";
    var loader = document.getElementById("loading");
    //if (loader) { alert(loader.innerHTML);}
    loader.innerHTML = myInner;    

	return new AJAXRequest("GET", BASE_URL + "Handler/Title.ashx", "titleID="+titleID + "&cpt=" + cpt + "&device=" + dt + "&grpID=" + grpID + "&sortby=" + sby, processTitleListViewXML);
}


function processTitleListViewXML( myAJAX ) {
//alert('processing - ' + myAJAX.readyState + ' status - ' + myAJAX.status);
  if (myAJAX.readyState == 4) { 
    if (myAJAX.status == 200) {
      logger(myAJAX.responseText);
      xml = myAJAX.responseXML;

      if (xml.documentElement) {
            var titleElements = xml.getElementsByTagName("Title");
            if (titleElements[0]) {
		        var titleID = GetElementText(titleElements, "titleID"); 
		        var titleName =  GetElementText(titleElements, "name"); 
		        var titleType = GetElementText(titleElements, "titleType");
		        var childTitlesCount = GetElementText(titleElements, "childTitlesCount");

                var myHTML = "";

                if (titleType == 29 && childTitlesCount > 0) {  //if TV and has childTitles
                
                    myHTML += "<a class=\"qblistn\"  onclick=\"qbtog(" + titleID + ")\"><b>&#x25ba;</b><u>&#x25bc;</u> " + titleName + "</a>";
                    myHTML += "<table class=\"qbhide\" cellpadding=5><tr><td valign=top width=20></td><td valign=top>";
                    myHTML += "";
                    
                    //go through all children
                    var childCollection = titleElements[0].getElementsByTagName("ChildTitleCollection"); 
                    var i = 0;
                    for (i = 0; i < childTitlesCount; i++) {
                        var child = childCollection[0].getElementsByTagName("ChildTitle")[i];
                        var childTitleID = child.getElementsByTagName("titleID")[0].firstChild.nodeValue; 
                        var childTitleName = child.getElementsByTagName("name")[0].firstChild.nodeValue;  
                        if (childTitleID != "") {
 			                myHTML += "<div id=\"qblist" + childTitleID + "\" class=\"qblistclosed\">"; 
                            myHTML += "<a class=\"qblistn\" onclick=\"getTitleListView(" + childTitleID + ", " + cpt + ", " + dt + ", " + grpID + ", " + sby + ")\"><b>&#x25ba;</b><u>&#x25bc;</u> " + childTitleName + "</a>";
	                        myHTML += "</div>";
	                    } 
                    }
                    
                    myHTML += "</td></tr></table>";
                }
                else {
		            var titleSummary = GetElementText(titleElements, "summary");  
		            var titleCast = GetElementText(titleElements, "cast");  
		            var titleRating = GetElementText(titleElements, "rating");  
		            var titleUserRating = GetElementText(titleElements, "userRating"); 
		            var titleBoxart = GetElementText(titleElements, "boxart"); 
		            var titlePrice = GetElementText(titleElements, "price");  
		            var titlePurchaseText = GetElementText(titleElements, "purchaseText"); 
		            var titlePurchaseLink = GetElementText(titleElements, "purchaseLink");
 			        //myHTML += "<div id=\"qblist" + titleID + "\" class=\"qblistclosed\">";
 			        
 			        if (titlePurchaseText == "Subscribe to Watch") {
 			            var BasePath = "";
 			            titlePurchaseLink = "javascript:GoToSubscriptionsPage('" + BasePath + "'); Redirect('" + titlePurchaseLink + "');";
 			        }

                    myHTML += "<a class=\"qblistn\"  onclick=\"qbtog(" + titleID + ")\"><b>&#x25ba;</b><u>&#x25bc;</u> " + titleName + "</a>";
                    myHTML += "<table class=\"qbhide\" cellpadding=5 width=100%><tr><td valign=top>";
                    myHTML += "<a href=\"" + titlePurchaseLink + "\">";
                    myHTML += "<img src=\"images/boxart/95/" + titleBoxart + "95.jpg\" /></a>";
                    myHTML += "</td><td align=left>";
                    if (titlePurchaseLink != "" && titleName != "") {
                        myHTML += "<div>";
                        myHTML += "<a href=\"" + titlePurchaseLink + "\" class=\"qbtitle\">" + titleName + "</a><br/>";
                        myHTML += "<br/></div>"; }
                    if (titleSummary != "") {
                        myHTML += "<div>" + titleSummary + "</div><br/>"; }
                    if (titleCast != "") {
                        //<![CDATA[ ]]>
                        titleCast = titleCast.replace("<![CDATA[","");
                        titleCast = titleCast.replace("]]>","");
                        myHTML += "<div class=\"qbstarring\">" + titleCast + "</div><br/>"; }
                    if (titleRating != "" && titleType != 2) {
                        myHTML += "<div><b>RATING:</b> " + titleRating + "</div><br/>"; }
                    if (titleUserRating != "") {
                        myHTML += "<table cellpadding=\"0\"  width=100%><tr><td align=\"left\"><img src=\"theme/std2/img/stars" + titleUserRating + ".gif\"  /></td>"; }
                    myHTML += "<td align=\"right\" class=\"qbprice\">";
                    if (titlePurchaseText == "Subscribe to Watch" || titlePurchaseText == "More Info") {
                        myHTML += "<a href=\""+ titlePurchaseLink + "\">" + titlePurchaseText + "</a></td></tr></table>";
                    }                
                    else {
                        myHTML += titlePurchaseText + " for <a href=\""+ titlePurchaseLink + "\">" + titlePrice + "</a></td></tr></table>";
                    }
                    //myHTML += "</div>";
                    myHTML += "</td></tr></table>";
	                //myHTML += "</div>";          
	            }
    
                //alert(myHTML);      
               
                var titleTable = document.getElementById("qblist" + titleID);
                if (titleTable) {
//                    alert("titleTable found" + titleID + titleName);
                    if (titleTable.innerHTML) {
                    
//                    alert("titleTable innerHTML = " + titleTable.innerHTML);
                    titleTable.innerHTML = myHTML;
                    qbtog(titleID);
                   } //else { alert("no inner html found");  }
                }
                //else { alert("no div found!"); }
                
            }
            else { alert("no Title!"); }
      }
    } 
    else {   alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);    }
    var loader = document.getElementById("loading");
    loader.innerHTML = " ";

  }
}


