/* SILVERBACK */
/* Common Widget Functions
----------------------------------------------------------------------------- */


//$(window).load(function() {

function widgets() {

	$('select[id*="country"]').each(localizeAddress);
	$('select[id*="country"]').change(localizeAddress);

	$('div.activities').each(function() {
	
		var target = $(this);
		changeProperties(target);
	
	});

	$('div.contacts').each(function() {
	
		var target = $(this);
		changeProperties(target, "allowed");
	
	});

}

//});


/* Property List Editor
----------------------------------------------------------------------------- */


function changeProperties(target, duplicates) {

	var $this = target;
	var properties = $this.attr('class').replace('group ','').replace('currently ','');

	$this.find('label small a').each(function() {
	
		$(this).click(removeProperty);

	});

	if (duplicates != "allowed") {

		$this.children('label').each(function() {

			var property = $(this).children('input').attr('name').match(/[0-9]+/);
			var label = $(this).siblings('div').find('option[value=' + property + ']').text();

			$(this).siblings('div').find('option[value=' + property + ']').attr('disabled','disabled');
			$(this).siblings('div').find('option[value=' + property + ']').text(label + ' (Added)');

		});

	}

	$this.find('div select').change(function() {

		if ($(this)[0].selectedIndex != 0) {

			var property = $(this).children('option:selected').val();
			var $property = $(this).parent().parent().parent().find('label.' + property);
			var label = $(this).children('option:selected').text();

			if ($(this).parent().parent().parent().find('label.' + property).length > 0) {

				$property.show();
				$property.next().show();
				$property.children('input').removeAttr('disabled').show().focus();
				$property.attr('class','');

				if (duplicates != "allowed") {

					$(this).children('option:selected').attr('disabled','disabled');
					$(this).children('option:selected').text(label + ' (Added)');

				}

			} else if (label.match('(Added)')) {

			
			} else {

				var $insertionPoint = $(this).parent().parent();

				if (duplicates != "allowed") {

					$('<label><em>' + label + '</em><input type="text" name="' + properties + '[' + property + ']" /><small><a href="#">Remove</a></small></label><br />').insertBefore($insertionPoint);
					$(this).children('option:selected').attr('disabled','disabled');
					$(this).children('option:selected').text(label + ' (Added)');

				} else {

					$('<label><em>' + label + '</em><input type="text" name="' + properties + '[' + property + '][]" /><small><a href="#">Remove</a></small></label><br />').insertBefore($insertionPoint);
				
				}

				$(this).parent().parent().parent().find('input:last').focus();

			}

			$('label small a').click(removeProperty);

			$(this)[0].selectedIndex = 0;

		}

	});

}

function removeProperty() {

	var property = $(this).parent().siblings('input').attr('name').match(/[0-9]+/).toString();
	var label = $(this).parent().parent().siblings('div').find('option[value=' + property + ']').text().replace(' (Added)','');
	
	$(this).parent().parent().siblings('div').find('option[value=' + property + ']').removeAttr('disabled');
	$(this).parent().parent().siblings('div').find('option[value=' + property + ']').text(label);
	
	$(this).parent().parent().next().hide();
	$(this).parent().parent().hide();
	$(this).parent().parent().addClass(property);
	
	$(this).parent().parent().children('input').attr('disabled','disabled');
	
	return false;

}


/* Address Localization
----------------------------------------------------------------------------- */


function localizeAddress() {

	var country = $(this).val();

	if (country == "US") {
	
		$(this).parent().parent().find('select[id*="state"]').parent().show();
		$(this).parent().parent().find('select[id*="state"]').parent().next().show();
		$(this).parent().parent().find('input[id*="region"]').parent().hide();
		$(this).parent().parent().find('input[id*="region"]').parent().next().hide();	
		$(this).parent().parent().find('input[id*="code"]').siblings('em').text("ZIP Code");;	
	
	} else {
	
		$(this).parent().parent().find('input[id*="region"]').parent().show();
		$(this).parent().parent().find('input[id*="region"]').parent().next().show();			
		$(this).parent().parent().find('select[id*="state"]').parent().hide();
		$(this).parent().parent().find('select[id*="state"]').parent().next().hide();
		$(this).parent().parent().find('input[id*="code"]').siblings('em').text("Postal Code");;	
	
	}

}
