var $chk = function(obj){
return !!(obj || obj === 0);
};
var alertBox = new Class({
Implements: [Options, Events],
options: {
mobile:false,
delay: 500
},
initialize: function(options){
this.setOptions(options);
this.parameters=new Array();
this.boxContainer=new Element('div',{class:'alertbox-container'}).inject($(document.body))
this.box=new Element('div',{class:'alertbox'}).inject(this.boxContainer);
this.boxLayer=new Element('i.alertboxlayer').inject(this.boxContainer,'after');
this.boxLayer.addEvent('click',function(e){
if(e!==undefined){
e.stopPropagation();
}
this.hideBox(true);
}.bind(this));
if(Browser.name=='ie'&&parseInt(Browser.version)<=10){
this.boxContainer.addEvent('click',function(e){
if(e.target===this.boxContainer){
e.stopPropagation();
this.boxLayer.fireEvent('click');
}
}.bind(this));
}
this.boxContentOuter=new Element('div');
this.boxContent=new Element('div').inject(this.boxContentOuter);
new Element('img',{'src':'/img/b.svg'}).inject(this.box);
new Element('img',{'src':'/img/klodka.svg'}).inject(this.box);
this.boxContentOuter.inject(this.box);
this.box.addClass(this.options.additionalClass||'');
var buttonsContainer=new Element('div',{class:'buttons'}).inject(this.box);
new Element('input').inject(buttonsContainer);
this.yesButton=new Element('button',{class:'yes','html':'ok'}).inject(buttonsContainer);
this.noButton=new Element('button',{class:'no','html':'no'}).inject(buttonsContainer);
this.box.getElements('button').addEvent('click',function(event){
this.beforeClose(event.target.getParent('button')?event.target.getParent('button').hasClass('yes'):event.target.hasClass('yes'));
}.bind(this));
// this.box.addClass('show').removeClass('show');
this.msgChain = new Chain();
this.clear();
},
clear: function(){
if(!this.opened){
this.chainLength=0;
this.chainStarted=false;
}
},
alert: function(message,options){
var parameters={type:'alert',klass:options.klass||'',width:options.width,message:message,callbacks:options.callbacks,backOnClose:options.backOnClose};
this.parameters.push(parameters);
this.chainLength++;
this.setMsgChain();
return this;
},
confirm: function(message,options){
var parameters={type:'confirm',klass:options.klass||'',width:options.width,message:message,callbacks:options.callbacks,backOnClose:options.backOnClose};
this.parameters.push(parameters);
this.chainLength++;
this.setMsgChain();
return this;
},
prompt: function(message,options){
var parameters={type:'prompt',klass:options.klass||'',width:options.width,message:message,callbacks:options.callbacks,backOnClose:options.backOnClose};
this.parameters.push(parameters);
this.chainLength++;
this.setMsgChain();
return this;
},
setMsgChain: function(){
var waitTime=2000;
// if($chk(this.options.callback) || this.options.autoDismiss == false || this.options.dismissOnEvent) waitTime = 0;
this.msgChain.wait(
this.options.delay
).chain(
function(){
this.showMsg();
}.bind(this)
).wait(
waitTime
);
if(!this.chainStarted){
this.chainStarted=true;
this.msgChain.callChain();
}
},
showMsg: function(){
if(this.parameters[this.chainLength-1].type==='alert'){
if(this.options.mobile){
alert(this.parameters[this.chainLength-1].message);
(this.parameters[this.chainLength-1].callbacks[0]['callback']||function(){})();
this.msgChain.callChain();
}
else{
this.showBox();
}
}
else if(this.parameters[this.chainLength-1].type=='confirm'){
if(this.options.mobile){
var result=confirm(this.parameters[this.chainLength-1].message);
if(result){
this.parameters[this.chainLength-1].callbacks[0]['callback']();
}
else{
this.parameters[this.chainLength-1].callbacks[1]['callback']();
}
this.msgChain.callChain();
}
else{
this.showBox();
}
}
else if(this.parameters[this.chainLength-1].type=='prompt'){
if(this.options.mobile){
var reply=prompt(this.parameters[this.chainLength-1].message);
this.parameters[this.chainLength-1].callbacks[0]['callback'](reply);
this.msgChain.callChain();
}
else{
this.showBox();
}
}
},
showBox:function(){
this.opened=true;
if(this.parameters[this.chainLength-1].klass==''){
this.box.removeClass('pmb');
}
else{
this.box.addClass('pmb');
}
this.box.setStyle('width',parseInt(this.parameters[this.chainLength-1].width || 356));
this.boxContent.set('html',this.parameters[this.chainLength-1].message);
if(this.parameters[this.chainLength-1].callbacks[0]['label']=='')
this.yesButton.addClass('hidden');
else
this.yesButton.removeClass('hidden').getElement('span').set('text',this.parameters[this.chainLength-1].callbacks[0]['label']);
if(this.parameters[this.chainLength-1].callbacks[1]['label']=='')
this.noButton.addClass('hidden');
else
this.noButton.removeClass('hidden').getElement('span').set('text',this.parameters[this.chainLength-1].callbacks[1]['label']);
this.box.removeClass('alert prompt confirm').addClass(this.parameters[this.chainLength-1].type);
this.boxContainer.addClass('show');
//$(document.body).setStyle('overflow','hidden');
},
beforeClose:function(yes){
var type=this.parameters[this.chainLength-1].type;
if(type=='confirm'){
if(yes){
this.parameters[this.chainLength-1].callbacks[0]['callback']();
}
else{
this.parameters[this.chainLength-1].callbacks[1]['callback']();
}
}
else if(type=='prompt'){
if(yes){
this.parameters[this.chainLength-1].callbacks[0]['callback'](this.box.getElement('input').value);
}
else{
this.parameters[this.chainLength-1].callbacks[1]['callback'](this.box.getElement('input').value);
}
}
else{
if(yes){
this.parameters[this.chainLength-1].callbacks[0]['callback']();
}
else{
this.parameters[this.chainLength-1].callbacks[1]['callback']();
}
}
this.hideBox();
},
hideBox:function(fromLayer){
var backOnClose=this.parameters[this.chainLength-1].backOnClose||'';
this.boxContainer.removeClass('show');
this.chainLength--;
this.parameters.pop();
if(this.chainLength==0){
this.chainStarted=false;
}
this.closed=true;
//$(document.body).setStyle('overflow','auto');
this.msgChain.callChain();
if(fromLayer&&backOnClose){
(function(){
window.location.href=backOnClose;
}).delay(1000);
}
},
});