var Share = new Class({
	options: {
		'form_url': '/share/index.php'
	},
	initialize: function(options) {
		this.setOptions(options);
		this.page = $('page');
		this.shareBtn = $$('#content-btn-share a')[0];
		
		if (this.shareBtn) {
			this.shareBtn.addEvent('click', this.clickOpen.bind(this));
		}	
		this.shareBottomBtn = $('content-btn-share-bottom');
		
		if (this.shareBottomBtn) {
			this.shareBottomBtn.addEvent('click', this.clickOpen.bind(this));
		}	
	},
	cacheBuster: function() {
		return parseInt(Math.random()*99999999);
	},
	createShareDiv: function() {
		this.shareDiv = new Element('div', {
   			'id': 'share'
		});
		this.shareDiv.setHTML('<div id="share-content"><div id="share-bg"></div><div id="share-inner"></div></div><div id="share-bottom"></div>');
	},	
	removeShareDiv: function() {
		if (this.shareDiv) {	
			this.shareDiv.remove();
			this.shareDiv = undefined;
		}
	},	
	clickOpen: function(e) {
		if (!this.shareDiv) {
			this.createShareDiv();
			this.shareDiv.injectInside(this.page);
			this.loadForm();
		} else {
			this.clickClose();	
		}
		
		// scroll to the form
		var scroller = new Fx.Scroll(window, {duration: 1000});
		scroller.toElement('wrapper');
		
		// cancel the event
		var event = new Event(e);
		event.stop();
	},
	clickClose: function(e) {
		if (this.shareDiv) {	
			this.removeShareDiv();
		}
		
		// cancel the event
		var event = new Event(e);
		event.stop();
	},
	loadForm: function() {
		this.shareInner = $('share-inner');
		var myAjax = new Ajax(this.options.form_url + "?" + this.cacheBuster(), {
			method: 'get', 
			update: this.shareInner,
			onComplete: this.initForm.bind(this)
		}).request();
	},
	initForm: function() {
		// fix the form onSubmit if it's there
		this.shareForm = $('share-form');
		if (this.shareForm) {
			this.shareForm.addEvent('submit', this.submitForm.bind(this));
		}
		// init the close button
		this.closeBtn = $('share-close');
		if (this.closeBtn) {
			this.closeBtn.addEvent('click', this.clickClose.bind(this));
		}
		this.sendCloseBtn = $('send-close');
		if (this.sendCloseBtn) {
			this.sendCloseBtn.addEvent('click', this.clickClose.bind(this));
		} 
		// if send-success is present, close the form
		if ($('send-success')) {
			this.removeShareDiv();
		}
		
		// init maxlenght on the textarea
		objs = this.shareForm.getElementsByTagName("textarea");
		for (oi=0;oi<objs.length;oi++) {
			thisObj = objs[oi];
			// this part enforces the maxLength
			thisObj.addEvent('keyup', ismaxlength); 
		}
		
	}, 
	submitForm: function(e) {
		this.shareForm.send({
			method: 'post', 
			update: this.shareInner,
			onComplete: this.initForm.bind(this)
		});	
		// cancel the event
		var event = new Event(e);
		event.stop();
	}	
});
Share.implement(new Options, new Events);

// function to ensure max length
function ismaxlength(){
	var mlength=this.getAttribute? parseInt(this.getAttribute("maxlength")) : ""
	if (this.getAttribute && this.value.length>mlength)
	this.value=this.value.substring(0,mlength)
	return false;
}

 
window.addEvent('domready', function() {
	new Share();
});
					  
	