// Bubble-Bobble. last-update: 2005.06.30.
	// (c) snow-materia "http://sm.useyan.com/"
	// (*) require DaD.js
	// This script create new element-layer that you can Drag and Drop (D&D).


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	Setting
var B_B = {
	// class-name to get link elements
	trigger_class : 'bubble',

	// class-name to set new layer
	box_class : 'bobble',	// box
	bar_class : 'bobble-bar',	// bar
	content_class : 'bobble-content',	// content

	// margin
	box_margin_x : 20,	// box margin x at create box [px]
	box_margin_y : 20,	// box margin y at create box [px]
	plus_margin_x : 12,	// plus box margin x by elements-number
	plus_margin_y : 5,	// plus box margin y by elements-number
	init_margin : 10	// initialize plus box margin by elements-number
};


// Bubble set
function Bubble (e, obj, src, title) {
	// initialize
	if (!obj) return null;
	if (!src) src = obj.getAttribute('href') || obj.getAttribute('src') || null;
	if (!src) return null;
	if (!title) title =
		obj.getAttribute('title') || obj.firstChild && obj.firstChild.nodeValue || src;

	// trigger
	this.trigger = obj;

	// document
	var d = document;
	var body = d.body;

	// box
	var box = d.createElement('DL');
	box.className = B_B['box_class'];
	body.appendChild(box);

	// (x, y) coordinates of event-cursor.
	var xy = getEventPosition(e);
	var x = xy['page_x'] || xy['scroll_x'] || 0;
	var y = xy['page_y'] || xy['scroll_y'] || 0;

	// previous B-B elements length
	var b_b_e_leng = Bubble.list.length;
	if (B_B['init_margin']) b_b_e_leng %= B_B['init_margin'];

	// base box-style
	var bxs = box.style;
	bxs['position'] = 'absolute';
	bxs['top']  = (y + B_B['box_margin_y'] + B_B['plus_margin_y'] * b_b_e_leng) + 'px';
	bxs['left'] = (x + B_B['box_margin_x'] + B_B['plus_margin_x'] * b_b_e_leng) + 'px';

	// DaD-trigger
	var bar = d.createElement('DT');
	bar.className = B_B['bar_class'];
	box.appendChild(bar);
	bar.appendChild( d.createTextNode(title) );

	// new DaD-layer
	this.DaD = new DaD(box, bar);

	// DaD-content
	var content = d.createElement('DD');
	content.className = B_B['content_class'];
	box.appendChild(content);

	// image ?
	if (src.match(/(?:png|jpe?g|jpe|gif|bmp)$/i)) {
		var img = d.createElement('IMG');
		img.src = src;
		content.appendChild(img);
	} else {
		var iframe = d.createElement('IFRAME');
		iframe.src = src;
		content.appendChild(iframe);
	}

	return this;
}

// Bubble-object-list
Bubble.list = new Array();


// Bubble-Bobble start
function b_b_start (e) {
	var obj = getEventElement(this, e);
	if (!obj) return true;
	while (obj.parentNode && obj.nodeName != 'A') {
		obj = obj.parentNode;
	}
	if (obj.nodeName != 'A') return true;

	var b_b_e = Bubble.list;

	var previous = 0;
	for (var i = b_b_e.length; i--;) {
		if (b_b_e[i] && b_b_e[i].trigger == obj) { previous = b_b_e[i]; break; }
	}

	// previously ?
	if (previous) {
		previous.DaD.visible();
	}
	// new
	else {
		var title = obj.getAttribute('title') || '';
		var text = getInnerText(obj);
		text = (text) ? text +' ('+ title +')' : title;
		Bubble.list[ b_b_e.length ] = new Bubble(e, obj, obj.href, text);
	}

	// return false
	if (e.returnValue) e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
	return false;
}


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	initialize
function b_b_init () {
	// old UA ?
	if (!document.getElementById) return;

	// <A> list
	var a_list = document.getElementsByTagName('A');

	// loop
	var trigger = B_B['trigger_class'];
	for (var i = a_list.length; i--;) {
		// className == 'bubble' ?
		if (a_list[i].className != trigger || !a_list[i].href) continue;

		// set event
		d_d_addEvents('click', b_b_start, false, a_list[i]);
	}
}


// onload
if (window.DaD && window.d_d_addEvents) {
	d_d_addEvents('load', b_b_init, false, window);
}
