// File: readXML.js
// blue to use: #388ec9
// Dark Blue: #16468a
// Gray for the lines: #d2d2d2
// Dark Gray: #505050
// Start function when DOM has completely loaded
$(document).ready(function() {

    // Open the students.xml file
    $.get("/resource/resmgr/Podcast/BridgePointOnLineCurrentSeri.xml", {}, function(xml) {

        // Build an HTML string
        myHTMLOutput = '';
        PCVideoLink = '';
        PCAudioLink = '';
        LastCategory = '';
        LastTitle = '';
		LastPubDate='';		
		LastAuthor='';
        // Run the function for each student tag in the XML file
        $('item', xml).each(function(i) {

            PCCategory = $(this).find("category").text();
            PCTitle = $(this).find("itunes\\:subtitle").text();
            PCDesc = $(this).find("description").text();
            PCLink = $(this).find("link").text();
            PCAuthor = $(this).find("itunes\\:author").text();
            PCPubDate = $(this).find("pubDate").text();
            PCType = $(this).find("itunes\\:keywords").text();
            // Build row HTML data and store in string
            if (PCCategory != '') {
                if (PCCategory != LastCategory) {
                    if (LastCategory == '') {
                        myHTMLOutput += '<br><br><table class="BPMedia">';
                        myHTMLOutput += '<colgroup>'
                        myHTMLOutput += '<col class="BPCol1"></col>'
                        myHTMLOutput += '<col class="BPCol2"></col>'
                        myHTMLOutput += '<col class="BPCol3"></col>'
                        myHTMLOutput += '<col class="BPCol4"></col>'
                        myHTMLOutput += '<col class="BPCol5"></col></colgroup>'
                        myHTMLOutput += '<tbody>'

                    } else {
                    if (LastTitle != PCTitle) {
                            mydata = BuildMessageHTML(LastTitle, LastDesc, PCAudioLink, LastAuthor, LastPubDate, PCVideoLink);
                            myHTMLOutput = myHTMLOutput + mydata;
                            PCVideoLink = '';
                            PCAudioLink = '';
                        }
                        myHTMLOutput += '</tbody></table><br><br><table class="BPMedia">';
                        myHTMLOutput += '<colgroup>'
                        myHTMLOutput += '<col class="BPCol1"></col>'
                        myHTMLOutput += '<col class="BPCol2"></col>'
                        myHTMLOutput += '<col class="BPCol3"></col>'
                        myHTMLOutput += '<col class="BPCol4"></col>'
                        myHTMLOutput += '<col class="BPCol5"></col></colgroup>'
                        myHTMLOutput += '<tbody>'
                    }
                    if (PCTitle != LastTitle) {
                        mydata = BuildTitleHTML(PCCategory);
                        myHTMLOutput = myHTMLOutput + mydata;
                        //if (LastTitle != PCTitle) {
                        //    mydata = BuildMessageHTML(PCTitle, PCDesc, PCAudioLink, PCAuthor, PCPubDate, PCVideoLink);
                        //    myHTMLOutput = myHTMLOutput + mydata;
                        //    PCVideoLink = '';
                        //    PCAudioLink = '';
                        //}

                    }
                    else {
                        //do nothing
                    }
                    if (PCType == 'Video') {
                        PCVideoLink = PCLink;
                    }
                    else {
                        PCAudioLink = PCLink;
                    }

                }
                else {

                    if (PCTitle != LastTitle) {
                        mydata = BuildMessageHTML(LastTitle, LastDesc, PCAudioLink, LastAuthor, LastPubDate, PCVideoLink);
                        myHTMLOutput = myHTMLOutput + mydata;
                        //if (LastTitle != '') {
                           // mydata = BuildMessageHTML(PCTitle, PCDesc, PCAudioLink, PCAuthor, PCPubDate, PCVideoLink);
                           // myHTMLOutput = myHTMLOutput + mydata;
                            PCVideoLink = '';
                            PCAudioLink = '';
                        //}
                    }
                    else {
                        //do nothing
                    }
                    if (PCType == 'Video') {
                        PCVideoLink = PCLink;
                    }
                    else {
                        PCAudioLink = PCLink;
                    }
                }
            }
            LastCategory = PCCategory;
            LastTitle = PCTitle;
			LastAuthor=PCAuthor;
			LastPubDate=PCPubDate;
			LastDesc=PCDesc;
            //, PCTitle, PCDesc, PCLink);
        });
        myHTMLOutput += '</table><br><br><br>';

        // Update the DIV called Content Area with the HTML string
        $("#ContentArea").append(myHTMLOutput);
        $(".BPMedia tr:nth-child(2n+3)").addClass("colored");
    });
});
 
 
 
 function BuildTitleHTML(PCCategory){
 	
	// Build HTML string and return
     output = '';
     output += '<tr>';
     output += '<td class="title" colspan="5" padding-left="0"><div><span class="title">' + PCCategory + '</span></div></td>';
     output += '</tr>';
     output += '<tr class="header">'
     output += '<td>Date</td>'
     output += '<td>Message Title</td>'
     output += '<td>Speaker</td>'
     output += '<td>Listen</td>'
	 output += '<td>Watch</td>'
     output += '</tr>'

     //<div><span class="title">Crazy Love (part 1)&nbsp;- God's Love for Us </span><span class="subtitle">(Dec 2009)</span></div>
	
	
	return output;
}
function BuildMessageHTML(PCTitle, PCDesc, PCAudioLink, PCAuthor, PCPubDate, PCVideoLink) {

    // Build HTML string and return


    PCDate = new Date(PCPubDate);
	var today = new Date();


    title = 'title="' + PCTitle + ' - ' + PCAuthor + '"';
    onclickStr = "onclick=\"javascript: BPpageTracker._trackEvent('Messages','Play','" + PCTitle + "');\"";
    
    output = '';
    output += '<tr>';
    output += '<td>' + formatDate(PCDate, "MM/dd/yyyy") + '</td>';
    output += '<td>' + PCTitle + '</td>';
    //output += '<td>' + PCDesc + '</td>';
    output += '<td>' + PCAuthor + '</td>';
	if (PCDate<=today) {
	    if (PCAudioLink !=''){
	    output += '<td><a ' + title + onclickStr + ' href="' + PCAudioLink + '"><img height="32" width="32" border="0" src="/resource/resmgr/Podcast/listen3_32x32.png" alt="" /></a></td>';
		}
		else
		{
			       output+= '<td>&nbsp;</td>'
		}
	    if (PCVideoLink != '') {
	        output += '<td><a ' + title + onclickStr + ' href="' + PCVideoLink + '"><img height="32" width="32" border="0" src="/resource/resmgr/Podcast/video1_32x32.png" alt="" /></a></td>';
	        }

	        else {
	       output+= '<td>&nbsp;</td>'
	    } 
	    }
	else
	{
	    output += '<td></td>';
		output += '<td></td>';
		}	
    output += '</tr>';
//<a title="You're Going to do what - Tim Whipple" onclick="javascript: BPpageTracker._trackEvent('Messages','Play','You're Going to do what?'); " href="/resource/resmgr/Messages/You_re_going_to_do_what.mp3" target="_blank"><img border="0" alt="" src="/resource/resmgr/media/audio_icon6.gif" width="20" height="20" /></a>


    return output;
}



