function AccordionInit() {

	var accTitle = $$('.pgsshowinfo');
	var accContent = $$('.pgsshowmoreinfo');	
	var pwAccordion = new Accordion(
		accTitle, accContent, {
			
			onActive: function(tog){								
				if($('show_' + tog.source).getElement('div[id=sd_desc]').getText() == "")
				{
					getShowInfo(tog.code, tog.source);
				}
				tog.setStyle('border-bottom', 'none');
				$$(tog.childNodes[1]).setStyle('background-image', 'url(/pic/btnHideDet.gif)', 'background-position', '0 15px', 'background-repeat', 'no-repeat').setHTML('<a href="javascript:void(0);">Hide Details</a>');					
			},

			onBackground: function(tog){

				tog.setStyle('border-bottom', '1px solid #c4c0b4');
				$$(tog.childNodes[1]).setStyle('background-image', 'url(/pic/btnShowDet.gif)', 'background-position', '0 15px', 'background-repeat', 'no-repeat').setHTML('<a href="javascript:void(0);">Show Details</a>');
			
			},

			alwaysHide: true,
			start: "all-closed"
		}
	);
}

window.addEvent('domready', function(){	
	AccordionInit();
});

function getShowInfo(showCode, sourceCode)
{	
	var myAjax = new Ajax("http://www.qvc.com/pguide/show_info_service.asp?showCode=" + showCode + "&sourceCode=" + sourceCode, {method: 'get',async: true, onComplete:function(output){   processDetails(output, sourceCode);}, onFailure:function(){alert("Fail " + sourceCode + " - " + showCode);}}).request();						
}

function processDetails(myJSON, sc)
{	
	var show = Json.evaluate(myJSON);					
	var showInfoDiv = $('show_' + sc);		
	showInfoDiv.getElement('div[id=sd_title]').setText(show.name);
	//Our desc is full of garbage charcaters so the first thing we will do is strip some meta data
	//It appears as ## up to 100 chars ##
	//Here is a regular expression to pull it out
	var reMeta = /##.*##/g;
	var showDesc = show.desc;
	showDesc = showDesc.replace(reMeta,"");
	//There are html tags in the desc so we use .setHTML
	showInfoDiv.getElement('div[id=sd_desc]').setHTML(showDesc);	
	showInfoDiv.getElement('div[id=sd_date]').setText(show.date);
	showInfoDiv.getElement('div[id=sd_time]').setText(show.time);
	
	if(show.upcomingShows.length > 1)
	{		
		var ucTable = showInfoDiv.getElement('table[class=pgtblsched]');				
        var tbody = document.createElement('tbody');  
        var newCaption = document.createElement('caption');	
		newCaption.setAttribute('class','pgtblschedcap');
		captxt = document.createTextNode('Upcoming Shows');
		newCaption.appendChild(captxt);			    
		ucTable.appendChild(newCaption); 	  
		
		for(i = 0; i < show.upcomingShows.length;i++)
		{			
			if(show.upcomingShows[i])
			{
			    		    				
                var newtr = document.createElement('tr');               
				newtr.setAttribute('class','bggrey');
				var newtx1 = "";				
				newtx1 = document.createTextNode(show.upcomingShows[i].airDate);								
				var newtd1 = document.createElement('td');
				var newtx2 = "";				                           
				newtx2 = document.createTextNode(show.upcomingShows[i].showTime);				
				var newtd2 = document.createElement('td');         
				var newtx3 = document.createTextNode('Add Reminder');
				var newtd3 = document.createElement('td');         
                var newInput1 = document.createElement('input');
                newInput1.setAttribute('type','checkbox');
                newInput1.setAttribute('name','RemindMeCheck');   
                var ReminderValue = show.upcomingShows[i].sourceCode + "~" + show.name + "~"+  show.upcomingShows[i].airDate + "~"+ show.upcomingShows[i].showTime + "|";             
				newInput1.setAttribute('value',ReminderValue);				                                
                newtd1.appendChild(newtx1);
                newtr.appendChild(newtd1);
                newtd2.appendChild(newtx2);
                newtr.appendChild(newtd2);   
                newtd3.appendChild(newInput1);             
                newtd3.appendChild(newtx3);                
                newtr.appendChild(newtd3);                
                tbody.appendChild(newtr);
                ucTable.appendChild(tbody);  
             }                							
		}		 
	}
}
