var fade_index = 0;

var ContextMenus = new Array();
var this_page_id = '';

function AddContextMenuItem(id,icon,href,caption){
	x = new Array();
	x["icon"] = icon;
	x["href"] = href;
	x["caption"] = caption;

	if (!xDef(ContextMenus[id])) ContextMenus[id] = new Array();
	r = ContextMenus[id];
	r[r.length] = x;
}

function AddContextMenuSeparator(id){
	if (!xDef(ContextMenus[id])) ContextMenus[id] = new Array();
	r = ContextMenus[id];
	r[r.length] = 'separator';
}


var __context_menu_delayed = null;
function ShowContextMenuDelayed(e,id){
	ShowContextMenu(e,id);
	//if (__context_menu_delayed != null) clearTimeout(__context_menu_delayed);
	//__context_menu_delayed = setTimeout('ShowContextMenu(\''+e+'\',\''+id+'\');',1000);
}
function HideContextMenuDelayed(){
	//if (__context_menu_delayed != null) clearTimeout(__context_menu_delayed);
	HideContextMenu();
}




var showingContextMenu = false;

function ShowContextMenu(e, id){

	UnTip(); // This is because we are using wz_tooltip.js
	
	a = xGetElementsByClassName('innerTileSelected');
	var i =0;
	var targetid = 'g'+id;
	for (i=0;i<a.length;i++)
	{
		if (a[i].id==targetid) break;
	}
	if (a.length>1 && i<a.length)
	{
		m = ContextMenus['massAction'];
	}
	else
	{
		m = ContextMenus[id];
	}
	
	s = '<div class="border"><ul>';
	for (i = 0; i < m.length; i++){
		mm = m[i];
		if (mm == 'separator')
			s += '</ul><hr /><ul>';
		else
			s += '<li><a href="'+mm['href']+'" title="'+mm['caption']+'"><img src="'+mm['icon']+'" width="16" height="16" alt="'+mm['caption']+'" />'+mm['caption']+'</a></li>';
	}
	s += '</ul></div>';
	

	xInnerHtml ( 'contextMenu' , s );
	xMoveTo( 'contextMenuContainer', xPageX(e)+xWidth(e)/2, xPageY(e)+xHeight(e)/2 );

	if (xIE4Up) fnLoadPngs();
	
	showingContextMenu = true;
	setTimeout('xShow(\'contextMenuContainer\');showingContextMenu=false;',100);
}

function ShowContextMenuAt(id,x,y)
{
	UnTip(); // This is because we are using wz_tooltip.js
	
	m = ContextMenus[id];
		
	s = '<div class="border"><ul>';
	for (i = 0; i < m.length; i++){
		mm = m[i];
		if (mm == 'separator')
			s += '</ul><hr /><ul>';
		else
			s += '<li><a href="'+mm['href']+'" title="'+mm['caption']+'"><img src="'+mm['icon']+'" width="16" height="16" alt="'+mm['caption']+'" />'+mm['caption']+'</a></li>';
	}
	s += '</ul></div>';
	

	xInnerHtml ( 'contextMenu' , s );
	xMoveTo( 'contextMenuContainer', x , y );

	if (xIE4Up) fnLoadPngs();
	
	showingContextMenu = true;
	setTimeout('xShow(\'contextMenuContainer\');showingContextMenu=false;',100);
}

//document.onclick = HideContextMenu;

//document.ondblclick = HideContextMenu;

function HideContextMenu(){
		
	if (!xIE4Up) xHide( 'contextMenuContainer' );
	else {
		if (!showingContextMenu){
			
			xHide( 'contextMenuContainer' );
			xInnerHtml ( 'contextMenu' , '' );
		}
	}
}



function fadeIn(e) {
	if(!(e=xGetElementById(e))) return null;
	xShow(e);
	
	if (e.filters)
		e.filters.alpha.opacity = fade_index;		
	else  
		e.style.MozOpacity = fade_index / 100; 
	
	fade_index += 20;
	alert(fade_index);
	goIn = setTimeout("fadeIn(e)", 5);
	
	if(fade_index >= 100) {
		clearTimeout(goIn);
		fade_index = 0;
	}
}

function updateselect(e,list){

	if(!(e=xGetElementById(e))) return null;
		
	e.options.length=0;

    for (i=0; i<list.length; i++)
		e.options[e.options.length] = new Option(list[i].split("|")[0], list[i].split("|")[1]);
}

function checkRegion(e, startCheck, endCheck){
	i = startCheck;
	while (i <= endCheck){
		t = e+i;
		
		if(!(t=xGetElementById(t))) return null;
		
		t.checked = true;
		i++;
	}
	
}

function uncheckRegion2(e, startCheck, endCheck){
	i = startCheck;
	while (i <= endCheck){
		t = e+i;
		
		if(!(t=xGetElementById(t))) return null;
		
		t.checked = false;
		i++;
	}
	
}

function uncheckRegion(e,uncheck){ 
	if(!(e=xGetElementById(e))) return null;
	
	e.checked = false;
}


function calculateChars(e1,e2,maxlength){
	if(!(e1=xGetElementById(e1))) return null;
	if(!(e2=xGetElementById(e2))) return null;
	leftlength = maxlength-e1.value.length;
	if (leftlength > -1)
		e2.innerHTML = leftlength;
	if (leftlength  <= 0) {
		e1.value = e1.value.substring(0,maxlength);
	  e2.innerHTML = "0";
	}
       
   
}


