/**
 * HtfImagePopupView
 */
function HtfImagePopupView(helper) {
	FlDefaultView.call(this, helper);

	helper.allocateZDepth(2);

	this.bounds = { 'x': 0, 'y': 0, 'w': 100, 'h': 100 };

	var _this = this;

	// Átméretezési animáció segédváltozói.
	this.size0 = null;
	this.size1 = null;
	this.twResize = new HTTween(function(x, mode) { _this.widgetResizeAnim(x, mode); }, 'sinoidal', 800).init(0);
}

jgtc.__extends(HtfImagePopupView, FlDefaultView);

HtfImagePopupView.prototype.widgetFadeAnim = function(x, mode) {
	if(is.ie) this.shade.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (x * 60) + ")";
	else this.shade.style.opacity = x * 0.6;

	FlDefaultView.prototype.widgetFadeAnim.call(this, x, mode);
}

HtfImagePopupView.prototype.widgetResizeAnim = function(x, mode) {
	if(is.ie) this.welements.content.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (x * 100) + ")";
	else this.welements.content.style.opacity = x;

	this.setSize(Math.round(x * (this.size1.w - this.size0.w) + this.size0.w), Math.round(x * (this.size1.h - this.size0.h) + this.size0.h));
}

HtfImagePopupView.prototype.createJsonML = function() {
	var jsonml = this.helper.getParam('skin');
	if(!jsonml) {
		var d_content;
		switch(this.helper.getParam('type')) {
			case 'img':
				d_content = [ "img", { 'src': jgtc.rootImgURL + "spacer.gif", 'fl:element': "content", 'fl:width': 0, 'fl:height': 0, 'fl:button': "close" } ];
				break;
			case 'swf':
				d_content = [ "div", { 'fl:element': "content", 'fl:width': 0, 'fl:height': 0, 'style': "overflow: hidden;" } ];
				break;
			case 'url':
				d_content = [ "iframe", { 'src': "about:blank", 'frameBorder': 0, 'fl:element': "content", 'fl:width': 0, 'fl:height': 0, 'style': "overflow: hidden;" } ];
				break;
			default:
				throw "Gallery. Unsupported type: '" + this.helper.getParam('type') + "'";
		}

		jsonml = [ "div", { 'class': this.helper.getParam('className'), 'fl:left': -80, 'fl:top': -80, 'fl:width': 80, 'fl:height': 80 },
			[ "p",
				[ "a", { 'class': "close", 'fl:button': "close" }, "Bezár" ]
			],
			[ "ins", { 'style': 'visibility: hidden;', 'fl:width': 2, 'fl:visibility': "loaded" },
				d_content
			],
			[ "p",
				[ "b", { 'fl:element': "title" } ]
			]
		];
	}
	return jsonml;
}

HtfImagePopupView.prototype.createShadeJsonML = function() {
	var jsonml = this.helper.getParam('skinShade');
	if(!jsonml) {
		jsonml = [ "div", { 'class': this.helper.getParam('shadeClassName'), 'fl:width': "client", 'fl:height': "client" } ];
	}
	return jsonml;
}


HtfImagePopupView.prototype.create = function() {
	if([ "url", "img", "swf" ].indexOf(this.helper.getParam('type')) == -1) {
		throw "HtfImagePopupView. Unsupported type: " + this.helper.getParam('type');
	}

	var jsutil = this.createJsonMLUtil();
	this.widget = jsutil.build(this.createJsonML());
	this.shade = jsutil.build(this.createShadeJsonML());

	document.body.appendChild(this.shade);
}

HtfImagePopupView.prototype.initWidget = function() {
	var _this = this;
	jgtc.captureEvent(window, "resize", function() {
		with(_this.bounds) {
			_this.setSize(w, h);
		}
	}, false);

	this.setSize(100, 100);
}

HtfImagePopupView.prototype.getZIndex = function() {
	return Number(this.shade.style.zIndex);
}

HtfImagePopupView.prototype.setZIndex = function(z) {
	this.shade.style.zIndex = z;
	this.widget.style.zIndex = z + 1;
}

HtfImagePopupView.prototype.setLoadState = function(loaded, params) {

	if(loaded) {
		this.widget.className = this.helper.getParam('className');

		var w, h;
		var hparam = this.helper.getParam;
		var html = document.compatMode == "BackCompat" ? document.body : document.documentElement;
		var wmax = html.clientWidth - hparam('paddingLeft') - hparam('paddingRight');
		var hmax = html.clientHeight - hparam('paddingTop') - hparam('paddingBottom');

		w = hparam('width') || params.width;
		h = hparam('height') || params.height;

		var r = Math.min(wmax / w, hmax / h);
		if(r < 1) {
			w = Math.round(w * r);
			h = Math.round(h * r);
		}

		if(hparam('fancy') && hparam('type') == 'img') {
			this.size0 = { 'w': this.bounds.w, 'h': this.bounds.h };
			this.size1 = { 'w': w, 'h': h };
			this.twResize.start(0, 1);
		} else {
			this.setSize(w, h);
		}

		console.log("G: %d, %d", w, h);
	} else {
		this.widget.className = this.helper.getParam('className') + " " + this.helper.getParam('loadingClassName');
	}

	FlDefaultView.prototype.setLoadState.call(this, loaded, params);
}

HtfImagePopupView.prototype.setSize = function(w, h) {
	var html = document.compatMode == "BackCompat" ? document.body : document.documentElement;
	var hparam = this.helper.getParam;
	var x = Math.round((html.clientWidth - w) / 2) + hparam('paddingLeft');
	var y = Math.round((html.clientHeight - h) / 2) + hparam('paddingTop');

	this.setBounds(x, y, w, h);
}

HtfImagePopupView.prototype.destroy = function() {
	FlDefaultView.prototype.destroy.call(this);

	document.body.removeChild(this.shade);
}

HtfImagePopupView.prototype.getParamsValidator = function() {
	return [
		[ 'width', 'number', false ],
		[ 'height', 'number', false ],
		[ 'paddingTop', 'number', 40 ],
		[ 'paddingRight', 'number', 40 ],
		[ 'paddingBottom', 'number', 40 ],
		[ 'paddingLeft', 'number', 40 ],

		[ 'close', 'boolean', true ],
		[ 'reload', 'fixed', false ],
		[ 'minmax', 'fixed', false ],
		[ 'fixabs', 'fixed', false ],
		[ 'fancy', 'boolean', true ],
		[ 'skin', 'object', null ],
		[ 'skinShade', 'object', null ],

		[ 'className', 'string', "htf_popup" ],
		[ 'shadeClassName', 'string', "htf_popup_shade" ],
		[ 'loadingClassName', 'string', "htf_popup_loading" ],

		[ 'title', 'string', "" ],
		[ 'titleLoading', 'string', "loading..." ],

		[ 'swf', 'object', {} ],

		[ 'autoBounds', 'fixed', true ]
	];
}

/**
 * HtfSimplePopupView
 */
function HtfSimplePopupView(helper) {
	FlDefaultView.call(this, helper);

	helper.allocateZDepth(2);

	this.bounds = { 'x': 0, 'y': 0, 'w': 100, 'h': 100 };

	var _this = this;
	this._resizeEventListener = function() {
		with(_this.bounds) {
			var hparam = _this.helper.getParam;
			var html = document.compatMode == "BackCompat" ? document.body : document.documentElement;
			var wmax = html.clientWidth - hparam('paddingLeft') - hparam('paddingRight');
			var hmax = html.clientHeight - hparam('paddingTop') - hparam('paddingBottom');

			var w = _this.originalSize.w;
			var h = _this.originalSize.h;

			var r = Math.min(wmax / w, hmax / h);
			if(r < 1) {
				w = Math.round(w * r);
				h = Math.round(h * r);
			}

			_this.setBounds(0, 0, w, h);
		}
	}
}

jgtc.__extends(HtfSimplePopupView, FlDefaultView);

HtfSimplePopupView.prototype.widgetFadeAnim = function(x, mode) {
	if(is.ie) this.shade.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (x * 60) + ")";
	else this.shade.style.opacity = x * 0.6;

	FlDefaultView.prototype.widgetFadeAnim.call(this, x, mode);
}

HtfSimplePopupView.prototype.createJsonML = function() {
	var jsonml = this.helper.getParam('skin');
	if(!jsonml) {
		var d_content;
		switch(this.helper.getParam('type')) {
			case 'url':
				d_content = [ "iframe", { 'src': "about:blank", 'frameBorder': 0, 'fl:element': "content", 'fl:width': 0, 'fl:height': 0, 'style': "overflow: hidden;", 'scrolling': "no" } ];
				break;
			default:
				throw "Gallery. Unsupported type: '" + this.helper.getParam('type') + "'";
		}

		jsonml = [ "div", { 'class': this.helper.getParam('className'), 'fl:left': 0, 'fl:top': 0, 'fl:width': 0, 'fl:height': 0 },
			[ "p", { 'fl:display': "unloaded" },
				[ "a", { 'class': "close", 'fl:button': "close" }, "Bezár" ]
			],
			[ "del", { 'style': 'display: none;', 'fl:width': 0, 'fl:display': "loaded" },
				d_content
			]
		];
	}
	return jsonml;
}

HtfSimplePopupView.prototype.createShadeJsonML = function() {
	var jsonml = this.helper.getParam('skinShade');
	if(!jsonml) {
		jsonml = [ "div", { 'class': this.helper.getParam('shadeClassName'), 'fl:width': "client", 'fl:height': "client" } ];
	}
	return jsonml;
}

HtfSimplePopupView.prototype.create = function() {
	if([ "url", "img" ].indexOf(this.helper.getParam('type')) == -1) {
		throw "HtfSimplePopupView. Unsupported type: " + helper.getParam('type');
	}

	var jsutil = this.createJsonMLUtil();
	this.widget = jsutil.build(this.createJsonML());
	this.shade = jsutil.build(this.createShadeJsonML());

	document.body.appendChild(this.shade);
}

HtfSimplePopupView.prototype.initWidget = function() {
	var widget = FlDefaultView.prototype.initWidget.call(this);

	var w, h;
	var hparam = this.helper.getParam;
	var html = document.compatMode == "BackCompat" ? document.body : document.documentElement;
	var wmax = html.clientWidth - hparam('paddingLeft') - hparam('paddingRight');
	var hmax = html.clientHeight - hparam('paddingTop') - hparam('paddingBottom');

	w = hparam('width');
	h = hparam('height');

	this.originalSize = { 'w': w, 'h': h };

	var r = Math.min(wmax / w, hmax / h);
	if(r < 1) {
		w = Math.round(w * r);
		h = Math.round(h * r);
	}

	this.setBounds(0, 0, w, h);

	console.log("G: %d, %d", w, h);

	jgtc.captureEvent(window, "resize", this._resizeEventListener, false);
}

HtfSimplePopupView.prototype.getZIndex = function() {
	return Number(this.shade.style.zIndex);
}

HtfSimplePopupView.prototype.setZIndex = function(z) {
	this.shade.style.zIndex = z;
	this.widget.style.zIndex = z + 1;
}

HtfSimplePopupView.prototype.setLoadState = function(loaded, params) {
	if(loaded) {
		this.widget.className = this.helper.getParam('className');
	} else {
		this.widget.className = this.helper.getParam('className') + " " + this.helper.getParam('loadingClassName');
	}

	FlDefaultView.prototype.setLoadState.call(this, loaded, params);
}

HtfSimplePopupView.prototype.setBounds = function(x, y, w, h) {
	var html = document.compatMode == "BackCompat" ? document.body : document.documentElement;
	var x = Math.round((html.clientWidth - w) / 2);
	var y = Math.round((html.clientHeight - h) / 2);

	FlDefaultView.prototype.setBounds.call(this, x, y, w, h);
}

HtfSimplePopupView.prototype.destroy = function() {
	FlDefaultView.prototype.destroy.call(this);

	document.body.removeChild(this.shade);

	jgtc.releaseEvent(window, "resize", this._resizeEventListener, false);
}

HtfSimplePopupView.prototype.getParamsValidator = function() {
	return [
		[ 'width', 'number', 800 ],
		[ 'height', 'number', 600 ],
		[ 'paddingTop', 'number', 0 ],
		[ 'paddingRight', 'number', 0 ],
		[ 'paddingBottom', 'number', 0 ],
		[ 'paddingLeft', 'number', 0 ],

		[ 'close', 'fixed', true ],
		[ 'reload', 'fixed', false ],
		[ 'minmax', 'fixed', false ],
		[ 'fixabs', 'fixed', false ],
		[ 'fancy', 'boolean', true ],
		[ 'skin', 'object', null ],
		[ 'skinShade', 'object', null ],

		[ 'className', 'string', "htf_popup" ],
		[ 'shadeClassName', 'string', "htf_popup_shade" ],
		[ 'loadingClassName', 'string', "htf_popup_loading" ],

		[ 'autoBounds', 'fixed', true ]
	];
}


function HTPopup(vElement, descriptor) {
	if(typeof vElement == "string") vElement = document.getElementById(vElement);

	jgtc.captureEvent(vElement, "click", function (e) {
		if(!e) e = event;
		jgtc.discardEvent(e);

		if(!'type' in descriptor) descriptor.type = 'url';
		if(typeof descriptor.view != 'function') descriptor.view = HtfImagePopupView;
		descriptor.ref = vElement.href;
		if(vElement.title) descriptor.title = vElement.title;

		console.dir(descriptor);

		HTAfl.open("ht_popup", descriptor);
	}, true);
}

function HTInPopup(vElement, descriptor) {
	if(typeof vElement == "string") vElement = document.getElementById(vElement);

	descriptor = jgtc.validateObject(descriptor, [
		[ 'dWidthMax', 'number', false ],
		[ 'dHeightMax', 'number', false ],
		[ 'dWidth', 'number', false ],
		[ 'dHeight', 'number', false ]
	], true);

	var widget = HTAfl.getSelfWidget();
	var bounds = null;
	var origSize = null;

	widget.htfAddMessageListener("rebound", function(wname, msgName, msg) {
		_re(msg);
	});

	widget.htfAddMessageListener("load", function(wname, msgName, msg) {
		_re(widget.htfGetBounds());
	});

	function _re(b) {
		if(vElement.offsetWidth == 0) {
			return;
		}

		if(origSize == null) {
			origSize = { 'w': vElement.offsetWidth, 'h': vElement.offsetHeight };
		}

		if(bounds != null && b.w == bounds.w && b.h == bounds.h) {
			return;
		}

		bounds = b;

//		console.log("re: %d %d", b.w, b.h);

		var dw = descriptor.dWidthMax;
		var dh = descriptor.dHeightMax;
		if(dh !== false || dw !== false) {
			var wmax = b.w + (dw === false ? 0 : dw);
			var hmax = b.h + (dh === false ? 0 : dh);

			w = origSize.w;
			h = origSize.h;

//			console.log('_re: %d, %d, %d, %d', w, h, wmax, hmax);

			var r = Math.min(wmax / w, hmax / h);
			if(r < 1) {
				w = Math.round(w * r);
				h = Math.round(h * r);

				if(dw !== false) vElement.style.width = w + "px";
				if(dh !== false) vElement.style.height = h + "px";
			}
		}

		dw = descriptor.dWidth;
		dh = descriptor.dHeight;
		if(dw !== false) vElement.style.width = (b.w + dw) + "px";
		if(dh !== false) vElement.style.height = (b.h + dh) + "px";
	}

	_re(widget.htfGetBounds());
}

/**
 * Automata setup, onloadkor (ht:popup)
 */
jgtc.captureEvent(window, "load", function() {
	var elem = document.getElementsByTagName("*");
	for(var i = 0; i < elem.length; i++) {
		var element = elem[i];
		var descr;
		var d;
		if((descr = element.getAttribute("ht:popup")) != null) {
			try {
				eval("d=eval({" + descr + "});");
				new HTAfl.getManagerWindow().HTPopup(element, d);
			} catch(e) {
				console.warn("HTPopup, error while init: %o", e);
			}
		} else if((descr = element.getAttribute("ht:ipopup")) != null) {
			console.log("ht:ipopup - %s", descr);

			try {
				eval("d=eval({" + descr + "});");
				new HTInPopup(element, d);
			} catch(e) {
				console.warn("HTInPopup, error while init: %o", e);
			}
		}
	}

	console.log("Site gallery helper inited...");
});

