// file global.js

function get_window_size() {
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	arrayWindowSize = new Array(windowWidth, windowHeight);
	return(arrayWindowSize);
}

// per il momento la funzione non è utilizzata
function adjust_frame_position() {
	arrayWindowSize = get_window_size();

	win_left = (arrayWindowSize[0] - 980) / 2;
	if (win_left < 0) win_left = 0;

	win_top = (arrayWindowSize[1] - 600) / 2;
	if (win_top < 0) win_top = 0;

	$('main_cnt').setStyle({top: (win_top + 0) + 'px'});
	$('main_cnt').setStyle({left: (win_left + 0) + 'px'});

	$('main_cnt').show();
}

var num_links;
var aLinks = new Array();

function body_onload(go_categoria) {
	//adjust_frame_position();

	//window.onresize = function() {
	//	adjust_frame_position();
	//}

	for(var i = 1; i <= num_links; i++) {
		aLinks[i] = new Array();
		aLinks[i]['playing'] = 0;
		aLinks[i]['state'] = 'closed';
	}

	var button_width = 0;
	var button_id = '';
	$$("a.button_width").each( function(button) {
		button_width = button.getWidth();
		button_id = button.identify();

		$(button_id + '_c_t').setStyle({width: (button_width - 46) + 'px'});
		$(button_id + '_c_b').setStyle({width: (button_width - 46) + 'px'});
	});

	if (go_categoria) {
		document.location.href = '#' + go_categoria;
	}

	new Lightbox();
}

var close_link = function (effect) {
	var closing_link_id = effect.element.id.substring('submenu_'.length);
	if (closing_link_id && aLinks[closing_link_id]['playing']) {
		aLinks[closing_link_id]['state'] = 'closed';
		aLinks[closing_link_id]['playing'] = 0;
	}
}

var open_link = function (effect) {
	var opening_link_id = effect.element.id.substring('submenu_'.length);
	if (opening_link_id && aLinks[opening_link_id]['playing']) {
		aLinks[opening_link_id]['state'] = 'opened';
		aLinks[opening_link_id]['playing'] = 0;
	}
}

function close_submenu(submenu_id) {
	if (!aLinks[submenu_id]['playing'] && (aLinks[submenu_id]['state'] == 'opened') && $('submenu_' + submenu_id)) {
		aLinks[submenu_id]['playing'] = 1;
		aLinks[submenu_id]['state'] = 'closing';
		Element.removeClassName('link_' + submenu_id, 'group_opened');
		Element.addClassName('link_' + submenu_id, 'group_closed');
		new Effect.BlindUp('submenu_' + submenu_id, { duration: 0.5, afterFinish: close_link });
	}
}

function open_submenu(submenu_id) {
	var open_ok = true;
	for(var i = 1; i <= num_links; i++) {
		if (aLinks[i]['state'] == 'opening') open_ok = false;
	}
	if (open_ok && !aLinks[submenu_id]['playing'] && (aLinks[submenu_id]['state'] == 'closed') && $('submenu_' + submenu_id)) {
		aLinks[submenu_id]['playing'] = 1;
		aLinks[submenu_id]['state'] = 'opening';
		Element.removeClassName('link_' + submenu_id, 'group_closed');
		Element.addClassName('link_' + submenu_id, 'group_opened');
		new Effect.BlindDown('submenu_' + submenu_id, { duration: 0.5, afterFinish: open_link });
	}
}

function close_all_submenu(submenu_id) {
	for(var i = 1; i <= num_links; i++) {
		if (!aLinks[i]['playing'] && (aLinks[i]['state'] == 'opened') && $('submenu_' + i) && (i != submenu_id)) {
			aLinks[i]['playing'] = 1;
			aLinks[i]['state'] = 'closing';
			Element.removeClassName('link_' + i, 'group_opened');
			Element.addClassName('link_' + i, 'group_closed');
			new Effect.BlindUp('submenu_' + i, { duration: 0.5, afterFinish: close_link });
		}
	}
}

function switch_submenu(submenu_id) {
	close_all_submenu();

	var open_ok = true;
	for(var i = 1; i <= num_links; i++) {
		if (aLinks[i]['state'] == 'opening') open_ok = false;
	}
	if (open_ok && !aLinks[submenu_id]['playing'] && (aLinks[submenu_id]['state'] == 'closed') && $('submenu_' + submenu_id)) {
		aLinks[submenu_id]['playing'] = 1;
		aLinks[submenu_id]['state'] = 'opening';
		Element.removeClassName('link_' + submenu_id, 'group_closed');
		Element.addClassName('link_' + submenu_id, 'group_opened');
		new Effect.BlindDown('submenu_' + submenu_id, { duration: 0.5, afterFinish: open_link });
	}
}

function toggle_info(id_menu, id_submenu) {
	if ($(id_menu) && ($(id_menu).hasClassName('section_closed'))) {
		$(id_menu).removeClassName('section_closed');
		$(id_menu).addClassName('section_opened');

//		new Effect.BlindUp(id_submenu + '_closed', { duration: 0.5 });
//		new Effect.BlindDown(id_submenu + '_opened', { duration: 0.5 });

		Element.hide(id_submenu + '_closed');
		Element.show(id_submenu + '_opened');
	}
	else if ($(id_menu) && ($(id_menu).hasClassName('section_opened'))) {
		$(id_menu).removeClassName('section_opened');
		$(id_menu).addClassName('section_closed');

//		new Effect.BlindDown(id_submenu + '_closed', { duration: 0.5 });
//		new Effect.BlindUp(id_submenu + '_opened', { duration: 0.5 });

		Element.show(id_submenu + '_closed');
		Element.hide(id_submenu + '_opened');
	}
}

function openNew(url, name, parms) {
	window.open(url, name, parms);
}

function showSWF(swf_file, div_id, width, height, bgcolor) {
	html = '';
	html += '<object' + "\n";
	html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' + "\n";
	html += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' + "\n";
	html += 'width="' + width + '"' + "\n";
	html += 'height="' + height + '"' + "\n";
	html += 'id="' + div_id + '_fileswf_id">' + "\n";
	html += '<param name="bgcolor" value="' + bgcolor + '" />' + "\n";
	html += '<param name="allowScriptAccess" value="sameDomain" />' + "\n";
	html += '<param name="movie" value="' + swf_file + '" /><param name="quality" value="high" /><embed' + "\n";
	html += 'src="' + swf_file + '"' + "\n";
	html += 'bgcolor="' + bgcolor + '"' + "\n";
	html += 'quality="high"' + "\n";
	html += 'width="' + width + '"' + "\n";
	html += 'height="' + height + '"' + "\n";
	html += 'name="' + div_id + '_fileswf_name"' + "\n";
	html += 'allowScriptAccess="sameDomain"' + "\n";
	html += 'type="application/x-shockwave-flash"' + "\n";
	html += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />' + "\n";
	html += '</object>' + "\n";

	$(div_id).update(html);
}

var revert_delay = 500;
var revert_timeout = 100;
var revert_time = 0;
var timeout_id;

function start_revert() {
	revert_time = revert_delay;
	timeout_id = window.setTimeout('do_revert()', revert_timeout);
}

function do_revert() {
	revert_time = revert_time - revert_timeout;

	if (revert_time < 0) {
		close_all_submenu();
	}
	else {
		timeout_id = window.setTimeout('do_revert()', revert_timeout);
	}
}

function stop_revert() {
	if (timeout_id) window.clearTimeout(timeout_id);
}