var siteCalendar = Class.create(); siteCalendar.prototype = { initialize: function(container_id) { this.container = $(container_id); if(!this.container) { return false; } this.first_load = true; this.years = null; this.months = null; this.year_dropper = null; this.month_dropper = null; this.delay = 50; this.timeout = null; this.current_list_id = null; this.current_dropper_id = null; this.waiting_response = false; this.wait = 'Loading...'; this.buffer_keys = new Array(); this.buffer_content = new Array(); this.buffer_index = -1; this.year = new Date().getFullYear(); this.month = new Date().getMonth() + 1; return true; }, init: function() { this.timestamp = new Date(); this.timestamp = Date.parse( this.timestamp ) + this.timestamp.getMilliseconds(); this.timestamp += Math.round( Math.random() * this.timestamp ); this.year_handlers = this.container.getElementsByClassName('year_list'); this.month_handlers = this.container.getElementsByClassName('month_list'); if( this.year_handlers.length ) { this.year_handlers[0].id = 'drop_year_'+this.timestamp; this.year_handlers[1].id = 'list_year_'+this.timestamp; this.year_handlers[1].addClassName('list'); this.year_dropper = this.year_handlers[0]; this.observeDropper(this.year_dropper); this.observeList( this.year_handlers[1] ); } if( this.month_handlers.length ) { this.month_handlers[0].id = 'drop_month_'+this.timestamp; this.month_handlers[1].id = 'list_month_'+this.timestamp; this.month_handlers[1].addClassName('list'); this.month_dropper = this.month_handlers[0]; this.observeDropper(this.month_dropper); this.observeList( this.month_handlers[1] ); } if( this.first_load ) { this.first_load = false; } return true; }, observeDropper: function( dropper ) { Event.observe ( dropper, 'click', this.showList.bind(this, dropper ) ); Event.observe ( dropper, 'mouseover', this.stopHiding.bind(this) ); Event.observe ( dropper, 'mouseout', this.slowHide.bind(this) ); return true; }, observeList: function( list_obj ) { Element.addClassName(list_obj,'hidden_list'); this.links = list_obj.getElementsByTagName('a'); for( var i=0; i< this.links.length; i++) { this.links[i].onclick = function(){return false;} Event.observe ( this.links[i], 'click', this.catchLink.bind(this, this.links[i] ) ); } Event.observe ( list_obj, 'mouseover', this.stopHiding.bind(this) ); Event.observe ( list_obj, 'mouseout', this.slowHide.bind(this) ); return true; }, catchLink: function( link ) { this.load(link.href); return false; }, load: function( url ) { this.waiting_response = true; this.last_url = url; this.buffer_index = this.buffer_keys.indexOf(this.last_url.split('date_')[1]); if( this.buffer_index != -1 ) { this.reload(this.buffer_content[this.buffer_index]); this.buffer_index = -1; return true; } this.data = this.last_url.split('_'); this.id = this.data[1]; this.year = this.data[2]; this.month = this.data[3]; this.day = null; if( this.first_load ) { this.day = this.data[4]; if( !this.container.hasClassName('calendar') ) {this.container.addClassName('calendar');} } else { this.quickHide(); if( this.wait.length ) { Element.update(this.current_dropper_id,this.wait); } } this.calendar_request = new Ajax.Request( 'calendar', { method: "post", parameters: {id: this.id, year: this.year, month: this.month, day: this.day}, onComplete: this.saveToBuffer.bind(this) } ); return true; }, saveToBuffer: function( request ) { this.buffer_keys.push(this.last_url.split('date_')[1]); this.buffer_content.push(request.responseText); this.reload(request.responseText); return true; }, reload: function( content ) { Element.update(this.container, content); this.waiting_response = false; this.init(); return true; }, showList: function( dropper_obj ) { if( !this.waiting_response ) { this.current_dropper_id = dropper_obj.id; Element.addClassName(this.current_dropper_id,'dropped'); this.current_list_id = this.current_dropper_id.replace('drop','list') Element.removeClassName(this.current_list_id,'hidden_list'); } return true; }, stopHiding: function() { if( this.timeout ) { clearTimeout(this.timeout); } return true; }, slowHide: function() { this.timeout = setTimeout( this.quickHide.bind(this), this.delay ); return true; }, quickHide: function() { Element.addClassName(this.current_list_id,'hidden_list'); Element.removeClassName(this.current_dropper_id,'dropped'); return true; }, changeMonth: function( destination ) { if( destination == 'up' ) { this.month++; if( this.month == 13 ) { this.month = 1; this.year ++; } } if( destination == 'down' ) { this.month--; if( this.month == 0 ) { this.month = 12; this.year--; } } this.calendar_request = new Ajax.Request( 'calendar', { method: "post", parameters: {year: this.year, month: this.month}, onComplete: this.reloadCalendar.bind(this) } ); }, reloadCalendar: function(request) { if( request.responseText ) { this.reload(request.responseText); } } }