/* $Id$ */
var dragging = null;
var g_tm = null;

function init_dlg() {
	$(document).bind('mousemove',function(e) {
		if ( dragging ) {
			var dx = e.pageX - dragging.startP[0]; var dy = e.pageY - dragging.startP[1];
			dragging.dlg.offset({left:dragging.startD.left + dx,top:dragging.startD.top + dy});
		}
	});
//	$(document).keypress(function(e) { if ( e.which == 0 || e.which == 27 ) close_ajax_dlg(); });
	$(document).data('initialized',1);
}

function close_ajax_dlg() { clearTimeout(g_tm); $('#ajax_dlg').fadeOut('slow',function() {$('#ajax_dlg').remove();}); }
function display_ajax_dlg(html,ops) {
	if ( $('#ajax_dlg').length > 0 ) $('#ajax_dlg').fadeOut('fast',function() {$('#ajax_dlg').remove(); display_ajax_dlg(html,ops);});
	if ( $(document).data('initialized') != 1 ) init_dlg();
	if ( !ops ) ops = {};
	var c = $('.title','<div>'+html+'</div>').html();
	if ( c ) $('body').append('<div id="ajax_dlg">'+html+'</div>');
	else $('body').append('<div id="ajax_dlg"><div class="title"><div>'+ops.title+'</div></div><div id="ajax_content">'+html+'</div></div>');
	if ( !$('#ajax_dlg .title img').length ) $('#ajax_dlg .title').append('<img src="/images/cross.png" />');
	$('.title img').click(function() { $('#ajax_dlg').fadeOut('slow',function() {$('#ajax_dlg').remove();}) });
	$('.title').bind('mousedown',function(e) {$('.title').css('cursor','move'); dragging = {dlg:$('#ajax_dlg'),startP:[e.pageX,e.pageY],startD:$('#ajax_dlg').offset()};});
	$('.title').bind('mouseup',function(e) {$('.title').css('cursor','default'); dragging = null});
	$('.close_btn').click(function() {close_ajax_dlg()});
	position_ajax_dlg(ops);
}

function position_ajax_dlg(ops) {
	if ( $('#ajax_dlg').height() > 0.90 * $(window).height() ) $('#ajax_content').height(0.90 * $(window).height());
	if ( $('#ajax_content').width() > $('#ajax_dlg').width() ) $('#ajax_dlg').width($('#ajax_content').width() + 15);
	if ( ops.position == 'mouse' ) {
		if ( ops.pageX + $('#ajax_dlg').width() > $(window).width() ) $('#ajax_dlg').css('left',ops.pageX - $('#ajax_dlg').width());
		else $('#ajax_dlg').css('left',ops.pageX);
		$('#ajax_dlg').css('top',ops.pageY+ 20);
	} else if ( $('#'+ops.position).length > 0 ) {
		var c = $('#'+ops.position).offset();
		$('#ajax_dlg').css('left',c.left);
		$('#ajax_dlg').css('top',c.top);
	} else {
		$('#ajax_dlg').css('left',($(window).width() - $('#ajax_dlg').width()) / 2);
		$('#ajax_dlg').css('top',$(window).scrollTop() + ($(window).height() - $('#ajax_dlg').height()) / 2);
	}
	var d = $('#ajax_dlg').offset();
	if ( $('#ajax_dlg').height() > 0.90 * $(window).height() - d.top ) {
		$('#ajax_content').height(0.90 * $(window).height() - d.top);
	}
	if ( ops.fade ) {
		$('#ajax_dlg').bind('mouseleave',function(e) {g_tm = setTimeout(close_ajax_dlg,ops.fade);});
		$('#ajax_dlg').bind('mouseover',function(e) {clearTimeout(g_tm);});
	}
	$('#ajax_dlg').fadeIn('slow');
}

function post_form(frm,url) {
	if ( false != verify_form_fields(validate_req) ) {
		$.post(url,$(frm).serialize(),function(html) { $('#ajax_content').html(html+'<div style="text-align: center; padding: 10px"><input type="button" onclick="$(\'#ajax_dlg\').fadeOut(\'slow\',function() {$(\'#ajax_dlg\').remove();})" value="Close" />'); });
	}
	return false;
}

function verify_form_fields(flds) {
	var success = true;
	for ( i = 0; i < flds.length; i++ ) {
		var f = $('#'+flds[i]);
		if ( f.length && f.val() == '' ) {
			f.css('background-color','Yellow');
			if ( success ) f.focus();
		       	success = false;
		} else if ( f ) f.css('background-color','White');
	}
	if ( !success ) alert("Please provide values for mandatory fields.");
	return success;
}

$(function() {
	$('.qmenu').bind('mouseover',function(e) {
		 $.get('/srv/json.php/QUICK_MENU_AJAX',function(html) {display_ajax_dlg(html,{title:'Quick Menu',position:'mouse',fade:800,pageX:e.pageX,pageY:e.pageY});})
	});
});

