/*#############################################################
Name: Niceforms
Version: 2.0
Author: Lucian Slatineanu
URL: http://www.emblematiq.com/projects/niceforms/

Feel free to use and modify but please keep this copyright intact.
#################################################################*/


//FAC PRELOAD LA IMAGES DE ONMOUSEOVER



//Global Variables
var NF = new Array();

//---------------------------------------------------------------------------------------------------------
function NFDo(what, _style, _type) {

       _style = typeof(_style) != 'undefined' ? _style : 1;
       _type = typeof(_type) != 'undefined' ? _type : 'domain_search';

	var niceforms = document.getElementsByTagName('form');
	var identifier = new RegExp('(^| )'+_type+'( |$)');

	for(var q = 0; q < niceforms.length; q++) {if(identifier.test(niceforms[q].className)) {
		if(what == "start") { //Load Niceforms
			NF[q] = new niceform(niceforms[q], _style);
			niceforms[q].start();
		}
		else { //Unload Niceforms
			niceforms[q].unload();
			NF[q] = "";
		}
	}}
}
//---------------------------------------------------------------------------------------------------------


function niceform(nf, _style) {

        _style = typeof(_style) != 'undefined' ? _style : 1;

	nf._select = new Array();
	nf.add_select = function(obj) {this._select[this._select.length] = obj; selects(obj, _style);}
	nf.start = function() {
		var allSelects = this.getElementsByTagName('select');
		for(var w = 0; w < allSelects.length; w++) {
			this.add_select(allSelects[w]);
		}
		//Start
		for(w = 0; w < this._select.length; w++) {this._select[w].init(w);}
	}
		nf.unload = function() {
		//Stop
		for(w = 0; w < this._select.length; w++) {this._select[w].unload();}
	}
}

function selects(el, _style) { //extend Selects

         _style = typeof(_style) != 'undefined' ? _style : 1;

         if(_style == 1) {

            var selectRightWidthSimple = 19;
            var selectRightWidthScroll = 2;
            var selectMaxHeight = 200;
        
         } else if (_style == 2) {

            var selectRightWidthSimple = 14;
            var selectRightWidthScroll = 2;
            var selectMaxHeight = 200;

         }
        
	el.oldClassName = el.className;
	el.dummy = document.createElement('div');
	el.dummy.className = "NFSelect_"+_style;
	el.dummy.style.width = el.offsetWidth + 'px';
	el.dummy.ref = el;
	el.left = document.createElement('span');
	el.left.className = "NFSelectLeft_"+_style;
	el.right = document.createElement('div');
	el.right.className = "NFSelectRight_"+_style;
	el.txt = document.createTextNode(el.options[0].text);
	el.bg = document.createElement('div');
	el.bg.className = "NFSelectTarget_"+_style;
	el.bg.style.display = "none";
	el.opt = document.createElement('ul');
	el.opt.className = "NFSelectOptions_"+_style;
	el.dummy.style.left = findPosX(el) + 'px';
	el.dummy.style.top = findPosY(el) + 'px';
	el.opts = new Array(el.options.length);
	el.init = function(pos) {
/*
        for(var q = 0; q < this.options.length; q++) {
                if (this.options[q].title) {
                    var img = new Element('img', {src: this.options[q].title});
                    img.setAttribute('src', 'styles/default/imageset/ro/ext_flags/ro.gif');
                    img.setAttribute('style', 'margin-left: 5px; margin-top: 5px; ');
                    this.left.appendChild(img);
                    //this.dummy.appendChild(img);
                    //document.body.appendChild(img);
                }
        }
*/


		this.dummy.appendChild(this.left);
		this.right.appendChild(this.txt);
		this.dummy.appendChild(this.right);
		this.bg.appendChild(this.opt);
		this.dummy.appendChild(this.bg);

		for(var q = 0; q < this.options.length; q++) {

			this.opts[q] = new option(this.options[q], q);
			this.opt.appendChild(this.options[q].li);


			this.options[q].lnk.onclick = function() {
				this._onclick();
				this.ref.dummy.getElementsByTagName('div')[0].innerHTML = this.ref.options[this.pos].text;
				this.ref.options[this.pos].selected = "selected";
				for(var w = 0; w < this.ref.options.length; w++) {this.ref.options[w].lnk.className = "";}
				this.ref.options[this.pos].lnk.className = "NFOptionActive_"+_style;
			}
		}
		if(this.options.selectedIndex) {
			this.dummy.getElementsByTagName('div')[0].innerHTML = this.options[this.options.selectedIndex].text;
			this.options[this.options.selectedIndex].lnk.className = "NFOptionActive_"+_style;
		}
		this.dummy.style.zIndex = 999 - pos;
		this.parentNode.insertBefore(this.dummy, this);
		this.className = "NFhidden_"+_style;
	}
	el.unload = function() {
		this.parentNode.removeChild(this.dummy);
		this.className = this.oldClassName;
	}
	el.dummy.onclick = function() {
		var allDivs = document.getElementsByTagName('div'); for(var q = 0; q < allDivs.length; q++) {if((allDivs[q].className == "NFSelectTarget_"+_style) && (allDivs[q] != this.ref.bg)) {allDivs[q].style.display = "none";}}
		if(this.ref.bg.style.display == "none") {this.ref.bg.style.display = "block";}
		else {this.ref.bg.style.display = "none";}
		if(this.ref.opt.offsetHeight > selectMaxHeight) {
			this.ref.bg.style.width = this.ref.offsetWidth - selectRightWidthScroll + 0 + 'px';
			this.ref.opt.style.width = this.ref.offsetWidth - selectRightWidthScroll + 'px';
		}
		else {
			this.ref.bg.style.width = this.ref.offsetWidth - selectRightWidthSimple + 0 + 'px';
			this.ref.opt.style.width = this.ref.offsetWidth - selectRightWidthSimple + 'px';
		}
	}
	el.bg.onmouseout = function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if((reltg.nodeName == 'A') || (reltg.nodeName == 'LI') || (reltg.nodeName == 'UL')) return;
		if((reltg.nodeName == 'DIV') || (reltg.className == 'NFSelectTarget_'+_style)) return;
		else{this.style.display = "none";}
	}
	el.dummy.onmouseout = function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if((reltg.nodeName == 'A') || (reltg.nodeName == 'LI') || (reltg.nodeName == 'UL')) return;
		if((reltg.nodeName == 'DIV') || (reltg.className == 'NFSelectTarget_'+_style)) return;
		else{this.ref.bg.style.display = "none";}
	}
	

	if (_style == 2) {
          el.dummy.onmouseover = function() { el.dummy.className = "NFSelect_over_"+_style; el.left.className = "NFSelectLeft_over_"+_style; el.right.className = "NFSelectRight_over_"+_style; }
          el.left.onmouseover = function() { el.dummy.className = "NFSelect_over_"+_style; el.left.className = "NFSelectLeft_over_"+_style; el.right.className = "NFSelectRight_over_"+_style; }
          el.right.onmouseover = function() { el.dummy.className = "NFSelect_over_"+_style; el.left.className = "NFSelectLeft_over_"+_style; el.right.className = "NFSelectRight_over_"+_style; }

          el.dummy.onmouseout = function() { el.dummy.className = "NFSelect_"+_style; el.left.className = "NFSelectLeft_"+_style; el.right.className = "NFSelectRight_"+_style; }
          el.left.onmouseout = function() { el.dummy.className = "NFSelect_"+_style; el.left.className = "NFSelectLeft_"+_style; el.right.className = "NFSelectRight_"+_style; }
          el.right.onmouseout = function() { el.dummy.className = "NFSelect_"+_style; el.left.className = "NFSelectLeft_"+_style; el.right.className = "NFSelectRight_"+_style; }
        }



	//el.onfocus = function() {this.dummy.className += " NFfocused";}
	el.onblur = function() {this.dummy.className = this.dummy.className.replace(/ NFfocused/g, "");}
	el.onkeydown = function(e) {
		if (!e) var e = window.event;
		var thecode = e.keyCode;
		var active = this.selectedIndex;
		switch(thecode){
			case 40: //down
				if(active < this.options.length - 1) {
					for(var w = 0; w < this.options.length; w++) {this.options[w].lnk.className = "";}
					var newOne = active + 1;
					this.options[newOne].selected = "selected";
					this.options[newOne].lnk.className = "NFOptionActive_"+_style;
					this.dummy.getElementsByTagName('div')[0].innerHTML = this.options[newOne].text;
				}
				return false;
				break;
			case 38: //up
				if(active > 0) {
					for(var w = 0; w < this.options.length; w++) {this.options[w].lnk.className = "";}
					var newOne = active - 1;
					this.options[newOne].selected = "selected";
					this.options[newOne].lnk.className = "NFOptionActive_"+_style;
					this.dummy.getElementsByTagName('div')[0].innerHTML = this.options[newOne].text;
				}
				return false;
				break;
			default:
				break;
		}
	}


}

function option(el, no) { //extend Options

        /*
        el.img = document.createElement('img');
        el.img.setAttribute('src', 'styles/default/imageset/ro/ext_flags/ro.gif');
        el.img.setAttribute('style', 'margin-left: 5px; margin-top: 5px; ');
        */
	el.li = document.createElement('li');
	el.lnk = document.createElement('a');
	el.lnk.href = "javascript:;";
	el.lnk.ref = el.parentNode;
	el.lnk.pos = no;
	el.lnk._onclick = el.onclick || function () {};
	el.txt = document.createTextNode(el.text);
	el.lnk.appendChild(el.txt);
	//el.li.setAttribute('style', 'background: transparent url(styles/default/imageset/ro/ext_flags/ro.gif) no-repeat 0 50%; ');
	el.li.appendChild(el.lnk);
}

//Get Position
function findPosY(obj) {
	var posTop = 0;
	do {posTop += obj.offsetTop;} while (obj = obj.offsetParent);
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	do {posLeft += obj.offsetLeft;} while (obj = obj.offsetParent);
	return posLeft;
}

//Add events


window.onload = function () { 
  NFDo('start'); 
  NFDo('start', 2, 'language_currency');
}
window.onresize = function() { 	
  NFDo('stop'); 
  NFDo('start');  
  NFDo('stop', 2, 'language_currency');
  NFDo('start', 2, 'language_currency');
}


