var PopUp = new Class({
	initialize: function(options){
		this.options = options;
		this.overlay = $('overlay');
		this.popup = new Element('div', {'class':'pop_up'});
		this.isOpen = false;
	},
	request: function(name){
		this.id = name;
		this.popup.set({'id':name});
		
		this.ajaxRequest = new Request.HTML({
			method: 'post',
			url: '/inc/' + this.id + '.php',
			update: this.popup,
			onSuccess: this.open()
		});
		this.ajaxRequest.send();
	},
	submit: function(form_element){
		this.form = form_element;
		this.ajaxRequest = new Request.HTML({
			method: 'post',
			url: '/inc/' + this.id + '.php',
			update: this.popup
		});
		this.ajaxRequest.send({data: this.form});
	},
	open: function() {
		this.isOpen = true;
		
		this.popup.fade('hide');
		// inject pop-up into document
		this.popup.inject(this.overlay, 'before');
	
	/*if ( $chk($('header')) && $chk($('footer')) )
			this.page_height = $('header').getSize().y + $('container').getSize().y + $('footer').getSize().y - 50;
		if ( $chk($('header')) && !($chk($('footer'))))
			this.page_height = $('header').getSize().y + $('container').getSize().y;
		else
			this.page_height = $(document).getSize().y;*/
			
		//incase the page is not long enough
		if ( $chk($('footer')) )
			$('footer').setStyle('height', 250);
		
		winScroller.toTop();
		
		// positions the pop-up
		left_padding = (window.getSize().x - this.popup.getSize().x) / 2;
		this.popup.setStyle('left', left_padding);

		this.page_height = $(document.body).getScrollSize().y;
		this.overlay.setStyle('height', this.page_height);
		this.overlay.toggleClass('hide');
		
		// fade popup
		this.popup.set('tween', {duration: 'long'});
		this.popup.fade('in');
	},
	close: function() {
		this.isOpen = false;
		this.popup.fade('out');
		setTimeout(function(){
				popupController.popup.destroy();
				popupController.overlay.toggleClass('hide');
				//incase the page was not long enough
				if ( $chk($('footer')) )
					$('footer').setStyle('height', 150);
		}, 1000);
	},
	toggle: function(name){
		this.isOpen = false;
		this.popup.fade('out');
		setTimeout(function(){
				popupController.popup.destroy();
				popupController.overlay.toggleClass('hide');
				popupController.request(name);
		}, 1000);
	},
	preview: function(form){
		this.id = 'preview_story';
		this.popup.set({'id':this.id});
		this.ajaxRequest = new Request.HTML({
			method: 'post',
			url: '/inc/preview_story.php',
			update: this.popup,
			onSuccess: this.open()
		});
		this.ajaxRequest.send(form);
	},
	wallpaper: function(){
		this.id = 'wallpaper';
		this.form = $('url_form');
		this.popup.set({'id':this.id});
		this.ajaxRequest = new Request.HTML({
			method: 'post',
			url: '/inc/wallpaper.php',
			update: this.popup,
			onSuccess: this.open()
		});
		this.ajaxRequest.send(this.form);
	},
	edit: function(){
		this.id = 'admin_edit';
		this.form = $('story_id');
		this.popup.set({'id':this.id});
		this.ajaxRequest = new Request.HTML({
			method: 'post',
			url: '/inc/admin_edit.php',
			update: this.popup,
			onSuccess: this.open()
		});
		this.ajaxRequest.send(this.form);
	}
});

function init(){
	popupController = new PopUp();
	winScroller = new Fx.Scroll(document);
}
window.addEvent("domready", init);

function toggle_readAll(){
	$('readAll').toggleClass('hide');
	$('story_intro').toggleClass('hide');
	$('rating').toggleClass('center');
}

function toggle_popup(name){
	if (popupController.isOpen == true) popupController.close();
	else if( name != null ) popupController.request(name);
}

function switch_popup(name){
	popupController.toggle(name);
}

function submit_form(form_element){
	popupController.submit(form_element);
}

function popup_wallpaper(name){
	popupController.wallpaper();
}

function edit_story(){
	popupController.edit();
}


//facebook share from flash
// AsP: redirects to a local script for tracking.. then redirects to the facebook page.
function fbs_click(url,title,summary,img) {
	
	fb_url = '/helper/tracker-facebook.php?s=100'+
	'&p[medium]=100'+
	'&p[title]='+encodeURIComponent(title)+
	'&p[summary]='+encodeURIComponent(summary)+
	'&p[url]='+encodeURIComponent(url)+
	'&p[images][0]='+encodeURIComponent(img);
	
	window.open(fb_url,'sharer','toolbar=0,status=0,width=626,height=436');
}