//
// Copyright (c) 2007 Denis Zubo | http://www.zubo.ru/
//

/*****************************
**   Hint show and hide
******************************/

var aSignItems = new Array();
var aClickables = new Array();
var aHints = new Array();
var KeepHint = new Array();
var IsLog = false;

function HintsInit() {
  var aSigns = getElementsByClassName(gebid('layout'), 'div', 'sign');
  if(aSigns.length>0){
  	for(var i=0; i < aSigns.length ; i++ ){
  		aItems = getElementsByClassName(aSigns[i], 'div', 'item');
  		if(aItems.length>0){
	  		for(var j=0; j < aItems.length ; j++ ){
		  		aSignItems.push(aItems[j]);
		  		aDivs = getElementsByClassName(aItems[j], 'div', 'clickable');
		  		if(aDivs.length>0){
		  			aClickables.push(aDivs[0]);
				  }
				  var aDivs = getElementsByClassName(aItems[j], 'dl', 'context-hint');
			  	if(aDivs.length>0){
			  		aHints.push(aDivs[0]);
			  	}
		  	}
		  }
  	}
  }
  if(aClickables.length > 0){
  	for(i=0; i < aClickables.length; i++ ){
	  	aClickables[i].onclick = CreateShowHandler(i);
	  	KeepHint[i] = false;
	  	aHints[i].onclick = CreateMakeKeepHandler(i);
		}
	}
}
///

function ShowHint(n){
	if(aClickables.length > 0){
  	for(var i=0; i < aClickables.length; i++ ){
			HideHint(i);
		}
	}
	if (IsLog) console.log('+showing', n);
	addClass(aSignItems[n], 'selected');
	MakeKeep(n);
	aClickables[n].onclick = CreateHideHandler(n);
	oLayout = gebid('layout');
	if(oLayout) {
		if (IsLog) console.log('oLayout');
		oLayout.onclick = CreateHideHandler(n);
	};
}
///

function HideHint(n){
	if (IsLog) console.log('-hiding...', n);
	if(KeepHint[n]){
		MakeNotKeep(n);
		return;
	}
	if (IsLog) console.log('-hiding', n);
	removeClass(aSignItems[n], 'selected');
	aClickables[n].onclick = CreateShowHandler(n);
	oLayout = gebid('layout');
	if(oLayout){
		if (IsLog) console.log('oLayout clear onclick');
		oLayout.onclick = "";
	}
}
///

function CreateShowHandler(n){
	if (IsLog) console.log('CreateShowHandler', n);
	return function(){
		ShowHint(n);
	}
}
///

function CreateHideHandler(n){
	if (IsLog) console.log('CreateHideHandler', n);
	return function(){
		HideHint(n);
	}
}
///

function MakeKeep(n){
	if(IsLog) console.log('making keep', n);
	KeepHint[n] = true;
}
///

function MakeNotKeep(n){
	if(IsLog) console.log('making not keep', n);
	KeepHint[n] = false;
}
///

function CreateMakeKeepHandler(n){
	if(IsLog) console.log('CreateMakeKeepHandler', n);
	return function(){
		MakeKeep(n);
	}
}
///