// define arrays for source lists, target lists, add buttons and remove buttons
var dual_select_sources 		= new Array();
var dual_select_targets 		= new Array();
var dual_select_add_buttons 	= new Array();
var dual_select_remove_buttons 	= new Array();


/*
activate the Add button
and disable the Remove button
whenever a group is clicked.
It will also deselect anything
selected in the source list
*/
function targetChangeHandler (target_list_id){
	for (var i = 0; i < dual_select_groups.length; i++) {
		single_select_group = dual_select_groups[i];
		if (typeof $("#target_" + single_select_group) != "undefined") {
			if ("target_" + single_select_group == target_list_id && $("#" + target_list_id)[0].selectedIndex > -1) {
				$("#source_" + single_select_group)[0].selectedIndex = -1;
				$("#remove_button_" + single_select_group).removeAttr("disabled");
				$("#add_button_" + single_select_group).attr("disabled", "disabled");
			} else {
				$("#source_" + single_select_group)[0].selectedIndex = -1;
				$("#target_" + single_select_group)[0].selectedIndex = -1;
				$("#remove_button_" + single_select_group).attr("disabled", "disabled");
				$("#add_button_" + single_select_group).attr("disabled", "disabled");
			}
		}
	}
}

/*
activate the Remove button
and disable the Add button whenever
an entitlement is clicked.
It will also deselect anything selected
in the available groups list
*/
function sourceChangeHandler (source_list_id){
	for (var i = 0; i < dual_select_groups.length; i++) {
		single_select_group = dual_select_groups[i];
		if (typeof $("#source_" + single_select_group) != "undefined") {
			if ("source_" + single_select_group == source_list_id && $("#" + source_list_id)[0].selectedIndex > -1){
				$("#target_" + single_select_group)[0].selectedIndex = -1;
				$("#remove_button_" + single_select_group).attr("disabled", "disabled");
				$("#add_button_" + single_select_group).removeAttr("disabled");
			} else {
				$("#source_" + single_select_group)[0].selectedIndex = -1;
				$("#target_" + single_select_group)[0].selectedIndex = -1;
				$("#remove_button_" + single_select_group).attr("disabled", "disabled");
				$("#add_button_" + single_select_group).attr("disabled", "disabled");
			}
		}
	}
}

/*
add an assigned entitlement and
remove it to the available groups list.
*/
function addButtonHandler (){
	jQuery.each(dual_select_sources, function (index, single_select_source ){
		var single_select = single_select_source[0];
		if (single_select.selectedIndex > -1){
			var dual_select_name;
			jQuery.each(dual_select_groups, function (index, single_select_group ){
				if ("source_" + single_select_group == single_select.id){
					dual_select_name = single_select_group;
				}
			});
			moveOption(single_select, $("#target_" + dual_select_name)[0], "copy");
			if (single_select.selectedIndex = -1){
				$("#add_button_" + dual_select_name).attr("disabled", "disabled");
			}
			single_select_source.focus();
		}
	});
}

/*
removed an assigned entitlement and
return it to the available groups list.
*/
function removeButtonHandler (){
	jQuery.each(dual_select_targets, function (index, single_select_target ){
		var single_select = single_select_target[0];
		if (single_select.selectedIndex > -1){
			var dual_select_name;
			jQuery.each(dual_select_groups, function (index, single_select_group ){
				if ("target_" + single_select_group == single_select.id){
					dual_select_name = single_select_group;
				}
			});
			moveOption(single_select, $("source_" + dual_select_name)[0], "delete");
			if (single_select.selectedIndex = -1){
				$("#remove_button_" + dual_select_name).attr("disabled", "disabled");
			}
			single_select.focus();
		}
	});
}

/*
move an HTML option tag
from one Select tag to
another Select tag.
*/
function moveOption ( fromList, toList, action ){
  var selIndex = fromList.selectedIndex;
  var removed = new Array();

  if (selIndex > -1){
	//fromList.options.each(function ( child ){
	for (var i = 0; i < fromList.length; i++) {
      child = fromList.options[i];
	  if (child.selected){
		var opt = document.createElement("option");
        opt.value = child.value;
        var txt = document.createTextNode( child.text );
        opt.appendChild(txt);
        if (action == "copy") {
			// determine the "in array" status
			var in_array = false;
			//toList.options.each(function ( toList_child ){
			for (var a = 0; a < toList.length; a++) {
				if (toList.options[a].value == child.value) {
					in_array = true;
				}
			}
			// copy only if cild is not yet in target array
			if (!in_array) {
				// Some browsers did not like "toList.appendChild( opt )"
                if( !toList.appendChild( opt ) ){
                    // If that didn't work, try it this way
                    toList.add( opt );
                }
			}
        }
        removed[removed.length] = child;
      }
    }
	if (action == "delete") {
		jQuery.each(removed, function (index, child){
		  child.parentNode.removeChild(child);
        });
    }
  }
}


/*!
	@fn         getDualListOptions()
	@brief      Retrives list of all options for
				list with given name
	@param      dual_select_group_name The name of group to handle
	@return     void
*/
function getDualListOptions(dual_select_group_name, resetter_id){
	var controller_url = "";
	if (typeof frontend == "undefined" || frontend != true) {
		controller_url += "../";
	}
	controller_url += path + "application/controller/dualSelectOptionGetter.NEW.php?lang=" + locale + "&sid=" + key + "&action=get_all_options&select_list=" + dual_select_group_name;
	if (typeof provider_id != "undefined") {
		controller_url += "&pid=" + provider_id;
	}

	if (typeof resetter_id != "undefined") {
		var resetter = $('#' + resetter_id);
		controller_url = controller_url + "&parameter=" + resetter.val();
	}

	//alert(controller_url);
	$.getJSON(controller_url,
		function (json){
			all_nodes = json;
			jQuery.each(dual_select_sources, function (index, single_select_source){
				var single_select = single_select_source[0];
				if (single_select.id == "source_" + dual_select_group_name){
					for (i = single_select.length - 1; i>=0; i--) {
						single_select.remove(i);
					}
					jQuery.each(all_nodes, function(index, single_node){
		                var opt = document.createElement("option");
		                opt.value = single_node.option_value;
		                var txt = document.createTextNode( single_node.name );
		                opt.appendChild(txt);
						if( !single_select.appendChild( opt ) ){
		                    // If that didn't work, try it this way
		                    single_select.add( opt );
		                }
					});
				}
			})
		}
	);
}


/*
 The init() function is run once when the page is loaded.
 1) initialize variables;
 2) Set object state (e.g. disable buttons);
 3) connect events to event handlers
*/
//var init_dual_select = function () {
function init_dual_select() {
	if (typeof dual_select_groups == "object") {
		// generate select elements and add and remove buttons
		jQuery.each(dual_select_groups, function (index, dual_select_group_name) {
			var list_size = 10;
			if (typeof dual_select_height != "undefined") {
				list_size = dual_select_height;
			} else {
				if (dual_select_group_name == "languages") {
					list_size = 30;
				} else if (dual_select_group_name == "countries") {
					list_size = 20;
				}
			}

			// set source list
			var source_list = $("#source_" + dual_select_group_name);

			if (source_list != null && typeof source_list != "undefined") {

				source_list.attr("name", "source_" + dual_select_group_name);
				source_list.attr("multiple", "multiple");
				source_list.attr("size", list_size);
				if (typeof dual_select_width != "undefined"){
					source_list.width(dual_select_width);
				} else {
					source_list.width("250px");
				}

				dual_select_sources.push(source_list);

				// get options for each dual select
				getDualListOptions(dual_select_group_name);

				// set target list
				var target_list = $("#target_" + dual_select_group_name);
				target_list.attr("name", "target_" + dual_select_group_name);
				target_list.attr("multiple", "multiple");
				target_list.attr("size", list_size);
				if (typeof dual_select_width != "undefined"){
					target_list.width(dual_select_width);
				} else {
					target_list.width("250px");
				}

				dual_select_targets.push(target_list);

				// set add buttons
				var add_button = $("#add_button_" + dual_select_group_name);
				add_button.attr("disabled", "disabled");
				dual_select_add_buttons.push(add_button);

				// set remove buttons
				var remove_button = $("#remove_button_" + dual_select_group_name);
				remove_button.attr("disabled", "disabled");
				dual_select_remove_buttons.push(remove_button);
			}
		});
	}
}
