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


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 focus_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 focus_ch_create(mi)
{
	if (mi.is_header) {
		var wdg = focus_get_wdg(mi.wdg);
		wdg.left.style.cursor = 'pointer';
		wdg.center.style.cursor = 'pointer';
		wdg.right.style.cursor = 'pointer';
	}
}

function focus_ch_mouseover(mi)
{
	if (document.page_loaded && mi.is_header) {
		var wdg = focus_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 && obj.tagName != 'BODY') {
			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;
		}

		if (mi.selection == 0) {
			wdg.left.className = 'cmenu_hd_normhover_left';
			wdg.center.className = 'cmenu_hd_normhover_body';
			wdg.right.className = 'cmenu_hd_normhover_right';
		}
		else {
		}
	}
}

function focus_menu_close(mi)
{
	if (document.page_loaded) {
		var wdg = focus_get_wdg(mi.wdg);

		if (mi.selection == 0) {
			wdg.left.className = 'cmenu_hd_norm_left';
			wdg.center.className = 'cmenu_hd_norm_body';
			wdg.right.className = 'cmenu_hd_norm_right';
		}
		else {
		}
	}
}

function focus_ch_mouseout(mi)
{
	if (mi.is_header && mi.menuitems.length == 0)
		focus_menu_close(mi);
}

function focus_ch_panel_hide(mi)
{
	if (mi.is_header)
		focus_menu_close(mi);
}


// window handlers

function focus_h_scroll()
{
	if (document['focusSearch'])
		document['focusSearch'].set_position();

	if (document['focusTabs'])
		document['focusTabs'].set_position();
}
addEvent(window, 'scroll', focus_h_scroll);

window_w = 0;
window_h = 0;
function focus_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['focusSearch'])
			document['focusSearch'].set_position();
	
		if (document['focusTabs'])
			document['focusTabs'].set_position();
	}
}
addEvent(window, 'resize', focus_h_resize);

function search_click()
{
	if (document.page_loaded) {
		if (!document['focusSearch'])
			document['focusSearch'] = new FocusSearch();

		document['focusSearch'].h_click();
	}
}


// page initialization

function page_init()
{
	document.page_loaded = true;

	document['focusTabs'] = new FocusTabs({
		iname: 'focusTabs',
		source_id: 'pos_inset',
		cell_width: 305,
		cell_height: 70,

		class_gap: 'focus_tabs_gap',
		class_tab_content: 'focus_tabs_content',

		class_tab_norm_left: 'focus_tabs_norm_left',
		class_tab_norm_body: 'focus_tabs_norm_body',
		class_tab_norm_right: 'focus_tabs_norm_right',

		class_tab_hover_left: 'focus_tabs_hover_left',
		class_tab_hover_body: 'focus_tabs_hover_body',
		class_tab_hover_right: 'focus_tabs_hover_right',

		class_tab_sel_left: 'focus_tabs_sel_left',
		class_tab_sel_body: 'focus_tabs_sel_body',
		class_tab_sel_right: 'focus_tabs_sel_right'
	});

	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: 90,

			ch_create: focus_ch_create,
			ch_mouseover: focus_ch_mouseover,
			ch_mouseout: focus_ch_mouseout,
			ch_panel_hide: focus_ch_panel_hide,

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