if (typeof ecnext == "undefined") {
	ecnext = {};
}

/* DOM Functions */
ecnext.byId = function (element_or_id) {
	return (typeof element_or_id == 'string') ? document.getElementById(element_or_id) : element_or_id;
};

ecnext.setSelected = function(element_or_id, value) {
	var e = ecnext.byId(element_or_id);
	var selected;
	if (e) {
		var noptions = e.options.length;
		for (var i=0; i<noptions; ++i) {
			var o = e.options[i];
			if (o.value == value) {
				selected = {
					value: o.value,
					description: o.text
				};
				e.selectedIndex = i;
				break;
			}
		}
	}

	return selected;
};


ecnext.getSelected = function (element_or_id) {
	var selected = null;

	var e = ecnext.byId(element_or_id);
	if (e) {
		var o = e.options[e.selectedIndex];
		if (o) {
			selected = {
				value: o.value,
	   			description: o.text
			};
		}
	}

	return selected;
};

ecnext.getAllSelected = function (element_or_id) {
	var allSelected = [];

	var e = ecnext.byId(element_or_id);
	if (e) {
		var noptions = e.options.length;
		for (var i=0; i<noptions; ++i) {
			var o = e.options[i];
			if (o.selected) {
				var selected = {
					value: o.value,
					description: o.text
				};
				allSelected.push(selected);
			}
		}
	}

	return allSelected;
};

ecnext.getAllOptions = function (element_or_id) {
	var options = [];

	var e = ecnext.byId(element_or_id);
	if (e) {
		var noptions = e.options.length;
		for (var i=0; i<noptions; ++i) {
			var o = e.options[i];
			options.push({
				value: o.value,
				description: o.text
			});
		}
	}

	return options;
};

ecnext.clearSelectList = function (element_or_id) {
	var e = ecnext.byId(element_or_id);
	if (e) {
		e.options.length = 0;
	}
};

ecnext.sortSelectList = function(element_or_id) {
	var options = ecnext.getAllOptions(element_or_id);
	var e = ecnext.byId(element_or_id);
	if (e) {
		options.sort(function (a, b) {
			if (a.description < b.description) { return -1; }
			if (a.description > b.description) { return 1; }
			return 0;
		});

		var noptions = e.options.length;
		for (var i=0; i<noptions; ++i) {
			var o = e.options[i];
			o.text = options[i].description;
			o.value = options[i].value;
		}
	}
};

ecnext.addSelectOptions = function(kwArgs) {
	if (kwArgs.node) {
		var node = kwArgs.node;
		for (var i=0; i<kwArgs.options.length; ++i) {
			var optionInfo = kwArgs.options[i];
			var option = new Option(optionInfo.description, optionInfo.value);

			if ('disabled' in optionInfo && optionInfo.disabled) {
				option.disabled= 1;
			}

			if ('index' in optionInfo) {
				node.options[optionInfo.index] = option;
			}
			else {
				node.options[node.options.length] = option;
			}
		}
	}
};

ecnext.removeSelectedOptions = function(element_or_id) {
	var e = ecnext.byId(element_or_id);
	if (e) {
		while (e.selectedIndex >= 0) {
			e.options[e.selectedIndex] = null;
		}
	}
};

ecnext.getRadio = function(radios) {
	var value = '';
	var radioNode = ecnext.getSelectedRadioNode(radios);
	if (radioNode) { value = radioNode.value };
	return value;
};

ecnext.getSelectedRadioNode = function(radios) {
	var radioNode = null;
	if (radios) {
		for (i=0; !radioNode && i<radios.length; ++i) {
			var r = radios[i];
			if (r.checked) {
				radioNode = r;
			}
		}
	}
	return radioNode;
};

ecnext.setRadio = function(radios, value) {
	var radio;
	if (radios) {
		for (i=0; !radio && i<radios.length; ++i) {
			var r = radios[i];
			if (r.value == value) {
				r.checked = true;
				radio = r;
			}
		}
	}
	return radio;
};

ecnext.addOnLoad = function (func) {
	//add a function to be run onLoad
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func; 
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

//ECNext Objects

ecnext.sequence = function (p) {
	/////////////////////////////////////////////////////////////////////////////////////////////////
	//parameters:
	//  start: 0 [int] - where to start the sequence
	/////////////////////////////////////////////////////////////////////////////////////////////////
	//public methods:
	//  next() - returns the next value in the sequence (increments the current value)
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	if(p == undefined) {
		p= {"start": 0};
	}

	this.current= p.start;
	this.next= function() {this.current++; return this.current;}
}

ecnext.toolTip = function (p) {
	/////////////////////////////////////////////////////////////////////////////////////////////////
	//parameters:
	//  width: 200 [int] - the width of the tooltip
	//  bgcolor: '#F6F9FD' [string] - background color for the tooltip
	//  bordercolor: '#D1E1F6' [string] - border color for the tooltip
	//  xpos: 'right' [string; 'right' or 'left'] - position of the tooltip relative to mouse
	//  xoffset: 0 [int] - x offset of the tooltip ** 
	//     ** applies in the direction set by xpos (positive value moves it further away from mouse)
	//  yoffset: 0 [int] - y offset of the tooltip
	//  contentText: '' [string] - content to be used in tooltip
	//  content: '' [string or dom object] - the ID or the element to get it's content from * 
	//     * the content of the tooltip will be the innerHTML of the element supplied
	//       (ignored if contentText is supplied)
	/////////////////////////////////////////////////////////////////////////////////////////////////
	//public methods:
	//  show(event) - shows the tooltip at the current mouse position (according to event)
	//  hide() - hides the tooltip
	/////////////////////////////////////////////////////////////////////////////////////////////////

	if(p == undefined) {
		p= {};
	}
	if(!p.width) {
		p.width= 200;
	}
	if(!p.bgcolor) {
		p.bgcolor= '#F6F9FD';
	}
	if(!p.bordercolor) {
		p.bordercolor= '#D1E1F6';
	}
	if(!p.xpos) {
		p.xpos= 'right';
	}
	if(!p.xoffset) {
		p.xoffset= 0;
	}
	if(!p.yoffset) {
		p.yoffset= 0;
	}
	this.width= p.width;
	this.bgcolor= p.bgcolor;
	this.bordercolor= p.bordercolor;
	this.xpos= p.xpos;
	this.xoffset= p.xoffset;
	this.yoffset= p.yoffset;
	
	if(typeof(tooltip_seq) == 'undefined') {
		tooltip_seq= new ecnext.sequence();
	}
	
	this.id= tooltip_seq.next();
	this.name= 'ec_tooltip_'+this.id;
	
	//console.debug('create tooltip:'+this.name);
	
	this.hidden= true;
	
	this.remove_old= false;
	if(p.contentText) {
		this.innerHTML= p.contentText;
	}
	else if(p.content && ecnext.byId(p.content) != undefined) {
		this.innerHTML= ecnext.byId(p.content).innerHTML;
		this.remove_old= true;
	}
	else {
		this.innerHTML= '';
	}
	
	this.content= '<div id="'+this.name+'" style="z-index: 20; background-color:transparent; background-image:url(/manta/images/featured_listings/shadow.png); background-repeat:repeat; display:none; position:absolute;"><div style="position:relative; background:'+this.bgcolor+'; border:1px solid '+this.bordercolor+'; padding:4px; width:'+this.width+'px; left:-4px; top:-4px">'+this.innerHTML+'</div></div>';
	
	this.show= function (e) {
		//console.debug('show tooltip:'+this.name);
		if(this.hidden) {
			var coords= findCoords(e);
			if(this.xpos == 'left') {
				this.left = (coords[0]-this.width-10-this.xoffset) + 'px';
			}
			else {
				this.left = (coords[0]+10+this.xoffset) + 'px';
			}
			this.top= (coords[1]+10+this.yoffset) + 'px';

			if(ecnext.byId(this.name) == undefined) {
				var _this = this;
				ecnext.addOnLoad(function(){_this._show()});
			}
			else {
				this._show();
			}
		}
	}
	this._show= function () {
		var target = ecnext.byId(this.name);
		if(target != undefined) {
			target.style.left= this.left;
			target.style.top = this.top;
			target.style.display="";
			this.hidden= false;
		}
	}
	this._hide= function () {
		var target = ecnext.byId(this.name);
		if(target != undefined) {
			target.style.display="none"
			this.hidden= true;
		}
	}
	this.hide= function () {
		if(ecnext.byId(this.name) == undefined) {
			var _this = this;
			ecnext.addOnLoad(function(){_this.hide()});
		}
		else {
			this._hide();
		}
	}
	
	this.rendered= false;
	this.render= function () {
		if(this.rendered){return}
		
		if(this.remove_old && ecnext.byId(p.content) != undefined) {
			ecnext.byId(p.content).parentNode.removeChild(ecnext.byId(p.content));
		}
		var div= document.createElement("div");
		div.innerHTML= this.content;
		document.body.appendChild(div);
		
		this.rendered= true;
	}
	
	var _this = this;
	ecnext.addOnLoad(function(){_this.render()});
}

ecnext.textContent = function(elementOrId, newTxt) {
	var element = ecnext.byId(elementOrId);

	var propName = 'innerText';
	if (!(propName in element)) {
		propName = 'textContent';
	}

	var txt = element[propName];
	if (arguments.length > 1) {
		element[propName] = newTxt;
	}

	return txt;
}
