/*
 * @file
 * @author  Original:     German Escallon
 * @author  Contributors:
 * @date    Created:      July 2, 2009
 * @date    Last edited:  August 19, 2009
 *
 * This file handles the GUIDE tab
 * Copyright Adtec Digital, Inc. 2009
 ******************************************************************************/
function DisplayGuide(){UpdateCurrentTab("navguide");var b='<b>Guide</b><br /><div id="'+_EIT_EDITPNL_DIV+'"></div><div id="'+_EIT_TTIP_DIV+'"></div><table><tr>  <td valign="top"><div id="channelLineupTable" style="cursor: pointer;"></div></td>  <td valign="top" id="channelGuideTableTd" ><div id="channelGuideTable"></div></td></tr></table>';$("main_div").className="yui-skin-sam";$("main_div").innerHTML=b;$(_EIT_TTIP_DIV).className="yui-tt";var a=function(){InitChannelLineupTable();InitChannelGuideTable();RenderEitGuidePanelAndTtip()};_yuiEvent.onAvailable("channelLineupTable",a,this)}function InitChannelLineupTable(){var g=new YAHOO.util.DataSource(_EITSCH_LOC);g.responseType=YAHOO.util.DataSource.TYPE_JSON;g.connMethodPost=true;g.responseSchema={resultsList:"Channels",fields:["ch_key","ch_num","ch_name"]};var d=[{key:"ch_num",label:"<b>#</b>",resizeable:true,sortable:true,formatter:function(i,h,j,k){i.innerHTML=k}},{key:"ch_name",label:"<b>Program Name</b>",resizeable:true,sortable:true,formatter:function(i,h,j,k){i.innerHTML=k}}];var e=function(i){var j=YAHOO.util.Event.getTarget(i);var h=this.getRecord(j);ShowGuideForProgram(h)};var b={id:_EIT_REQID.GETAVAILABLEPROGRAMS,req:Math.random()};var c=_yuiStringify(b);var f={initialRequest:c};var a=new YAHOO.widget.DataTable("channelLineupTable",d,g,f);a.CLASS_SELECTED=_CHGUIDE_DT_CSSCLASS;a.subscribe("cellClickEvent",e);a.subscribe("rowClickEvent",a.onEventSelectRow)}function InitChannelGuideTable(){var f=new YAHOO.util.DataSource(_EITSCH_LOC);f.responseType=YAHOO.util.DataSource.TYPE_JSON;f.connMethodPost=true;f.responseSchema={resultsList:"Events",fields:["ch_key","startTime","endTime","duration","description","star_rating","mpaa_rating","slvContent_rating","showName","epi_title","cast","release_year","show_type","cc","stereo","sourceType"]};var c=[{key:"startTime",label:"<b>Start</b>",resizeable:true,sortable:true,formatter:function(j,i,k,n){var l=new Date();l.setTime(n*1000);var m=_DAYSOFWEEK[l.getDay()];var g=(l.getHours()>=10?l.getHours():"0"+l.getHours());var h=(l.getMinutes()>=10?l.getMinutes():"0"+l.getMinutes());j.innerHTML=m+" "+g+":"+h}},{key:"endTime",label:"<b>End</b>",resizeable:true,sortable:true,formatter:function(j,i,k,n){var l=new Date();l.setTime(n*1000);var m=_DAYSOFWEEK[l.getDay()];var g=(l.getHours()>=10?l.getHours():"0"+l.getHours());var h=(l.getMinutes()>=10?l.getMinutes():"0"+l.getMinutes());j.innerHTML=m+" "+g+":"+h}},{key:"showName",label:"<b>Name</b>",resizeable:true,sortable:true,formatter:function(h,g,i,j){h.innerHTML=j}},{key:"epi_title",label:"<b>Title</b>",resizeable:true,sortable:true,formatter:function(h,g,i,j){h.innerHTML=j}},{key:"edit",label:"",resizeable:true,sortable:false,formatter:function(h,g,i,j){h.innerHTML="<button>Edit</button>";h.style.cursor="pointer"}},{key:"delete",label:"",resizeable:true,sortable:false,formatter:function(h,g,i,j){switch(g.getData("sourceType")){case _EIT_SRCTYPE.ORDB:h.innerHTML='<img src="/images/x.gif" />';h.style.cursor="pointer";break;case _EIT_SRCTYPE.A76:h.innerHTML='<img src="/images/x_disabled.gif" />';h.style.cursor="";break;default:break}}}];var e=function(i){var g=this.getRecord(_yuiEvent.getTarget(i));var j=this.getTrEl(g);var h=new YAHOO.widget.Tooltip(""+Math.random()+"",{text:GetEitStrFromDTRec(g),context:j.id,container:_EIT_TTIP_DIV,width:_GUIDE_TOOLTIP_WIDTH})};var d=function(h){var i=YAHOO.util.Event.getTarget(h);var g=this.getColumn(i);var j=this.getRecord(i);switch(g.key){case"edit":DisplayEditEITPanel(j);break;case"delete":switch(j.getData("sourceType")){case _EIT_SRCTYPE.ORDB:if(confirm("Delete Override Entry?\n(This action can't be undone)")){DeleteOverrideEvtFromDTRec(j)}break;case _EIT_SRCTYPE.A76:break;default:break}break;default:break}};var b=function(g,h){switch(h.getData("sourceType")){case _EIT_SRCTYPE.ORDB:g.style.backgroundColor=_productColor;break;case _EIT_SRCTYPE.A76:break;default:break}return true};var a={height:_GUIDEDATATABLEHEIGHT,MSG_EMPTY:"Select a Channel from the list to display its guide information.",formatRow:b,initialLoad:false};_channGuideDatatable=new YAHOO.widget.ScrollingDataTable("channelGuideTable",c,f,a);_channGuideDatatable.subscribe("cellClickEvent",d);_channGuideDatatable.subscribe("rowMouseoverEvent",e)}function ShowGuideForProgram(a){_currEITProgramObj=a;var b={id:_EIT_REQID.SHOWGUIDEFORPROGRAM,req:Math.random(),ch_key:a.getData("ch_key")};var c=_yuiStringify(b);ReloadChGuideDatatable(c)}function ReloadChGuideDatatable(c){document.body.style.cursor="wait";var a=_channGuideDatatable.getDataSource();_channGuideDatatable.initializeTable();_channGuideDatatable.disable();var d=function(){this.onDataReturnAppendRows.apply(this,arguments);this.render();document.body.style.cursor="";_channGuideDatatable.undisable()};var b={success:d,failure:function(){if(_debug){console.log("AJAX Error: Request: "+c+" failed")}},scope:_channGuideDatatable};a.sendRequest(c,b)}function RenderEitGuidePanelAndTtip(){var a={width:_EIT_GUIDE_PNL_WIDTH,autofillheight:"body",fixedcenter:true,constraintoviewport:true,underlay:"matte",modal:true,close:true,visible:false,draggable:true};_editEitPanel=new YAHOO.widget.Panel(_EIT_EDITPNL_DIV,a);var c='<form id="editEitPanelFrm"><table>   <tr><td colspan="2"><div id="'+_EDITEITEVTPNLERRORDIVID+'" style="color:'+_RED+'"></div></td></tr><tr>  <td><b>Start Time:</b><input id="startTimeTextfield"></input></td>  <td><b>End Time:</b><input id="endTimeTextfield"></input></td></tr><tr>  <td><b>Show Name:</b><input id="showNameTextfield" maxlength="256"></input></td>  <td><b>Episode Title:</b><input id="episodeTitleTextfield" maxlength="256"></input></td></tr><tr>  <td><b>Duration:</b><input id="durationTextfield"></input></td>  <td><b>Release Year:</b><select id="releaseYearSelect">'+GetSelectOptions("guide_releaseYear")+'</select></td></tr><tr>  <td><b>Description:</b><br /><textarea id="descriptionTextarea"></textarea></td>  <td><b>Cast:</b><br /><textarea id="castTextarea"></textarea></td></tr><tr>  <td><b>Mpaa Rating:</b><select id="mpaaRatingSelect">'+GetSelectOptions("guide_mpaaRating")+'</select></td>  <td><b>Star Rating:</b><select id="starRatingSelect">'+GetSelectOptions("guide_starRating")+'</select></td></tr><tr>  <td><b>SLV Content:</b><select id="slvContentSelect">'+GetSelectOptions("guide_slvContent")+'</select></td>  <td><b>Show Type:</b><input id="showTypeTextfield" maxlength="256"></input></td></tr><tr>  <td><b>CC:</b><select id="ccSelect">'+GetSelectOptions("guide_cc")+'</select></td>  <td><b>Stereo:</b><select id="stereoSelect">'+GetSelectOptions("guide_stereo")+'</select></td></tr></table><br /><br /><input type="hidden" id="'+_SRCTYPEHIDDENFLDID+'" /><button id="editEventImg">Edit Event</button>&nbsp;&nbsp;<button id="cancelEditEventImg">Close</button><br /></form>';_editEitPanel.setHeader("Edit EIT Event");_editEitPanel.setBody(c);_editEitPanel.render();var b=function(){_yuiEvent.addListener("editEventImg","click",ValidateEditEITForm,$("editEitPanelFrm"),true);_yuiEvent.addListener("cancelEditEventImg","click",ResetAndCloseEditEITPanelForm,null,false);_yuiEvent.addListener(["startTimeTextfield","endTimeTextfield"],"change",CalculateEITEventDuration,null,false);_yuiEvent.addListener("durationTextfield","change",CalculateEITEventEndTime,null,false)};_yuiEvent.onAvailable("editEitPanelFrm",b,this)}function ValidateEditEITForm(){var a=null;if(isNaN(Date.parse(Trim($("startTimeTextfield").value)))){a="Invalid Start Time"}if(isNaN(Date.parse(Trim($("endTimeTextfield").value)))){a="Invalid End Time"}if(a===null){SubmitEditedEITEvent()}else{$(_EDITEITEVTPNLERRORDIVID).innerHTML=a}}function ResetAndCloseEditEITPanelForm(){_editEitPanel.hide()}function DisplayEditEITPanel(b){$(_EDITEITEVTPNLERRORDIVID).innerHTML="";if(b.getData("duration")<0){b.setData("duration",(b.getData("endTime")-b.getData("startTime")))}if(b.getData("sourceType")!=_EIT_SRCTYPE.ORDB){$("startTimeTextfield").disabled=false;$("endTimeTextfield").disabled=false;$("durationTextfield").disabled=false}else{$("startTimeTextfield").disabled=true;$("endTimeTextfield").disabled=true;$("durationTextfield").disabled=true}$("durationTextfield").value=MilisecondsToHHMMSS(b.getData("duration")*_MILISECONDSPERSECOND);$("showNameTextfield").value=b.getData("showName");$("episodeTitleTextfield").value=b.getData("epi_title");$("releaseYearSelect").value=b.getData("release_year");$("descriptionTextarea").value=b.getData("description");$("castTextarea").value=b.getData("cast");$("mpaaRatingSelect").value=b.getData("mpaa_rating");$("starRatingSelect").value=b.getData("star_rating");$("slvContentSelect").value=b.getData("slvContent_rating");$("showTypeTextfield").value=b.getData("show_type");$("ccSelect").value=b.getData("cc");$("stereoSelect").value=b.getData("stereo");$(_SRCTYPEHIDDENFLDID).value=b.getData("sourceType");var a=new Date(b.getData("startTime")*_MILISECONDSPERSECOND);$("startTimeTextfield").value=a;a=new Date(b.getData("endTime")*_MILISECONDSPERSECOND);$("endTimeTextfield").value=a;_editEitPanel.show()}function CalculateEITEventEndTime(){$(_EDITEITEVTPNLERRORDIVID).innerHTML="";var a=Date.parse(Trim($("startTimeTextfield").value));var b=HHMMSSToMiliseconds(Trim($("durationTextfield").value));if(b==_INVALID_DURATION_IN_MSECS){$(_EDITEITEVTPNLERRORDIVID).innerHTML="Duration must be in HH:MM:SS format. "}else{var c=new Date(a+b);$("endTimeTextfield").value=c}}function CalculateEITEventDuration(){var a=Date.parse(Trim($("startTimeTextfield").value));var c=Date.parse(Trim($("endTimeTextfield").value));var b=c-a;$("durationTextfield").value=MilisecondsToHHMMSS(b)}function SubmitEditedEITEvent(){var e=function(g){if(!_EITOVERLAPERRORREGEX.test(g.responseText)){ShowGuideForProgram(_currEITProgramObj);_editEitPanel.hide()}else{$(_EDITEITEVTPNLERRORDIVID).innerHTML=g.responseText}};var f={success:e,failure:function(g){if(_debug){console.log("AJAX ERROR: Eit Override not submitted.")}}};var b=Date.parse(Trim($("startTimeTextfield").value))/_MILISECONDSPERSECOND;var c=Date.parse(Trim($("endTimeTextfield").value))/_MILISECONDSPERSECOND;var a={id:_EIT_REQID.SUBMITOVERRIDEENTRY,req:Math.random(),ch_key:_currEITProgramObj.getData("ch_key"),showName:Trim($("showNameTextfield").value),epi_title:Trim($("episodeTitleTextfield").value),release_year:parseInt($("releaseYearSelect").value,_DEFAULT_NUM_BASE),description:Trim($("descriptionTextarea").value),cast:Trim($("castTextarea").value),mpaa_rating:parseInt($("mpaaRatingSelect").value,_DEFAULT_NUM_BASE),star_rating:parseInt($("starRatingSelect").value,_DEFAULT_NUM_BASE),slvContent_rating:parseInt($("slvContentSelect").value,_DEFAULT_NUM_BASE),show_type:Trim($("showTypeTextfield").value),cc:parseInt($("ccSelect").value,_DEFAULT_NUM_BASE),stereo:parseInt($("stereoSelect").value,_DEFAULT_NUM_BASE),startTime:b,endTime:c,duration:(c-b),sourceType:$(_SRCTYPEHIDDENFLDID).value};var d=_yuiStringify(a);_yuiConnect.asyncRequest("POST",_EITSCH_LOC,f,d)}function GetEitStrFromDTRec(c){var e=new Date(c.getData("startTime")*_MILISECONDSPERSECOND);var b=new Date(c.getData("endTime")*_MILISECONDSPERSECOND);var d=DateObjToDateTimeStr(e);var g=DateObjToDateTimeStr(b);var f=(c.getData("release_year")<_MIN_RELEASE_YEAR||c.getData("release_year")>_MAX_RELEASE_YEAR)?_NA:c.getData("release_year");var a="<table><tr>  <td><b>Start Time:</b></td><td>"+d+"</td>  <td><b>End Time:</b></td><td>"+g+"</td></tr><tr>  <td><b>Show Name:</b></td><td>"+c.getData("showName")+"</td>  <td><b>Episode Title:</b></td><td>"+c.getData("epi_title")+"</td></tr><tr>  <td><b>Duration:</b></td><td>"+c.getData("duration")+"</td>  <td><b>Release Year:</b></td><td>"+f+"</td></tr><tr>  <td><b>Mpaa Rating:</b></td><td>"+_MPAA_RATING_STR[c.getData("mpaa_rating")]+"</td>  <td><b>Star Rating:</b></td><td>"+_STAR_RATING_STR[c.getData("star_rating")]+"</td></tr><tr>  <td><b>SLV Content:</b></td><td>"+_SLV_CONTENT_STR[c.getData("slvContent_rating")]+"</td>  <td><b>Show Type:</b></td><td>"+c.getData("show_type")+"</td></tr><tr>  <td><b>CC:</b></td><td>"+_ClOSED_CAPTION_STR[c.getData("cc")]+"</td>  <td><b>Stereo:</b></td><td>"+_STEREO_STR[c.getData("stereo")]+"</td></tr><tr>  <td><b>Description:</b><br />"+c.getData("description")+"<td></td></td>  <td><b>Cast:</b><br />"+c.getData("cast")+"<td></td></td></tr></table>";return a}function DeleteOverrideEvtFromDTRec(b){var e=function(f){if(_EITERRORREGEX.test(f.responseText)){SetInfoMessage(f.responseText,_DEFAULT_INFO_MSG_TIMEOUT,_RED);if(_debug){console.log(f.responseText)}}else{ShowGuideForProgram(_currEITProgramObj)}};var a={success:e,failure:function(f){if(_debug){console.log("AJAX Error: Override event not deleted")}}};var c={id:_EIT_REQID.DELETEOVERRIDEENTRY,req:Math.random(),startTime:b.getData("startTime"),endTime:b.getData("endTime"),ch_key:b.getData("ch_key")};var d=_yuiStringify(c);_yuiConnect.asyncRequest("POST",_EITSCH_LOC,a,d)}CreateMetaTagElement("guide");