/**
 * Main script for the BonusThemes.com TeraHost Template
 *
 * @package bt_terahost
 * @version 1.0
 * @copyright BonusThemes.com 2008
 * @link http://www.bonusthemes.com/goto/bt_terahost
 */


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


// CMenu handlers

function terahost_get_wdg(wdg_center)
{
	return {
		left: wdg_center.previousSibling.nodeType == 1 ? wdg_center.previousSibling : wdg_center.previousSibling.previousSibling,
		center: wdg_center,
		right: wdg_center.nextSibling.nodeType == 1 ? wdg_center.nextSibling : wdg_center.nextSibling.nextSibling
	}
}

function terahost_ch_create(mi)
{
	if (mi.is_header) {
		var wdg = terahost_get_wdg(mi.wdg);
		wdg.left.style.cursor = 'pointer';
		wdg.center.style.cursor = 'pointer';
		wdg.right.style.cursor = 'pointer';
	}
}

function terahost_ch_mouseover(mi)
{
	if (document.page_loaded && mi.is_header) {
		var wdg = terahost_get_wdg(mi.wdg);

		mi.panel_ch_x = 0;
		mi.panel_ch_y = wdg.center.offsetHeight;

		var obj = wdg.left;
		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 = wdg.left.offsetWidth + wdg.center.offsetWidth + wdg.right.offsetWidth;

		if (typeof mi.td_center_class == 'undefined') {
			mi.td_left_class = wdg.left.className;
			mi.td_center_class = wdg.center.className;
			mi.td_right_class = wdg.right.className;
		}

		wdg.left.className = 'cmenu_hd_hover_left';
		wdg.center.className = 'cmenu_hd_hover_body';
		wdg.right.className = mi.td_right_class == 'cmenu_hd_norm_menu' || mi.td_right_class == 'cmenu_hd_sel_menu' ? 'cmenu_hd_hover_menu' : 'cmenu_hd_hover_right';
	}
}

function terahost_menu_close(mi)
{
	if (document.page_loaded) {
		var wdg = terahost_get_wdg(mi.wdg);
		wdg.left.className = mi.td_left_class;
		wdg.center.className = mi.td_center_class;
		wdg.right.className = mi.td_right_class;
	}
}

function terahost_ch_mouseout(mi)
{
	if (mi.is_header && mi.menuitems.length == 0)
		terahost_menu_close(mi);
}

function terahost_ch_panel_hide(mi)
{
	if (mi.is_header)
		terahost_menu_close(mi);
}


// window handlers

window_w = 0;
window_h = 0;
function terahost_h_resize()
{
	var w = 0, h = 0;
	if (window.innerWidth) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientWidth) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	if (window_w != w || window_h != h) {
		window_w = w;
		window_h = h;

		if (document['terahostDdp1'])
			document['terahostDdp1'].set_position();
	}
}
addEvent(window, 'resize', terahost_h_resize);


// page initialization

function page_init()
{
	document.page_loaded = true;

	document['terahostDdp1'] = new DDPanel({
		iname: 'terahostDdp1',
		source_id: 'pos_ddp1',
		interval: 20,
		speed: 10,
		offset_id: 'r4_r5_r6_edge_l',
		offset_tag_align: 'right',
		offset_tag_valign: 'top',
		offset_tag_x: 40,
		offset_cnt_align: 'right',
		offset_cnt_x: 30,
		offset_y: -19,
		class_cnt_tl: 'ddp1_tl',
		class_cnt_tc: 'ddp1_tc',
		class_cnt_tr: 'ddp1_tr',
		class_cnt_ml: 'ddp1_ml',
		class_cnt_mc: 'ddp1_content',
		class_cnt_mr: 'ddp1_mr',
		class_cnt_bl: 'ddp1_bl',
		class_cnt_bc: 'ddp1_bc',
		class_cnt_br: 'ddp1_br',
		class_tag_l: 'ddp1_tag_l',
		class_tag_c: 'ddp1_tag',
		class_tag_r: 'ddp1_tag_r'
	});

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

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

			animate: true,
			anim_step: 10,
			anim_interval: 40,

			transparency: true,
			transparency_ie6: false,

			min_opacity: 0,
			max_opacity: 100,

			ch_create: terahost_ch_create,
			ch_mouseover: terahost_ch_mouseover,
			ch_mouseout: terahost_ch_mouseout,
			ch_panel_hide: terahost_ch_panel_hide,

			panel_offset1_x: 0,
			panel_offset1_y: 0,
			panel_offset2_x: 0,
			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();
		}
	}
}

addEvent(window, 'load', 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);
	}
}
