/*
  (c) 2008-2014 Samnan ur Rehman
 @web        http://mywebsql.net
 @license    http://mywebsql.net/license
*/
var curEditField=null,curEditType=null,fieldInfo=null,editOptions={sortable:!0,highlight:!0,selectable:!0,editEvent:"dblclick",editFunc:editTableCell},selectedRow=-1,res_modified=!1,editHorizontal=!1,editListOpen=!1;
function setupTable(a,b){res_modified=!1;b.editEvent||(b.editEvent="dblclick");b.editFunc||(b.editFunc=editTableCell);b.sortable&&2<$("#"+a+" tbody tr").length&&(createTableHeader(a.substr(0,4)),1==b.sortable?$("#dataHeader thead th").live("click",function(){if($(this).attr("class").match(/tch|th_nosort/))return!0;var a=$(this).index();editTableName&&""!=editTableName&&a--;goSort(a)}):"inline"==b.sortable&&$("#"+a).sorttable());b.highlight&&($("#"+a+" > tbody > tr").live("mouseenter",function(){$(this).addClass("ui-state-hover")}),
$("#"+a+" > tbody > tr").live("mouseleave",function(){$(this).removeClass("ui-state-hover")}));b.selectable&&$("#"+a+" > tbody > tr").live("click",function(){null!=selectedRow&&$(selectedRow).removeClass("ui-state-active");$(this).addClass("ui-state-active");selectedRow=this});b.editable&&(editOptions=b,$("#"+a+" td.edit").bind(b.editEvent,b.editFunc),jQuery.support.touch&&$("#"+a+" td.edit").bind("taphold",b.editFunc),$("#inplace-text textarea").unbind("keydown").bind("keydown",checkEditField))}
function editTableCell(){td=$(this);null!=curEditField&&closeEditor(!0);isBlob=td.find("span.i").length;txt=(isText=td.find("span.d").length)?td.find("span.d").text():isBlob?td.find("span.i").text():td.text();tstyle=td.hasClass("tr")?"right":"left";td.data("defText",txt);curEditField=this;index=td.index()-2;fi=getFieldInfo(index);w=td.width()-(isBlob?22:0);h=td.height();td.attr("width",w);(input=createCellEditor(td,fi,txt,w,h,tstyle))&&setTimeout(function(){input.focus();td.ensureVisible($("#results-div"),
editHorizontal)},50)}
function closeEditor(a,b){if(curEditField){obj=$(curEditField);txt="";var c={};if(a){if(1<arguments.length&&null==b?(c.value="NULL",c.setNull=!0):(txt=c.value="simple"==curEditType?obj.find("input").val():$("#inplace-text textarea").val(),c.setNull=!1),c.value!=obj.data("defText")||c.setNull&&!obj.hasClass("tnl")||!c.setNull&&obj.hasClass("tnl"))obj.parent().hasClass("n")||obj.parent().addClass("x"),obj.data("edit",c).addClass("x"),res_modified=!0,"function"==typeof showNavBtn&&showNavBtn("update",
"gensql"),c.setNull?obj.removeClass("tl").addClass("tnl"):obj.removeClass("tnl").addClass("tl"),txt=c.value}else txt=obj.data("defText");"text"==curEditType?(c.setNull?obj.find("span.i").text("NULL").removeClass("tl").addClass("tnl"):obj.find("span.i").text(0==txt.length?"":txt.length<=MAX_TEXT_LENGTH_DISPLAY?txt:"Text Data ["+formatBytes(txt.length)+"]").removeClass("tnl"),obj.find("span.d").text(txt)):0==obj.find("span.i").length?obj.text(txt):obj.find("span.i").text(txt);obj.removeAttr("width");
curEditField=null;"text"==curEditType&&$("#inplace-text").hide();editListOpen=!1;resizeTableHeader("data")}}
function checkEditField(a){editHorizontal=!1;keys="text"==curEditType?[9]:[13,9,38,40];if(-1!=keys.indexOf(a.keyCode)){a.preventDefault();elem=!1;if(9==a.keyCode)elem=a.shiftKey?$(curEditField).prev(".edit"):$(curEditField).next(".edit"),elem.length||(tr=a.shiftKey?$(curEditField).parent().prev():$(curEditField).parent().next(),tr.length&&(elem=a.shiftKey?tr.find("td.edit:last"):tr.find("td.edit:first"))),editHorizontal=!0;else if(38==a.keyCode||40==a.keyCode)tr=38==a.keyCode?$(curEditField).parent().prev():
$(curEditField).parent().next(),tr.length&&(elem=tr.find("td").eq($(curEditField).index()));$("#inplace-text textarea").unbind("blur");closeEditor(!0);elem&&elem.length&&elem.trigger(editOptions.editEvent)}else 27==a.keyCode&&closeEditor(!1)}
function createCellEditor(a,b,c,d,q,f){curEditType="simple";keyEvent="keydown";input=null;code='<form name="cell_editor_form" class="cell_editor_form" action="javascript:void(0);">';1==b.blob?"binary"==b.type?(code+='<input type="text" readonly="readonly" name="cell_editor" class="cell_editor" style="text-align:'+f+";width: "+(d-2)+'px;" />',code+="</form>",a.find("span.i").html(code),input=a.find("input"),input.val(c).bind(keyEvent,checkEditField).blur(function(){closeEditor(!0)})):(span=$(a).find("span.d"),
c=span.text(),optionsGet("ui-edit-popup")?($("#dialog-text-editor").dialog({autoOpen:!0,width:500,height:300,modal:!0,open:function(){$("#text-editor").val(c);window.hotkeys.suspend(!0)},buttons:[{text:__("Cancel"),click:function(){window.hotkeys.suspend(!1);$(this).dialog("close")}},{text:__("Save"),click:function(){c=$("#text-editor").val();span.text(c);a.find("span.i").text(0==c.length?"":c.length<=MAX_TEXT_LENGTH_DISPLAY?c:"Text Data ["+formatBytes(c.length)+"]").removeClass("tnl");var b={};b.value=
c;b.setNull=!1;a.parent().hasClass("n")||a.parent().addClass("x");a.data("edit",b).addClass("x");"function"==typeof showNavBtn&&showNavBtn("update","gensql");curEditField=null;window.hotkeys.suspend(!1);$(this).dialog("close")}}]}),input=null):(d=a.width()-20,200>d&&(d=200),textarea=$("#inplace-text textarea"),textarea.width(d).val(c),$("#inplace-text").show().position({of:a,my:"left top",at:"left top",offset:0}),$("#inplace-text textarea").blur(function(){closeEditor(!0)}),curEditType="text",input=
textarea)):(code+='<input type="text" name="cell_editor" class="cell_editor" style="text-align:'+f+";width: "+(d-2)+'px;" />',b.list&&0<b.list.length?code+='<a href="javascript:void(0)" class="cell_editlist">&#x25BE;</a>':!b.type||"datetime"!=b.type&&"date"!=b.type||(code+='<div class="dp"></div><a href="javascript:void(0)" class="cell_editlist">&#x25BE;</a>;'),code+="</form>",a.html(code),input=a.find("input"),input.val(c).select().bind(keyEvent,checkEditField).blur(function(a){editListOpen||closeEditor(!0)}),
b.list&&0<b.list.length?($(".cell_editor").css({width:d-20+"px"}).autocomplete({minLength:0,source:b.list,open:function(a,b){$(".cell_editor").autocomplete("widget").css({width:(160<d?d-2:160)+"px"})},close:function(a,b){$(".cell_editor").focus();editListOpen=!1}}),$(".cell_editlist").mousedown(function(a){a.preventDefault();editListOpen=!0;$(".cell_editor").focus().autocomplete("search","");return!1})):!b.type||"datetime"!=b.type&&"date"!=b.type||($(".cell_editor").css({width:d-20+"px"}),$(".dp").datepicker({dateFormat:"yy-mm-dd",
changeMonth:!0,changeYear:!0,onSelect:function(a,b){$(".dp").fadeOut(300);a+=$(".cell_editor").data("datetime");$(".cell_editor").val(a).focus();editListOpen=!1}}),$(".cell_editlist").mousedown(function(a){a.preventDefault();editListOpen=!0;a=$(".cell_editor").val();$(".cell_editor").data("datetime",a.substr(10));$(".dp").datepicker("setDate",a.substr(0,10));$(".dp").fadeIn(300);$(".cell_editor").focus();return!1})));return input}
function createTableHeader(a){var b="#"+a+"Table";if(!(2>=$(b+" tbody tr").length)){var c="data"==a?"#results-div":"#info-div";$("#"+a+"Header").remove();var d=$(b).clone();d.find("tbody").remove();d.attr("id",a+"Header").appendTo(c);$(c).scroll(function(){var a=parseInt($(this).scrollTop());d.css({top:a+"px"})})}}
function resizeTableHeader(a){var b="#"+a+"Table";if(!(2>=$(b+" tbody tr").length)){a=$("#"+a+"Header");a.width($(b).width());var b=$(b+" thead th"),c=b.length;for(i=0;i<c;i++){var d=$(b[i]).width();a.find("thead th").eq(i).width(d)}}}
$.fn.ensureVisible=function(a,b){b?(pl=a.prop("scrollLeft"),pw=a.width(),p=this.position(),w=this.width(),pw<p.left+w?a.prop("scrollLeft",p.left+w):0>p.left&&a.prop("scrollLeft",p.left)):(pt=a.prop("scrollTop"),ph=a.height(),p=this.position(),h=this.height(),ph<p.top+h?a.prop("scrollTop",p.top+h):0>p.top&&a.prop("scrollTop",p.top))};
$.fn.setSearchFilter=function(a){""==a?$("tr",this).removeClass("ui-helper-hidden"):(string=a.toUpperCase(),$("tbody tr",this).each(function(){var a=!1;$("td",this).each(function(){if($(this).text().toUpperCase().match(string))return a=!0});a?$(this).removeClass("ui-helper-hidden"):$(this).addClass("ui-helper-hidden")}))};
(function(a){a.fn.sorttable=function(b){return this.each(function(){var c=a(this),d=a("#"+c.attr("id").replace("Table","Header"));b=b||{};b=a.extend({},a.fn.sorttable.default_sort_fns,b);d.on("click.sorttable","thead th",function(){var d=a(this),f=0,e=a.fn.sorttable.dir;d.parents("tr").find("th").slice(0,d.index()).each(function(){var b=a(this).attr("colspan")||1;f+=parseInt(b,10)});var g=d.data("sort-default")||e.ASC;d.data("sort-dir")&&(g=d.data("sort-dir")===e.ASC?e.DESC:e.ASC);var m=d.data("sort")||
null;null!==m&&(c.css("display"),setTimeout(function(){var k=[],n=b[m],l=c.children("tbody").children("tr");l.each(function(b,c){var d=a(c).children().eq(f),e=d.data("sort-value"),d="undefined"!==typeof e?e:d.text();k.push([d,c])});k.sort(function(a,b){return n(a[0],b[0])});g!=e.ASC&&k.reverse();l=a.map(k,function(a){return a[1]});c.children("tbody").append(l);c.find("th").data("sort-dir",null).removeClass("sorting-desc sorting-asc");d.data("sort-dir",g).addClass("sorting-"+g);d.parent().find("span").remove();
c.find("thead span").remove();d.find("div").append(g==e.ASC?"<span>&nbsp;&#x25B4;<span>":"<span>&nbsp;&#x25BE;<span>");c.find("thead th:eq("+f+") div").append(g==e.ASC?"<span>&nbsp;&#x25B4;<span>":"<span>&nbsp;&#x25BE;<span>");resizeTableHeader("info");a("tbody tr",c).each(function(b){a("td:eq(0)",this).html(b+1)});c.css("display")},10))})})};a.fn.sorttable.dir={ASC:"asc",DESC:"desc"};a.fn.sorttable.default_sort_fns={numeric:function(a,c){return"NULL"!=a&&"NULL"!=c?parseInt(a,10)-parseInt(c,10):"NULL"==
a?-1:1},"float":function(a,c){return"NULL"!=a&&"NULL"!=c?parseFloat(a)-parseFloat(c):"NULL"==a?-1:1},text:function(a,c){return"NULL"!=a&&"NULL"!=c?a.localeCompare(c):"NULL"==a?-1:1},"text-ins":function(a,c){a=a.toLocaleLowerCase();c=c.toLocaleLowerCase();return a.localeCompare(c)}}})(jQuery);
