// Copyright (c) 2010, Fast Lane Institute for Knowledge Transfer

CMS.define('FL_SchedulePreview',{initialize:function(config)
{this.config=config;this.schedulepanel=$(config.nodename+'_panel');this.initRegionSwitch();},initRegionSwitch:function()
{this.regionswitch=$(this.config.nodename+'_regionswitch');if(!this.regionswitch)
{return;}
var len;if(!this.config.regions||!(len=this.config.regions.length))
{return;}
var rswitchtbl='<table class=" noprint regionswitch" style="width:100%;"><tr>';for(var i=0;i<len;i++)
{var domid=this.config.nodename+'_switch'+i;rswitchtbl+='<td'+(i?'':' class="current"')+'><span><a href="javascript:void(0);" id="'+domid+'">'+this.config.regions[i].label+'</a></span></td>';this.config.regions[i].node=domid;}
rswitchtbl+='</tr></table>';this.regionswitch.replace(rswitchtbl);this.regionswitch.show();for(var i=0;i<len;i++)
{Event.observe(this.config.regions[i].node,'click',this.switchRegion.bind(this));}
var loadingindicator=document.createElement('div');this.loadingindicator=$(loadingindicator);this.loadingindicator.innerHTML=this.config.loadingindicator;this.config.regions[0].cachedhtml=this.schedulepanel.innerHTML;this.panelheight=this.schedulepanel.clientHeight;},switchRegion:function(evt)
{var item=evt.element();if(item.id)
{matches=item.id.match(/switch([0-9]+)/);if(matches.length)
{switchid=matches[1];}}
if(switchid)
{$(item.parentNode.parentNode).addClassName('current');$(item.parentNode.parentNode).siblings().each(function(el){el.removeClassName('current');});if(this.config.regions[switchid].cachedhtml)
{this.schedulepanel.update(this.config.regions[switchid].cachedhtml);}
else
{this.updatePanel(switchid);}}},updatePanel:function(regid)
{data={region:this.config.regions[regid].code,type:'guaranteed'};if(this.config.showlimit)
{data.showlimit=this.config.showlimit;}
new Ajax.Updater({success:this.schedulepanel},this.config.getschedule,{parameters:data,onLoading:this.indicateLoading.bind(this),onComplete:this.cachePanel.bind({controller:this,'regid':regid})});},cachePanel:function(response)
{if(!this.controller.config.regions[this.regid].cachedhtml)
{this.controller.config.regions[this.regid].cachedhtml=response.responseText;}},indicateLoading:function()
{Element.update(this.schedulepanel,this.loadingindicator.innerHTML);}});