function debug(object){if(window['console']){console.debug(object);}}
function log(object){if(window['console']){console.log(object);}}
jQuery.jlm={base:'',params:{},components:{},controllers:{},templates:{},addCallback:function(controller,action,callback){if(typeof(jQuery.jlm.controllers[controller])=='undefined'){jQuery.jlm.controllers[controller]={};}
if(typeof(jQuery.jlm.controllers[controller][action])=='undefined'){jQuery.jlm.controllers[controller][action]=[];}
jQuery.jlm.controllers[controller][action].push(callback);},bind:function(routes,callback){var routesArr=routes.split(',');jQuery.each(routesArr,function(){var route=jQuery.jlm.trim(this);var parts=route.split('.');var controller='';var action='';if(parts.length==2){controller=parts[0];action=parts[1];}else if(parts.length==1){controller=parts[0];action='__global';}else{return alert('JLM error: Routes paramter should be in controller.action format!');}
jQuery.jlm.addCallback(controller,action,callback);});},addComponent:function(name,object){this.components[name]=object;},component:function(name,initOn,componentFunction){jQuery.jlm.components[name]={startup:componentFunction,initOn:initOn};},config:function(params){this.base=params.base;this.params.controller=params.controller;this.params.action=params.action;this.params.prefix=params.prefix;this.params.custom=params.custom;},getTemplate:function(templatePath){tparts=templatePath.split('/');var content=null;var dir=this.params.controller;var template=templatePath;if(tparts.length==2){dir=tparts[0];template=tparts[1];}
if(typeof(jQuery.jlm.templates[dir])=='undefined'){return content;}
if(typeof(jQuery.jlm.templates[dir][template])!=='undefined'){content=jQuery.jlm.templates[dir][template];}
return content;},dispatch:function(){if(typeof(jQuery.jlm.controllers['app_controller'])=='object'&&typeof(jQuery.jlm.controllers['app_controller']['beforeFilter'])=='object'){jQuery.jlm.execute('app_controller','beforeFilter');}
if(typeof(jQuery.jlm.controllers['app_controller'])=='object'&&typeof(jQuery.jlm.controllers['app_controller'][jQuery.jlm.params.action])=='object'){jQuery.jlm.execute('app_controller',jQuery.jlm.params.action);}
if(typeof(jQuery.jlm.controllers[jQuery.jlm.params.controller])=='object'&&typeof(jQuery.jlm.controllers[jQuery.jlm.params.controller][jQuery.jlm.params.action])=='object'){jQuery.jlm.execute(jQuery.jlm.params.controller,jQuery.jlm.params.action);}
jQuery.each(jQuery.jlm.components,function(){if(typeof(this.initOn)==='string'){if(this.initOn==='*'){return this.startup();}
var routesArr=this.initOn.split(',');var execute=false;jQuery.each(routesArr,function(){var route=jQuery.jlm.trim(this);var parts=route.split('.');var controller='';var action='';if(parts.length==2){controller=parts[0];action=parts[1];if(jQuery.jlm.params.controller==controller&&jQuery.jlm.params.action==action){execute=true;}}else if(parts.length==1){controller=parts[0];if(jQuery.jlm.params.controller==controller){execute=true;}}});if(execute){this.startup();}}});},execute:function(controller,action,args){jQuery.each(this.controllers[controller][action],function(){this.apply(this,[args]);});},redirect:function(url,appendBase){if(typeof(appendBase)==='undefined')appendBase=true;var absUrl=url;if(appendBase)absUrl=jQuery.jlm.base+absUrl;window.location.href=absUrl;},template:function(templatePath,viewVars){var templateContent=this.getTemplate(templatePath);if(templateContent==null){alert('JLM template "jlm/views/'+templatePath+'.html" is not present!');return null;}
var template=TrimPath.parseTemplate(templateContent);if(typeof(viewVars)=='undefined'){viewVars={};}
viewVars.BASE=this.base;viewVars.PREFIX=this.params.prefix;return template.process(viewVars);},element:function(elementName,viewVars){return $.jlm.template('elements/'+elementName,viewVars);},trim:function(str,chars){function ltrim(str,chars){chars=chars||"\\s";return str.replace(new RegExp("^["+chars+"]+","g"),"");}
function rtrim(str,chars){chars=chars||"\\s";return str.replace(new RegExp("["+chars+"]+$","g"),"");}
return ltrim(rtrim(str,chars),chars);}};function tme_getPageSize(){var de=document.documentElement;var w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth;var h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;arrayPageSize=[w,h];return arrayPageSize;}
function tme_selectTest(id){var tid='#'+id+'-text';$('.test').hide();$('.test-flag').removeClass('current');$('#'+id).addClass('current');$(tid).fadeIn();}
function tme_nextTest(){next=$('.test-flag.current').next('.test-flag');alert(next);if(!next){alert('no next');next=$('.test-flag:first');alert('next is '.next.attr('id'));}
tme_selectTest(next.attr('id'));setTimeout(tme_nextTest,1000);}
$.jlm.bind('app_controller.beforeFilter',function(){});$.jlm.bind('programs.view, lessons.view, pages.home',function(){var ps=tme_getPageSize();var h=ps[1];var size='';if(h<600){size='s';}else if(h<800){size='m';}else{size='l';}
$('.colorbox').each(function(index){var href=$(this).attr('href');var sized_href=href+'/'+size;$(this).attr('href',sized_href);});$('.colorbox').colorbox();});$.jlm.bind('subscriptions.admin_add, subscriptions.admin_edit',function(){$('.datepicker').datepicker({dateFormat:'yy-mm-dd'});});$.jlm.bind('schools.admin_view, schools.admin_index, signups.admin_index, users.admin_index',function(){$('a.delete-link').click(function(){var row=$(this).parents('tr');var url=$(this).attr('href');if(confirm('Are you sure you want to delete this record?')){$.post(url,null,function(data){row.remove();},'json');}
return false;});});$.jlm.bind('categories.view',function(){var pos=$('li.current').position();if(pos){$('div.playlist').scrollTop(pos.top);}});$.jlm.bind('categories.admin_view',function(){$('.sortable').sortable({axis:'y',cursor:'pointer',items:'tr.item',revert:'true',stop:function(event,ui){var rows=$('#'+$(this).attr('id')+' tr.item');var sep='';var data='';for(i=0;i<rows.length;i++){var row=rows[i];data+=(sep+$(row).attr('rel'));sep=',';}
$.post('/admin/lessons/reorder/',{'data[new_order]':data},function(returnData){ui.item.effect('highlight',{speed:'slow'});});}});});$.jlm.bind('lessons.view',function(){});$.jlm.bind('lessons.admin_edit',function(){$('#add-video').click(function(){$('#add-video-form').toggle();});});$.jlm.bind('pages.home',function(){});function countSelectedPrograms(){boxes=$('.program-checkbox');var count=0;for(var i=0;i<boxes.length;i++){if(boxes[i].checked){count++;}}
return count;}
$.jlm.bind('tariffs.view',function(){function checkCount(){var count=countSelectedPrograms();$('.pq').removeClass('selected');$('.pq-'+count).addClass('selected');if(count==0){$('#signup-submit').attr('disabled','disabled');$('#signup-submit a').addClass('aico-grey');$('#signup-submit a').removeClass('aico-purple');}else{$('#signup-submit').attr('disabled','');$('#signup-submit a').addClass('aico-purple');$('#signup-submit a').removeClass('aico-grey');}}
checkCount();$('.program-checkbox').click(checkCount);});$.jlm.bind('videos.admin_test',function(){$('.load-video').click(function(){var url=$(this).attr('href');flowplayer('video').setClip(url).play();return false;});});
