/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */

if(!dojo._hasResource["esri.dijit.TimeSlider"]){dojo._hasResource["esri.dijit.TimeSlider"]=true;dojo.provide("esri.dijit.TimeSlider");dojo.require("dojox.form.RangeSlider");dojo.require("dijit.form.HorizontalRuleLabels");dojo.require("dijit.form.HorizontalRule");dojo.require("dojox.timing._base");(function(){var _1=[dojo.moduleUrl("dojox","form/resources/RangeSlider.css"),dojo.moduleUrl("esri.dijit","css/TimeSlider.css")];var _2=document.getElementsByTagName("head").item(0),_3;for(var i=0,il=_1.length;i<il;i++){_3=document.createElement("link");_3.type="text/css";_3.rel="stylesheet";_3.href=_1[i];_2.appendChild(_3);}})();dojo.declare("esri.dijit.TimeSlider",[dijit._Widget,dijit._Templated],{widgetsInTemplate:true,templateString:"   <div class=\"esriTimeSlider\">\r\n   <table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\r\n   <tr>\r\n   <td align=\"right\" valign=\"middle\"><button dojoType=\"dijit.form.Button\" showLabel=\"false\" iconClass=\"tsButton tsPlayButton\" dojoAttachEvent=\"onClick:_onPlay\" dojoAttachPoint=\"playPauseBtn\" type=\"button\">${NLS_play}</button></td>\r\n   <td align=\"center\" valign=\"middle\" width=\"80%\" id=\"tsTmp\"></td>\r\n   <td align=\"left\" valign=\"middle\" width=\"30\"><button dojoType=\"dijit.form.Button\" showLabel=\"false\" iconClass=\"tsButton tsPrevButton\" dojoAttachEvent=\"onClick:_onPrev\" dojoAttachPoint=\"previousBtn\" type=\"button\">${NLS_previous}</button></td>\r\n   <td align=\"left\" valign=\"middle\"><button dojoType=\"dijit.form.Button\" showLabel=\"false\" iconClass=\"tsButton tsNextButton\" dojoAttachEvent=\"onClick:_onNext\" dojoAttachPoint=\"nextBtn\" type=\"button\">${NLS_next}</button></td>\r\n   </tr>    \r\n   </table>\r\n   </div>\r\n",basePath:dojo.moduleUrl("esri.dijit"),_slideDuration:1000,_defaultCount:10,constructor:function(_4,_5){dojo.mixin(this,esri.bundle.widgets.timeSlider);this._iconClass="tsButton tsPlayButton";this.playing=false;this.loop=false;this.thumbCount=1;this.thumbMovingRate=1000;this._createTimeInstants=false;this._options=dojo.mixin({excludeDataAtTrailingThumb:false,excludeDataAtLeadingThumb:false},_4.options||{});},startup:function(){this.inherited(arguments);this._timer=new dojox.timing.Timer();this._timer.setInterval(this.thumbMovingRate);this._timer.onTick=dojo.hitch(this,"_bumpSlider",1);this._createSlider();},destroy:function(){this._timer.stop();this._timer=null;this.timeStops=null;this._slider.destroy();this._slider=null;if(this._hTicks){this._hTicks.destroyRecursive();this._hTicks=null;}if(this._hLabels){this._hLabels.destroyRecursive();this._hLabels=null;}this.inherited(arguments);},onTimeExtentChange:function(){},onPlay:function(){},onPause:function(){},onNext:function(){},onPrevious:function(){},_onHorizontalChange:function(){var _6=this._sliderToTimeExtent();this.onTimeExtentChange(_6);},_onPlay:function(){this.playing=!this.playing;this._updateUI();if(this.playing){this._timer.start();this.onPlay(this._sliderToTimeExtent());}else{this._timer.stop();this.onPause(this._sliderToTimeExtent());}var _7=this._getSliderValue();this._offset=dojo.isArray(_7)?(_7[1]-_7[0]):0;},_onNext:function(){if(!this.playing){this._bumpSlider(1);this.onNext(this._sliderToTimeExtent());}},_onPrev:function(){if(!this.playing){this._bumpSlider(-1);this.onPrevious(this._sliderToTimeExtent());}},createTimeStopsByCount:function(_8,_9){if(!_8||!_8.startTime||!_8.endTime){console.log(this.NLS_invalidTimeExtent);return;}_9=_9||this._defaultCount;var _a=Math.ceil((_8.endTime-_8.startTime)/(_9-1));this.createTimeStopsByTimeInterval(_8,_a,"esriTimeUnitsMilliseconds");},createTimeStopsByTimeInterval:function(_b,_c,_d,_e){if(!_b||!_b.startTime||!_b.endTime){console.log(this.NLS_invalidTimeExtent);return;}this.fullTimeExtent=new esri.TimeExtent(_b.startTime,_b.endTime);if(_e&&_e.resetStartTime===true){this._resetStartTime(this.fullTimeExtent,_d);}this._timeIntervalUnits=_d;var te=this.fullTimeExtent.startTime;var _f=[];while(te<=_b.endTime){_f.push(te);te=_b._getOffsettedDate(te,_c,_d);}if(_f.length>0&&_f[_f.length-1]<_b.endTime){_f.push(te);}this.setTimeStops(_f);},getCurrentTimeExtent:function(){return this._sliderToTimeExtent();},setTimeStops:function(_10){this.timeStops=_10||[];this._numStops=this.timeStops.length;this._numTicks=this._numStops;if(esri._isDefined(this.fullTimeExtent)===false){this.fullTimeExtent=new esri.TimeExtent(_10[0],_10[_10.length-1]);}},setLoop:function(_11){this.loop=_11;},setThumbCount:function(_12){this.thumbCount=_12;this.singleThumbAsTimeInstant(this._createTimeInstants);if(this._slider){this._createSlider();}},setThumbIndexes:function(_13){this.thumbIndexes=dojo.clone(_13)||[0,1];this._initializeThumbs();},setThumbMovingRate:function(_14){this.thumbMovingRate=_14;if(this._timer){this._timer.setInterval(this.thumbMovingRate);}},setLabels:function(_15){this.labels=_15;if(this._slider){this._createSlider();}},setTickCount:function(_16){this._numTicks=_16;if(this._slider){this._createSlider();}},singleThumbAsTimeInstant:function(_17){this._createTimeInstants=(_17&&this.thumbCount===1);},next:function(){this._onNext();},pause:function(){this.playing=false;this._updateUI();this._timer.stop();},play:function(){if(this.playing===true){return;}this.playing=false;this._onPlay();},previous:function(){this._onPrev();},_updateUI:function(){dojo.removeClass(this.playPauseBtn.iconNode,this._iconClass);this._iconClass=this.playing?"tsButton tsPauseButton":"tsButton tsPlayButton";dojo.addClass(this.playPauseBtn.iconNode,this._iconClass);this.previousBtn.set("disabled",this.playing);this.nextBtn.set("disabled",this.playing);},_createSlider:function(){if(this._slider){this._slider.destroy();this._slider=null;}var _18=this.domNode;while(_18.parentNode&&!_18.dir){_18=_18.parentNode;}var _19={onChange:dojo.hitch(this,"_onHorizontalChange"),showButtons:false,discreteValues:this._numStops,slideDuration:this._slideDuration,"class":"ts",id:"ts",dir:_18.dir};var ts=dojo.create("div",{id:"ts"},dojo.byId("tsTmp"),"first");dojo.create("div",{id:"timeSliderTicks"},ts,"first");dojo.create("div",{id:"timeSliderLabels"},ts);if(this.thumbCount===2){this._createRangeSlider(_19);}else{this._createSingleSlider(_19);}this.thumbIndexes=this.thumbIndexes||[0,1];this._createHorizRule();this._createLabels();if(this._createTimeInstants===true){dojo.query(".dijitSliderProgressBarH, .dijitSliderLeftBumper, .dijitSliderRightBumper").forEach("dojo.style(item, { background: 'none' });");}this._initializeThumbs();dojo.disconnect(this._onChangeConnect);this._onChangeConnect=dojo.connect(this._slider,"onChange",dojo.hitch(this,"_updateThumbIndexes"));},_createRangeSlider:function(_1a){this._isRangeSlider=true;this._slider=new dojox.form.HorizontalRangeSlider(_1a,dojo.byId("ts"));},_createSingleSlider:function(_1b){this._isRangeSlider=false;this._slider=new dijit.form.HorizontalSlider(_1b,dojo.byId("ts"));},_createHorizRule:function(){if(this._hTicks){this._hTicks.destroyRecursive();this._hTicks=null;}if(this._numTicks<2){return;}this._hTicks=new dijit.form.HorizontalRule({container:"topDecoration",ruleStyle:"","class":"tsTicks",count:this._numTicks,id:"tsTicks"},dojo.byId("timeSliderTicks"));},_createLabels:function(){if(this._hLabels){this._hLabels.destroyRecursive();this._hLabels=null;}if(this.labels&&this.labels.length>0){this._hLabels=new dijit.form.HorizontalRuleLabels({labels:this.labels,labelStyle:"","class":"tsLabels",id:"tsLabels"},dojo.byId("timeSliderLabels"));}},_initializeThumbs:function(){if(!this._slider){return;}this._offset=this._toSliderValue(this.thumbIndexes[1])||0;var t1=this._toSliderValue(this.thumbIndexes[0]);t1=(t1>this._slider.maximum||t1<this._slider.minimum)?this._slider.minimum:t1;if(this._isRangeSlider===true){var t2=this._toSliderValue(this.thumbIndexes[1]);t2=(t2>this._slider.maximum||t2<this._slider.minimum)?this._slider.maximum:t2;t2=t2<t1?t1:t2;this._setSliderValue([t1,t2]);}else{this._setSliderValue(t1);}this._onHorizontalChange();},_bumpSlider:function(dir){var val=this._getSliderValue();var max=val,min=max;var _1c=dir;if(dojo.isArray(val)){min=val[0];max=val[1];_1c=[{"change":dir,"useMaxValue":true},{"change":dir,"useMaxValue":false}];}if((Math.abs(min-this._slider.minimum)<1e-10&&dir<0)||(Math.abs(max-this._slider.maximum)<1e-10&&dir>0)){if(this._timer.isRunning){if(this.loop){this._timer.stop();this._setSliderValue(this._getSliderMinValue());var _1d=this._sliderToTimeExtent();this.onTimeExtentChange(_1d);this._timer.start();this.playing=true;}else{this.pause();}}}else{this._slider._bumpValue(_1c);}},_updateThumbIndexes:function(){var val=this._getSliderValue();if(dojo.isArray(val)){this.thumbIndexes[0]=this._toSliderIndex(val[0]);this.thumbIndexes[1]=this._toSliderIndex(val[1]);}else{this.thumbIndexes[0]=this._toSliderIndex(val);}},_sliderToTimeExtent:function(){if(!this.timeStops||this.timeStops.length===0){return;}var _1e=new esri.TimeExtent();var val=this._getSliderValue();if(dojo.isArray(val)){_1e.startTime=new Date(this.timeStops[this._toSliderIndex(val[0])]);_1e.endTime=new Date(this.timeStops[this._toSliderIndex(val[1])]);this._adjustTimeExtent(_1e);}else{_1e.startTime=(this._createTimeInstants===true)?new Date(this.timeStops[this._toSliderIndex(val)]):new Date(this.fullTimeExtent.startTime);_1e.endTime=(this._createTimeInstants===true)?_1e.startTime:new Date(this.timeStops[this._toSliderIndex(val)]);}return _1e;},_adjustTimeExtent:function(_1f){if(this._options.excludeDataAtTrailingThumb===false&&this._options.excludeDataAtLeadingThumb===false){return;}if(_1f.startTime.getTime()===_1f.endTime.getTime()){return;}if(this._options.excludeDataAtTrailingThumb){var _20=_1f.startTime;_20.setUTCSeconds(_20.getUTCSeconds()+1);}if(this._options.excludeDataAtLeadingThumb){var _21=_1f.endTime;_21.setUTCSeconds(_21.getUTCSeconds()-1);}},_resetStartTime:function(_22,_23){switch(_23){case "esriTimeUnitsSeconds":_22.startTime.setUTCMilliseconds(0);break;case "esriTimeUnitsMinutes":_22.startTime.setUTCSeconds(0,0,0);break;case "esriTimeUnitsHours":_22.startTime.setUTCMinutes(0,0,0);break;case "esriTimeUnitsDays":_22.startTime.setUTCHours(0,0,0,0);break;case "esriTimeUnitsWeeks":_22.startTime.setUTCDate(_22.startTime.getUTCDate()-_22.startTime.getUTCDay());break;case "esriTimeUnitsMonths":_22.startTime.setUTCDate(1);_22.startTime.setUTCHours(0,0,0,0);break;case "esriTimeUnitsDecades":_22.startTime.setUTCFullYear(_22.startTime.getUTCFullYear()-(_22.startTime.getUTCFullYear()%10));break;case "esriTimeUnitsCenturies":_22.startTime.setUTCFullYear(_22.startTime.getUTCFullYear()-(_22.startTime.getUTCFullYear()%100));break;}},_getSliderMinValue:function(){if(this._isRangeSlider){return [this._slider.minimum,this._slider.minimum+this._offset];}else{return this._slider.minimum;}},_toSliderIndex:function(val){var idx=Math.floor((val-this._slider.minimum)*this._numStops/(this._slider.maximum-this._slider.minimum));if(idx<0){idx=0;}if(idx>=this._numStops){idx=this._numStops-1;}return idx;},_toSliderValue:function(val){return val*(this._slider.maximum-this._slider.minimum)/(this._numStops-1)+this._slider.minimum;},_getSliderValue:function(){return this._slider.get("value");},_setSliderValue:function(val){this._slider._setValueAttr(val,false,false);}});}