/**
 * Main script for the BonusThemes.com Delicious Template
 *
 * @package bt_delicious
 * @version 1.0
 * @copyright BonusThemes.com 2009
 * @link http://www.bonusthemes.com/goto/bt_delicious
 */


function bt_add_event(obj, event, func) {
	if (obj.addEventListener)
		obj.addEventListener(event, func, false);
	else if (obj.attachEvent)
		obj.attachEvent('on' + event, func);
}


// CMenu handlers

function delicious_ch_create(mi) {
	if (mi.is_header)
		mi.wdg.style.cursor = 'pointer';
}

function delicious_ch_mouseover(mi) {
	if (document.page_loaded && mi.is_header) {
		mi.panel_ch_x = 0;
		mi.panel_ch_y = mi.wdg.offsetHeight + 3;

		var obj = mi.wdg;
		mi.panel_ch_x += obj.offsetLeft;
		obj = obj.offsetParent;
		while (obj) {
			mi.panel_ch_x += obj.offsetLeft;
			mi.panel_ch_y += obj.offsetTop;
			obj = obj.offsetParent;
		}
		mi.panel_ch_min_w = mi.wdg.offsetWidth;

		if (typeof mi.td_center_class == 'undefined')
			mi.td_center_class = mi.wdg.className;

		if (mi.td_center_class == 'cmenu_hd_norm')
			mi.wdg.className = 'cmenu_hd_sel';
	}
}

function delicious_menu_close(mi) {
	if (document.page_loaded)
		if (typeof mi.td_center_class != 'undefined')
			mi.wdg.className = mi.td_center_class;
}

function delicious_ch_mouseout(mi) {
	if (mi.is_header && mi.menuitems.length == 0)
		delicious_menu_close(mi);
}

function delicious_ch_panel_hide(mi) {
	if (mi.is_header)
		delicious_menu_close(mi);
}

function delicious_h_resize_search() {
	if (document.bttpl_search_box) {
		var x = -8, y = 52, obj = document.bttpl_search_source;
		while (obj) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}

		document.bttpl_search_box.style.left = x + 'px';
		document.bttpl_search_box.style.top = y + 'px';

		document.bttpl_search_clipl.style.left = (x + 25) + 'px';
		document.bttpl_search_clipl.style.top = (y - 23) + 'px';

		document.bttpl_search_clipr.style.left = (x + 262) + 'px';
		document.bttpl_search_clipr.style.top = (y - 23) + 'px';
	}
}
bt_add_event(window, 'resize', delicious_h_resize_search);

function delicious_init_search() {
	// find search module module style
	var obj = document.getElementById('mod_search_searchword');
	if (!obj || !obj.parentNode || obj.parentNode.tagName != 'DIV' || !obj.parentNode.className || obj.parentNode.className.substr(0, 6) != 'search')
		return;

	var search_class = obj.parentNode.className;
	search_class = search_class.substr(6, search_class.length);


	// find source
	var node = document.getElementById('pos_search_box');
	if (!node)
		return;

	var content_node = null;
	node = node.firstChild;
	while (node) {
		if (node.nodeType == 1) {
			// if the node is a regular DOM element, handle it as a data source

			var header = node.firstChild;
			while (header && header.nodeType != 1)
				header = header.nextSibling;

			if (header && header.tagName == 'H3')
				node.removeChild(header);

			content_node = node;
			break;
		}
		else {
			// move to the next data block
			node = node.nextSibling;
		}
	}
	if (content_node == null)
		return;


	// create and position new module
	document.bttpl_search_source = obj.parentNode;

	document.bttpl_search_box = document.createElement('div');
	document.getElementsByTagName('body')[0].appendChild(document.bttpl_search_box);
	document.bttpl_search_box.style.position = 'absolute';
	document.bttpl_search_box.innerHTML = '\
<table cellspacing="0" cellpadding="0" width="299">\
	<tr>\
		<td width="32" valign="top"><img src="' + document.bttpl_path + '/images/layout/' + search_class + '_box_tl.gif" width="32" height="49" /></td>\
		<td class="' + search_class + '_box_tc"></td>\
		<td width="32" valign="top"><img src="' + document.bttpl_path + '/images/layout/' + search_class + '_box_tr.gif" width="32" height="49" /></td>\
	</tr>\
	<tr>\
		<td width="32" class="' + search_class + '_box_ml"></td>\
		<td class="' + search_class + '_box_mc">\
' + content_node.innerHTML + '\
		</td>\
		<td width="32" class="' + search_class + '_box_mr"></td>\
	</tr>\
	<tr>\
		<td colspan="3"><img src="' + document.bttpl_path + '/images/layout/' + search_class + '_box_bottom.gif" width="299" height="112" /></td>\
	</tr>\
</table>\
';

	document.bttpl_search_clipl = document.createElement('div');
	document.getElementsByTagName('body')[0].appendChild(document.bttpl_search_clipl);
	document.bttpl_search_clipl.className = search_class + '_clip';
	document.bttpl_search_clipl.style.position = 'absolute';

	document.bttpl_search_clipr = document.createElement('div');
	document.getElementsByTagName('body')[0].appendChild(document.bttpl_search_clipr);
	document.bttpl_search_clipr.className = search_class + '_clip';
	document.bttpl_search_clipr.style.position = 'absolute';

	delicious_h_resize_search();
}

// page initialization

function delicious_page_init() {
	document.page_loaded = true;

	delicious_init_search();

	if (document['cmenu_items']) {
		document['mc'] = new CMenu({
			iname: 'mc',

			edge_min: 'menuarea',

			side: 'down',
			orientation: 'right',

			animate: true,
			anim_step: 10,
			anim_interval: 30,

			transparency: true,
			transparency_ie6: false,

			min_opacity: 0,
			max_opacity: 100,

			ch_create: delicious_ch_create,
			ch_mouseover: delicious_ch_mouseover,
			ch_mouseout: delicious_ch_mouseout,
			ch_panel_hide: delicious_ch_panel_hide,

			panel_offset1_x: 0,
			panel_offset1_y: 0,
			panel_offset2_x: 7,
			panel_offset2_y: 0,

			itemborder_class: 'cmenu_itemborder',
			separator_class: 'cmenu_separator',
			panel_class: 'cmenu_panel',
			panel_tl_class: 'cmenu_tl',
			panel_tc_class: 'cmenu_tc',
			panel_tr_class: 'cmenu_tr',
			panel_ml_class: 'cmenu_ml',
			panel_mr_class: 'cmenu_mr',
			panel_bl_class: 'cmenu_bl',
			panel_bc_class: 'cmenu_bc',
			panel_br_class: 'cmenu_br',

			mi_empty_normal_class: 'cmenu_mi_empty_normal',
			mi_empty_hover_class: 'cmenu_mi_empty_hover',
			mi_full_normal_class: 'cmenu_mi_full_normal',
			mi_full_hover_class: 'cmenu_mi_full_hover'

		});

		var len = document['cmenu_items'].length;
		if (len > 0) {
			for (var i = 0; i < len; i++) {
				var item = document['cmenu_items'][i];

				if (typeof item.parent_id == 'undefined')
					this[item.id] = document['mc'].add_item(item);
				else
					this[item.id] = this[item.parent_id].add_item(item);
			}
		
			document['mc'].run();
		}
	}
}

bt_add_event(window, 'load', delicious_page_init);


var goto_top_type = -1;
var goto_top_itv = 0;

function goto_top_timer() {
	var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;
	var moveby = 15;

	y -= Math.ceil(y * moveby / 100);
	if (y < 0)
		y = 0;

	if (goto_top_type == 1)
		document.documentElement.scrollTop = y;
	else
		document.body.scrollTop = y;

	if (y == 0) {
		clearInterval(goto_top_itv);
		goto_top_itv = 0;
	}
}

function goto_top() {
	if (goto_top_itv == 0) {
		if (document.documentElement && document.documentElement.scrollTop)
			goto_top_type = 1;
		else if (document.body && document.body.scrollTop)
			goto_top_type = 2;
		else
			goto_top_type = 0;

		if (goto_top_type > 0)
			goto_top_itv = setInterval('goto_top_timer()', 50);
	}
}

