var LOCK_FLAG = 0;
	var STDATA;
	var PLAYLIST; //Will hold an array of items to play
	var PL_POSITION = -1; //Will hold an index of a play item
	var GDATE;
	
function initEasyReport() {
		var am = new AjaxManager();
		var xmlurl = "/gen/fantasy/sefr/videos.xml";
		am.request(xmlurl, initializePage);
	}
	
	function initializePage(response)
	{
			if (response.xml){
			STDATA = toJSObject(response.xml);
			
			//alert(STDATA.group[0].clips.clip);
			
				var allGroups = STDATA.group;
				var gLen = allGroups.length;
				var g = 0;
				var GDATE;
				if(allGroups.length){
				var dateTable = new SimpleTable(gLen,1);
				var cell, cGrp;
				dateTable.id = "tbltabs";
				do {
					cGrp = allGroups[g];
					cell = dateTable.Cell(g,0);
					cell.setAttribute("onclick", "cellClicked(this); buildPlaylistGrid(" + g + ");playVideo();");
					cell.setAttribute("onMouseOver", "paintDate(this)");
					cell.setAttribute("onMouseOut", "revertDate(this)");
					cell.value = new SimpleLink("javascript: buildPlaylistGrid(" + g + ");playVideo(); ", cGrp.date).toHTML();
					if (gLen==2 && g==1){
						cell.setAttribute("class","dateON");
						cell.setAttribute("selected","1");
						grpNum = 1;
					}else if (gLen==3 && g==2) {
						cell.setAttribute("class","dateON");
						cell.setAttribute("selected","1");
						grpNum = 2;
					}else if (gLen==4 && g==3) {
						cell.setAttribute("class","dateON");
						cell.setAttribute("selected","1");
						grpNum = 3;
					}else if (gLen==5 && g==4) {
						cell.setAttribute("class","dateON");
						cell.setAttribute("selected","1");
						grpNum = 4;
					}else {
						cell.setAttribute("class","dateOFF");
					}	
				} while(g++ < gLen-1);
				
				dateT = getObject("dates");
				dateT.innerHTML = dateTable.toHTML();	
				buildPlaylistGrid(grpNum);
				LOCK_FLAG = 1;
			} else {
				
				var dateTable = new SimpleTable(1,1);
				var cell, cGrp;
				cGrp = allGroups;
				cell = dateTable.Cell(0,0);
					cell.setAttribute("onclick", "cellClicked(this); buildPlaylistGrid(0);playVideo();");
					cell.setAttribute("onMouseOver", "paintDate(this)");
					cell.setAttribute("onMouseOut", "revertDate(this)");
					cell.value = new SimpleLink("javascript: buildPlaylistGrid(0);playVideo(); ", cGrp.date).toHTML();
					cell.setAttribute("class","dateON");
					cell.setAttribute("selected","1");
					
					dateT = getObject("dates");
				dateT.innerHTML = dateTable.toHTML();	
				buildPlaylistGrid(0);
				LOCK_FLAG = 1;
			}
			}
				
	}
	function paintRow(obj){
			var currRow = obj;
			if (LOCK_FLAG == 0){
				currRow.className = "rowON";
			}
	}
	function revertRow(obj){
			var currRow = obj;
			var rowLock = currRow.getAttribute("selected");
			if (rowLock != 1){
			currRow.className = "rowOFF";
			}
	}
	
	function selectDate(obj){
		var tbldate = getObject("tbltabs");
		var colsdate = tbldate.rows[0].cells;
		//alert("selectdate" + colsdate.length);
		if (LOCK_FLAG == 0){
		if (colsdate.length){
			if (obj != null){ 
			var colLen = colsdate.length -1;
			var currCell;
			do {
				currCell = colsdate[colLen];
				currCell.setAttribute("selected", "0");
				currCell.className = "dateOFF";	
			}while(colLen--)
				currCell = obj;
				currCell.setAttribute("selected", "1");
				currCell.className = "dateON";
			}
			}
		}
	}
	function selectVideo(idx){
		var tblName = "tbl_" + nTable.uid;
		var tblVid = getObject(tblName);
		var rowsVid = tblVid.rows;
		//alert("selectvideo")
		//if (LOCK_FLAG == 0){
		if (rowsVid.length){
			if (idx >= 0 && idx < rowsVid.length){ 
			var rowLen = rowsVid.length -1;
			var currRow;
			do {
				currRow = rowsVid[rowLen];
				currRow.setAttribute("selected", "0");
				currRow.className = "rowOFF";
			} while(rowLen--);
				currRow = rowsVid[idx];
				currRow.setAttribute("selected", "1");
				currRow.className = "rowON";
			}
			//}
		}
	}
	function paintDate(obj){
			//alert("MOUSEover");
			var currCell = obj;
			if (LOCK_FLAG == 0){
				currCell.className = "dateON";
			}
	}
	function revertDate(obj){
			var currCell = obj;
			var currVal = currCell
			var cellLock = currVal.getAttribute("selected");
			if (cellLock != 1){
			currCell.className = "dateOFF";
			}
	}
	function cellClicked(cell){
		//if (LOCK_FLAG == 0){
			selectDate(cell);
		//}
	}
	
	function rowClicked(row) {
		var idx = row.getAttribute("idx");
		if (LOCK_FLAG == 0){
			selectVideo(idx);
			row.setAttribute("selected", "1");
			PL_POSITION = idx-1;
			playVideo();
			//alert(flvPath);
		}
		
	}
	
	function playVideo() {
		//alert("playvideo");
		PL_POSITION++;
		if (LOCK_FLAG == 0){
			if(PL_POSITION < PLAYLIST.clips.clip.length) {
				var playItem = PLAYLIST.clips.clip[PL_POSITION];
				var playPath = playItem.flvpath.Text;
				flashPlay(playPath);
				selectVideo(PL_POSITION);
			}
		}
	}
		function playVideoUnlock() {
		//alert("playvideoUnlock");
		playlist = new Array();
		LOCK_FLAG = 0;
		
		PL_POSITION++;
			if (LOCK_FLAG == 0){
			if (PLAYLIST && PLAYLIST.clips.clip.length) {
				if(PL_POSITION < PLAYLIST.clips.clip.length) {
					var playItem = PLAYLIST.clips.clip[PL_POSITION];
					var playPath = playItem.flvpath.Text;
					flashPlay(playPath);
					selectVideo(PL_POSITION);
				}
			} else if (PLAYLIST) {
				var playItem = PLAYLIST.clips.clip;
					var playPath = playItem.flvpath.Text;
					flashPlay(playPath, 0);
					selectVideo(PL_POSITION);
			}
		}
	}
	function flashPlay(path) {
			//o=getObject("flvplayer");
			//a = o.Attributes["flashvars"];
			//a. nodeValue = null;
			var relevantMonth = "";
			var relevantDay = "";
			if (PLAYLIST.date) {
				var month=new Array(12);
				month[0]="Jan";
				month[1]="Feb";
				month[2]="Mar";
				month[3]="Apr";
				month[4]="May";
				month[5]="Jun";
				month[6]="Jul";
				month[7]="Aug";
				month[8]="Sep";
				month[9]="Oct";
				month[10]="Nov";
				month[11]="Dec";
				
				for(var j=0; j<month.length; j++) {
					if (month[j] == PLAYLIST.date.substring(0,3)) {
						relevantMonth = j;
					}
				}
				relevantDay = PLAYLIST.date.substring(4);
			}
				
			var clickedDate = new Date();
			clickedDate.setFullYear('2008',relevantMonth,relevantDay);
			
			var aspectRatioChangeDate = new Date();
			aspectRatioChangeDate.setFullYear('2008','2','25');
			var thisversion = 4;
			var flv_width=512;
			var flv_height="";
			if (clickedDate >= aspectRatioChangeDate) {
				flv_height=288;
			} else {
				 flv_height=384;
			}
			var mwF = new FlashObject("http://mlb.mlb.com/flash/video/y2006/flvplayer.swf?v="+thisversion, "flvplayer", (flv_width+2), (flv_height+23), "8", "#000000");

			
			//mwFO.variables = new Object();
			//alert("FLASH_PLAY");
	
			//alert('in flashPlay height is ' + flv_height);
			mwF.volume=100;
			mwF.addVariable("flv_width", flv_width);
		  mwF.addVariable("flv_height", flv_height);
			mwF.addVariable("init_volume", 100);
			mwF.variables.endofplaylist_js_call="javascript:playVideo();";
		  
			playlist = new Array();
			
			playlist.push({flv:path,controls:"yes"});
			//playlist.push({flv:plvid,controls:"yes"});
			
			//mwFO.addVariable("playlist0flv", playlist[0])
			
			//playlist.push({flv:plvid,controls:"yes"});
			for (var i=0;i<playlist.length;i++){
			var o = playlist[i];
			if (o.flv){mwF.addVariable("playlist"+i+"flv", o.flv);}
			if (o.controls){mwF.addVariable("playlist"+i+"controls", o.controls);}
			if (o.pixel){mwF.addVariable("playlist"+i+"pixel", o.pixel);}
			}
			
			mwF.addParam("menu", "false");
			mwF.addParam("wmode", "transparent");
			
			mwF.write("flash_videoplayer");			
	}
	var nTable
	
	function createPlaylistRow(imgBtn, imgClip, blurb) {
		var res = "";
			var struc = new SimpleTable(3, 1);
				cell1 = struc.Cell(0,0);
				cell2 = struc.Cell(1,0);
				cell3 = struc.Cell(2,0);
				
				cell1.value = imgBtn;
				cell2.value = imgClip;
				cell3.value = blurb;
				
				res = struc.toHTML();
		return res;
	}
	
	function buildPlaylistGrid(grpIdx) {
		if(LOCK_FLAG == 0) {
			PL_POSITION = -1;
			
			if (STDATA.group.length) {
			PLAYLIST = STDATA.group[grpIdx];
			PLAYLIST.date = STDATA.group[grpIdx].date;
			} else {
			PLAYLIST = STDATA.group;
			}
				nTable = new GridTable();
				nTable.width = "364px";
				nTable.border = 0;
				//nTable.padding = 0;
				nTable.style = "vidclips"
				nTable.spacing = 0;
				nTable.padding = 0;
				nTable.hideHeader=true;
				
				var col1 = new GridColumn("Content");
					
							
				nTable.appendColumn(col1);
				
				
				
				var img = document.createElement("img");
				var photo = document.createElement("img");
				var link = document.createElement("a");
				
				var clips = PLAYLIST.clips.clip;
				var thisClip;
				

				
				if(clips.length) {
					var clipCount = clips.length - 1;
					var i = 0;
					do {
						thisClip = clips[i];
						link.setAttribute("href","test.html");
						link.innerHTML = thisClip.headline.Text;					
						img.setAttribute("src", thisClip.click.Text);
						photo.setAttribute("src", thisClip.photo.Text);
						var content = "<div class=\"content\"><div class=\"headline\">" + thisClip.headline.Text + "</div>" + "<div class=\"blurb\">" + thisClip.blurb.Text + "</div></div>"; 
						var btn_num = i + 1;
						var btn = "<div class=\"btn_" + btn_num + "\">" + getOuterHTML(img) + "</div>";
						var photo_bdr = "<div class=\"photo\">" +  getOuterHTML(photo) + "</div>";
						if (i == 4){
							nTable.insertRow([createPlaylistRow(btn, photo_bdr, content)], "staplesRow", "rowClicked(this.parentNode)","paintRow(this.parentNode)","revertRow(this.parentNode)");
						} else {
							nTable.insertRow([createPlaylistRow(btn, photo_bdr, content)], "clipRow", "rowClicked(this.parentNode)","paintRow(this.parentNode)","revertRow(this.parentNode)");
						}
					} while (i++ < clipCount);
				} else {
					thisClip = clips;
					nTable.insertRow([thisClip.pubdate.Text]);
				}
				getObject("clips").innerHTML=nTable.renderGrid();
				selectVideo(0);
		} //Lock
	}