String.prototype.contains = function(search) {
    return this.indexOf(search) != -1; 
}
var neo = neo ? neo : new Object();
neo.CustomDropDown = neo.CustomDropDown ? neo.CustomDropDown : new Object();
neo.Utils = neo.Utils ? neo.Utils : new Object();

/**
 *  Customized dropdown control. Will get invoked on document 
 *  load for all select with class 'neoCustomDropDown'.
 */
neo.CustomDropDown = function(){
	var activeSel;
	var activeCDD;
	var activeDD;
	var isDDOpen = false;
	
	function init(){
		var selectList = $("select.neoCustomDropDown");
		for(var x=0; x<selectList.length; x++){
			replace(selectList[x])
		}
	}
	function replace(el){
		if($(el).attr("id") == "" || $(el).attr("id")== undefined){
			var serial = "sel_" + Math.floor(Math.random()*1234567890);
			$(el).attr("id", serial)
		}else {
			var serial = $(el).attr("id");
		}
		var elParent = el.parentNode;
		var cDD = doControl(el);
		$(cDD).click(function(event){neo.CustomDropDown.show(event)});

		elParent.appendChild(cDD);

		$(cDD).attr("sel", serial);
		$(cDD).attr("id", serial + "_cDD");
		$(el).attr("cdd", serial + "_cDD");
		if(el.disabled){
			$(cDD).attr("disabled", true);
			$(cDD).addClass("disabled");
		}
		setCDDText(cDD, $("#"+el.id+" option:selected").text());
		$(el).css("display", "none");
	}
	function show(event){
		activeCDD = event.currentTarget;
		if($(activeCDD).attr("disabled") == "true") return;
		activeSel 	= document.getElementById($(activeCDD).attr("sel"));
		doDropDown();
		setPosition();
		doShim();
		$(activeDD).css("display", "block");
		$(window).bind("resize", neo.CustomDropDown.close);
		isDDOpen = true;
	}
	function close(){
		$(activeDD).html("");
		$(activeDD).scrollTop(0);
		$(activeDD).css("display", "none");
		$(shim).css("display", "none");
		activeSel = null;
		activeCDD = null;
		activeDD = null;
		$(window).unbind("resize", neo.CustomDropDown.close);
		isDDOpen = false;
	}
	function doControl(el){
		var _width =  Number($(el).width()) - 24;
		var oDiv = document.createElement("div");
		oDiv.className = "customDropDownControl";

		var oSpan = document.createElement("span");
		oSpan.className = "ddButton";

		var oText = document.createElement("span");
		oText.className = "ddText";
		
		$(oDiv).css("width", _width);
		$(oText).css("width", _width - 3);
		
		oDiv.appendChild(oText);
		oDiv.appendChild(oSpan);
		return oDiv;
	}
	function doOptions(){
		var kids = $("#"+ $(activeSel).attr("id")).children();
		for(var k=0, i=0; k<kids.length; k++){
			if(kids[k].nodeName.toLowerCase() == "optgroup"){
				$(activeCDD).attr("hasOptGroup", true);
				var optGroup = document.createElement("div");
				$(optGroup).addClass("optionGroup");
				
				var optGroupH = document.createElement("span");
				$(optGroupH).html($(kids[k]).attr("label"));
				
				optGroup.appendChild(optGroupH);
				activeDD.appendChild(optGroup);
				var optList = $(kids[k]).find("option");
				for(var y=0; y<optList.length; y++){
					doOption(optList[y], optGroup, i)
					i++;
				}
			}else{
				doOption(kids[k], activeDD, i);
				i++;
			}
		}
	}
	function doOption(obj, parent, i){
		var opt = document.createElement("a");
		$(opt).attr("optIndex", i);
		$(opt).text($(obj).text());
		$(opt).click(function(event){neo.CustomDropDown.setSelected(event)});
		$(opt).mouseover(function(event){neo.CustomDropDown.setHighlighted(event)});
		$(opt).mouseout(function(event){neo.CustomDropDown.setHighlighted(event)});
		parent.appendChild(opt);
	}
	function doDropDown(){
		if(document.getElementById("customDDOptions")){
			activeDD = document.getElementById("customDDOptions");
		}else{
			activeDD = document.createElement("div");
			$(activeDD).attr("id", "customDDOptions");
			$(activeDD).addClass("customDDOptions");
			document.body.appendChild(activeDD);
		}
		$(activeDD).css("width", "auto");
		doOptions();
	}
	function doShim(){
		if(document.getElementById("customDDShim")){
			shim = document.getElementById("customDDShim");
		}else{
			shim = document.createElement("div");
			shim.id = "customDDShim"
			shim.onclick = close;
			document.body.appendChild(shim)
		}
		$(shim).css("width", document.body.clientWidth);
		$(shim).css("height", $("#doc3").height() > document.body.clientHeight ? $("#doc3").height() : document.body.clientHeight);
		$(shim).css("display", "block");
	}
	function setPosition(){
		var _aCDD_width 	= Number($(activeCDD).width()) + 30; 	//add padding
		var _aCDD_height 	= Number($(activeCDD).height()) + 2; 	//add padding
		var _aDD_width 		= Number($(activeDD).width()); 		//add border
		var _aDD_height 	= Number($(activeDD).height());
		
		if(_aDD_width < _aCDD_width)
			_aDD_width = _aCDD_width; 
		
		$("."+activeDD.className+ " a").css("width", _aDD_width);
		
		if($(activeCDD).attr("hasOptGroup")) _aDD_width += 10;
		
		$(activeDD).css("width", _aDD_width + 24);
		
		if(neo.Utils.getParam("lc")=="ar" || neo.Utils.getParam("lc")=="he"){
			var pLeft = (neo.Utils.findPosX(activeCDD) + _aCDD_width) - (_aDD_width + 26);
			if(pLeft < 0)
				pLeft = neo.Utils.findPosX(activeCDD);
		}else{
			var pLeft = neo.Utils.findPosX(activeCDD);
			if(pLeft + _aDD_width > $(window).width() - 24)
				pLeft = (pLeft + _aCDD_width) - (_aDD_width  + 26);
		}

		$(activeDD).css("left", pLeft);
		
		var pTop 	= neo.Utils.findPosY(activeCDD) + _aCDD_height - 1;
		var scrollTOffset = neo.Utils.getScrollY();
		if((pTop - scrollTOffset) + _aDD_height > $(window).height() - 22)
			pTop = (pTop - _aCDD_height - _aDD_height);
		
		$(activeDD).css("top", pTop);
	}
	function setSelected(event){
		var el = event.currentTarget;
		activeSel.selectedIndex = el.getAttribute("optIndex");
		if($(activeSel).attr("onchange") != undefined){
			$(activeSel).change();
		}
		setCDDText(activeCDD, $(el).text());
		close();
	}
	function setHighlighted(event){
		var el = event.currentTarget;
		if(event.type == "mouseover")
			$(el).addClass("highlighted");
		else if(event.type == "mouseout")
			$(el).removeClass("highlighted");
	}
	function setCDDText(cDD, txt){
		$("#"+cDD.id+ " span.ddText").text(txt);   
	}
	return {
		init:init,
		show:show,
		close:close,
		setSelected:setSelected,
		setHighlighted:setHighlighted,
		setPosition:setPosition
	};
}();

$(document).ready(function() {
	neo.CustomDropDown.init()
});

neo.Utils = {
	findPosX: function (obj) {
		var curleft = 0;
		if(obj.offsetParent){
			while(1){
			  curleft += obj.offsetLeft;
			  if(!obj.offsetParent) break;
			  obj = obj.offsetParent;
			}
		} else if(obj.x){
			curleft += obj.x;
		}
		return curleft;
	},
	findPosY: function (obj){
		var curtop = 0;
		if(obj.offsetParent){
			while(1){
			  curtop += obj.offsetTop;
			  if(!obj.offsetParent) break;
			  obj = obj.offsetParent;
			}
		}else if(obj.y){
			curtop += obj.y;
		}
		return curtop;
	},
	getScrollY: function () {
		var scrOfY = 0;
		if(document.body.scrollTop) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		} else if(document.documentElement.scrollTop) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	},
	/*
	 * Returns the value of the requested querystring parameter
	 */
	getParam: function (paramName){
		var docLoc = document.location.href;
		var indexOfHash = docLoc.indexOf('#');
		if (indexOfHash != -1) {
			docLoc = docLoc.substr(0, indexOfHash);
		}
		var qString = '';
		var params = new Array();
	
		if(docLoc.contains("?")){
			qString = docLoc.split("?");
			qString = qString[1];
		}
		if(qString.contains("&")){
			params = qString.split("&");
		}
		else if(qString.contains("=")){
			params.push(qString);
		}
		for(var i=0; i<params.length; i++){
			param = params[i].split("=");
			if(param[0] == paramName){
				return param[1];
			}
		}
		return '';
	}
};