/***
Dynamic Drop-Down 1.0
Made by BeCurious Internet & Hospitality
http://www.curious.nl
***/

theWindow = new Array();
Buttons = new Array();

function checkForClickOutside() {
	$$('body').each(function(e) {
		e.observe("click", function(en) {

			if(theWindow[0] !== null) {

					if(!Event.element(en).descendantOf(theWindow[0] + "_replace")) {

						if(Event.element(en).id != theWindow[0] + "_button") {

							Effect.Fade(theWindow[0] + '_replace', { duration: 0.2 });

						}

					}

			}

		});
	});
}


function InitiateDropDown(select_id) {
	Buttons.push(select_id);
	$(select_id).hide();
	$(select_id + "_button").show();
	OptionsType = $(select_id).select("option");
	if(OptionsType.length < 2) {
		$(select_id + "_replace").hide();
		$(select_id + "_button").hide();
	}
	else {
		$(select_id + "_button").show();
	}

	$$('#' + select_id + ' option').each(function(elem){
                if (elem.selected) {
					$(select_id + "_button").innerHTML = elem.text;
				}
	});
	TypeReplace = '<div class="first_option"></div><div class="option_container">';

	i = 0;
	OptionsType.each(function(optionType) {
			TypeReplace += '<div class="option option'+i+'" onclick="ProcessDropDown(' + i + ',\'' + select_id + '\',\'' + addslashes(optionType.innerHTML) + '\')">' + optionType.innerHTML + '</div>';
		i++;
	});

	TypeReplace += '</div><div class="last_option"></div>';
	$(select_id + "_replace").innerHTML = TypeReplace;

	DropDownObserve(select_id);

}

function DropDownObserve(select_id) {

	$(select_id + "_button").observe("click", function(e) {

		for(b=0; b<Buttons.length; b++) {

			if(Event.element(e).id != Buttons[b] + "_button") {

				if($(Buttons[b] + "_replace").visible) {

					Effect.Fade(Buttons[b] + "_replace", { duration: 0.2 });

				}

			}

		}

		theWindow[0] = select_id;
		Effect.toggle(select_id + '_replace', 'appear', { duration: 0.2, afterFinish: checkForClickOutside });

	});

}

function ProcessDropDown(id, select_id, option) {
	$(select_id).selectedIndex = id;
	$(select_id + "_button").innerHTML = option;
	$j('#'+select_id).change();
	Effect.toggle(select_id + '_replace', 'appear', { duration: 0.2 });
}

function addslashes (str) {
    // Escapes single quote, double quotes and backslash characters in a string with backslashes  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/addslashes    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday' 
    return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
