
var guideDialog;var curDay;$(function(){var winHeight=$(window).height();$("#tvGuide").height(winHeight-170);$("#openGuide").openDOMWindow({height:winHeight-100,width:$(window).width()-100,windowSourceID:"tvGuideContainer",eventType:"click",overlayColor:"#C21F26",positionType:"centered"});$("#openGuide").click(function(){$("#tvGuide").scrollLeft($("#channelHeaders").position().left);$("#tvGuide").scrollTop($("#timeHeader").position().left);}).click();scrollToTime();$("#tvGuide").scroll(function(e){$("#channelHeaders").css("left",$("#tvGuide").scrollLeft());$("#timeHeader").css("top",$("#tvGuide").scrollTop());}).scroll();$("#subCatSelect").data("values",[]);$("#catSelect").change(function(){var val=$(this).val();if(val){$(".catSelected").removeClass("catSelected");$("."+val+":visible").addClass("catSelected");getSubCategories(val);}else{$("#subCatSelect").attr("disabled","true");}}).change();$("#subCatSelect").change(function(){if($("#catSelect").val()){var other=$(this).val()?"."+classEncode($(this).val()):"";$(".catSelected").removeClass("catSelected");$("."+$("#catSelect").val()+other+":visible").addClass("catSelected");}});initGuide();curDay=new Date();resetNowLineHeight();moveNowLine();$(".tvGuideHelp").tooltip({showUrl:false,bodyHandler:function(){return $(".toolTipContent",this).clone().css("display","block");},delay:0});$("#chooseChannelBtn").click(function(){if(!dialog)
dialog=$("#selectChannelPopup").dialog({width:750,height:500,modal:true,dialogClass:"chooseChannelDialog",draggable:false,close:function(){$(".channelCheck:checked").each(function(){var id=$(this).attr("id").replace("check","");$("#"+id).css("display",'block');$("#"+id+"Header").css("display",'block');});$(".channelCheck:not(:checked)").each(function(){var id=$(this).attr("id").replace("check","");$("#"+id).css("display",'none');$("#"+id+"Header").css("display",'none');});resetNowLineHeight();sortChannels();var settings=getChannelOrder();$.getJSON(pluginUrl+"/primo-tv/tvQuery.php",{"settings":settings,"_wpnonce":nonce});}});else
dialog.dialog("open");});$("#hourSelect").change(function(){var date=new Date();var val=$(this).val();if(val!=""){date.setHours(val.substr(0,2));date.setMinutes(val.substr(3,2));}
scrollToTime(date);});$(".sortable").sortable({cursor:"move"});$(".channelGroup").click(function(){var checked=$(":checked",$(this).parent()).length>0;$($(this).attr("channels").split(",")).each(function(){$("#"+this+"check").attr("checked",checked?"checked":"");});});$("#channelGroupsExpandBtn").click(function(){$("#channelGroupsExpand").slideToggle();});$(".closeDetailsBtn").live("click",function(){$(".tvGuideDetails").fadeOut();});$("#closeGuideBtn").click(function(){$("#DOMWindowOverlay").click();});$("#closeSelectChannelBtn").click(function(){dialog.dialog("close");});$("#daySelect").change(function(){var val=$(this).val();var today=new Date();if(val=="twodays")
today.setDate(today.getDate()+2);else if(val=="tomorrow")
today.setDate(today.getDate()+1);showDay(today);});$("#checkAll").click(function(){var checked=$("#checkAll:checked").length>0;$(":checkbox",$("#sortableChannels")).attr("checked",checked?"checked":"");});$("#openGuide").css("display","inline");$("#globalLoading").css("display","none");});var requesting=[];function initGuide(){requesting=[];$(".program:not(.dayStart)").tooltip({showUrl:false,bodyHandler:function(){$this=$(this);if(!$this.data("tooltipContent")){var id=$this.attr("id");if($.inArray(id,requesting)<0){requesting.push(id);$.get(pluginUrl+"/primo-tv/tvQuery.php",{"tooltip":true,"_wpnonce":nonce,"id":id},function(result){result=$(result);id=result.attr("id").replace("tooltip","");if(id!=""){$("#tooltip"+id).parent().html(result);$("#tooltip .toolTipContent").css("display","block");$("#"+id).data("tooltipContent",result);}});}
return $("<div class='loading' id='tooltip"+id+"' height='20px'></div>")[0];}else return $($(this).data("tooltipContent")).css("display","block")[0];},track:true});$("#catSelect").change();$(".program:not(.dayStart)").click(function(){$(".tvGuideDetailsDetails").empty();var $this=this;$(".tvGuideDetails").css("background","white url('"+pluginUrl+"/primo-tv/images/loadingBg.gif') no-repeat center center");$(".tvGuideDetails").fadeIn(function(){$.get(pluginUrl+"/primo-tv/tvQuery.php",{"details":true,"_wpnonce":nonce,"id":$($this).attr("id")},function(result){$(".tvGuideDetails").css("background","white");$(".tvGuideDetailsDetails").html(result);});});});}
function showDay(day){$("#tvGuideLoading").fadeIn();$.get(pluginUrl+"/primo-tv/tvQuery.php",{"getDay":true,"_wpnonce":nonce,"date":day.getFullYear()+"-"+(day.getMonth()+1)+"-"+day.getDate()},function(result){$("#channelsContainer")[0].innerHTML=result;initGuide();$("#tvGuideLoading").fadeOut();curDay=day;moveNowLine();});}
function resetNowLineHeight(){$(".now").height($("#channelsContainer").height()+20);}
function getChannelOrder(){var result="";$(".channel:visible").each(function(){result+=$(this).attr("id")+","+($(this).css("display")=="block")+";";});$(".channel:not(:visible)").each(function(){result+=$(this).attr("id")+","+($(this).css("display")=="block")+";";});return result;}
var dialog;function moveNowLine(){var now=new Date();if((now.getHours()>6&&now.getDate()!=curDay.getDate())||(now.getHours()<6&&now.getDate()!=curDay.getDate()+1)){$(".now").css("display","none");return;}else
$(".now").css("display","block");var offset=now.getHours()<6?(70+((now.getMinutes()+((now.getHours()-6+24)*60))*minWidth)):(70+((now.getMinutes()+((now.getHours()-6)*60))*minWidth))
$(".now").css("left",offset+"px");window.setTimeout(moveNowLine,60000);}
function scrollToTime(date){date=date||new Date();var px=(date.getMinutes()+((date.getHours()-6)*60))*minWidth;px-=$("#tvGuide").width()/2;$("#tvGuide").scrollLeft(px);}
function classEncode(str){return str.replace(" ","").replace(";","").replace("-","").replace("\r","");}
function getSubCategories(cat){var cache=$("#subCatSelect").data("values");$("#subCatSelect .subCatOption").remove();for(var i=0,count=cache.length;i<count;i++){if(cache[i][0]==cat){fillSubCategories(cache[i][1]);return;}}
$.getJSON(pluginUrl+"/primo-tv/tvQuery.php",{"getsubcat":true,"cat":cat,"_wpnonce":nonce},function(result){$("#subCatSelect").data("values").push([cat,result]);fillSubCategories(result);});}
function fillSubCategories(subCats){for(var i=0,count=subCats.length;i<count;i++){$("#subCatSelect").append("<option class='subCatOption' value='"+subCats[i]+"'>"+subCats[i]+"</option>");}
$("#subCatSelect").attr("disabled","");}
function sortChannels(){$(".channelCheck:checked").each(function(){var id=$(this).attr("id").replace("check","");$("#channelsContainer").append($("#"+id));$("#channelHeaders").append($("#"+id+"Header"));});}