DateInput=(function($){function DateInput(el,opts){if(typeof(opts)!="object")opts={};$.extend(this,DateInput.DEFAULT_OPTS,opts);this.input=$(el);this.bindMethodsToObj("show","hide","hideIfClickOutside","keydownHandler","selectDate");this.build();this.selectDate();this.hide();};DateInput.DEFAULT_OPTS={month_names:["January","February","March","April","May","June","July","August","September","October","November","December"],short_month_names:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],short_day_names:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],start_of_week:1};DateInput.prototype={build:function(){var monthNav=$('<p class="month_nav">'+'<span class="button prev" title="[Page-Up]">&#171;</span>'+' <span class="month_name"></span> '+'<span class="button next" title="[Page-Down]">&#187;</span>'+'</p>');this.monthNameSpan=$(".month_name",monthNav);$(".prev",monthNav).click(this.bindToObj(function(){this.moveMonthBy(-1);}));$(".next",monthNav).click(this.bindToObj(function(){this.moveMonthBy(1);}));var yearNav=$('<p class="year_nav">'+'<span class="button prev" title="[Ctrl+Page-Up]">&#171;</span>'+' <span class="year_name"></span> '+'<span class="button next" title="[Ctrl+Page-Down]">&#187;</span>'+'</p>');this.yearNameSpan=$(".year_name",yearNav);$(".prev",yearNav).click(this.bindToObj(function(){this.moveMonthBy(-12);}));$(".next",yearNav).click(this.bindToObj(function(){this.moveMonthBy(12);}));var nav=$('<div class="nav"></div>').append(monthNav,yearNav);var tableShell="<table><thead><tr>";$(this.adjustDays(this.short_day_names)).each(function(){tableShell+="<th>"+this+"</th>";});tableShell+="</tr></thead><tbody></tbody></table>";this.dateSelector=this.rootLayers=$('<div class="date_selector"></div>').append(nav,tableShell).insertAfter(this.input);if($.browser.msie&&$.browser.version<7){this.ieframe=$('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);this.rootLayers=this.rootLayers.add(this.ieframe);$(".button",nav).mouseover(function(){$(this).addClass("hover")});$(".button",nav).mouseout(function(){$(this).removeClass("hover")});};this.tbody=$("tbody",this.dateSelector);this.input.change(this.bindToObj(function(){this.selectDate();}));this.selectDate();},selectMonth:function(date){var newMonth=new Date(date.getFullYear(),date.getMonth(),1);if(!this.currentMonth||!(this.currentMonth.getFullYear()==newMonth.getFullYear()&&this.currentMonth.getMonth()==newMonth.getMonth())){this.currentMonth=newMonth;var rangeStart=this.rangeStart(date),rangeEnd=this.rangeEnd(date);var numDays=this.daysBetween(rangeStart,rangeEnd);var dayCells="";for(var i=0;i<=numDays;i++){var currentDay=new Date(rangeStart.getFullYear(),rangeStart.getMonth(),rangeStart.getDate()+i,12,00);if(this.isFirstDayOfWeek(currentDay))dayCells+="<tr>";if(currentDay.getMonth()==date.getMonth()){dayCells+='<td class="selectable_day" date="'+this.dateToString(currentDay)+'">'+currentDay.getDate()+'</td>';}else{dayCells+='<td class="unselected_month" date="'+this.dateToString(currentDay)+'">'+currentDay.getDate()+'</td>';};if(this.isLastDayOfWeek(currentDay))dayCells+="</tr>";};this.tbody.empty().append(dayCells);this.monthNameSpan.empty().append(this.monthName(date));this.yearNameSpan.empty().append(this.currentMonth.getFullYear());$(".selectable_day",this.tbody).click(this.bindToObj(function(event){this.changeInput($(event.target).attr("date"));}));$("td[date="+this.dateToString(new Date())+"]",this.tbody).addClass("today");$("td.selectable_day",this.tbody).mouseover(function(){$(this).addClass("hover")});$("td.selectable_day",this.tbody).mouseout(function(){$(this).removeClass("hover")});};$('.selected',this.tbody).removeClass("selected");$('td[date='+this.selectedDateString+']',this.tbody).addClass("selected");},selectDate:function(date){if(typeof(date)=="undefined"){date=this.stringToDate(this.input.val());};if(!date)date=new Date();this.selectedDate=date;this.selectedDateString=this.dateToString(this.selectedDate);this.selectMonth(this.selectedDate);},changeInput:function(dateString){this.input.val(dateString).change();this.hide();},show:function(){this.rootLayers.css("display","block");$([window,document.body]).click(this.hideIfClickOutside);this.input.unbind("focus",this.show);$(document.body).keydown(this.keydownHandler);this.setPosition();},hide:function(){this.rootLayers.css("display","none");$([window,document.body]).unbind("click",this.hideIfClickOutside);this.input.focus(this.show);$(document.body).unbind("keydown",this.keydownHandler);},hideIfClickOutside:function(event){if(event.target!=this.input[0]&&!this.insideSelector(event)){this.hide();};},insideSelector:function(event){var offset=this.dateSelector.position();offset.right=offset.left+this.dateSelector.outerWidth();offset.bottom=offset.top+this.dateSelector.outerHeight();return event.pageY<offset.bottom&&event.pageY>offset.top&&event.pageX<offset.right&&event.pageX>offset.left;},keydownHandler:function(event){switch(event.keyCode)
{case 9:case 27:this.hide();return;break;case 13:this.changeInput(this.selectedDateString);break;case 33:this.moveDateMonthBy(event.ctrlKey?-12:-1);break;case 34:this.moveDateMonthBy(event.ctrlKey?12:1);break;case 38:this.moveDateBy(-7);break;case 40:this.moveDateBy(7);break;case 37:this.moveDateBy(-1);break;case 39:this.moveDateBy(1);break;default:return;}
event.preventDefault();},stringToDate:function(string){var matches;if(matches=string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)){return new Date(matches[3],this.shortMonthNum(matches[2]),matches[1],12,00);}else{return null;};},dateToString:function(date){return date.getDate()+" "+this.short_month_names[date.getMonth()]+" "+date.getFullYear();},setPosition:function(){var offset=this.input.offset();this.rootLayers.css({top:offset.top+this.input.outerHeight()-165,left:offset.left-230});if(this.ieframe){this.ieframe.css({width:this.dateSelector.outerWidth(),height:this.dateSelector.outerHeight()});};},moveDateBy:function(amount){var newDate=new Date(this.selectedDate.getFullYear(),this.selectedDate.getMonth(),this.selectedDate.getDate()+amount);this.selectDate(newDate);},moveDateMonthBy:function(amount){var newDate=new Date(this.selectedDate.getFullYear(),this.selectedDate.getMonth()+amount,this.selectedDate.getDate());if(newDate.getMonth()==this.selectedDate.getMonth()+amount+1){newDate.setDate(0);};this.selectDate(newDate);},moveMonthBy:function(amount){var newMonth=new Date(this.currentMonth.getFullYear(),this.currentMonth.getMonth()+amount,this.currentMonth.getDate());this.selectMonth(newMonth);},monthName:function(date){return this.month_names[date.getMonth()];},bindToObj:function(fn){var self=this;return function(){return fn.apply(self,arguments)};},bindMethodsToObj:function(){for(var i=0;i<arguments.length;i++){this[arguments[i]]=this.bindToObj(this[arguments[i]]);};},indexFor:function(array,value){for(var i=0;i<array.length;i++){if(value==array[i])return i;};},monthNum:function(month_name){return this.indexFor(this.month_names,month_name);},shortMonthNum:function(month_name){return this.indexFor(this.short_month_names,month_name);},shortDayNum:function(day_name){return this.indexFor(this.short_day_names,day_name);},daysBetween:function(start,end){start=Date.UTC(start.getFullYear(),start.getMonth(),start.getDate());end=Date.UTC(end.getFullYear(),end.getMonth(),end.getDate());return(end-start)/86400000;},changeDayTo:function(dayOfWeek,date,direction){var difference=direction*(Math.abs(date.getDay()-dayOfWeek-(direction*7))%7);return new Date(date.getFullYear(),date.getMonth(),date.getDate()+difference);},rangeStart:function(date){return this.changeDayTo(this.start_of_week,new Date(date.getFullYear(),date.getMonth()),-1);},rangeEnd:function(date){return this.changeDayTo((this.start_of_week-1)%7,new Date(date.getFullYear(),date.getMonth()+1,0),1);},isFirstDayOfWeek:function(date){return date.getDay()==this.start_of_week;},isLastDayOfWeek:function(date){return date.getDay()==(this.start_of_week-1)%7;},adjustDays:function(days){var newDays=[];for(var i=0;i<days.length;i++){newDays[i]=days[(i+this.start_of_week)%7];};return newDays;}};$.fn.date_input=function(opts){return this.each(function(){new DateInput(this,opts);});};$.date_input={initialize:function(opts){$("input.date_input").date_input(opts);}};return DateInput;})(jQuery);(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var running=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var div=$(this),ul=$("ul",div),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v;}
var li=$("li",ul),itemLength=li.size(),curr=o.start;div.css("visibility","visible");li.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});div.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var liSize=o.vertical?height(li):width(li);var ulSize=liSize*itemLength;var divSize=liSize*v;li.css({width:li.width(),height:li.height()});ul.css(sizeCss,ulSize+"px").css(animCss,-(curr*liSize));div.css(sizeCss,divSize+"px");if(o.btnPrev)
$(o.btnPrev).click(function(){return go(curr-o.scroll);});if(o.btnNext)
$(o.btnNext).click(function(){return go(curr+o.scroll);});if(o.btnGo)
$.each(o.btnGo,function(i,val){$(val).click(function(){return go(o.circular?o.visible+i:i);});});if(o.mouseWheel&&div.mousewheel)
div.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll);});if(o.auto)
setInterval(function(){go(curr+o.scroll);},o.auto+o.speed);function vis(){return li.slice(curr).slice(0,v);};function go(to){if(!running){if(o.beforeStart)
o.beforeStart.call(this,vis());if(o.circular){if(to<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*liSize)+"px");curr=to==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll;}else if(to>=itemLength-v+1){ul.css(animCss,-((v)*liSize)+"px");curr=to==itemLength-v+1?v+1:v+o.scroll;}else curr=to;}else{if(to<0||to>itemLength-v)return;else curr=to;}
running=true;ul.animate(animCss=="left"?{left:-(curr*liSize)}:{top:-(curr*liSize)},o.speed,o.easing,function(){if(o.afterEnd)
o.afterEnd.call(this,vis());running=false;});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled");}}
return false;};});};function css(el,prop){return parseInt($.css(el[0],prop))||0;};function width(el){return el[0].offsetWidth+css(el,'marginLeft')+css(el,'marginRight');};function height(el){return el[0].offsetHeight+css(el,'marginTop')+css(el,'marginBottom');};})(jQuery);function clear_it(object){object.each(function(){$(this).bind("focus",function(){var search_text=$(this).val();$(this).val('');$(this).bind("blur",function(){search_text=($(this).val()!=="")?$(this).val():search_text;$(this).val(search_text);});});});}
function ajaxify_ledger_entry_forms(){$('.ledger-entry-form').submit(function(){var action=$(this).attr("action");var data=$(this).serialize();var hours_td=$(this).parents("tr").children("td.hours");var add_input=$(this).parent("tr").find("input.add-hours-input");var complete_checkbox=$(this).parent("tr").find("input.station-complete-checkbox");$.ajax({type:"POST",data:data,url:action,cache:false,dataType:"json",success:function(data,textStatus){if(data['success']==false){hours_td.html('<span style="color: #cc0000;">'+data['errors']['hours']+'</span>');}else{hours_td.html(data['station_hours']+"/"+data['station_hours_required']);add_input.val("Add hours...");if(data['station_hours_complete']==true&&data['station_complete']==false){complete_checkbox.removeAttr('disabled');}
if(data['station_hours_complete']==true&&data['station_complete']==true){complete_checkbox.attr('disabled','disabled');}}},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false;});}
$(document).ready(function(){clear_it($('input[name=ledger_entry-hours]'));$.extend(DateInput.DEFAULT_OPTS,{stringToDate:function(string){var matches;if(matches=string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)){return new Date(matches[1],matches[2]-1,matches[3]);}else{return null;};},dateToString:function(date){var month=(date.getMonth()+1).toString();var dom=date.getDate().toString();if(month.length==1)month="0"+month;if(dom.length==1)dom="0"+dom;return date.getFullYear()+"-"+month+"-"+dom;}});$($.date_input.initialize);$(".apprentice-dashboard #edit-hours-table").hide()
$(".apprentice-dashboard #show-progress").live('click',function(e){location.reload();});$(".apprentice-dashboard #show-hours").live('click',function(e){$("#view-progress-table").hide()
$("#edit-hours-table").show()});$('.ledger-row').hide();$('.ledger-link').live('click',function(e){e.preventDefault();$('.ledger-row').hide();$(this).parents("tr").next("tr.ledger-row").show();});ajaxify_ledger_entry_forms();$('.competency-credential-form').submit(function(){var action=$(this).attr("action");var data=$(this).serialize();save_button=$(this).parent("tr").find("button");$.ajax({type:"POST",data:data,url:action,cache:false,dataType:"json",success:function(data,textStatus){if(data['success']==true){save_button.attr("disabled","disabled");save_button.val("Saved!");}},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false;});$('.assign-apprentice-to-chef-form').submit(function(){var action=$(this).attr("action");var data=$(this).serialize();save_button=$(this).parent("tr").find("button");$.ajax({type:"POST",data:data,url:action,cache:false,dataType:"json",success:function(data,textStatus){if(data['success']==true){save_button.attr("disabled","disabled");save_button.text("Saved!");}},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false;});$('.competency-details').hide();$('.competency-details-link').live('click',function(e){e.preventDefault();$(this).parents("tr").next("tr.competency-details").toggle('fast');});var item_count=$("ul.carousel").children().length;if(item_count>11){$("div.carousel").jCarouselLite({btnNext:".next",btnPrev:".previous",speed:200,visible:11});};$(".sortable").tablesorter();if($('#id_preferences-make_profile_public:checked').val()==null){$('#field_preferences-make_logbook_entries_public').hide();$('#field_preferences-make_photo_uploads_public').hide();$('#field_preferences-make_document_uploads_public').hide();}
$('#id_preferences-make_profile_public').live('click',function(e){if($('#id_preferences-make_profile_public:checked').val()==null){$('#field_preferences-make_logbook_entries_public').fadeOut();$('#field_preferences-make_photo_uploads_public').fadeOut();$('#field_preferences-make_document_uploads_public').fadeOut();}else{$('#id_preferences-make_logbook_entries_public').removeAttr('disabled');$('#field_preferences-make_logbook_entries_public').fadeIn();$('#id_preferences-make_photo_uploads_public').removeAttr('disabled');$('#field_preferences-make_photo_uploads_public').fadeIn();$('#id_preferences-make_document_uploads_public').removeAttr('disabled');$('#field_preferences-make_document_uploads_public').fadeIn();};});$('.accordion h4').live('click',function(e){e.preventDefault();var group_id=$(this).attr('id').split('-')[0]
var url="/enrollment_groups/apprentices/"+$(this).attr('id').replace('-','/');$('#enrollment-group-'+group_id).load(url,function(){$('.ledger-row').hide();ajaxify_ledger_entry_forms();});$(this).find('span').hide();});$('.chef-assignments-accordion h4').live('click',function(e){e.preventDefault();var group_id=$(this).attr('id').split('-')
var url="/enrollment_groups/chef-apprentice-assignments/"+$(this).attr('id').replace('-','/');$('#chef-assignments-enrollment-group-'+group_id).load(url,function(){$('.assign-apprentice-to-chef-form').submit(function(){var action=$(this).attr("action");var data=$(this).serialize();save_button=$(this).parent("tr").find("button");$.ajax({type:"POST",data:data,url:action,cache:false,dataType:"json",success:function(data,textStatus){if(data['success']==true){save_button.attr("disabled","disabled");save_button.text("Saved!");}},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false;});});$(this).find('span').hide();});});
