jQuery.fn.extend({
	PriceToFloat : function()
	{
		if (this.is(':input'))
		{
			var Price = this.val();
		} else
		{
			var Price = this.text();
		}

		return $.PriceToFloat(Price);
	},

	FloatToPrice : function(Price)
	{
		Price = jQuery.FloatToPrice(Price);

		if (this.is(':input'))
		{
			this.val(Price);
		} else
		{
			this.text(Price);
		}

		return this;
	},

	InvokeEvent : function()
	{
		if (!this.hasClass('newContent'))
		{
			this.addClass('newContent');
		}
		$.InvokeEvent();
	},
	tinyCounter : 0,
	getUniqueID : function()
	{
		var id = this.attr('id');
		if (id == '')
		{
			id = 'isi_cms_autoid_' + jQuery.fn.tinyCounter++;
			this.attr('id', id);
		}
		return id;
	}
});

jQuery.extend({
	LoadedScriptStyles : {},
	InvokeEvent : function()
	{
		$('.newContent').trigger('initcontent').removeClass('newContent');
	},
	RegisterLoadEvent : function(load_function)
	{
		$('.newContent').live('initcontent', load_function);
	},
	OpenWindow : function(url, id)
	{
		window.open(url, id, "width=850,height=670,scrollbars=yes,location=no");
	},
	LoadForm : function(options)
	{
		options = jQuery.extend({
			title : "",
			buttons : {
				"Schließen" : function()
				{
					$(this).dialog("close");
				}
			},
			height : 550,
			width : 600,
			id : "form_window",
			url : "index.php",
			options : "",
			onload : function()
			{
			}
		}, options);

		$('#' + options.id).remove();

		$.get(options.url, {}, function(response)
		{
			if (options.title == '')
			{
				options.title = response.title;
			}

			$.LoadCSS(response.style);
			$.LoadJavascript(response.jscript);

			var container = $("<div/>");
			container.attr("title", options.title);
			container.attr("id", options.id);
			container.css("display", "none");
			container.addClass('newContent');
			container.append(response.content);

			$('body').append(container);

			container.dialog({
				height : options.height,
				width : options.width,
				modal : true,
				autoOpen : false,
				close : function()
				{
					$(this).remove();
				},
				buttons : options.buttons
			});

			$.InvokeEvent();

			options.onload();
			container.dialog('open');
		});
	},
	GetSiteletUrl : function(parent, sitelet)
	{
		var url = global_options.ROOT + '/sitelet/';
		url += parent.replace(/\\/g, '/').replace(/^\//g, '');
		url += '/' + sitelet;

		return url;
	},
	/**
	 * Ruft den Inhalt eines Sitelets vom System ab
	 * 
	 * @param string
	 *            parent Klasse von der das Sitelet stammt
	 * @param string
	 *            sitelet Name des Sitelets (ohne Sitelet)
	 * @param object
	 *            options Zusätzliche Optionen
	 * @param onload
	 *            Event das nachdem laden ausgeführt wird
	 * @returns void
	 */
	CallSitelet : function(parent, sitelet, options, onload)
	{
		var url = $.GetSiteletUrl(parent, sitelet);

		options._frontend = global_options._frontend;
		if (typeof options._page == "undefined")
		{
			options._page = global_options._page;
		}

		$.get(url, options, function(response)
		{
			if (typeof response.style != "undefined")
			{
				$.LoadCSS(response.style);
			}
			if (typeof response.values != "undefined")
			{
				$.LoadValues(response.values);
			}
			if (typeof response.jscript != "undefined")
			{
				$.LoadJavascript(response.jscript);
			}

			onload(response);
		}, 'json');
	},
	LoadSiteletInDialog : function(options)
	{
		options = jQuery.extend({
			title : "",
			buttons : {
				"Close" : function()
				{
					$(this).dialog("close");
				}
			},
			height : 550,
			width : 600,
			id : "sitelet_window",
			parent : "",
			sitelet : "",
			options : "",
			onload : function()
			{
			}
		}, options);

		$.CallSitelet(options.parent, options.sitelet, options.options, function(response)
		{
			$('#' + options.id).remove();

			var container = $("<div/>");
			container.attr("title", options.title);
			container.attr("id", options.id);
			container.css("display", "none");
			container.addClass('newContent');
			container.append(response.content);

			$('body').append(container);

			if ($(container).find('.form_submit_block').length > 0)
			{
				options.buttons['Save'] = function(){
					$(this).parents('.ui-dialog:first').find('.form_submit').click();
				};
				$(container).find('.form_submit_block').hide();
			}

			container.dialog({
				height : options.height,
				width : options.width,
				modal : true,
				autoOpen : false,
				close : function()
				{
					$(this).remove();
				},
				buttons : options.buttons
			});

			$.InvokeEvent();

			options.onload();
			container.dialog('open');
		});
	},
	LoadJavascript : function(files)
	{
		for ( var I in files)
		{
			var key = '#script_' + I;
			if (typeof jQuery.LoadedScriptStyles[key] == 'undefined' && $(key).length == 0)
			{
				var script = $('<script src="' + files[I] + '" />').attr('type', 'text/javascript').attr('id', 'script_' + I);
				$('body').append(script);
				jQuery.LoadedScriptStyles[key] = true;
			}
		}
	},
	LoadValues : function(values)
	{
		for ( var I in values)
		{
			var key = '#script_' + I;
			if (typeof global_options[I] == 'undefined')
			{
				global_options[I] = values[I];
			}
		}
	},
	LoadCSS : function(files)
	{
		for ( var I in files)
		{
			var key = '#style_' + I;
			if (typeof jQuery.LoadedScriptStyles[key] == 'undefined' && $(key).length == 0)
			{
				var script = $('<link href="' + files[I] + '" />').attr('type', 'text/css').attr('id', 'style_' + I).attr('rel', 'stylesheet');
				$('head').append(script);
				jQuery.LoadedScriptStyles[key] = true;
			}
		}
	},

	PriceToFloat : function(Price)
	{
		if (typeof (Price) == 'undefined')
		{
			return 0.00;
		}

		try
		{
			var value = parseFloat(Price.replace(/,/, '.'));
			return (isNaN(value)) ? 0.00 : value;
		} catch (err)
		{
			return 0.00;
		}

		return null;
	},
	FloatToPrice : function(Price)
	{
		if (isNaN(Price))
		{
			return '0,00';
		}
		Price = parseFloat(Price).toFixed(2);
		Price += '';
		return Price.replace(/\./, ',');
	},
	randomString : function(string_length)
	{
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var randomstring = '';
		for ( var i = 0; i < string_length; i++)
		{
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum, rnum + 1);
		}
		return randomstring;
	},
	IgnoreKeyCode : function(keyCode)
	{
		var IgnoreCodes = new Array(13, 37, 38, 39, 40, 9, 16, 17, 65, 67, 45);
		for ( var i = 0; i < IgnoreCodes.length; i++)
		{
			if (IgnoreCodes[i] == keyCode)
			{
				return true;
			}
		}

		return false;
	}
});

