/**
 * javascript function library for sending article to a friend
 * @author benjamin.povirk@pop-tv.si, 8.7.08
 */


/**
 * function shows mbox for sending article
 * @return false
 */
function article_send_show(article_id) {

	if (typeof(article_id) == "undefined" || article_id == "")
		return false;

	article_send_mbox_open(500,500,true)

	new Ajax('/lbin/ajax/article_send.php?article_id='+article_id, {
		method: 'get',
		evalScripts: true,
		encoding: 'utf-8',
		update: $('MBOX_window')
	}).request();
	
	return false;
}

/**
 * function creates mbox
 * @return false
 */
function article_send_mbox_open(MBOX_WIDTH,MBOX_HEIGHT,IS_DRAGGABLE)
{
	flash_show(false);

	if ( typeof(IS_DRAGGABLE) == "undefined" )
		IS_DRAGGABLE = true;

	// create holders if there isn't any
	mbox_create_holders( IS_DRAGGABLE );

	left_s = 503-(MBOX_WIDTH/2);
	top_s = window.getScrollTop() +(window.getHeight() - MBOX_HEIGHT)/2;

	// push down if it is still above content
	if (top_s < 100)
		top_s = 100;

	$("MBOX_window").setStyles({
		margin: 0,
		padding: 0,		
		position: 'absolute',
		left: left_s,
		top: top_s,
		display: 'block'
	});

	$("MBOX_dragger").setStyles({
		margin: 0,
		padding: 0,		
		position: 'absolute',				    
		left: left_s,
		top: top_s,
		display: 'block'
	});

	$("MBOX_overlay").setStyle('display','block');

	$("MBOX_overlay").setStyles({
		height: window.getScrollHeight(),
		width: window.getScrollWidth()
	});
	
	$("MBOX_window").setStyle("width", MBOX_WIDTH);
	$("MBOX_dragger").setStyle("width", MBOX_WIDTH);



	new Fx.Style('MBOX_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
	new Fx.Style('MBOX_window',  'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){  } }).start(0,1);

	return false;
}

/**
 * function closes mbox
 * @return false
 */
function bb_mbox_close()
{

	new Fx.Style('MBOX_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){ $("MBOX_overlay").setStyles('display:none;'); }}).start(0.6,0);
	new Fx.Style('MBOX_window',  'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function()
		    {
			$("MBOX_window").setStyles('display:none;');
			$("MBOX_window").innerHTML = '';
		    } }).start(1,0);
	$("MBOX_dragger").setStyles({"display": 'none'});

	flash_show(true);

	return false;
}

/**
 * function makes mbox with holders and makes it draggable
 * @return false
 */
function mbox_create_holders( is_draggable )
{
	if ($('MBOX_overlay'))
	{
                $("MBOX_overlay").remove();
                $("MBOX_dragger").remove();
                $("MBOX_window").remove();
	}

	include_css('/static/shared/css/mbox.css');
	// CREATE MBOX HOLDERS

	var mbox_overlay = new Element('div', {
		'id' : 'MBOX_overlay'
	})
	.injectInside(document.body);

	var mbox_dragger = new Element('div', {
		'id': 'MBOX_dragger'
	})
	.injectInside(document.body);

	var mbox_window = new Element('div', {
		'id': 'MBOX_window'
	})
	.injectInside(document.body);

	var mbox_window_loading = new Element('div', {
		'id': 'MBOX_loading'
	})
	.injectInside(mbox_window);

	var mbox_window_loading_img = new Element('img', {
		'src': "/static/shared/img/ajax-loader-white.gif",
		'alt': 'Loading'
	})
	.injectInside(mbox_window_loading);

	// CREATE DROP
        if ( is_draggable ) {
		var main_content = $('left');

		mbox_dragger.addEvent('mousedown', function(e) {
			e = new Event(e).stop();
			mbox_window.setStyles({'opacity':0.7});
			var drag = mbox_window.makeDraggable({
				container: main_content
			}); // this returns the dragged element
				drag.start(e); // start the event manual
		});

		mbox_window.addEvent('emptydrop', function() {
			mbox_dragger.setStyles({'left': this.getCoordinates().left,
						'top': this.getCoordinates().top});
				mbox_window.setStyles({'opacity':1});
		});
	}

	return false;
}



