
var ed={};ed.utils={};ed.utils.definiton=function(){this.namespace=function(namespace){if(typeof namespace!='string'||namespace.length==0){throw'Namespace has to be a non empty string value or dot spearated multiple values e.g. \'myapp.section.subsection\'';}
var chunks=namespace.split('.');var parent=window;for(var i=0;i<chunks.length;i++){parent[chunks[i]]=parent[chunks[i]]||{};parent=parent[chunks[i]];}}
this.noFn=function(){}
this.getBaseUrl=function(){if(!ed.baseUrl)throw'Base url could not be loaded. Add include_partial(\'edUI/baseUrl\'); to main layout';return ed.baseUrl;}
this.domElementExists=function(elementId){if($('#'+elementId).length){return true;}
return false;}
this.domElementAdd=function(elementId,elementTag){elementTag=typeof(elementTag)!='undefined'?options:'div';var element=document.createElement(elementTag);element.id=elementId;$('body').append(element);}
this.domElementRemove=function(elementId){$('#'+elementId).remove();}
this.generateId=function(){var baseId='ed';var counter=1;return this.generateUnique(baseId,counter);}
this.generateUnique=function(baseId,counter){if(this.domElementExists(baseId+'-'+counter)){counter++;return this.generateUnique(baseId,counter);}else{return baseId+'-'+counter;}}
this.safeHtmlId=function(string){string=string.replace(/[^a-z0-9]/gi,'');string=string.toLowerCase();return string;}
this.generateRandomHash=function(){var hash='';var newDate=new Date;var randomNo=Math.random();randomNo=randomNo*1000000;randomNo=randomNo.toFixed(0);hash=String(randomNo)+String(newDate.getTime());return hash;}}
ed.utils=new ed.utils.definiton();ed.utils.namespace('ed.dialog');ed.dialog.basic=function(options){var self=this;this.init=function(options){options=typeof(options)!='undefined'?options:new Array();self.id=options.id;if(!self.id)self.id=ed.utils.generateId();if(!ed.utils.domElementExists(this.id))ed.utils.domElementAdd(self.id);self.dialog=$('#'+this.id).dialog(self.getDefaultOptions());self.dialog.dialog('option',options);}
this.getDefaultOptions=function(){var defaultOptions={close:self.onCloseTrigger,open:self.onOpenTrigger,title:'Alert',modal:true,resizable:false,show:false,hide:false,autoOpen:false,width:'auto',height:'auto'};return defaultOptions;}
this.onOpenTrigger=function(){self.onOpen(self);}
this.onOpen=function(self){}
this.onCloseTrigger=function(){self.onClose(self);}
this.onClose=function(self){}
this.open=function(){self.dialog.dialog('open');}
this.close=function(){self.dialog.dialog('close');}
this.setTitle=function(title){this.dialog.dialog({title:title});}
this.setBody=function(content){$('#'+this.id).html(content);}
this.clearButtons=function(){this.dialog.dialog('option','buttons',{});}
this.addButton=function(buttonLabel,buttonFunction){var buttons=self.dialog.dialog('option','buttons');buttons[buttonLabel]={text:buttonLabel,class:'ed-dialog-button-'+ed.utils.safeHtmlId(buttonLabel),click:buttonFunction}
self.dialog.dialog('option','buttons',buttons);}
this.removeButton=function(buttonLabel){var buttons=self.dialog.dialog('option','buttons');delete buttons[buttonLabel];self.dialog.dialog('option','buttons',buttons);}
this.disableButton=function(buttonLabel){$('#'+self.id).next().children().children('button:contains("'+buttonLabel+'")').attr('disabled','disabled').addClass('ui-state-disabled');}
this.enableButton=function(buttonLabel){$('#'+self.id).next().children().children('button:contains("'+buttonLabel+'")').attr('disabled',false).removeClass('ui-state-disabled');}
this.buttonsClearFocus=function(){$('#'+self.id).next().children().children('button').removeClass('ui-state-focus');}
this.getOption=function(option){return self.dialog.dialog('option',option);}
this.setOption=function(option,value){self.dialog.dialog('option',option,value);}
this.destroy=function(){self.dialog.dialog('destroy');$('#'+self.id).remove();}
this.init(options);}
ed.utils.namespace('ed.dialog');ed.dialog.loading=function(){this.init({id:'ed-loading',width:200,height:80,show:'',hide:'',closeOnEscape:false});$('#'+this.id).prev().remove();this.setBody('<img src="/edUIPlugin/images/spinner-32x32.gif" alt="loading" />');}
ed.dialog.loading.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.alert=function(){var self=this;this.init({id:'ed-alert'});this.addButton('OK',this.close);this.showAlert=function(content,title){content=typeof(content)!='undefined'?content:'Alert';title=typeof(title)!='undefined'?title:'Alert';self.setBody(content);self.setTitle(title);self.open();}}
ed.dialog.alert.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.error=function(options){var self=this;options=typeof(options)!='undefined'?options:{};options.title=typeof(options.title)!='undefined'?options.title:'Error';options.content=typeof(options.content)!='undefined'?options.content:'Error';this.init(options);this.addButton('OK',this.close);var content='';content+='<div class="ed-ui-dialog-error">';if(!options.errorCode){self.setBody('Error');}else if(options.errorCode=='404'){content+='<div class="ed-ui-dialog-error-404">';content+='<div class="ed-ui-error-title">Page not found</div>';content+='<br /><br />';content+='<div class="ed-ui-error-subtitle">Error 404</div>';content+='</div>';self.setBody(content);self.setTitle('Error 404');}else if(options.errorCode=='500'){content+='<div class="ed-ui-dialog-error-500">';content+='<div class="ed-ui-error-title">Application error</div>';content+='<br /><br />';content+='<div class="ed-ui-error-subtitle">Error 500</div>';content+='/<div>';self.setBody(content);self.setTitle('Error 500');}
content+='</div>';self.setOption('close',function(){self.destroy();});}
ed.dialog.error.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.confirm=function(){var self=this;this.response=false;this.cancelCallback=function(){};this.confirmCallback=function(){};this.init({id:'ed-confirm'});self.addButton('Cancel',function(){self.close();});self.addButton('OK',function(){self.response=true;self.close();});self.setOption('close',function(){if(self.response){self.response=false;self.confirmCallback();}else{self.response=false;self.cancelCallback();}});this.showConfirm=function(options){var title=typeof(options.title)!='undefined'?options.title:'Question';var content=typeof(options.content)!='undefined'?options.content:'Question';this.event=typeof(options.event)!='undefined'?options.event:null;self.confirmCallback=typeof(options.confirmCallback)!='undefined'?options.confirmCallback:ed.utils.noFn;self.cancelCallback=typeof(options.cancelCallback)!='undefined'?options.cancelCallback:ed.utils.noFn;try{self.element=$(self.event.currentTarget);self.event.preventDefault();}catch(e){}
self.setTitle(title);self.setBody(content);self.open();}}
ed.dialog.confirm.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.loading=function(){this.init({id:'ed-loading',width:200,height:80,show:'',hide:'',closeOnEscape:false});$('#'+this.id).prev().remove();this.setBody('<img src="/edUIPlugin/images/spinner-32x32.gif" alt="loading" />');}
ed.dialog.loading.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.ajax=function(options){var self=this;this.method='GET';this.data={};this.url=typeof(options.url)!='undefined'?options.url:'';this.baseUrl=typeof(options.baseUrl)!='undefined'?options.baseUrl:'';this.relativeUrl=typeof(options.relativeUrl)!='undefined'?options.relativeUrl:'';options.title=typeof(options.title)!='undefined'?options.title:'Edit';options.width=typeof(options.width)!='undefined'?options.width:'auto';options.height=typeof(options.height)!='undefined'?options.height:'auto';this.init(options);this.dialogCallback=function(){var form=self.getForm();if(form.length){self.clearButtons();self.addButton('Cancel',self.close);self.addButton('OK',self.submit);this.method='POST';}else{self.clearButtons();self.addButton('Close',self.close);}
this.dialogCallbackPostExecute();}
this.dialogCallbackPostExecute=function(){}
this.submit=function(){var form=self.getForm();self.data={};$.each(form.serializeArray(),function(i,field){self.data[field.name]=field.value;});var action=form.attr('action');if(action)self.url=action;self.loadRemoteContent();}
this.loadRemoteContent=function(){self.showLoading();$.ajax({context:self,url:self.url,type:self.method,data:self.data,cache:false,success:function(data){self.setBody(data);self.dialogCallback();}});}
this.onError=function(event,xhr,settings){if(xhr.status==401){}}
this.getForm=function(){return $('#'+this.id+' form:first');}
this.showLoading=function(){this.setBody('<img src="/edUIPlugin/images/spinner-32x32.gif" alt="loading" />');}
self.setOption('open',function(){self.setMethod('GET');self.data={};self.loadRemoteContent();});this.getRelativeUrl=function(){return self.relativeUrl;}
this.setRelativeUrl=function(relativeUrl){self.relativeUrl=relativeUrl;self.url=self.baseUrl+self.relativeUrl;}
this.getBaseUrl=function(){return self.baseUrl;}
this.setBaseUrl=function(baseUrl){self.baseUrl=baseUrl;}
this.loadBaseUrl=function(){return ed.utils.getBaseUrl();}
this.setMethod=function(method){self.method=method;}
if(!this.baseUrl){this.baseUrl=this.loadBaseUrl();}
if(!this.url){this.url=this.baseUrl+''+this.relativeUrl;}}
ed.dialog.ajax.prototype=new ed.dialog.basic();ed.utils.namespace('ed.dialog');ed.dialog.login=function(options){var self=this;this.context=null;options=typeof(options)!='undefined'?options:{};options.width=typeof(options.width)!='undefined'?options.width:350;options.title=typeof(options.title)!='undefined'?options.title:'Login';options.content=typeof(options.content)!='undefined'?options.content:'';this.init(options);this.getFormValues=function(){var values={};values.username=$('#ed-login-dialog-username').val();values.password=$('#ed-login-dialog-password').val();return values;}
this.login=function(){$.ajax({context:self,url:ed.ui.asyncLoginUrl,type:'POST',data:{signin:self.getFormValues()},cache:false,global:false,success:function(data){var callResponse=jQuery.parseJSON(data);if(callResponse.status=='success'){self.close();}},error:function(xhr,textStatus,errorThrown){alert('error');}});}
this.addButton('Cancel',this.close);this.addButton('Login',this.login);self.setOption('close',function(){self.destroy();});var content='';content+='<div class="ed-login-dialog-form-holder">';content+='<label>Login:</label>';content+='<input id="ed-login-dialog-username" type="text" name="signin[username]" />';content+='<br />';content+='<label>Password:</label>';content+='<input id="ed-login-dialog-password" type="password" name="signin[password]" />';content+='</div>';self.setBody(content);}
ed.dialog.login.prototype=new ed.dialog.basic();ed.utils.namespace('ed');ed.autocomplete=function(options){var self=this;self.cache={};self.lastXhr={};self.loadingIco={};self.loadingIco.id='ed-autocomplete-loading';self.loadingIco.path='/edUIPlugin/images/ajax-loader.gif';self.loadingIco.topOffset=0;self.loadingIco.leftOffset=0;this.init=function(options){options=typeof(options)!='undefined'?options:new Array();self.id=options.id;self.url=options.url;self.jqUIautocomplete=$('#'+self.id).autocomplete(self.getDefaultOptions());self.jqUIautocomplete.autocomplete('option',options);}
this.getDefaultOptions=function(){var defaultOptions={minLength:2,change:self.change,search:self.search,open:self.open,select:self.select,source:self.source}
return defaultOptions;}
this.source=function(request,response){self.showLoading();var term=request.term;if(term in self.cache){self.hideLoading();response(self.cache[term]);return;}
self.lastXhr=$.getJSON(ed.utils.getBaseUrl()+self.url,request,function(data,status,xhr){self.cache[term]=data;if(xhr===self.lastXhr){self.hideLoading();response(data);}});}
this.select=function(event,ui){return self.onSelect(event,ui);}
this.onSelect=function(event,ui){}
this.change=function(event,ui){self.onChange(event,ui);}
this.onChange=function(event,ui){}
this.search=function(){if(self.renderItem){$('#'+self.id).data("autocomplete")._renderItem=function(ul,item){self.renderItem(ul,item);}}}
this.open=function(){}
this.showLoading=function(){var offset=$('#'+self.id).offset();var width=$('#'+self.id).width();$('body').append('<img id="'+self.loadingIco.id+'" src="'+self.loadingIco.path+'" alt="loading" />');$('#'+self.loadingIco.id).css('position','absolute');$('#'+self.loadingIco.id).css('left',(offset.left+width-20+self.loadingIco.leftOffset));$('#'+self.loadingIco.id).css('top',(offset.top+4+self.loadingIco.topOffset));$('#'+self.loadingIco.id).css('z-index',10000);}
this.hideLoading=function(){$('#'+self.loadingIco.id).remove();}
this.setLoadingIcoLeftOffset=function(offset){self.loadingIco.leftOffset=offset;}
this.setLoadingIcoTopOffset=function(offset){self.loadingIco.TopOffset=offset;}
this.setLoadingIcoPath=function(imagePath){self.loadingIco.path=imagePath;}
this.setLoadingIcoId=function(elementId){self.loadingIco.id=elementId;}
this.getOption=function(option){return self.jqUIautocomplete.autocomplete('option',option);}
this.setOption=function(option,value){self.jqUIautocomplete.autocomplete('option',option,value);}
this.init(options);}
ed.utils.namespace('ed');ed.datepicker=function(options){var self=this;self.id=null;self.jqUIdatepicker=null;this.init=function(userOptions){userOptions=typeof(userOptions)!='undefined'?userOptions:new Array();self.id=userOptions.id;self.jqUIdatepicker=$('#'+self.id).datepicker(self.getDefaultOptions());self.jqUIdatepicker.datepicker('option',userOptions);}
this.getDefaultOptions=function(){var defaultOptions={dateFormat:'dd/mm/yy',showAnim:'slideDown'}
return defaultOptions;}
this.getOption=function(option){return self.jqUIdatepicker.datepicker('option',option);}
this.setOption=function(option,value){self.jqUIdatepicker.datepicker('option',option,value);}
this.init(options);}
ed.utils.namespace('ed.ui');$(document).ready(function(){ed.ui.asyncLoginUrl=ed.utils.getBaseUrl()+'edSfGuardExtensionAuth/ajaxSignin';$.ajaxSetup({cache:false});$('body').ajaxStart(function(){$('body, a').css('cursor','wait');});$('body').ajaxStop(function(){$('body').css('cursor','auto');$('a').css('cursor','pointer');});$('body').ajaxError(function(event,xhr,settings){if(settings.context instanceof ed.dialog.login){if(xhr.status==401){return;}}
if(settings.context instanceof ed.dialog.ajax){settings.context.onError(event,xhr,settings);}
if(xhr.status==401){var loginDialog=new ed.dialog.login();loginDialog.open();}else{var errorDialog=new ed.dialog.error({errorCode:xhr.status});errorDialog.open();}});ed.ui.loading=new ed.dialog.loading();ed.ui.alert=new ed.dialog.alert();ed.ui.confirm=new ed.dialog.confirm();$('a.ed-confirm').live('click',function(e){var el=this;var rel=$(el).attr('rel');var title='Confirm',content='Are you sure?';if(rel){if(rel.indexOf('|')!=-1){string=rel.split('|')
title=string[0];content=string[1];}else{content=rel;}}
var confirmOptions={event:e,title:title,content:content,confirmCallback:function(){window.location=$(el).attr('href');}};ed.confirm(confirmOptions);});$('input.autofocus').first().focus();$('.ed-datepicker').each(function(){$(this).attr('autocomplete','off').attr('readonly',true);new ed.datepicker({id:$(this).attr('id')});});});ed.loadingOpen=function(){ed.ui.loading.open();}
ed.loadingClose=function(){ed.ui.loading.close();}
ed.alert=function(content,title){ed.ui.alert.showAlert(content,title);}
ed.confirm=function(options){ed.ui.confirm.showConfirm(options);}
