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


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

function solidarity_ch_mouseover(mi) {
	if (document.page_loaded && mi.is_header) {
		var wdg = solidarity_get_wdg(mi.wdg);

		mi.panel_ch_x = -wdg.left.offsetWidth;
		mi.panel_ch_y = mi.wdg.offsetHeight;

		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 = 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.td_center_class == 'cmenu_hd_norm_1_c') {
			wdg.left.className = 'cmenu_hd_hover_1_l';
			wdg.center.className = 'cmenu_hd_hover_1_c';
			wdg.right.className = 'cmenu_hd_hover_1_r';
		}
		else if (mi.td_center_class == 'cmenu_hd_norm_2_c') {
			wdg.left.className = 'cmenu_hd_hover_2_l';
			wdg.center.className = 'cmenu_hd_hover_2_c';
			wdg.right.className = 'cmenu_hd_hover_2_r';
		}
		else if (mi.td_center_class == 'cmenu_hd_norm_3_c') {
			wdg.left.className = 'cmenu_hd_hover_3_l';
			wdg.center.className = 'cmenu_hd_hover_3_c';
			wdg.right.className = 'cmenu_hd_hover_3_r';
		}
		else if (mi.td_center_class == 'cmenu_hd_norm_4_c') {
			wdg.left.className = 'cmenu_hd_hover_4_l';
			wdg.center.className = 'cmenu_hd_hover_4_c';
			wdg.right.className = 'cmenu_hd_hover_4_r';
		}
	}
}

function solidarity_menu_close(mi) {
	if (document.page_loaded) {
		var wdg = solidarity_get_wdg(mi.wdg);
		if (typeof mi.td_center_class != 'undefined') {
			wdg.left.className = mi.td_left_class;
			wdg.center.className = mi.td_center_class;
			wdg.right.className = mi.td_right_class;
		}
	}
}

function solidarity_ch_mouseout(mi) {
	if (mi.is_header && mi.menuitems.length == 0)
		solidarity_menu_close(mi);
}

function solidarity_ch_panel_hide(mi) {
	if (mi.is_header)
		solidarity_menu_close(mi);
}

function solidarity_h_resize_search() {
	if (document.bttpl_inset) {
		var x = 0, y = 0, obj = document.bttpl_display;
		while (obj) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}

		document.bttpl_inset.style.left = (x + document.x_padding) + 'px';
		document.bttpl_inset.style.top = (y + document.bttpl_display.offsetHeight - document.bttpl_inset.offsetHeight - document.y_padding) + 'px';

		document.bttpl_back.style.left = x + 'px';
		document.bttpl_back.style.top = (y + document.bttpl_display.offsetHeight - document.bttpl_inset.offsetHeight - 2 * document.y_padding) + 'px';
	}
}
bt_add_event(window, 'resize', solidarity_h_resize_search);


// page initialization

function solidarity_page_init() {
	document.page_loaded = true;

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

			edge_min: 'r1_wrapper',

			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: solidarity_ch_create,
			ch_mouseover: solidarity_ch_mouseover,
			ch_mouseout: solidarity_ch_mouseout,
			ch_panel_hide: solidarity_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();
		}
	}

	document.bttpl_inset = document.getElementById('inset_container');
	if (document.bttpl_inset) {
		document.bttpl_display = document.getElementById('pos_display');

		document.x_padding = 15;
		document.y_padding = 5;
		var opacity = 50;
		document.bttpl_back = document.createElement('div');
		document.getElementsByTagName('body')[0].appendChild(document.bttpl_back);

		document.getElementsByTagName('body')[0].appendChild(document.bttpl_inset);
		document.bttpl_inset.style.position = 'absolute';
		document.bttpl_inset.style.width = (document.bttpl_display.offsetWidth - 2 * document.x_padding) + 'px';
		document.bttpl_inset.style.visibility = 'hidden';
		document.bttpl_inset.style.display = 'block';
		document.bttpl_inset.style.visibility = 'visible';

		document.bttpl_back.className = 'inset_back';
		document.bttpl_back.style.position = 'absolute';
		document.bttpl_back.style.width = document.bttpl_display.offsetWidth + 'px';
		document.bttpl_back.style.height = (document.bttpl_inset.offsetHeight + 2 * document.y_padding) + 'px';
		document.bttpl_back.style.opacity = opacity / 100;
		document.bttpl_back.style.filter = 'alpha(opacity=' + opacity + ')';

		solidarity_h_resize_search();
	}
}

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

