var preload_imgs = new Array();
var hide_menu_cols;
var show_menu_cols;

function init() {
	// preload images
	var imgs = [ 'img/menu1_open.gif' , '/img/menu1_close.gif' , 'img/menu1_empty.gif' , '/img/menu2_open.gif' , 'img/menu2_close.gif' , 'img/menu2_empty.gif' , 'img/menu_top_left.gif' , 'img/menu_top_right.gif' , 'img/menu_bottom_left.gif' , 'img/menu_bottom_right.gif' ];
	for( e in imgs ) {
		preload_imgs[e] = new Image();
		preload_imgs[e].src = imgs[e];
	}
	// load menu vars
	var framset_menu = parent.window.document.getElementById('framset_menu');
	hide_menu_cols = framset_menu.getAttribute('hide_menu_width')+',*';
	show_menu_cols = framset_menu.getAttribute('show_menu_width')+',*';
	// check if menu is hidden
	var framset_menu = parent.window.document.getElementById('framset_menu');
	if( framset_menu.cols == hide_menu_cols ) {
		menu_toggle();
	}
}

function menu_restore() {
	// restore menu status, check in cookies
	menu_ss_restore();
}

////////////////////////////////////////
// menu items open/close
////////////////////////////////////////

// show specified menu item
// (open all parent items so that we can see it)
function show_menu_item( id ) {
	var id;
	var el;
	var open = new Array();
	el = document.getElementById(id);
	while( el.parentNode ){
		el = el.parentNode;
		if( el.className && el.className.indexOf('_sub') != -1 ) {
			// add menu el to open
			open.push( el.previousSibling );
		}
	}
	// open all menu in rever order
	// this is a workaround for a bug in IE, avoid a glitch in the rendering
	open.reverse();
	for( e in open ) {
		menu_item_toggle( open[e] );
	}
}

// show specified menu
function show_menu( id ) {
	var id;
	var el;
	el = document.getElementById(id);
	if( el ) {
		menu_item_toggle( el );
	}
}

// open/close a menu, (item onclick handler)
function menu_item_toggle( el ) {
	var el;
	// toggle sub menu
	toggle( el.nextSibling );
	// save status in session
	menu_ss_save( el );
	// switch images in el
	var imgs = el.getElementsByTagName('img');
	for( i=0 ; i<imgs.length; i++ ) {
		imgs[i].src = (imgs[i].src.indexOf('_open') == -1) ? imgs[i].src.replace('_close','_open') : imgs[i].src.replace('_open','_close');
	}
	// redraw all images
	// this is a workaround for IE that does not place img at the correct position
	btn_redraw();
}

function btn_redraw() {
	var el;
	for( i=0; i<document.images.length; i++ ){
		el = document.images[i];
		if( el.className == 'btn' ) {
			el.style.visibility = 'hidden';
			el.style.visibility = 'visible';
		}
	}
}

////////////////////////////////////////
// hide the whole menu to the left
////////////////////////////////////////

function menu_toggle( tab_el ) {
	var tab_el;
	var el = parent.window.document.getElementById('framset_menu');
	// toggle frame
	if( el.cols != hide_menu_cols ) {
		// create close tab
		var tab = document.createElement('img');
		tab.id = 'menu_tab_open';
		tab.src = tab_el.src.replace('_close','_open');
		tab.style.position = 'absolute';
		tab.style.left = '0px';
		tab.style.top = '20px';
		tab.style.cursor = 'pointer';
		tab.onclick = menu_toggle;
		document.body.appendChild( tab );
		// resize down
		el.cols = hide_menu_cols;
	} else {
		// remove tab img
		if( document.getElementById('menu_tab_open') ) document.body.removeChild( document.getElementById('menu_tab_open') );
		// resize up
		el.cols = show_menu_cols;
	}
}

////////////////////////////////////////
// menu session
////////////////////////////////////////

// restore menu status from menu session
function menu_ss_restore() {
	var ids = menu_ss_ids();
	var el;
	for( e in ids ) {
		//alert(ids[e]);		
		// make sure id is valid
		if( ! ids[e] || ! document.getElementById(ids[e]) ) continue;
		// toggle menu item
		el = document.getElementById(ids[e]);
		menu_item_toggle(el);
	}
}

// save menu status in menu session
function menu_ss_save( el ) {
	var el, is_open, ids, ids_str, key;
	// check menu status
	is_open = el.nextSibling.style.display == 'block' ? true : false;
	// get session ids
	ids = menu_ss_ids();
	// save to cookies, or remove
	if( is_open ) {
		// save
		if( ! in_array( el.id , ids ) ) {
			ids.push( el.id );
		}
		// cleanup, remove empty items
		for( e in ids ) {
			if( ! ids[e] ) ids.splice(e,1);
		}
		// create ids string to store in cookie
		ids_str = ids.join(',');
		// save cookie
		createCookie( 'menu_items' , ids_str );
	} else {
		// delete
		if( in_array( el.id , ids ) ) {
			key = array_key( el.id , ids );
			ids.splice( key , 1 );
		}
		ids_str = ids.join(',');
		createCookie( 'menu_items' , ids_str );
	}
}

// returns  an array of all ids store menu session
function menu_ss_ids() {
	var ids_str;
	var ids = new Array();
	// get list of id (string from cookie)
	ids_str = readCookie('menu_items');
	// return array of ids
	if( ids_str )
		return ids_str.split(',');
	else
		return new Array();
}

////////////////////////////////////////
// utils
////////////////////////////////////////

// toggle an element on and off
function toggle( el ) {
	var el;
	el.style.display = el.style.display != "block" ? 'block' : 'none'; 
}

// alert each element of an array
function alertr( myvar , max ) {
	var myvar, max;
	if( ! max ) max = 20;
	i=0;
	for( e in myvar ) {
		alert(e);
		if( i > max ) break;
		i++;
	}
}

function in_array( $value , $arr ) {
	var $value, $arr;
	for( e in $arr ) {
		if ( $arr[e] == $value ) return true;
	}
	return false;
}

function array_key( $value , $arr ) {
	var $value, $arr;
	for( e in $arr ) {
		if ( $arr[e] == $value ) return e;
	}
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

