/* imaginatic © botons version 1.0 2004 */

// variables
var toggleCategoriesView = new Array (),
	toggleAlpha = new Array (),
	toggleCategoriesStylesView = new Array (),
	toggleCategories = new Array (),
	toggleCategoriesStyles = new Array (),
	toggleOver,
	toggleActive,
	toggleActiveOver,
	toggleStatus = new Array (),
	toggleValues = new Array (),
	targetFrame,
	typeButton,
	formName,
	formElementName,
	array_links;

// preferencias
typeButton = 'string'; // options 'image', 'string' or 'radio'
toggleCategoriesStyles[0] = 'text-decoration: none;cursor: pointer;color: #5C899F;background: white;border-left: 1px solid white;border-right: 1px solid white;border-top:1px solid white;border-bottom:1px solid #77A5BC;font-weight: 400';
toggleCategoriesStyles[1] = 'text-decoration: none;cursor: pointer;color: white;background: #77A5BC;border-left: 1px solid #77A5BC;border-right: 1px solid #77A5BC;border-top:1px solid #77A5BC;border-bottom:1px solid #77A5BC;font-weight: 400';
toggleCategoriesStyles[2] = 'text-decoration: none;color: #5C899F;cursor: default;background: white;border-left: 1px solid #77A5BC;border-right: 1px solid #77A5BC;border-top:1px solid #77A5BC;border-bottom:1px solid white;font-weight: 800';
toggleCategoriesStyles[3] = 'text-decoration: none;color: #5C899F;cursor: default;background: white;border-left: 1px solid #77A5BC;border-right: 1px solid #77A5BC;border-top:1px solid #77A5BC;border-bottom:1px solid white;font-weight: 800';
toggleCategoriesStylesView[0] = 'text-decoration: none;cursor: pointer;background: white;font-weight: 400';
toggleCategoriesStylesView[1] = 'text-decoration: none;cursor: pointer;background: white;font-weight: 800';
toggleCategoriesStylesView[2] = 'text-decoration: none;cursor: default;background: white;font-weight: 800';
toggleCategoriesStylesView[3] = 'text-decoration: none;cursor: default;background: white;font-weight: 800';
targetFrame = '';
formName = '';
formElementName = '';

// inicializacion
window.onload = imaginatic_buttons;

// funciones

/* Funcion que inicializa el programa */
function imaginatic_buttons (){
    // Anadir eventos de raton
    afegirEvents (toggleCategoriesView);
    afegirEvents (toggleCategories);
}

/* Cambiamos el aspecto del boton y, si es necesario, llamamos a la funcion que cambia el contenido de la pagina */
function change_button (thetargetid,thetype,thearray){
	currentElement = searchItL (thetargetid,thearray).thePos;
	if (thetype == 'focus' || thetype == 'mouseover'){
		if (thearray[currentElement]['status'] == false){
			changeStyle (thetargetid,thearray[currentElement]['toggleOver']);
		} else if (thearray[currentElement]['status'] == true){
			changeStyle (thetargetid,thearray[currentElement]['toggleActiveOver']);
		}
	} else if (thetype == 'blur' || thetype == 'mouseout'){
		if (thearray[currentElement]['status'] == false){
			changeStyle (thetargetid,thearray[currentElement]['toggleFirst']);
		} else if (thearray[currentElement]['status'] == true){
			changeStyle (thetargetid,thearray[currentElement]['toggleActive']);
		}
	} else if (thetype == 'click'){
		for (a = 0;a < thearray.length;a++){
			theelementname = thearray[a]['id'];
			lacapa = refL(thearray[a]['value'],"noFrames");
			if (thetargetid == thearray[a]['id']){
				thearray[a]['status'] = true;
				lacapa.style.display = "inline";
				lacapa.style.visibility = "visible";
				changeStyle (thetargetid,thearray[a]['toggleActiveOver']);
			} else {
				thearray[a]['status'] = false;
				lacapa.style.display = "none";
				lacapa.style.visibility = "hidden";
				changeStyle (theelementname,thearray[a]['toggleFirst']);
			}
		}
	}
}

/* Cambia el contenido de la pagina */
function changeContent (typeButton,thetext){
	if (thetext > ''){
		if (typeButton == 'image' || typeButton == 'string'){
			if(targetFrame == ''){
				setTimeout ('canviarcontgeneric (\''+thetext+'\')',10);
			} else {
				top.frames[targetFrame].location = thetext;
			}
		}
		if (typeButton == 'radio'){
			theformLment = document.forms[formName].elements[formElementName];
			theformLment.value = thetext;
		}
	}
}
function changeStyle (lobj,loscambios){
	elobj = refL(lobj,"noFrames");
	if (typeButton == 'image' || typeButton == 'radio'){
		elobj.src = loscambios;
	}
	if (typeButton == 'string'){
		aplica_styles (loscambios,elobj);
	}
}
/* Convierte las variables de incializacion, si pasamos una cadena como argumento, a un array. 
	Si se pasa un array como argumento analiza su longitud */
function convertToArray (thetype,thevalue,whatarray){
	var thearray;
	if (thetype == 'string'){
		thearray = new Array ();
		for (a in whatarray){
			thearray[thearray.length] = thevalue;
		}
	} else if (thetype == 'object'){
		thearray = thevalue;
		if (thevalue.length != whatarray.length){
			alert ('Different length of arrays!');
			return false;
		}
	}
	return thearray;
}

/* Anade control de los eventos a cada boton */
function afegirEvents (elsnoms){
	for (a = 0;a <  elsnoms.length;a++){
		thisname = elsnoms[a]['id'];
		theobj = refL(thisname,'noFrames');
		theobj.style.cursor = "pointer";
		if (elsnoms[a]['status']){
			if (typeButton == 'image' || typeButton == 'radio'){
				theobj.src = elsnoms[a]['toggleActive'];
			} else if (typeButton == 'string'){
				aplica_styles (elsnoms[a]['toggleActive'],theobj);
			}
		} else {
			if (typeButton == 'image' || typeButton == 'radio'){
				theobj.src = elsnoms[a]['toggleFirst'];
			} else if (typeButton == 'string'){
				aplica_styles (elsnoms[a]['toggleFirst'],theobj);
			}
		}
	}
}

/* Funciones internas */
function refL(theelment,theFrame) {
    theDoc = (theFrame == 'noFrames')? document : top.frames[theFrame].document;
    if (document.getElementById) {
	    return theDoc.getElementById(theelment);
    } else if (document.all){
	    return eval('theDoc.all.' + theelment);
    }
}

function searchItL (searchedL,theArray) {
    var isMatch = false;
    for (i = 0;i < theArray.length;i++){
        if (theArray[i]['id'] == searchedL){
            isMatch = true;
            break
        }
    }
    this.isMatch = isMatch;
    this.thePos = i;
    return this
}

function import_prefs (namePrefs){
	this.addPrefs = eval (namePrefs);
	this.addPrefs ();
}

function registerbutton (me,value,thestatus,anarray,arraystyles,toggleFirstNew,toggleOverNew,toggleActiveNew,toggleActiveOverNew){
	thelength = anarray.length;
	anarray[thelength] = new Array ();
	anarray[thelength]['id'] = me;
	anarray[thelength]['value'] = value;
	anarray[thelength]['status'] = thestatus;
	anarray[thelength]['toggleFirst'] = (toggleFirstNew > '')? toggleFirstNew : arraystyles[0];
	anarray[thelength]['toggleOver'] = (toggleOverNew > '')? toggleOverNew : arraystyles[1];
	anarray[thelength]['toggleActive'] = (toggleActiveNew > '')? toggleActiveNew : arraystyles[2];
	anarray[thelength]['toggleActiveOver'] = (toggleActiveOverNew > '')? toggleActiveOverNew : arraystyles[3];
}

/* funcions afegides per no carregar accessos a servidor */
checked_elements = new Array ();
checked_parent_name = 'dades_checked';

function add_to_checked_list (the_element_text,render){
	is_in_list = new searchItLchecked (the_element_text,checked_elements);
	if (is_in_list.isMatch){
		checked_elements.splice (is_in_list.thePos,1);
	} else {
		checked_elements.push (the_element_text);
		checked_elements.sort (sortFuncAsc);
	}
	//representar dades
	if (render){
		render_list ();
	}
}

function render_list (the_text){
		span_dades_checked = document.getElementById (checked_parent_name);
		if (checked_elements.length > 0){
			the_text = " ";
			the_text += checked_elements.join (', ');
			a_remoure = span_dades_checked.childNodes[0];
			a_posar = document.createTextNode (the_text);
			span_dades_checked.replaceChild (a_posar,a_remoure);
		} else {
			a_remoure = span_dades_checked.childNodes[0];
			a_posar = document.createTextNode (' ');
			span_dades_checked.replaceChild (a_posar,a_remoure);
		}
}

function searchItLchecked (searchedL,theArray) {
    var isMatch = false;
    for (i = 0;i < theArray.length;i++){
        if (theArray[i] == searchedL){
            isMatch = true;
            break
        }
    }
    this.isMatch = isMatch;
    this.thePos = i;
    return this
}

function sortFuncAsc(record1, record2) {
	var value1 = record1.toLowerCase();
	var value2 = record2.toLowerCase();
	if (value1 > value2) return (1);
	if (value1 < value2) return (-1);
	return (0);
}
