/**
 * Dialog library proviedes several calls for opening common
 * or special Dialog from jQuerys UI-Dialog
 */
var Dialog = {

	defaultOptions: {
		autoOpen: false
	},

	optionSets: {
		'simpleDialog': {},

		'legalDialog': {
			width: 400,
			height: 380,
			resizable: false,
			dialogClass: 'legal-dialog'
		},

		'shippingDialog': {
			width: 400,
			height: 350,
			resizable: false,
			dialogClass: 'shipping-dialog'
		},

		'nlDialog': {
			width: 400,
			height: 150,
			resizable: false,
			dialogClass: 'nl-dialog'
		},

		'warrantyDialog': {
			width: 400,
			height: 150,
			resizable: false,
			dialogClass: 'warranty-dialog'
		},

		'articleDialog': {
			width: 400,
			height: 150,
			resizable: false,
			dialogClass: 'article-dialog'
		},

		'cartAddSuccessDialog': {
		    width: 220,
		    height: 0,
            resizable: false,
            dialogClass: 'success-dialog',
            open: function() {
            	var self = this;
            	var closeCallBack = function() {
            		$('#cartAddSuccessDialog').dialog('close');
            	};
            	window.setTimeout(closeCallBack, 2500);
            }
		},
		'cartMaxItemsReachedDialog': {
		    width: 220,
		    height: 150,
		    dialogClass: 'max-dialog',
			buttons: {
				'Zum Warenkorb': function() { window.location.href = shop.cartPageUrl},
				'Schließen': function() { $(this).dialog('close'); }
			},
            resizable: false
		}

	},

	loadedIframes: {
		shippingDialog: false,
		legalDialog: false
	},

	init: function() {
		$('.shipping').click(function() {
			$('#shippingDialog').dialog('open');
			if(false == Dialog.loadedIframes.shippingDialog) {
				$('#shippingDialog').prepend('<iframe src='+shop.shippingIframeUrl+' frameborder="0" marginheight="0" marginwidth="0" name="shippingIframe"></iframe>');
				Dialog.loadedIframes.shippingDialog = true;
			}
			return false;
		});
		$('.legal').click(function() {
			$('#legalDialog').dialog('open');
			if(false == Dialog.loadedIframes.legalDialog) {
				$('#legalDialog').prepend('<iframe src='+shop.legalIframeUrl+' frameborder="0" marginheight="0" marginwidth="0" name="legalIframe"></iframe>');
				Dialog.loadedIframes.legalDialog = true;
			}
			return false;
		});

		$('.data').click(function() {
			$('#nlDialog').dialog('open');
			return false;
		});

		$('.warrantyNo').click(function() {
			$('#warrantyDialog').dialog('open');
			return false;
		});

		$('.articleNo').click(function() {
			$('#articleDialog').dialog('open');
			return false;
		});

		$('.dialogContainer').each(function(index, element) {
			$(element).dialog( jQuery.extend(Dialog.defaultOptions, Dialog.optionSets[element.id]) );
		});


	},

	/**
	 * Simple Wrapper for dialog from jquery ui
	 * @param string 	containerId 		id of div to make a dialog of
	 * @param string 	title				optional, null to take title from given div (default)
	 * @param string 	content				optional, null to take content from given div (default)
	 * @param int 		autoClose			optional, in ms to close dialog after <autoClose> seconds, defaut: 0 (no autoclose)
	 * @param object 	additionalOptions	optional, object of adiitional options for dialog
	 */
	callDialog: function(containerId, title, content, autoClose, additionalOptions) {
		title = title || null;
		content = content || null;
		autoClose = autoClose || 0;
		additionalOptions = additionalOptions || {};

		if(title != null) {
			$('#'+containerId).attr('title', title);
		}
		if(content != null) {
			$('#'+containerId).html(content);
		}
		var options = jQuery.extend(Dialog.defaultOptions, additionalOptions);
		$('#'+containerId).dialog(options);
		if(autoClose > 0) {
			var closeIt = function() {
				$('#'+containerId).dialog('close');
			};
			window.setTimeout(closeIt, autoClose);
		}
	},

	/**
	 * simpleDialog ist just a very simple Dialog
	 * Used in Cart-Control...
	 */
	simpleDialog: function(title, content, autoClose, additionalOptions) {
		var additionalOptions = additionalOptions || {};
		var autoClose = autoClose || 0;
		// buttons: { "Ok": function() { $(this).dialog("close"); }}
		var containerSelector = '#simpleDialog';
		// <autoClose> milliseconds  after dialog opened the dialog will close automatic.
		// if autoClose is 0, dialog won't close.

		$(containerSelector).attr('title', title);
		$(containerSelector).html(content);
		var options = jQuery.extend(Dialog.defaultOptions, additionalOptions, {
			open: function(event, ui) {}
		});
		$(containerSelector).dialog(options);
		if(autoClose > 0) {
			var closeIt = function() {
				$(containerSelector).dialog('close');
			};
			window.setTimeout(closeIt, autoClose);
		}
	},

	legalDialog: function() {
		var containerSelector = '#legalDialog';
		$(containerSelector).attr('title', 'AGB');
		$(containerSelector).html('<iframe src='+shop.legalIframeUrl+' name="legalIframe"></iframe>');
		$(containerSelector).dialog({
			width: 400,
			height: 380,
			resizable: false,
			dialogClass: 'legal-dialog'
		});
	},

	shippingDialog: function() {
		var containerSelector = '#shippingDialog';
		$(containerSelector).attr('title', 'Versandkosten');

		$(containerSelector).dialog({
			width: 400,
			height: 350,
			resizable: false,
			dialogClass: 'shipping-dialog'
		});
	},

	end: 0
};
function PrintIframe(){
    frames["legalIframe"].focus();
    frames["legalIframe"].print();
};
