/*
 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.Gallery"]){dojo._hasResource["esri.dijit.Gallery"]=true;dojo.provide("esri.dijit.Gallery");dojo.require("esri.dijit._TouchBase");dojo.declare("esri.dijit.Gallery",null,{container:null,_items:[],_currentIndex:0,_left:0,_offset:10,_slideDiv:null,_thumbnailStyle:"default",_focusedIndex:null,_selectedIndex:-1,_slideStep:1,_showTitle:true,constructor:function(_1,_2){this.container=dojo.byId(_2);this._touchBase=esri.dijit._TouchBase(this.container,null);this._slideDiv=dojo.create("div",{"class":"slideContainer"},this.container,"first");this.events=[dojo.connect(this._touchBase,"onTouchStart",this,this._onTouchStartHandler),dojo.connect(this._touchBase,"onTouchMove",this,this._onTouchMoveHandler),dojo.connect(this._touchBase,"onTouchEnd",this,this._onTouchEndHandler),dojo.connect(this._touchBase,"onclick",this,this._onClickHandler)];this._resizeHandle=dojo.connect(window,"onorientationchange",dojo.hitch(this,this._onResizeHandler));this._items=_1.items;if(_1.thumbnailStyle&&_1.thumbnailStyle=="small"){this._thumbnailStyle="small";this._offset=4;this._slideStep=3;this._thumbnailWidth=100;}else{this._thumbnailStyle="default";this._offset=10;this._slideStep=1;this._thumbnailWidth=200;}if(_1.showTitle==false){this._showTitle=false;}this.container.setAttribute((document.all?"className":"class"),"esriMobileGallery");var _3,_4;for(var i=0;i<this._items.length;i++){var _5,_6,_7,_8;_5=dojo.create("div",{"class":"thumbnailcontainer"},this._slideDiv);_8=dojo.create("img",{"class":"thumbnail",src:this._items[i].thumbnailUrl},_5);_8._index=i;_8._item=this._items[i];_6=dojo.create("div",{"class":"title","innerHTML":this._items[i].title},_5);_7=dojo.create("div",{"class":"title"},_5);if(this._thumbnailStyle=="small"){dojo.addClass(_5,"small");dojo.addClass(_8,"small");dojo.addClass(_6,"small");dojo.addClass(_7,"small");dojo.addClass(this._slideDiv,"small");}if(!this._showTitle){_6.style.display="none";}_4=dojo.contentBox(_5).h;if(!_3){_3=_4;}if(_4>_3){_3=_4;}}for(var i=0;i<this._slideDiv.childNodes.length;i++){dojo.contentBox(this._slideDiv.childNodes[i],{h:_3});}this._thumbnailWidth=dojo.contentBox(this._slideDiv.childNodes[0]).w;this._slideDiv.style.width=this._items.length*(this._thumbnailWidth+this._offset)+"px";if(window.orientation){if(window.orientation==90||window.orientation==-90){dojo.addClass(this.container,"galleryLandscape");}}this._calculateSlideStep();},startup:function(){this._focusedIndex=0;this.onFocus(this._items[this._focusedIndex]);},destroy:function(){dojo.forEach(this.events,dojo.disconnect);dojo.disconnect(this._resizeHandle);this._touchBase=null;dojo.query("img",this.container).forEach(function(_9){_9._index=null;_9._item=null;dojo.destroy(_9);_9=null;});this._items=null;dojo.destroy(this._slideDiv);dojo.destroy(this.container);this.container=this._slideDiv=null;},setFocus:function(_a){if(!_a){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_a){this._setFocus(i);this.onFocus(this._items[this._focusedIndex]);}}},select:function(_b){if(!_b){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_b){if(this._selectedIndex!=-1){this.onUnSelect(this._items[this._selectedIndex]);}this._select(i);this.onSelect(this._items[this._selectedIndex]);break;}}},getFocusedItem:function(){return this._items[this._focusedIndex];},getSelectedItem:function(){return this._items[this._selectedIndex];},next:function(){this._next();this._startTransition();this.onFocus(this._items[this._currentIndex]);},previous:function(){this._previous();this._startTransition();this.onFocus(this._items[this._currentIndex]);},getTitleNode:function(_c){if(!_c){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_c){return this._slideDiv.childNodes[i].childNodes[1];break;}}},getInfoNode:function(_d){if(!_d){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_d){return this._slideDiv.childNodes[i].childNodes[2];break;}}},showTitle:function(_e){if(!_e){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_e){this._slideDiv.childNodes[i].childNodes[1].style.display="block";break;}}},hideTitle:function(_f){if(!_f){return;}for(var i=0,il=this._items.length;i<il;i++){if(this._items[i]==_f){this._slideDiv.childNodes[i].childNodes[1].style.display="none";break;}}},onFocus:function(_10){},onSelect:function(_11){},onUnSelect:function(_12){},_onClickHandler:function(e){if(e.target instanceof HTMLImageElement){var _13=e.target._index;if(this._selectedIndex!=-1){this.onUnSelect(this._items[this._selectedIndex]);}this._select(_13);this.onSelect(this._items[this._selectedIndex]);}},_onTouchStartHandler:function(e){this._moveDirection=null;this._left=this._currentIndex*(-(this._thumbnailWidth)-this._offset);this._slideDiv.style.WebkitTransitionDuration="0s";},_onTouchMoveHandler:function(e){if(!this._moveDirection){if(Math.abs(e.curY)>Math.abs(e.curX)){this._moveDirection="vertical";}else{this._moveDirection="horizontal";}}if(this._moveDirection=="vertical"){if(!this._touchBase._preventDefault==false){this._touchBase._preventDefault=false;}return;}else{if(this._moveDirection=="horizontal"){if(!this._touchBase._preventDefault){this._touchBase._preventDefault=true;}var _14=this._left+e.curX;this._slideDiv.style.webkitTransform="translate3d("+_14+"px, 0, 0)";}}},_onTouchEndHandler:function(e){var _15=this._currentIndex;this._slideDiv.style.WebkitTransitionDuration="0.5s";if(e.swipeDirection=="left"){this._next();}else{if(e.swipeDirection=="right"){this._previous();}}this._left=this._currentIndex*(-(this._thumbnailWidth)-this._offset);this._slideDiv.style.webkitTransform="translate3d("+this._left+"px, 0, 0)";if(_15!=this._currentIndex){this.onFocus(this._items[this._currentIndex]);}},_onResizeHandler:function(e){var _16=window.orientation;switch(_16){case 0:dojo.removeClass(this.container,"galleryLandscape");break;case 90:case -90:dojo.addClass(this.container,"galleryLandscape");break;}this._calculateSlideStep();},_startTransition:function(){this._slideDiv.style.WebkitTransitionDuration="0.5s";this._left=this._currentIndex*(-(this._thumbnailWidth)-this._offset);this._slideDiv.style.webkitTransform="translate3d("+this._left+"px, 0, 0)";},_markUnSelected:function(_17){dojo.removeClass(_17,"selected");dojo.removeClass(_17.parentNode,"selected");dojo.removeClass(_17.parentNode.childNodes[1],"selected");dojo.removeClass(_17.parentNode.childNodes[2],"selected");},_markSelected:function(_18){dojo.addClass(_18,"selected");dojo.addClass(_18.parentNode,"selected");dojo.addClass(_18.parentNode.childNodes[1],"selected");dojo.addClass(_18.parentNode.childNodes[2],"selected");},_next:function(){if(this._currentIndex+this._slideStep<this._items.length){this._currentIndex+=this._slideStep;this._focusedIndex=this._currentIndex;}},_previous:function(){if(this._currentIndex-this._slideStep>=0){this._currentIndex-=this._slideStep;}this._focusedIndex=this._currentIndex;},_setFocus:function(_19){this._focusedIndex=_19;this._currentIndex=Math.floor(_19/this._slideStep)*this._slideStep;this._startTransition();},_select:function(_1a){if(this._selectedIndex!=-1){this._markUnSelected(this._slideDiv.childNodes[this._selectedIndex].childNodes[0]);}this._selectedIndex=_1a;this._markSelected(this._slideDiv.childNodes[this._selectedIndex].childNodes[0]);},_calculateSlideStep:function(){var _1b=dojo.contentBox(this.container);var _1c=Math.floor((_1b.w+this._offset)/(this._thumbnailWidth+this._offset));if(_1c>=1){var _1d=_1b.w-(this._thumbnailWidth+this._offset)*_1c;if(_1d>0){this._slideDiv.style.marginLeft=Math.floor(_1d/2)+"px";}else{this._slideDiv.style.marginLeft="0px";}this._slideStep=_1c;}else{this._slideStep=1;}if(this._selectedIndex!=-1){this._setFocus(this._selectedIndex);}else{this._setFocus(this._focusedIndex);}}});}