ma.page.navody = {
	init: function() {
		ma.page.navody.load();
	},

	load: function() {
		ma.page.startLoading({
			title: 'Návody :: Nothrem Sinsky',
			message: 'Čekejte, prosím. Načítám seznam návodů...'
		});

		ma.page.getSubdomains(this._loadCallback.setScope(this));
	},

	_loadCallback: function(subdomains) {
		var
			domain,
			url,
			requests,
			i, cnt;

		requests = new ma.ajax.AjaxCache();
		ma.page.navody.requests = requests;

		for (i = 0, cnt = subdomains.length; i < cnt; i++) {
			domain = subdomains[i];
			requests.add({
				data: {
					method: 'server.getDescription',
					params: {
						domain: domain
					}
				},
				callback: this._descriptionCallback,
				callbackScope: this,
				callbackParams: {
					domain: domain
				}
			});
		}

		//send two requests at one to get quicker response
		requests.send();
	},

	_descriptionCallback: function(response, success, params) {
		var
			requests = ma.page.navody.requests,
			desc,
			dom;

		//first send next request (if any remains)...
		if (requests.length()) {
			requests.send();
		}
		else {
			ma.page.body.removeChild('navodyLoading');
		}

		//... then process the result
		if (success) {
			desc = response.json.result.description;
			if (!desc || !desc.isNavod) {
				return; //invalid description or not the one we need - ignore this domain
			}

			dom = {
				className: 'navod',
				children: [
					{
						tagName: 'h2',
						innerHTML: desc.name
					},
					{
						tagName: 'p',
						innerHTML: desc.description
					},
					{
						tagName: 'p',
						innerHTML: 'Autor: ' + desc.author
					},
					{
						tagName: 'p',
						innerHTML: 'Kontakt: ' + desc.email
					},
					{
						tagName: 'a',
						href: 'http://' + desc.domain + '.chobits.ch/',
						innerHTML: 'Přejít na návod'
					}
				]
			};

			if (desc.logo) {
				if (300 < desc.logo.height || 600 < desc.logo.width) {
					desc.logo.height = Math.half(desc.logo.height);
					desc.logo.width = Math.half(desc.logo.width);
				}
				dom.children = [
					{
						tagName: 'table',
						width: '100%',
						children: [
							{
								tagName: 'tbody',
								children: [
									{
										tagName: 'tr',
										children: [
											{
												tagName: 'td',
												className: 'popis',
												children: dom.children
											},
											{
												tagName: 'td',
												className: 'logo',
												children: [
													{
														tagName: 'img',
														src: 'http://' + desc.domain + '.chobits.ch/' + desc.logo.url,
														width: desc.logo.width,
														height: desc.logo.height,
														style: {
															'float': 'right'
														}
													}
												]
											}
										] //tr items
									}
								] //tbody items
							}
						] ///table items
					}
				];
			}

			ma.page.body.add(dom).animateIn();
		} //this domain does not contain description - ignore it

		if (!requests.length()) {
			ma.page.stopLoading();
		}
	}
};

