var AOP=AOP||{};AOP.refMe={scriptSrc:'//widget.refme.com/scripts/refme-cite.js',service:AOP.baseUrl+'/services/aop-cambridge-core/refme',componentRefMeObj:null,componentRefMeWidget:null,productRefMeObj:null,productRefMeWidget:null,productWidgetClassName:'export-citation-widget-product',componentWidgetClassName:'export-citation-widget-component',productButtonClassName:'export-citation-product',componentButtonClassName:'export-citation-component',componentListButtonClassName:'export-citation-list',widgetWidth:330,messages:{error:'Could not export the citation. Please <a href="'+ AOP.baseUrl+'/contact">contact customer services</a>.',noContent:'No content selected. Please select items you would like to export a citation for.',notAvailable:'You cannot export a citation for the selected content type.'},widgetConfig:{branded:false},loadError:function(){var _self=this;createAlertBox(null,$('#ajaxMessages'),'error',_self.messages.error);},warningNoContent:function(){var _self=this;createAlertBox(null,$('#ajaxMessages'),'error',_self.messages.noContent);},isRefMeLoaded:function(){return typeof RefME!=='undefined';},getProductIds:function(){var selectedComponents=[];$('input[name="productParts"]:checked').each(function(){if($(this).is(':visible')){selectedComponents.push($(this).attr('data-prod-id'));}});return selectedComponents;},setWidgetPosition:function(widget,top,left){if(!widget){return;}
widget.css({position:'relative'});if(top||top===0){widget.css({top:Number(top)});}
if(left||left===0){widget.css({left:Number(left)});}},setContentReferences:function(refMeObj,citations){if(!refMeObj){return;}
if(refMeObj.hasOwnProperty('setReferences')){refMeObj.setReferences(citations);}},getData:function(widget,productIds,handler){var _self=this;if(!widget){return;}
productIds=!Array.isArray(productIds)?[productIds]:productIds;if(_self.isRefMeLoaded()&&productIds.length>0){$.post(_self.service,{'productIds[]':productIds}).done(function(resp){widget.find('.loader').hide();if(!resp.success){_self.loadError();widget.hide();}else{handler(resp);}});}},initRefMeObjs:function(){var _self=this;_self.componentRefMeWidget=$('.'+ _self.componentWidgetClassName);_self.productRefMeWidget=$('.'+ _self.productWidgetClassName);var productWidgetElement=_self.productRefMeWidget.length!==0?_self.productWidgetClassName:_self.productButtonClassName;var componentWidgetElement=_self.componentRefMeWidget.length!==0?_self.componentWidgetClassName:_self.componentButtonClassName;if(_self.productRefMeWidget.length>0){_self.productRefMeObj=RefME.createButton({element:'.'+ productWidgetElement,config:_self.widgetConfig});}
if(_self.componentRefMeWidget.length>0){_self.componentRefMeObj=RefME.createButton({element:'.'+ componentWidgetElement,config:_self.widgetConfig});}},getVerticalOffset:function(button){var containerAttr=button.attr('data-container');var offsetAttr=button.attr('data-offset');var offsetTop=0;if(!containerAttr&&!offsetAttr){return 0;}
if(containerAttr){offsetTop=button.closest('.'+ containerAttr).position();offsetTop=offsetTop.top||offsetTop.top===0?Math.round(offsetTop.top,0):0;}
if(offsetAttr){offsetTop+=Number(offsetAttr);}
return offsetTop;},showLoader:function(widget){if(!widget){return;}
var refMeIframe=widget.find('iframe');if(refMeIframe.length>0){refMeIframe.hide();}
widget.find('.loader').show();widget.show();},resetWidget:function(widget,opts){if(!widget){return;}
var _self=this;var top=opts.top?opts.top:null;var left=opts.left?opts.left:null;var disableWidgetReposition=opts.disableWidgetReposition?opts.disableWidgetReposition:false;if(!disableWidgetReposition){_self.setWidgetPosition(widget,top,left);}
_self.showLoader(widget);},preload:function(productId,callback){var _self=this;if(!productId){return callback(new Error('Product id not supplied'));}
_self.getData(_self.productRefMeWidget,productId,function(resp){_self.setContentReferences(_self.productRefMeObj,resp.data);_self.productRefMeWidget.hide().click();return callback();});},revealWidget:function(widget){if(!widget){return;}
widget.click().show();},init:function(){var _self=this;var opts={dataType:'script',cache:true,url:_self.scriptSrc};$.ajax(opts).done(function(){_self.initRefMeObjs();$('.'+ _self.componentListButtonClassName).on('click',function(e){e.preventDefault();var widget=_self.componentRefMeWidget;var button=$(this);if(button.hasClass('disabled')){return;}
if(button.attr('data-refme-disabled')==='true'){createAlertBox(null,$('#ajaxMessages'),'error',_self.messages.notAvailable);return;}
var productIds=_self.getProductIds();if(productIds.length===0){_self.warningNoContent();widget.hide();}else{_self.resetWidget(widget,{top:_self.getVerticalOffset(button)});$('html, body').animate({scrollTop:Math.round(widget.offset().top,0)- 50},'fast');_self.getData(widget,productIds,function(resp){_self.setContentReferences(_self.componentRefMeObj,resp.data);_self.revealWidget(widget);});}});$('body').on('click','.'+ _self.componentButtonClassName,function(e){e.preventDefault();var widget=_self.componentRefMeWidget;var button=$(this);var productId=button.attr('data-prod-id');_self.resetWidget(widget,{top:_self.getVerticalOffset(button)});_self.getData(widget,productId,function(resp){_self.setContentReferences(_self.componentRefMeObj,resp.data);_self.revealWidget(widget);});});$('.'+ _self.productButtonClassName).on('click',function(e){e.preventDefault();var widget=_self.productRefMeWidget;var button=$(this);var productId=button.attr('data-prod-id');var disableWidgetReposition=$(this).attr('data-refme-disable-widget-reposition');disableWidgetReposition=disableWidgetReposition!==undefined;var parent=widget.parent();var top=_self.getVerticalOffset(button);var calculateLeftPosition=function(parent){return parent.width()- _self.widgetWidth;};var showWidget=function(){if(!disableWidgetReposition){_self.setWidgetPosition(widget,top,calculateLeftPosition(parent));$(window).on('resize',function(){_self.setWidgetPosition(widget,top,calculateLeftPosition(parent));});}
_self.revealWidget(widget);};if(typeof _self.productRefMeObj.socket!=='undefined'){showWidget();return;}
_self.resetWidget(widget,{top:top,left:calculateLeftPosition(parent),disableWidgetReposition:disableWidgetReposition});_self.getData(widget,productId,function(resp){_self.setContentReferences(_self.productRefMeObj,resp.data);showWidget();});});$('.bookmarks [data-load]').on('click',function(){_self.componentRefMeWidget.hide();});$('input[name="productParts"]').on('change',function(){if(AOP&&AOP.refMe){_self.componentRefMeWidget.hide();}});$.event.trigger({type:'refmeLoaded',message:'refme loaded',time:new Date()});}).fail(function(){var buttons=[_self.componentListButtonClassName,_self.productButtonClassName,_self.componentButtonClassName];$.each(buttons,function(key,button){$('body').on('click','.'+ button,function(e){e.preventDefault();_self.loadError();});});});}};$(document).ready(function(){AOP.refMe.init();});