$(document).ready (function ()
{
	General.styleCheckboxes ();
	General.styleRadios ();
	General.styleSelects ();
	$("#hrefLang").click (function ()
	{
		General.selectLang ();
		return false;
	});
});

var General =
{
	selectLang : function ()
	{
		$("#ulLangSelect").show (500);
	},

	styleCheckboxes : function ()
	{
		$("input[type='checkbox']").each (function ()
		{
			if ($(this).is (":checked") === true)
			{
				$(this).wrap ("<span class=\"chkbsel\"></span>");
			}
			else
			{
				$(this).wrap ("<span class=\"chkbnosel\"></span>");
			}
			$(this).parent ().click (function ()
			{
				if ($(this).attr ("class") == "chkbsel")
				{
					$(this).removeClass ("chkbsel").addClass ("chkbnosel");
					$(this).children ("input").removeAttr ("checked");
				}
				else
				{
					$(this).removeClass ("chkbnosel").addClass ("chkbsel");
					$(this).children ("input").attr ("checked", "checked");
				}
			});
			$(this).hide ();
		});
	},

	styleRadios : function ()
	{
		$("input[type='radio']").each (function ()
		{
			if ($(this).is (":checked") === true)
			{
				$(this).wrap ("<span class=\"chkbsel\"></span>");
			}
			else
			{
				$(this).wrap ("<span class=\"chkbnosel\"></span>");
			}
			$(this).parent ().click (function ()
			{
				$("input[name='" + $(this).children ("input").attr ("name") + "']").parent ().removeClass ("chkbsel").addClass ("chkbnosel");
				$(this).removeClass ("chkbnosel").addClass ("chkbsel");
				$("input[name='" + $(this).children ("input").attr ("name") + "']").removeAttr ("checked");
				$(this).children ("input").attr ("checked", true);
			});
			$(this).hide ();
		});
	},

	styleSelects : function ()
	{
		$("select").each (function ()
		{
			$(this).css ({border : "1px solid #ddd", "padding" : "5px"});
		});
		/*
		$("select").each (function ()
		{
			var newid = "div_" + $(this).attr ("id");
			if ($("#" + newid).length > 0)
			{
				$("#" + newid + " ul").remove ();
				$("#" + newid + " span").remove ();
				$("#" + newid + " a").remove ();
				$(this).unwrap ();
			}
			$(this).hide ();
			$(this).wrap ("<div id=\"" + newid + "\" class=\"select\"></div>");
			$("#" + newid).append ("<span></span><a href=\"#\" class=\"drop\"></a><ul></ul><br />");
			$("#" + newid + " a.drop").click (function ()
			{
				if ($("#" + newid + " ul").is (":hidden"))
				{
					$("#" + newid + " ul").show ("fast");
				}
				else
				{
					$("#" + newid + " ul").hide ("fast");
				}
				return false;
			});
			$("#" + newid).css ({width : $(this).width () + 43});
			$("#" + newid).children ("span").css ({width : $("#" + newid).width () - 43}).text ($(this).children ("option:nth-child(1)").text ());
			$(this).children ("option").each (function ()
			{
				$("#" + newid + " ul").append ("<li><a href=\"#\" rel=\"" + $(this).val () + "\">" + $(this).text () + "</a></li>");
			});
			$("#" + newid + " ul").css ({width : $(this).width () + 43});
			$("#" + newid + " ul a").click (function (e)
			{
				$("#" + newid + " span").text ($(e.target).text ());
				$("#" + newid + " ul").hide ("fast");
				$("#" + newid + " select option").removeAttr ("selected");
				$("#" + newid + " select option[value='" + $(e.target).attr ("rel")  + "']").attr ("selected", "selected");
				$("#" + newid + " select").trigger ("change");
				return false;
			});
		});
		*/
	},

	formatNumber : function (refFloat)
	{
		var fixed = (Math.ceil (parseFloat (refFloat) * 100) / 100).toString ();
		switch (true)
		{
			case fixed.indexOf (".") == -1:
				fixed += ".00";
				break;
			case fixed.length - fixed.indexOf (".") == 2:
				fixed += "0";
				break;
		}

		return fixed
	},

	createCookie : function (name, value, days)
	{
		if (days)
		{
			var date = new Date ();
			date.setTime (date.getTime () + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires="+date.toGMTString();
		}
		else
		{
			var expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	},

	readCookie : function (name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split (';');
		for (var i = 0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0) == ' ')
			{
				c = c.substring (1, c.length);
			}
			if (c.indexOf(nameEQ) == 0)
			{
				return c.substring (nameEQ.length, c.length);
			}
		}
		return null;
	},

	eraseCookie : function (name)
	{
		General.createCookie (name, "", -1);
	}
}
