
var Popup = Class.create({
	
	initialize:	function(options) {
	
		this._extenders = [];
	
		this._element =	options['element'];
		
		this._hook = options['hook'];
		
		this._onCancel = this._cancel ? this._cancel.bindAsEventListener(this) : null;
		
		this._onAccept = this._accept ? this._accept.bindAsEventListener(this) : null;
		
		this._onSubmit = this._submit ? this._submit.bindAsEventListener(this) : null;
		
		this._onKeyPress = this._keypress ? this._keypress.bindAsEventListener(this) : null;
		
		this._closeButton = new Element('a', {'href': '#', 'class': 'close', 'title': $_LOCALE['title_close']}).observe('click', this._onCancel);
		
		if (Prototype.Browser.IE && parseFloat(navigator.appVersion.split('MSIE ')[1].substr(0,3)) < 7) {
		
			this._shim = new Element('iframe', {
			
			      style: 'position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0); top:0; left:0; width:400px; height: 400px; z-index:1',
			      
			      src: 'javascript:false;',
			      
			      frameborder: 0
			      
			    });
			
			this._element.insert(this._shim);
		
		}
		
		this._element
			
			.insert(new Element('div', {'class': 'popup_top'}))
		
			.insert(this._closeButton)
			
			.insert(new Element('div', {'class': 'popup_inner'}))
			
			.insert(new Element('div', {'class': 'popup_bottom'}));
			
		var offset = this._hook.cumulativeOffset();
		
		this._holder = new Element('div',{'class': 'popup_holder'}).insert(this._element);
		
		this._holder.setStyle({
		
			left:	offset[0] + 'px',
			
			top:	offset[1] + 'px'
		
		});
		
		$(document.body).insert(this._holder);
		
		if (document.popup) {
		
			document.popup._destroy();
		
		}
		
		document.popup = this;
		
		document.observe('keyup',this._onKeyPress);
		
		return this;
	
	},

	_set: function() {
		
		var V = document.viewport.getDimensions(), H = this._hook.viewportOffset(), E = this._element.getDimensions(), h = this._hook.getDimensions();
		
		if (H.left + E.width > V.width) {
		
			this._element.setStyle({
			
					right: 	-Math.round(h.width/2) + 'px'
		
			});
			
			this._element.up().addClassName('slide_left');
		
		} else {
		
			this._element.setStyle({
			
					left: Math.round(h.width/2) + 'px'
		
			});
			
			this._element.up().addClassName('slide_right');
		
		}
		
		if (V.height - (H.top + E.height) > H.top - E.height) {
		
			this._element.setStyle({
			
				top:	h.height + 30 + 'px'
			
			}).addClassName('slide_bottom');
			
			new Effect.Morph(this._element.show(),{style: 'top: ' + Math.round(1.5*h.height) + 'px', duration: 0.35});
		
		} else {
		
			this._element.setStyle({
			
				bottom:	h.height + 30 + 'px'
			
			}).addClassName('slide_top');
			
			new Effect.Morph(this._element.show(),{style: 'bottom: ' + Math.round(h.height/2) + 'px', duration: 0.35});
		
		}
		
	},
	
	_keypress:		function(event) {
		
		var keycode = Event.keyCode(event);
		
		this._listeners.each(function(listener) {
		
			if (listener.keycode == keycode) {
			
				listener.action(event);
				
				event.stop();
			
			}
		
		});
		
		return true;
	
	},
	
	_destroy:	function() {
	
		document.stopObserving('keyup', this._onKeyPress);
	
		this._closeButton.stopObserving('click', this._onCancel);
		
		if (this._submitButton) {
		
			this._submitButton.stopObserving('click', this._onAccept);
		
		} else {
		
			this._element.select('form').each(function(form) {
			
				form.stopObserving('submit',this._onSubmit);
			
			},this);
		
		}
		
		if (this._shim) {
		
			this._shim.remove();
		
		}
		
		this._extenders.invoke('close');
		
		this._element.hide().up().remove();
		
		document.popup = null;
	
	}

});

var Info = Class.create(Popup, {

	initialize:	function($super,options) {
	
		this._element = new Element('div', {'class': 'popup'}).hide();
	
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }];
		
		this._element.down('.popup_inner').insert(new Element('p').update(options['info']));
			
		this._set();
		
		return this;
	
	},
	
	_cancel:	function(event) {
		
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	}

});

var Comment = Class.create(Popup, {

	initialize:	function($super,options) {
	
		this._element = new Element('form', {'action': options['url'], 'method': 'post', 'class': 'popup new_comment'}).hide();
	
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }];
		
		this._submitButton = new Element('span', {'class': 'btn'})
		
			.insert(new Element('button', {'type': 'submit', 'class': 'submit'}).update($_LOCALE['action_reply']).observe('click', this._onAccept));
		
		this._element.down('.popup_inner')

			.insert(new Element('label', {'for': 'inline_message'}).update($_LOCALE['label_comment']))
			
			.insert(new Element('textarea', {'name': 'message', 'id': 'inline_message', 'rows': '6', 'cols': '60', 'title': $_LOCALE['title_message'] }))
			
			.insert(this._submitButton);
			
		this._set();
		
		return this;
	
	},
	
	_cancel:	function(event) {
		
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	},
	
	_accept:	function(event) {
	
		event.stop();	// IE8 double submit fix;
		
		if (this._element.down('textarea').value.blank() || this._element.down('textarea').value.length > 65000) {
		
			new Effect.Highlight(this._element.down('textarea'), { startcolor: '#ff9999', restorecolor: '#ffffff' });
		
		} else {
		
			this._element.down('textarea').value = this._element.down('textarea').value.stripTags().stripScripts();
		
			this._submitButton.disabled = true;	// IE8 double submit fix;
			
			this._submitButton.value = "Подождите…";
			
			if ('form' == this._element.tagName.toLowerCase()) {
			
				this._element.submit();
				
			} else if (null != this._element.down('form')) {
			
				this._element.down('form').submit();
				
			}
		
		}
	
	}

});

var Rename = Class.create(Popup, {

	initialize:	function($super,options) {
	
		this._element = new Element('form', {'action': options['url'], 'method': 'post', 'class': 'popup new_comment'}).hide();
	
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }, { keycode: 13, action: this._accept.bind(this) }];
		
		this._submitButton = new Element('span', {'class': 'btn'})
		
			.insert(new Element('button', {'type': 'submit', 'class': 'submit'}).update($_LOCALE['action_rename']).observe('click', this._onAccept));
		
		this._element.down('.popup_inner')

			.insert(new Element('label', {'for': 'inline_message'}).update($_LOCALE['label_title']))
			
			.insert(new Element('textarea', {'name': 'message', 'id': 'inline_message', 'rows': '2', 'cols': '60', 'title': $_LOCALE['title_newname'] }))
			
			.insert(this._submitButton);
			
		this._element.down('textarea').value = options['name'];
			
		this._set();
		
		return this;
	
	},
	
	_cancel:	function(event) {
		
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	},
	
	_accept:	function(event) {
	
		event.stop();	// IE8 double submit fix;
		
		if (this._element.down('textarea').value.blank()) {
		
			new Effect.Highlight(this._element.down('textarea'), { startcolor: '#ff9999', restorecolor: '#ffffff' });
		
		} else {
		
			this._element.down('textarea').value = this._element.down('textarea').value.stripTags().stripScripts();
		
			this._submitButton.disabled = true;	// IE8 double submit fix;
			
			this._submitButton.value = "Подождите…";
			
			if ('form' == this._element.tagName.toLowerCase()) {
			
				this._element.submit();
				
			} else if (null != this._element.down('form')) {
			
				this._element.down('form').submit();
				
			}
		
		}
	
	}

});

var Buy = Class.create(Popup, {

	initialize:	function($super, options) {

		this._element = new Element('div', {'class': 'popup'}).hide();
		
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }];
		
		new Ajax.Request(options['url'],{
	
			parameters:	{
			
				ajax_request:	true
			
			},
			
			onSuccess:	function(transport) {
			
				this._element.down('div.popup_inner').update(transport.responseText);
				
				this._set();
				
				this._element.select('form').each(function(form) {
				
					form.observe('submit',this._onSubmit);
				
				},this);
			
			}.bind(this),
			
			onFailure:	function() {
			
				this._cancel(null);
			
			}.bind(this)
		
		});

		return this;
		
	},

	_cancel:	function(event) {
		
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	},
	
	_submit:	function(event) {
	
		event.stop();	// IE8 double submit fix;
		
		var form = event.element();
		
		this._element.select('form').each(function(form) {
		
			form.stopObserving('submit',this._onSubmit);
		
		},this);
		
		new Ajax.Request(form.action, {
		
			parameters:	Object.extend({
			
				ajax_request:	true
			
			},form.serialize(true)),
			
			onSuccess:	function(transport) {
			
				this._element.down('div.popup_inner').update(transport.responseText);
				
				this._element.select('form').each(function(form) {
				
					form.observe('submit',this._onSubmit);
				
				},this);
			
			}.bind(this),
			
			onFailure:	function() {
			
				this._cancel(null);
			
			}.bind(this)
		
		})
	
	}

});

var Play = Class.create(Popup, {

	initialize:	function($super, options) {

		this._element = new Element('div', {'class': 'popup'}).hide();
		
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }];
		
		this._type = options['type'] || 'music';
		
		this._element.down('.popup_inner')
			
			.insert(new Element('div', {'class': 'player'}).setStyle({
			
				width:	'100%',
				
				height:	this._type == 'video' ? '230px' : '30px'
			
			}));
		
		this._extenders.push($f(this._element.down('.player'), {
		
			src: $_CONTEXT_PATH + 'resources/swf/loco/flowplayer.commercial-3.1.2.swf', 
		
			wmode:	'opaque',

			key: '#@e664011b9c6d62a761b'
			
		}, {
		
			key: '#@e664011b9c6d62a761b',
			 
		    plugins: {
		    
		        controls: {
		        
		            fullscreen: false,
		            
		            height: 30
		            
		        }
		        
		    }, 
		 
		    clip: {
			
				url:	options['url'],
		    
			    autoBuffering:	false,
		    	
		        autoPlay:		false,
		        
		        scaling:		'fit',
		        
		        accelerated:	true
		        
		    }
		 
		}));

		this._set();

		return this;
	
	},
	
	_cancel:	function(event) {
	
		if (event) {
		
			event.stop();
		
		}
	
		this._destroy();
	
	}

});

var Invite = Class.create(Popup, {

	initialize:	function($super, options) {

		this._element = new Element('form', {'action': options['url'], 'method': 'post', 'class': 'popup new_comment'}).hide();
		
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }];

		this._submitButton = new Element('span', {'class': 'btn'})
			
			.insert(new Element('button', {'type': 'submit', 'class': 'submit'}).update($_LOCALE['action_send_request']).observe('click', this._onAccept));
		
		this._element.down('.popup_inner')

			.insert(new Element('label', {'for': 'inline_message'}).update('<p>' + $_LOCALE['label_send_request'].replace('$1',options['person']) + '</p>'))
			
			.insert(new Element('p',{'class': 'toggeler'}).update('<span>' + $_LOCALE['action_show_message'] + '</span>'))
			
			.insert(new Element('textarea', {
			
				'class':	'toggeled',
				'name':		'message',
				'id':		'inline_message',
				'name':		'message',
				'rows':		'6',
				'cols':		'60',
				'title':	$_LOCALE['title_message']
				
			}).hide())
			
			.insert(this._submitButton);
			
		this._onToggle = this._toggle.bindAsEventListener(this);
			
		this._toggeler = this._element.down('.toggeler').observe('click',this._onToggle);
		
		this._toggeled = this._element.down('.toggeled');
		
		this._set();

		return this;
	
	},
	
	_cancel:	function(event) {
	
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	},
	
	_accept:	function(event) {
	
		event.stop();	// IE8 double submit fix;
		
		if (this._element.down('textarea').visible() && this._element.down('textarea').value.blank()) {
		
			new Effect.Highlight(this._element.down('textarea'), { startcolor: '#ff9999', restorecolor: '#ffffff' });
		
		} else {
		
			this._element.down('textarea').value = this._element.down('textarea').value.stripTags().stripScripts();
		
			this._submitButton.disabled = true;	// IE8 double submit fix;
			
			this._submitButton.value = "Подождите…";
		
			if ('form' == this._element.tagName.toLowerCase()) {
			
				this._element.submit();
				
			} else if (null != this._element.down('form')) {
			
				this._element.down('form').submit();
				
			}
		
		}
	
	},
	
	_toggle:	function(event) {
	
		if (this._toggeled.visible()) {
		
			this._toggeler.down('span').update($_LOCALE['action_show_message']);
		
		} else {
		
			this._toggeler.down('span').update($_LOCALE['action_hide_message']);
		
		}
	
		this._toggeled.toggle();
	
	}

});

var Remove = Class.create(Popup, {

	initialize:	function($super,options) {
	
		this._element = new Element('form', {'action': options['url'], 'method': 'post', 'class': 'popup'}).hide();
	
		$super({ element: this._element, hook: $(options['link']) });
		
		this._listeners = [{ keycode: 27, action: this._cancel.bind(this) }, { keycode: 13, action: this._accept.bind(this) }];
		
		this._submitButton = new Element('span', {'class': 'btn'})
			
			.insert(new Element('button', {'type': 'submit', 'class': 'submit'}).update($_LOCALE['action_delete']).observe('click', this._onAccept));
		
		this._cancelButton = new Element('span', {'class': 'btn'})
			
			.insert(new Element('button', {'type': 'submit', 'class': 'submit'}).update($_LOCALE['action_cancel']).observe('click', this._onCancel));
		
		this._element.down('.popup_inner')

			.insert(new Element('strong').update($_LOCALE['label_delete']))
			
			.insert(this._submitButton)
			
			.insert(this._cancelButton);
			
		this._set();
		
		return this;
	
	},
	
	_destroy:	function($super) {
	
		this._cancelButton.stopObserving('click', this._onCancel);
		
		$super();
	
	},
	
	_cancel:	function(event) {
		
		if (event) {
		
			event.stop();
		
		}
		
		this._destroy();
	
	},
	
	_accept:	function(event) {
	
		event.stop();	// IE8 double submit fix;
	
		this._submitButton.disabled = true;	// IE8 double submit fix;
		
		this._submitButton.value = "Подождите…";
		
		if ('form' == this._element.tagName.toLowerCase()) {
		
			this._element.submit();
			
		} else if (null != this._element.down('form')) {
		
			this._element.down('form').submit();
			
		}
	
	}

});

