popdoc_source = '<div id="popdoc_source" class="popdoc popmenu_form" style="top:0px; left:0px; width:0px; height:0px; display:block;"><table class="popdoc"><tr onselectstart="return false;" onmousedown="if (bz_is_ff){ event.preventDefault(); }" onmouseup="if (bz_is_ff){ event.preventDefault(); }" class="popdoc_header"><td class="corn_tl"><img src="'+lib_location+'images/pix.gif" /></td><td class="handler bord_t">';
popdoc_source += '<a id="popdoc_source_close" class="close" href="javascript:void(0);" >';
popdoc_source += '<img src="';
popdoc_source += lib_location;
popdoc_source += 'images/pix.gif" alt="close" /></a>';
popdoc_source += '<b id="popdoc_source_title">&nbsp;</b>';
popdoc_source += '</td><td class="corn_tr"><img src="'+lib_location+'images/pix.gif" /></td></tr><tr><td class="popdoc_content" colspan="3"><div class="popdoc_content_border"><div class="popdoc_margin" id="popdoc_source_content">';
popdoc_source += '</div></div></td></tr><tr class="footer"><td class="corn_bl"><img src="'+lib_location+'images/pix.gif" /></td><td class="bord_b"></td><td class="corn_br"><img src="'+lib_location+'images/pix.gif" /></td></tr></table></div>';

popdoc_source_loading = "<div class='popdoc_loading'>Loading...</div>";

popdoc_counter = 0;

popdocs = new Array();

$(document).ready(function () {
	
	// Create popdoc park, to save all popdocs and mantain order (z index)
	$('body').append('<div id="popdoc_park"></div>');
	
	// Set opener on links
	$('.popdoc').click(function () {
		popdoc_create();
	});
	
	setTimeout('popdoc_create()',1000);
	
});

function popdoc_create() {
	popdoc_counter++;
	$('#popdoc_park').append(popdoc_source);
	var id = 'popdoc_' + popdoc_counter;
	$('#popdoc_source').attr('id',id);
	
	popdocs[id] = new Array;
	popdocs[id]['width'] = 300;
	popdocs[id]['height'] = 350;
	
	$('#' + id + ' .handler').mousedown(function () {
		popdoc_dragdrop_press(id);
	});
	
	$('#' + id).mouseup(function () {
		popdoc_focus(id);
	});
	
	$('#' + id + ' .close').click(function () {
		popdoc_close(id);
	});
	
	popdoc_open(id);	
}

function popdoc_open(id) {
	$('#'+id).animate({
		width:300,
		height:350
	},300,function () {
		
	});
}

function popdoc_close(id){

	if (typeof(popdocs[id])!="undefined" && typeof(popdocs[id]["timeout"])!="undefined"){
		window.clearTimeout(popdocs[id]["timeout"]);
	}

	if (typeof(popdocs[id])!="undefined" && typeof(popdocs[id]["interval"])!="undefined"){
		window.clearInterval(popdocs[id]["interval"]);
	}

	$('#' + id).animate({
		width:'0',
		height:'0'
	},300,function () {
		$(this).remove();
		popdocs[id] = null;
	});

}

//__________________________________________________ DRAG DROP
popdoc_dragdrop_diff_x = false;
popdoc_dragdrop_diff_y = false;

// When press handler
function popdoc_dragdrop_press(id) {
	popdoc_focus(id);
	
	if (typeof(popdocs[id])!=undefined && typeof(popdocs[id]["interval"])!=undefined){
		window.clearInterval(popdocs[id]["interval"]);
	}
	
	$(document).bind('mousemove.popdoc',popdoc_dragdrop_mousemove);
	$(document).bind('mouseup.popdoc',popdoc_dragdrop_release);
	
}

// When draging
function popdoc_dragdrop_mousemove (e) {

	var mouse_x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+window.parent.document.body.scrollLeft;
	var mouse_y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+window.parent.document.body.scrollTop;
	
	// Save the position of the click on the handler
	if (!popdoc_dragdrop_diff_x){
		popdoc_position = $('#' + popdoc_current_focus_id).position();
		if (popdoc_position!=null) {
			popdoc_dragdrop_diff_x = mouse_x-popdoc_position.left;
		}
	}

	if (!popdoc_dragdrop_diff_y){
		popdoc_position = (popdoc_position!=undefined) ? popdoc_position : $('#' + popdoc_current_focus_id).position();
		if (popdoc_position!=null) {
			popdoc_dragdrop_diff_y = mouse_y-popdoc_position.top;
		}
	}

	x = (mouse_x-popdoc_dragdrop_diff_x);
	y = (mouse_y-popdoc_dragdrop_diff_y);
	
	x = (x<0) ? 0 : x;
	y = (y<0) ? 0 : y;
	
	$('#' + popdoc_current_focus_id).css('left',x);
	$('#' + popdoc_current_focus_id).css('top',y);
}

//When stop draging
function popdoc_dragdrop_release () {
	
	popdoc_dragdrop_diff_x = false;
	popdoc_dragdrop_diff_y = false;

	var popdoc = $('#' + popdoc_current_focus_id);
	var popdoc_x = $(popdoc).position().left;
	var popdoc_y = $(popdoc).position().top;
	popdoc_dragdrop_diff_x = false;
	popdoc_dragdrop_diff_y = false;

	popdoc_x = eval(popdoc_x)+eval(popdocs[popdoc_current_focus_id]["width"]/2);
	popdoc_y = eval(popdoc_y)+eval(popdocs[popdoc_current_focus_id]["height"]/2);
	
	popdocs[popdoc_current_focus_id]["percent_x"] = Math.round((popdoc_x/$(window).width())*100); 
	popdocs[popdoc_current_focus_id]["percent_y"] = Math.round((popdoc_y/$(window).height())*100);
	//$('body').append('<br/>' + popdocs[popdoc_current_focus_id]["percent_y"]);
	popdocs[popdoc_current_focus_id]["pos_x"] = popdoc_x;
	popdocs[popdoc_current_focus_id]["pos_y"] = popdoc_y;
	
	$(document).unbind('mousemove.popdoc',popdoc_dragdrop_mousemove);
	$(document).unbind('mouseup.popdoc',popdoc_dragdrop_release);
	
}

// __________________________________________________ FOCUS

popdoc_current_focus_id = null;

function popdoc_focus(id){
	if (id!=null && popdoc_current_focus_id!=id){
		popdoc_current_focus_id = id;
		popdocs[id]["timeout"] = window.setTimeout('popdoc_focus_timeout("' + id + '")',100);
	}
}

function popdoc_focus_timeout(id){

	var popdoc = document.getElementById(id);
	var park = document.getElementById('popdoc_park');
	
	focused = park.lastchild;

	if ($(focused).attr('id')!=id){
		park.appendChild(popdoc);
	}
}
