ips.templates.set('gallery.notes.wrapper'," <div class='cGalleryNote' data-controller='gallery.front.view.note' data-noteID='{{id}}' data-note=\"{{note}}\" {{#editable}}data-editable{{/editable}} data-posLeft='{{left}}' data-posTop='{{top}}' data-dimWidth='{{width}}' data-dimHeight='{{height}}'> <div class='cGalleryNote_border'></div> <div class='cGalleryNote_note' style='display: none'>  <div>{{note}}</div> </div></div>");ips.templates.set('gallery.notes.delete',"  <a href='#' data-action='delete' class='cGalleryNote_delete' data-ipsTooltip title='{{#lang}}delete_note{{/lang}}'>&times;</a>");ips.templates.set('gallery.notes.edit',"  <textarea>{{note}}</textarea> <ul class='ipsList_inline'>  <li><button data-action='save' class='ipsButton ipsButton_light ipsButton_verySmall'>{{#lang}}save_note{{/lang}}</button></li>  <li><a href='#' data-action='cancel'>{{#lang}}cancel_note{{/lang}}</a></li> </ul>");;
;(function($,_,undefined){"use strict";ips.controller.register('gallery.front.view.image',{_sizeBuffer:20,_ajaxObj:null,_scrolling:false,_rtl:false,_curWidth:0,_curHeight:0,_windowDims:null,_inLightbox:false,_preloadAjax:{next:null,prev:null},initialize:function(){this.on('click','[data-action="nextImage"]',this.nextImage);this.on('click','[data-action="prevImage"]',this.prevImage);this.on('menuOpened',this.menuOpened);this.on('menuClosed',this.menuClosed);this.on(document,'keydown',this.keyDown);this.on(window,'resize',_.debounce(_.bind(this.windowResize,this),250));this.on('click','[data-role="toggleFullscreen"]',this.toggleFullscreen);this.on('click','[data-action="setAsCover"]',this.setAsCover);this.on('click','[data-action="setAsProfile"]',this.setAsProfile);this.on('click','[data-action="rotateImage"]',this.rotateImage);History.Adapter.bind(window,'statechange',_.bind(this.stateChange,this));this.setup();},setup:function(){if($('html').attr('dir')=='rtl'){this._rtl=true;}
this._windowDims={width:$(window).width(),height:$(window).height()};this._setUpSizing(false);this._setUpLightboxEvents();if(this.scope.closest('#cLightbox').length){this._inLightbox=true;}
this._checkForPreload();delete Hammer.defaults.cssProps.userSelect;var mc=new Hammer(this.scope.find('.elGalleryHeader').get(0));var self=this;mc.on('swipeleft',function(e){if(!self.scope.find('.cGalleryNote_editing').length){$(document).trigger('lightboxNextImage');}});mc.on('swiperight',function(e){if(!self.scope.find('.cGalleryNote_editing').length){$(document).trigger('lightboxPrevImage');}});var self=this;$(document).on('hideDialog',function(){if(self.scope.closest('#cLightbox').length&&!ips.ui.dialog.hasOpenDialogs()){$('body').addClass('ipsNoScroll');}});$(document).on('openDialog',function(){if(self.scope.closest('#cLightbox').length){$('body').removeClass('ipsNoScroll');}});},menuOpened:function(){this.scope.find('.elGalleryImage').addClass('cGalleryImageHover');},menuClosed:function(e){this.scope.find('.elGalleryImage').removeClass('cGalleryImageHover');},_setUpLightboxEvents:function(){var self=this;$(document).on('lightboxImageShown',function(){if(self.scope.find('[data-action="prevImage"]').length){$(document).trigger('lightboxEnable_prev');}else{$(document).trigger('lightboxDisable_prev');}
if(self.scope.find('[data-action="nextImage"]').length){$(document).trigger('lightboxEnable_next');}else{$(document).trigger('lightboxDisable_next');}});$(document).on('lightboxNextImage',function(e,data){self.scope.find('[data-action="nextImage"]').click();});$(document).on('lightboxPrevImage',function(e,data){self.scope.find('[data-action="prevImage"]').click();});},stateChange:function(){var state=History.getState();if(state.data.controller!='gallery.front.view.image'){if(state.data.controller!='gallery.front.browse.imageLightbox'&&(_.isUndefined(state.data.initialLaunch)||!state.data.initialLaunch)){return;}}
if(_.isUndefined(state.data.lightbox)||state.data.lightbox!==this._inLightbox){return;}
ips.utils.analytics.trackPageView(state.data.realUrl);if($('#cLightbox').length&&!$('#cLightbox').is(':visible')){$('#cLightbox').show();}
if(!$('#cLightbox').length||!$('#cLightbox').is(':visible')){this._scrollPage();}
this._loadURL(state.data.realUrl,false);},_scrollPage:function(){if(this._scrolling){return;}
var self=this;var elemPosition=ips.utils.position.getElemPosition(this.scope);var viewportHeight=$(window).height();var docScrollTop=$(document).scrollTop();if((elemPosition.absPos.top-docScrollTop<0)||elemPosition.absPos.top>viewportHeight+docScrollTop){this._scrolling=true;$('html, body').animate({scrollTop:elemPosition.absPos.top+'px'},function(){self._scrolling=false;});}},keyDown:function(e){if($(e.target).closest('input, textarea, .ipsComposeArea, .ipsComposeArea_editor').length){return;}
switch(e.keyCode){case ips.ui.key.LEFT:this.scope.find('[data-action="prevImage"]').click();break;case ips.ui.key.RIGHT:this.scope.find('[data-action="nextImage"]').click();break;}},nextImage:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');var id=$(e.currentTarget).attr('data-imageID');var title=$(e.currentTarget).attr('title');History.pushState({controller:'gallery.front.view.image',imageID:id,realUrl:url,direction:'next',lightbox:this._inLightbox},title,ips.utils.url.removeParams(['lightbox','browse'],url));},prevImage:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');var id=$(e.currentTarget).attr('data-imageID');var title=$(e.currentTarget).attr('title');History.pushState({controller:'gallery.front.view.image',imageID:id,realUrl:url,direction:'prev',lightbox:this._inLightbox},title,ips.utils.url.removeParams(['lightbox','browse'],url));},setAsProfile:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');ips.ui.alert.show({type:'confirm',icon:'question',message:ips.getString('set_as_photo_confirm'),callbacks:{ok:function(){ips.getAjax()(url,{showLoading:true}).done(function(response){ips.ui.flashMsg.show(response.message);}).fail(function(){window.location=url;});}}});},setAsCover:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');ips.getAjax()(url,{showLoading:true}).done(function(response){ips.ui.flashMsg.show(response.message);}).fail(function(){window.location=url;});},rotateImage:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');var self=this;ips.getAjax()(url,{showLoading:true}).done(function(response){self.scope.find('[data-role="theImage"]')[0].src=response.src;self.scope.find('[data-role="theImage"]').css({'width':response.width+'px','height':response.height+'px'});self.scope.find('[data-role="theImage"]').closest('.cGalleryViewImage').css({'width':response.width+'px','height':response.height+'px'});ips.ui.flashMsg.show(response.message);}).fail(function(){window.location=url;});},windowResize:function(e){if($(window).width()!==this._windowDims.width||$(window).height()!==this._windowDims.height){this._setUpSizing(true);this._windowDims={width:$(window).width(),height:$(window).height()};}},_cachedUrls:{},_loadURL:function(url,cacheOnly,direction){var self=this;if(cacheOnly&&!_.isUndefined(this._cachedUrls[url])){return;}
if(!cacheOnly){this.cleanContents();this._setImageLoading();var urlToRequest=url;}
else{if(url.match(/\?/)){if(url.slice(-1)!='?'){var urlToRequest=url+'&preload=1';}}else{var urlToRequest=url+'?preload=1';}}
if(_.isUndefined(cacheOnly)||!cacheOnly){if(this._ajaxObj&&_.isFunction(this._ajaxObj.abort)){this._ajaxObj.abort();}}else{if(this._preloadAjax[direction]&&_.isFunction(this._preloadAjax[direction].abort)){this._preloadAjax[direction].abort();}}
if(self._cachedUrls[url]){self._updateImage(self._cachedUrls[url]);self._markImageRead(url);return;}
var ajax=ips.getAjax();if(cacheOnly){this._preloadAjax[direction]=ajax;}else{this._ajaxObj=ajax;}
ajax(urlToRequest,{dataType:'json'}).done(function(response){self._cachedUrls[url]=response;if(!cacheOnly){self._updateImage(response);}}).fail(function(jqXHR,textStatus,errorThrown){if(Debug.isEnabled()){Debug.error(errorThrown);}else{if(!cacheOnly){window.location=url;}}});},_markImageRead:function(url){var self=this;if(url.match(/\?/)){if(url.slice(-1)!='?'){url=url+'&do=markread';}}else{url=url+'?do=markread';}
ips.getAjax()(url,{dataType:'json'}).done(function(response){Debug.log("Marked read");});},_updateImage:function(response){this.scope.find('[data-role="imageInfo"]').closest('.cGalleryLightbox_info').show().end().html(response.info);this.scope.find('[data-role="imageFrame"]').replaceWith(response.image);if(response.comments){this.scope.find('[data-role="imageComments"]').html(response.comments);}else{this.scope.find('[data-role="imageComments"]').html('');}
$('nav.ipsBreadcrumb [data-role="breadcrumbList"] > li:last-child').html(response.title);$(document).trigger('contentChange',[this.scope]);$(document).trigger('imageUpdated',[{closeLightbox:(response.image.match(/<video /ig)||response.image.match(/<embed /ig))?true:false,updateImage:{imageElem:null,largeImage:(response.image.match(/<video /ig)||response.image.match(/<embed /ig))?null:this.scope.find('[data-role="theImage"]')[0].src,commentsURL:null,meta:null}}]);if(this.scope.find('[data-action="prevImage"]').length){$(document).trigger('lightboxEnable_prev');}else{$(document).trigger('lightboxDisable_prev');}
if(this.scope.find('[data-action="nextImage"]').length){$(document).trigger('lightboxEnable_next');}else{$(document).trigger('lightboxDisable_next');}
this._checkForPreload()
this._setUpSizing(false);},_checkForPreload:function(){if(this.scope.find('[data-action="nextImage"]').length){Debug.log("Caching next image");this._loadURL(this.scope.find('[data-action="nextImage"]').attr('href'),true,'next');}
if(this.scope.find('[data-action="prevImage"]').length){Debug.log("Caching prev image");this._loadURL(this.scope.find('[data-action="prevImage"]').attr('href'),true,'prev');}},_setImageLoading:function(loading){var description=this.scope.find('[data-role="imageDescription"]');var stats=this.scope.find('[data-role="imageStats"]');var image=this.scope.find('[data-role="imageFrame"]');description.css({height:description.outerHeight()+'px'}).html('').addClass('ipsLoading');stats.css({height:stats.outerHeight()+'px'}).html('').addClass('ipsLoading');image.html('').addClass('ipsLoading');this.scope.find('[data-role="imageInfo"]').closest('.cGalleryLightbox_info').hide();$(document).trigger('imageLoading',[]);},_setUpSizing:function(forceResize){if(ips.utils.responsive.currentIs('phone')){this._setUpSizingMobile();}else{this._setUpSizingDesktop(forceResize);}},_setUpSizingMobile:function(forceResize){var isLightbox=this.scope.is('[data-role="lightbox"]');var frame=this.scope.find('[data-role="imageFrame"]');var frameHeight=$(window).height()-80;var imageData=frame.attr('data-imageSizes');if(isLightbox){$(window).scrollTop(0);frame.css({height:frameHeight+'px'});var maxHeight=frameHeight;var maxWidth=$(window).width();}else{var maxHeight=frameHeight;var maxWidth=frame.width();}
var ratio=1;if(imageData){imageData=$.parseJSON(imageData);ratio=imageData['large'][0]/ imageData['large'][1];var marginTop=0;var imageSize={width:imageData['large'][0],height:imageData['large'][1]};if(imageSize['width']>maxWidth){imageSize['width']=maxWidth;imageSize['height']=Math.round(imageSize['width']/ ratio);}
if(imageSize['height']>maxHeight){imageSize['height']=maxHeight;imageSize['width']=Math.round(imageSize['height']*ratio);}
this.scope.find('[data-role="notesWrapper"], [data-role="theImage"]').css({width:imageSize['width']+'px',height:imageSize['height']+'px',}).show();}},_setUpSizingDesktop:function(forceResize){var isLightbox=this.scope.is('[data-role="lightbox"]');var frame=this.scope.find('[data-role="imageFrame"]');var imageSizer=this.scope.find('[data-role="imageSizer"]');var infoPanel=this.scope.find('[data-role="imageInfo"]');var infoPanelWidth=infoPanel.width();var imageData=frame.attr('data-imageSizes');if(isLightbox){var maxHeight=$(window).height()-(this._sizeBuffer*2);var maxWidth=$(window).width()-(this._sizeBuffer*2)-infoPanelWidth;frame.css({height:'auto'});}else{var maxHeight=frame.height();var maxWidth=frame.width();}
var ratio=1;if(maxHeight<400){maxHeight=400;}
if(imageData){imageData=$.parseJSON(imageData);ratio=imageData['large'][0]/ imageData['large'][1];var marginTop=0;var imageSize={width:imageData['large'][0],height:imageData['large'][1]};if(imageSize['width']>maxWidth){imageSize['width']=maxWidth;imageSize['height']=Math.round(imageSize['width']/ ratio);}
if(imageSize['height']>maxHeight){imageSize['height']=maxHeight;imageSize['width']=Math.round(imageSize['height']*ratio);}
this.scope.find('[data-role="notesWrapper"], [data-role="theImage"]').css({width:imageSize['width']+'px',height:imageSize['height']+'px',}).show();}},toggleFullscreen:function(e){e.preventDefault();if($('#cLightbox').is('[data-fullScreen]')){$('#cLightbox').removeAttr('data-fullScreen');}
else{$('#cLightbox').attr('data-fullScreen',"true");}}});}(jQuery,_));;
;(function($,_,undefined){"use strict";ips.controller.register('gallery.front.view.note',{_editing:false,_editable:false,_draggingNotEditing:false,_hoverTimerOn:null,_hoverTimerOff:null,_note:'',initialize:function(){this.on('click','.cGalleryNote_border',this.startEditing);this.on('click','[data-action="save"]',this.saveNote);this.on('click','[data-action="cancel"]',this.cancelNote);this.on('click','[data-action="delete"]',this.deleteNote);this.on('mousedown','.cGalleryNote_note',this.mouseDown);this.on('mouseenter',this.mouseEnter);this.on('mouseleave',this.mouseLeave);this.setup();},setup:function(){var self=this;if(!_.isUndefined(this.scope.attr('data-editable'))&&!ips.utils.responsive.currentIs('phone')){this._editable=true;}
this._note=this.scope.attr('data-note');this._baseURL=ips.getSetting('baseURL')+'index.php?app=gallery&module=gallery&controller=notes&imageId='+this.scope.closest('.cGalleryViewImage').attr('data-imageID');this._buildNote();this._setUpEditable();this._initialPosition();if(this.scope.attr('data-noteID')=='new'){this.scope.find('.cGalleryNote_border').click();}},saveNote:function(e){e.preventDefault();var self=this;var note=this.scope.find('.cGalleryNote_note textarea').val();var savePosition=false;this.scope.draggable('enable');if(!$.trim(note)){return;}
if(this.scope.attr('data-noteID')=='new'){savePosition=true;}
this._saveNote(note,savePosition).done(function(){self._note=note;self._stopEditing();});},cancelNote:function(e){if(this.scope.attr('data-noteID')=='new'){this.deleteNote(e);return;}
e.preventDefault();this.scope.draggable('enable');this._stopEditing();},deleteNote:function(e){e.preventDefault();var self=this;ips.ui.alert.show({type:'confirm',icon:'question',message:ips.getString('delete_note_confirm'),callbacks:{ok:function(){self._doDeleteNote();}}});},mouseEnter:function(){var self=this;if(this._hoverTimerOn){clearTimeout(this._hoverTimerOn);}
if(!this._editing){this._hoverTimerOn=setTimeout(function(){if(!self.scope.find('.cGalleryNote_note').is(':visible')){ips.utils.anim.go('fadeIn fast',self.scope.find('.cGalleryNote_note'));}});}},mouseLeave:function(){var self=this;if(this._hoverTimerOff){clearTimeout(this._hoverTimerOff);}
if(!this._editing){this._hoverTimerOff=setTimeout(function(){if(self.scope.find('.cGalleryNote_note').is(':visible')){ips.utils.anim.go('fadeOut fast',self.scope.find('.cGalleryNote_note'));}});}},mouseDown:function(){},startEditing:function(){if(!this._editable||this._draggingNotEditing){return;}
if(this._editing===true){this.scope.find('.cGalleryNote_note > div textarea').focus();return;}
this._editing=true;this.scope.addClass('cGalleryNote_editing').append(ips.templates.render('gallery.notes.delete')).find('.cGalleryNote_note > div').html(ips.templates.render('gallery.notes.edit',{note:this._note})).find('textarea').focus();},_doDeleteNote:function(){var url=this._baseURL;var self=this;if(this.scope.attr('data-noteID')=='new'){ips.utils.anim.go('fadeOutDown',self.scope).done(function(){self.scope.remove();});return;}
ips.getAjax()(url+'&delete=1&id='+this.scope.attr('data-noteID')).done(function(){ips.utils.anim.go('fadeOutDown',self.scope).done(function(){self.scope.remove();});})},_saveNote:function(noteContent,savePosition){var deferred=$.Deferred();var self=this;var url=this._baseURL;var position='';var note='';if(this.scope.attr('data-noteID')=='new'){url+='&add=1';}else{url+='&edit=1&id='+this.scope.attr('data-noteID');}
if(savePosition){position=this._getPosition();}
if(noteContent){note=noteContent;}
if(this.scope.find('[data-action="save"]').length&&note){this.scope.find('[data-action="save"]').prop('disabled',true).text(ips.getString('saving_note'));}
ips.getAjax()(url,{data:{note:note,position:position}}).done(function(response){if(self.scope.find('[data-action="save"]').length&&note){self.scope.find('[data-action="save"]').prop('disabled',false).text('Save');}
if(_.isObject(response)&&response.id){self.scope.attr('data-noteID',response.id);}
deferred.resolve();}).fail(function(){deferred.reject();});return deferred.promise();},_getPosition:function(){var position=[];var parent=this.scope.closest('.cGalleryViewImage');var notePos=this.scope.position();position[0]=(notePos['left']/ parent.width())*100;position[1]=(notePos['top']/ parent.height())*100;position[2]=(this.scope.width()/ parent.width())*100;position[3]=(this.scope.height()/ parent.height())*100;return position.join(',');},_stopEditing:function(){this._editing=false;this._draggingNotEditing=false;this.scope.removeClass('cGalleryNote_editing').find('.cGalleryNote_note > div').text(this._note).end().find('.cGalleryNote_delete').remove();},_buildNote:function(){this.scope.find('.cGalleryNote_note > div').text(this._note);},_setUpEditable:function(){if(!this._editable){return;}
var self=this;ips.loader.get(['core/interface/jquery/jquery-ui.js']).then(function(){self.scope.resizable({containment:self.scope.closest('.cGalleryViewImage'),handles:'se',stop:self._updatePosition.bind(self)});self.scope.draggable({containment:self.scope.closest('.cGalleryViewImage'),start:self._startDragging.bind(self),stop:self._updatePosition.bind(self)});self.scope.find('.ui-resizable-handle').on('mouseover',function(){self.scope.closest('.cGalleryViewImage').css({height:self.scope.closest('.cGalleryViewImage').height()+'px'});});});},_startDragging:function(){if(!this._editing){this._draggingNotEditing=true;}},_updatePosition:function(){var self=this;var parent=this.scope.closest('.cGalleryViewImage');var notePos=this.scope.position();var posLeft=this.scope.find('.cGalleryNote_note').css('left');var posRight=this.scope.find('.cGalleryNote_note').css('right');if(parseInt(posLeft)>0){if(notePos['left']+parseInt(this.scope.width())+parseInt(this.scope.find('.cGalleryNote_note').width())>parent.width()){if(notePos['left']-parseInt(this.scope.find('.cGalleryNote_note').width())>0){this.scope.find('.cGalleryNote_note').css('left',posRight);this.scope.find('.cGalleryNote_note').css('right',posLeft);}}}
else{if(notePos['left']-parseInt(this.scope.find('.cGalleryNote_note').width())<0){if(notePos['left']+parseInt(this.scope.width())+parseInt(this.scope.find('.cGalleryNote_note').width())<parent.width()){this.scope.find('.cGalleryNote_note').css('left',posRight);this.scope.find('.cGalleryNote_note').css('right',posLeft);}}}
if(this.scope.attr('data-noteID')=='new'){return;}
this._saveNote(false,true).done(function(){if(!self._editing){self._stopEditing();}});},_initialPosition:function(){var left=this.scope.attr('data-posLeft');var top=this.scope.attr('data-posTop');var width=this.scope.attr('data-dimWidth');var height=this.scope.attr('data-dimHeight');this.scope.css({left:left+'%',top:top+'%',width:width+'%',height:height+'%'});}});}(jQuery,_));;
;(function($,_,undefined){"use strict";ips.controller.register('gallery.front.view.notes',{_inAddingState:false,initialize:function(){this.on(document,'click','[data-action="addNote"]',this.startAddNote);this.setup();},setup:function(){var notes;try{notes=$.parseJSON(this.scope.attr('data-notesData'));}catch(err){}
if(notes&&notes.length){this._buildNotes(notes);}},startAddNote:function(e){e.preventDefault();this.scope.append(ips.templates.render('gallery.notes.wrapper',{id:'new',left:50,top:50,width:(100 / this.scope.width())*100,height:(100 / this.scope.height())*100,editable:true}));$(document).trigger('contentChange',[this.scope]);},_buildNotes:function(notes){if(notes.length){for(var i=0;i<notes.length;i++){this.scope.append(ips.templates.render('gallery.notes.wrapper',{id:notes[i].ID,left:notes[i].LEFT,top:notes[i].TOP,width:notes[i].WIDTH,height:notes[i].HEIGHT,note:notes[i].NOTE,editable:!_.isUndefined(this.scope.attr('data-editable'))?true:false}));}
$(document).trigger('contentChange',[this.scope]);}}});}(jQuery,_));;