(function($){

	$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
			nchars: "",
			allow: ""
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e)
								{
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false});
									
				}
			);

	};

	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};	

})(jQuery);



$(document).ready(function()
{
	if ($('#xmassplash').length == 1)
	{
		// Splash

		var tmp = $.uaMatch(navigator.userAgent);
		var ie6 = (tmp.browser == 'msie' && tmp.version.substr(0,1) == '6');


		$('#xmassplash a.close').click(function()
		{
			$('#xmassplash').hide();
			$('#xmassplashbackground').hide();
			return false;
		});

		if (ie6)
		{
			$('#xmassplashbackground').css('margin-left', (parseInt($('#xmassplashbackground').css('margin-left')) - 20) + 'px');
			$('#xmassplash').css('margin-left', (parseInt($('#xmassplash').css('margin-left')) - 20) + 'px');
		}
		else
		{
			$('#xmassplash').hide();
			$('#xmassplash a.close').hide();

			$('#xmassplash').show(500, function()
			{
				$('#xmassplash a.close').show();
			});
		}
	}
	else if ($('#xmaslayer').length == 1)
	{
		// Layer

		$('#xmaslayer').hide();
		$('#xmaslayer').show(500);
	}
	else if ($('#xmasauction').length == 1)
	{
		// Auktionsseiten

		var estimate_value = ($('#xmasauction form').length >= 1) ? $('#estimate').val() : '';

		if ($('#xmasauction a.action-auctions').length == 1)
		{
			var dbuttons = {
				'Zu den Auktionen':function()
				{
					document.location.href = $('#xmasauction a.action-auctions').attr('href');
				},
				'Fenster schließen':function()
				{
					$(this).dialog('close');
				}
			};
		}
		else if ($('#xmasauction form').length == 0)
		{
			var dbuttons = {
				'Anmelden':function()
				{
					document.location.href = $('#xmasauction a.action-login').attr('href');
				},
				'Registrieren':function()
				{
					document.location.href = $('#xmasauction a.action-register').attr('href');
				},
				'Fenster schließen':function()
				{
					$(this).dialog('close');
				}
			};
		}
		else
		{
			var dbuttons = {
				'Schätzung ändern':function()
				{
					$('#xmasauction form').submit();
				},
				'Schätzung abgeben':function()
				{
					$('#xmasauction form').submit();
				},
				'Fenster schließen':function()
				{
					$(this).dialog('close');
				}
			};
		}

		$('#xmasauction').dialog(
		{
			title:'Unser Weihnachts-Gewinnspiel',
			width: 380,
			modal:true,
			autoOpen:false,
			buttons:dbuttons,
			resizable:false
		});

		$('#xmasauction').bind('dialogopen', function(event, ui)
		{
			$('#xmasauction a').blur();
			$('button', $('#xmasauction').dialog('widget')).show();
			$('button:first', $('#xmasauction').dialog('widget')).hide();
			$('button:visible:first', $('#xmasauction').dialog('widget')).focus();
		});

		$('#xmasauction').bind('dialogclose', function(event, ui)
		{
			$('#ajaxresponse').remove();

			if ($('#xmasauction form').length >= 1)
			{
				$('#estimate').val(estimate_value);
			}
			$('#xmasauction').dialog('option', 'height', 'auto');
		});

		$('button.xmasauction').click(function()
		{
			$('#xmasauction').dialog('open');
		});

		$('#xmasauction form').submit(function()
		{
			$('#ajaxresponse').remove();

			$.post($(this).attr('action') + '?ajax_request=1', $(this).serialize(), function(data)
			{
				if (data.success == true)
				{
					$('button:visible:first', $('#xmasauction').dialog('widget')).hide();
					$('button:first', $('#xmasauction').dialog('widget')).show();
					estimate_value = $('#estimate').val();
				}

				if ($('#xmasauction div.ui-state-error').length >= 1)
				{
					$('#xmasauction div.ui-state-error').remove();
				}

				if ($('#xmasauction div.ui-state-highlight').length >= 1)
				{
					$('#xmasauction div.ui-state-highlight').remove();
				}

				$('#xmasauction form').before(data.response);
				$('#xmasauction').dialog('option', 'height', 'auto');

			}, 'json');

			return false;
		});

		$('#xmasauction form input[type="text"]').numeric();
	}
});
