(function($){$.fittable={defaults:{itemCount:10,ajaxUrl:"",table:'',plugin_prefix:'ft_',indicator:true,showPager:true,pagerID:'ft_pager',pager_li_Css:{display:"inline"},iconCss:{display:"inline"},dataType:'html',sort_asc_img:"/images/sort_asc.png",sort_desc_img:"/images/sort_desc.png",iconAttrs:{id:"sort",src:"/images/sort.png"},wrapDivAttrs:{},wrapDivCSS:{display:"inline"},headerIds:[],onSortIconClick:function(a){},afterLoading:function(data,table,th,a){},onLoading:function(data,table,th,a){},onError:function(request){}}};$.fn.extend({fittable:function(config){if($(this).data('fittable')!=null){return false;}var config=$.extend({},$.fittable.defaults,config);$(this).find('th:not(:empty)').each(function(i,val){if(config.headerIds&&config.headerIds[i]!=null&&config.headerIds[i]!=''){var headerId=config.headerIds[i];createSortIcon(config,this,headerId);}});config.table=this;$(this).data('fittable',config);return this;}});function createSortIcon(config,th,headerId){var aDiv=$("<div />").attr(config.wrapDivAttrs).css(config.wrapDivCSS).attr({id:config.plugin_prefix+headerId+'_div'});var alink=$("<a />").attr({href:config.ajaxUrl,id:config.plugin_prefix+headerId+'_a',rel:headerId}).appendTo(aDiv);$("<img />").attr(config.iconAttrs).attr({id:config.plugin_prefix+headerId+'_img'}).css(config.iconCss).appendTo(alink);aDiv.appendTo(th);alink.click(function(e){e.preventDefault();var col=$(this).attr('rel');var order=$(this).attr('title');if(order=='ASC'){order='DESC';$(this).find('img').attr({src:config.sort_desc_img});$(this).attr('title','DESC');}else{order='ASC';$(this).find('img').attr({src:config.sort_asc_img});$(this).attr('title','ASC');}otherIcons=$(th).parent().find('th').not(th).find('img');otherIcons.attr({src:config.iconAttrs.src});config.onSortIconClick.call(e,this);loadTableRows(config,this,th);});}function createPager(config,th,headerId){var pagerDiv=$('#'+config.pagerID);if(config.showPager&&pagerDiv.length>0){var aul=$("<ul />");$("<li />").attr(config.iconAttrs).attr({id:config.plugin_prefix+headerId+'_img'}).appendTo(aul);aul.appendTo(pagerDiv);aDiv.appendTo(th);alink.click(function(e){e.preventDefault();var col=$(this).attr('rel');var order=$(this).attr('title');if(order=='ASC'){order='DESC';$(this).find('img').attr({src:config.sort_desc_img});$(this).attr('title','DESC');}else{order='ASC';$(this).find('img').attr({src:config.sort_asc_img});$(this).attr('title','ASC');}otherIcons=$(th).parent().find('th').not(th).find('img');otherIcons.attr({src:config.iconAttrs.src});config.onSortIconClick.call(e,this);loadTableRows(config,this,th);});}}function loadTableRows(config,a,th){var extraParams={timestamp:+new Date()};$.ajax({mode:"abort",dataType:config.dataType,url:config.ajaxUrl,data:$.extend({col:$(a).attr('rel'),order:$(a).attr('title'),page:1},extraParams),beforeSend:function(){if(config.indicator){$(config.table).find('tr:not(:has(th))').css({opacity:0.2});config.onLoading.call(config.table,th,a);}},success:function(data){$(config.table).find('tr:not(:has(th))').remove();$(config.table).find('tbody').append(data);config.afterLoading.call(null,data,config.table,th,a);},onError:function(request){config.onError.call(request);}});}function simpleLoad(config){for(var x=1;x<config.imgTotal+1;x++){$("<img />").attr({id:"image"+x,src:config.imgDir+x+config.imgFormat,title:"Image"+x}).appendTo("#"+config.imgContainer).css({display:"none"});}};function complexLoad(config,fileNames){for(var x=0;x<fileNames.length;x++){$("<img />").attr({id:fileNames[x],src:config.imgDir+fileNames[x]+config.imgFormat,title:"The "+fileNames[x]+" nebula"}).appendTo("#"+config.imgContainer).css({display:"none"});}};})(jQuery);
