var app = {
	parentNode: window,
	widgets: {},

	ready: function(){
		window.setTimeout(function(){
			events.dispatch({
				type: "ready",
				target: app
			})
		}, 0);
	},
	destroy: function(){
		events.dispatch({
			type: "destroy",
			target: app
		});

		for (var type in app.__events)
			if (type != "ready")
				for (var handl in app.__events[type])
					delete app.__events[type][handl];
	}
};

new function(){
	app.newWidget = function(prototype) {
		return app.widgets[prototype.name] = new Widget(prototype);
	};

	function Widget(prototype) {
		if (!prototype.name)
			throw new Error("Widget Class must have name defined.");

		this.__class = extend(function(element, settings) {
			events.add(app, "destroy", bind(destroy, this));
			this.__bound = [];

			extend(this, settings, { element: element }).construct();
		}, {
			prototype: extend(
				copy(this._proto),
				prototype
			)
		});
	}

	function destroy(){
		this.destroy();
		this.removeEvent();
		this.unbind.apply(this.__bound);
		delete this.element.__widgets[this.name];
		this.element = null;
	}

	extend(Widget.prototype, {
		run: function(select, settings) {
			if(!select) return;
			select = select instanceof Array
				? select : [select];

			for (var i = 0; i < select.length; i++) {
				var widgets = select[i].__widgets;
				if (!widgets)
					widgets = select[i].__widgets = {};

				(widgets[this.name] = widgets[this.name]
					? extend(widgets[this.name], settings)
					: new this.__class(select[i], settings)
				).initilize();
			}
		},

		_proto: {
			construct: function(){},
			initilize: function(){},
			destroy: function(){},

			addEvent: function(type, method) {
				events.add(this.element, type, method = bind(method, this));
				return method;
			},
			removeEvent: function(type, method) {
				events.remove(this.element, type, method);
			},
			dispatchEvent: function(event) {
				if (typeof event == 'string')
					event = { type: event, target: this.element };
				else if (!event.target)
					event.target = this.element;
				events.dispatch(event);
			},
			bind: function() {
				for (var i = 0, a = arguments; i < a.length; i++) {
					this.element[a[i]] = bind(this[a[i]], this);
					this.__bound.push(a[i]);
				}
			},
			unbind: function() {
				for (var i = 0, a = arguments; i < a.length; i++)
					delete this.element[a[i]]
			}
		}
	});
};

app.newWidget({
	name: 'printVersion',
	sheets: {
		disabled: [],
		added: []
	},
	templates: {
		'printTrigger' : '<a id="printVersion" href="javascript:void(0);">%1</a>',
		'printButtons' : '<div class="print_header" id="printButtons"><img src="css/images/seb.print.logo.png" alt="SEB Enskilda" class="print_logo" /> <ul><li><button onclick="window.print();" class="btn print-now">%1</button></li><li><button class="btn close" id="printClose">%2</button></div></li></ul></div>'
    },
	translations: {
		lt: [ 'Spausdinti', 'Uždaryti' ],
		en: [ 'Print', 'Close' ]
	},
	currentLang: null,
	construct: function() {
		this.currentLang = document.body.className;
		if (!dom('banner') && !dom('broadcast')) {
			var printTrigger = dom.create(this.templates.printTrigger.format(this.translations[this.currentLang][0]));
			this.element.appendChild(printTrigger);
			events.add(dom('printVersion'), 'click', bind(this.switchToPrint, this));
		}
	},
	switchToPrint: function(e) {
		window.switchLayout = false;
		var tags = dom(dom(), 'style').concat(dom(dom(), 'link')), item, i = 0;
		for (; (item = tags[i]); i++) {
			if (item.rel == 'stylesheet' || item.type.indexOf('css') != -1) {
				if (/print/.test(item.media)) {
					var clone = item.cloneNode(false);
					clone.media = 'screen';
					this.sheets.added.push(clone);
					dom.before(clone, item);
				}
				else if (!/all/.test(item.media) && !item.disabled) {
					this.sheets.disabled.push(item);
					item.disabled = true;
				}
			}
		}
		var printButtons = dom.create(this.templates.printButtons.format(this.translations[this.currentLang][0], this.translations[this.currentLang][1]));
		dom.before(printButtons, dom('wr').firstChild);
		events.add(dom('printClose'), 'click', bind(this.switchToDefault, this));
		return false;
	},
	switchToDefault: function() {
		var item, i = 0;
		dom.remove(dom('printButtons'));
		for (; (item = this.sheets.added[i]); i++) {
			dom.remove(item);
		}
		i = 0;
		for (; (item = this.sheets.disabled[i]); i++) {
			item.disabled = false;
		}
	}

});

events.add(app || window, 'ready', function() {
	app.widgets.printVersion.run(dom('links'), {});
});


events.add(window, 'load', function(){
	if (!window.opera) try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {};
});

events.add(window, "unload", app.destroy);
