var OTHBindGenericFormEvents = function(selector)
{
	if(selector.length >= 1)
		selector = selector + ' '
	/* Prevent Event Bubbling */
	$j(selector + ".event-click-no-bubbling").each(function()
	{
		console.log($j(this).attr('id'));
		$j(this).click(function(e)
		{
			e.stopPropagation();
		});
	});
	/* Prevent Default Action */
	$j(selector + ".event-click-no-default").each(function()
	{
		$j(this).click(function(e)
		{
			e.preventDefault();
		});
	});
};

$j(function()
{
	/* Gloss Checkboxes */
	$j("input.gloss-checkbox").each(function()
	{
		c="";
		if($j(this).attr("checked"))
			c = c+" gloss-div-checkedbox";
		if($j(this).is(':disabled'))
			c = c+" gloss-div-checkbox-disabled";
		$j('<div class="gloss-div-checkbox'+c+'"></div>').insertAfter($j(this)).attr("check-for", $j(this).attr("id"));
		$j(this).hide();
	});

	$j("input.gloss-checkbox").change(function(e)
	{
		$j('div.gloss-div-checkbox[check-for="'+$j(this).attr("id")+'"]').removeClass('gloss-div-checkedbox');
		var p = $j(this).parent();
		if (p.hasClass('gloss-checkbox-holder'))
			p.removeClass('gloss-checkbox-holder-checked');
		if($j(this).attr("checked"))
		{
			$j('div.gloss-div-checkbox[check-for="'+$j(this).attr("id")+'"]').addClass('gloss-div-checkedbox');
			if (p.hasClass('gloss-checkbox-holder'))
				p.addClass('gloss-checkbox-holder-checked');
		}
	});

	$j("div.gloss-div-checkbox").click(function(e)
	{
		var target = $j("#"+$j(this).attr("check-for"));
		if(target.is(':disabled'))
			return;
		target.attr("checked", !target.attr("checked")).change();
	});

	$j("div.gloss-checkbox-holder").each(function()
	{
		$j(this).click(function()
		{
			var c = $j(this).children('input[type="checkbox"]');

			if(c.is(':disabled'))
				return;

			if(c.attr('checked'))
			{
				c.attr('checked', false).change();
				return;
			}
			c.attr('checked', true).change();
		}).hover(function(){$j(this).addClass('gloss-checkbox-holder-hover');}, function(){$j(this).removeClass('gloss-checkbox-holder-hover');});
	});

	$j('div.gloss-checkbox-holder .gloss-div-checkbox, div.gloss-checkbox-holder input[type="checkbox"]').each(function()
	{
		$j(this).addClass('event-click-no-bubbling');
	});

	$j('label.gloss-label .gloss-div-checkbox').each(function()
	{
		$j(this).addClass('event-click-no-default').addClass('event-click-no-bubbling');
	});

	/* Gloss Radio Buttons */
	$j("input.gloss-radio").each(function()
	{
		c="";
		if($j(this).attr("checked"))
			c = " gloss-div-radio-checked";
		$j('<div class="gloss-div-radio'+c+'"></div>').insertAfter($j(this)).attr("check-for", $j(this).attr("id"));
		$j(this).hide();
	});
	$j("input.gloss-radio").change(function()
	{
		$j('input.gloss-radio[name="'+$j(this).attr('name')+'"]').each(function()
		{
			$j('div.gloss-div-radio[check-for="'+$j(this).attr("id")+'"]').each(function()
			{
				$j(this).removeClass('gloss-div-radio-checked');
				var p = $j(this).parent();
				if (p.hasClass('gloss-radio-holder'))
					p.removeClass('gloss-radio-holder-checked');
			});
		});
		if($j(this).attr("checked"))
			$j('div.gloss-div-radio[check-for="'+$j(this).attr("id")+'"]').addClass("gloss-div-radio-checked");
	});
	$j("div.gloss-div-radio").click(function()
	{
		$j("#"+$j(this).attr("check-for")).attr("checked", !$j("#"+$j(this).attr("check-for")).attr("checked")).change();
	});

	$j("div.gloss-radio-holder").each(function()
	{
		$j(this).click(function()
		{
			$j(this).children('input').attr('checked', 'checked').change();
			$j(this).addClass('gloss-radio-holder-checked');
		}).hover(function(){$j(this).addClass('gloss-radio-holder-hover');}, function(){$j(this).removeClass('gloss-radio-holder-hover');});
	});

	/* Gloss Select */
	$j("select.gloss-select").each(function()
	{
		$j(this).selectmenu({width:$j(this).width()});
	});

	OTHBindGenericFormEvents('');

	/* The Great IE. */
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 8)
	{
		$j('form.gloss-form label').click(function(e)
		{
			var id = $j(this).attr('for');
			if(id == '')
				return;
			var ele = $j('#'+id)
			if(ele.attr('type') == 'checkbox')
				ele.attr('checked', !ele.attr('checked')).change();
			else
				ele.click();
		});
	}
});


