/**
 * ContentLoader Class
 * 
 * @author   Volodymyr Iatsyshyn
 * @date     2009-06-01
 * @see
 */

if (!window.Keepa)
	var Keepa = {};

function xxx(a1, a2, a3, a4, a5) {
  oldxxx.apply(this, new Array(a1, a2, a3));
}




var oldxxx = null;

dojo.provide('Keepa.ContentLoader');

dojo.declare('Keepa.ContentLoader', null, {
	containerClass: null,
	
	constructor: function (containerClass) {
		this.containerClass = containerClass;
		
		dojo.query('*[project-id]').forEach(function (e) {
			dojo.style(e, 'cursor', dojo.isIE > 5 && dojo.isIE < 8 ? 'hand' : 'pointer');
			e.mouseUpBind_ = dojo.connect(e, 'mouseup', this, 'onLinkMouseUp_');
		}, this);		
		
		dojo.subscribe('on-current-project-changed', this, 'onCurrentProjectChanged_');
		dojo.subscribe('open-menu-for', this, 'onOpenMenuFor_');
	},
	
	onOpenMenuFor_: function (projectId) {
	    var b = false;
	    dojo.query('*[project-id=' + projectId + ']').forEach(function (e) {
			if (!b && dojo.hasClass(e, 'design')) {
			    dojo.publish('open-design-menu', []);
			    b = !b;
			}
			else if (!b && dojo.hasClass(e, 'illustration')) {
			    dojo.publish('open-illustration-menu', []);
			    b = !b;
			}
		}, this);
	},
	
	onLinkMouseUp_: function (event) {
		if (event.button < 2) {
			dojo.publish('try-change-project', 
				[dojo.attr(event.currentTarget, 'project-id'), 
				 dojo.hasClass(event.currentTarget, 'no-default')]);
		}
		
		return this;
	},
	
	onCurrentProjectChanged_: function (projectId) {
		window.stepTwo();
		dojo.query('.' + this.containerClass).forEach(function (e) {
			e.innerHTML = 'Loading...';
		});

	  if (dojo.isOpera && XMLHttpRequest.prototype.open !== xxx) {
	    oldxxx = XMLHttpRequest.prototype.open;
			XMLHttpRequest.prototype.open = xxx;

	  }
		
		dojo.xhrGet({
			url: 'data/' + projectId + '.html', 
			load: dojo.hitch(this, 'onContentLoaded_'),
			error: dojo.hitch(this, 'onContentLoadError_')
		});
		
		return this;
	},
	
	onContentLoaded_: function (response, ioArgs) {
		dojo.query('.' + this.containerClass).forEach(function (e) {
			e.innerHTML = response;
			
			dojo.query('*[project-id]', e).forEach(function (el) {
				dojo.style(el, 'cursor', dojo.isIE > 5 ? 'hand' : 'pointer');
				if (!el.mouseUpBind_) {
					el.mouseUpBind_ = dojo.connect(el, 'mouseup', this, 'onLinkMouseUp_');
				}
			}, this);
			
			dojo.style(e.parentNode, 'height', '');
			var c_box = dojo.contentBox(e);			
        	dojo.style(e.parentNode, 'height', Math.max(c_box.h + 200, 1200) + 'px');			
		}, this);
		
		dojo.publish('container-size-changed', []);
		
		return this;
	},
	
	onContentLoadError_: function (response, ioArgs) {
		dojo.query('.' + this.containerClass).forEach(function (e) {
			e.innerHTML = '<span style="color:red">' + ioArgs.url + ' is not available</span>';
		});
		
		return this;
	}
});

dojo.mixin(Keepa.ContentLoader, {
	initialize: function () {
		new Keepa.ContentLoader('project-container');
		return this;
	}
});

dojo.addOnLoad(Keepa.ContentLoader, 'initialize');