(function($){
$.fn.Popup = function(method){
var $this = this;
var settings = {
id: undefined,
html: '
',
}
var methods = {
init: function(options){
if(options != undefined){
$.extend(settings, options);
}
if($this.html() == null || $this.html() == ''){
$this = methods.getContainer().addClass('popup-layer').html(settings.html);
$('.backing', $this).bind('click', function() {
methods.hide();
});
}
return $this;
},
getContainer: function(){
settings.id = $this.selector.replace('\#','') || 'popup-layer' + Math.floor(Math.random()*1000000000001);
if($('#' + settings.id).length == 0){
$('body').append('');
}
return $('#' + settings.id);
},
set: function(data){
if(data != undefined){
if(data.title != undefined && data.title != '') $('.title',$this).html(data.title);
if(data.content != undefined && data.content != '') $('.content',$this).html(data.content).css({'max-height':'none','overflow-x': 'hidden'});
}
$('закрыть').click(function(){methods.hide();}).appendTo($('h2.title',$this));
return $this;
},
show: function(data){
methods.init();
if(data != undefined){
methods.set(data);
}
$this.show();
var box = $('.popup', $this);
var pos_left = (($(window).width())/2) - ((box.innerWidth())/2) + 'px';
var pos_top = (($(window).height())/2) - ((box.innerHeight())/2) + 'px';
if($('.content',$this).height() > $(box).outerHeight(true)){
var max_heigth = (($(box).outerHeight(true))) - ($('.title',box).outerHeight(true)) - 30;
$('.content',box).css({'max-height':max_heigth});
}
$this.hide();
box.css({
'left':pos_left,
'top':pos_top
});
$this.fadeIn();
return $this;
},
hide: function(){
$this.fadeOut();
return $this;
}
};
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Метод ' + method + ' не существует в jQuery.Popup' );
}
return $this;
}
$.fn.Authorize = function(method){
var $this = this;
var settings = {
auth: $("#popup-to-authorize").Popup(),
reg: $("#popup-to-registration").Popup()
};
var templates = {
auth: function(){
return '\n\
\n\
\n\
\n\
\n\
Не займет много времени\n\
\n\
';},
reg: function(){
return '\n\
'}
}
var methods = {
init: function(config){
if (config && typeof(config) == 'object') {
$.extend(settings, config);
}
methods.auth();
methods.reg();
return $this;
},
auth: function(){
settings.auth.Popup('set', {title: 'Войти на сайт', content: templates.auth});
$('.openid_container',settings.auth).OpenID();
$('.to-authorize').live('click', function(){
settings.auth.Popup('show');
return false;
});
return $this;
},
reg: function(){
settings.reg.Popup('set', {title: 'Регистрация', content: templates.reg});
$('.openid_container',settings.reg).OpenID();
methods.regInitForm();
$('.popup',settings.reg).css({'max-width':'75%'});
$('.to-registration').live('click', function(){
settings.reg.Popup('show');
return false;
})
return $this;
},
check: function(data){
if(data != undefined){
if(data.redirect != undefined){
window.location = data.redirect;
} else {
if(data.bind != undefined){
$('form:eq(0)',settings.reg).show();
$('form:eq(0) input[name=login]',settings.reg).val(data.bind);
}
$('form:eq(1) input[name=login]',settings.reg).val(data.nickname);
$('form:eq(1) input[name=email]',settings.reg).val(data.email);
$('input[name=identity]',settings.reg).val(data.identity);
settings.reg.Popup('show');
}
}
},
regInitForm: function(){
var form_bind = $('form:eq(0)', settings.reg);
var form_reg = $('form:eq(1)', settings.reg);
form_reg.initAjaxForm(function(json){
if(json.response == 'success'){
window.location = json.redirect;
}
});
form_bind.initAjaxForm(function(json){
if(json.response == 'success'){
window.location = json.redirect;
}
if(json.response == 'error'){
alert(json.message);
}
});
return $this;
}
}
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Метод ' + method + ' не существует в jQuery.Auth' );
}
return $this;
}
})(jQuery);
$(document).ready(function(){
try{$("#popup-to-authorize").Authorize();}catch(e){}
});