Ext.namespace("Aino");

Aino.Search = Ext.extend(Ext.util.Observable, {

	emptySearchString: 'Zadajte vyhľadávané slovo....',

	constructor: function(config) {

		Ext.apply(this, config || {});

		this.el = Ext.get('vyhledavani-blok');

		this.el.child('.aino').on("click", function(e, t, o) {
			e.stopEvent();
			this.selectSearch(this.el.child('.aino'));
		}, this);

		this.el.child('.google').on("click", function(e, t, o) {
			e.stopEvent();
			this.selectSearch(this.el.child('.google'));
		}, this);

		this.el.child('.googleimg').on("click", function(e, t, o) {
			e.stopEvent();
			this.selectSearch(this.el.child('.googleimg'));
		}, this);

		this.el.child('.vyhledat').on("click", function(e, t, o) {
			e.stopEvent();
			this.doSearch();
		}, this);

		this.inputEl = this.el.child('input[type="text"]');

		this.inputEl.on({
			scope: this,
			focus: function(e, t, o) {
				if (this.isEmpty()) {
					t.value = '';
					this.updateColor();
				}
			},
			blur: function(e, t, o) {
				if (this.isEmpty()) {
					this.showSearchHint();
				}
			},
			keyup: function(e, t, o) {
				if (e.getKey() == e.RETURN) {
					e.stopEvent();
					this.doSearch();
				}

				if (e.getKey() == e.ESC) {
					e.stopEvent();
					t.blur();
					this.showSearchHint();
				}
			}
		});

		if (this.isEmpty()) {
			this.showSearchHint();
		} else {
			this.updateColor();
		}

		Aino.Search.superclass.constructor.call(this, config);
	},

	showSearchHint: function() {
		this.inputEl.dom.value = this.emptySearchString;
		this.updateColor();
	},

	selectSearch: function(t) {
		this.el.select('ul li').each(function(el) {
			el.removeClass('aktivni-vyhledavani');
		});

		t.addClass('aktivni-vyhledavani');
	},

	updateColor: function() {
		if (this.isEmpty() && this.getSearchString() !== '') {
			this.inputEl.setStyle('color', '#979797');
		} else {
			this.inputEl.setStyle('color', '#333');
		}
	},

	getSearchString: function() {
		return this.inputEl.dom.value;
	},

	isEmpty: function() {
		return this.getSearchString() === '' || this.getSearchString() == this.emptySearchString;
	},

	doSearch: function() {
		var c = this.el.child('.aktivni-vyhledavani');

		if (!this.isEmpty()) {
			if (c.hasClass('aino')) {
				window.location = PREFIX + '?q=' + encodeURIComponent(this.getSearchString());
			}

			if (c.hasClass('google')) {
				window.location = 'http://google.sk/search?q=' + encodeURIComponent(this.getSearchString());
			}

			if (c.hasClass('googleimg')) {
				window.location = 'http://images.google.sk/images?q=' + encodeURIComponent(this.getSearchString());
			}
		}
	}
});

Aino.Popular = Ext.extend(Ext.util.Observable, {

	constructor: function(config) {

		Ext.apply(this, config || {});

		this.el = Ext.get('updates-box');
		if (this.el) {
			this.latestBox = this.el.child('ul.latest');
			this.popularBox = this.el.child('ul.popular');
			this.latestLink = this.el.child('a.latest');
			this.popularLink = this.el.child('a.popular');

			this.latestLink.on("click", function(e, t, o) {
				e.stopEvent();
				this.latestBox.setDisplayed(true);
				this.popularBox.setDisplayed(false);
				this.latestLink.removeClass(['posledni-stranky-aktivni', 'posledni-stranky']).addClass('posledni-stranky-aktivni');
				this.popularLink.removeClass(['oblibene-stranky-aktivni', 'oblibene-stranky']).addClass('oblibene-stranky');
			}, this);

			this.popularLink.on("click", function(e, t, o) {
				e.stopEvent();
				this.latestBox.setDisplayed(false);
				this.popularBox.setDisplayed(true);
				this.latestLink.removeClass(['posledni-stranky-aktivni', 'posledni-stranky']).addClass('posledni-stranky');
				this.popularLink.removeClass(['oblibene-stranky-aktivni', 'oblibene-stranky']).addClass('oblibene-stranky-aktivni');
			}, this);
		}

		Aino.Popular.superclass.constructor.call(this, config);
	}
});

Aino.TextCounter = Ext.extend(Ext.util.Observable, {
	
	maxChars: 255,

	constructor: function(config) {

		Ext.apply(this, config || {});

		this.el.on("keypress", function() {
			if (this.maxChars - this.getCount() < 0) {
				this.el.dom.value = this.el.dom.value.substr(0, this.maxChars);
			}
			this.updateCount();
		}, this);

		this.el.on("keyup", function() {
			if (this.maxChars - this.getCount() < 0) {
				this.el.dom.value = this.el.dom.value.substr(0, this.maxChars);
			}
			this.updateCount();
		}, this);

		this.updateCount();

		Aino.TextCounter.superclass.constructor.call(this, config);
	},

	getCount: function() {
		return this.el.dom.value.length;
	},

	updateCount: function() {
		this.statEl.update('' + (this.maxChars - this.getCount()));
	}
});

Aino.AddPage = Ext.extend(Ext.util.Observable, {

	constructor: function(config) {

		Ext.apply(this, config || {});

		this.el = Ext.getBody().child('.pridani-form');
		if (this.el) {
			var ta = this.el.child('textarea[name="suggest_category"]');
			new Aino.TextCounter({
				statEl: ta.next('.znaky'),
				el: ta
			});

			ta = this.el.child('textarea[name="name"]');
			new Aino.TextCounter({
				maxChars: 60,
				statEl: ta.next('.znaky'),
				el: ta
			});

			ta = this.el.child('textarea[name="description"]');
			new Aino.TextCounter({
				statEl: ta.next('.znaky'),
				el: ta
			});

			ta = this.el.child('textarea[name="keywords"]');
			new Aino.TextCounter({
				statEl: ta.next('.znaky'),
				el: ta
			});
		}

		Aino.AddPage.superclass.constructor.call(this, config);
	}
});

Aino.Ranks = Ext.extend(Ext.util.Observable, {

	constructor: function(config) {

		Ext.apply(this, config || {});

		this.el = Ext.getBody().child('.instituce');

		Aino.Ranks.superclass.constructor.call(this, config);

		if (this.el && this.el.child('.pagelink')) {
			Ext.Ajax.request({
				method: 'POST',
				url: PREFIX + 'rpc',
				jsonData: {
					method: 'getRanks',
					params: [parseInt(this.el.child('.pagelink').getAttribute('x-id'), 10)]
				},
				scope: this,
				success: function(response) {
					var responseObj = Ext.decode(response.responseText, true);
					if (responseObj.result) {
						this.updateRanks(responseObj.result);
					}
				},
				failure: function(response) {
				}
			});
		}
	},

	updateRanks: function(r) {
		this.el.child('.srank-graf').setStyle('width', (r.srank)+'%');
		this.el.child('.pr-graf').setStyle('width', (r.prank * 10)+'%');
		this.el.child('.srank-text').update('' + r.srank + '/100');
		this.el.child('.pr-text').update('' + r.prank + '/10');
		this.el.child('.dmoz-ball').setVisible(r.dmoz);
		var wot = r.wot.split(' ');
		this.el.child('.wot-ball-t').dom.src = this.wotRankToBall(wot[0]);
		this.el.child('.wot-ball-v').dom.src = this.wotRankToBall(wot[1]);
		this.el.child('.wot-ball-p').dom.src = this.wotRankToBall(wot[2]);
		this.el.child('.wot-ball-c').dom.src = this.wotRankToBall(wot[3]);
	},

	wotRankToBall: function(r) {
		var ballNo;
		r = parseInt(r, 10);
		if (r === 0) {
			ballNo = 2;
		} else if (r <= 20) {
			ballNo = 4;
		} else if (r <= 40) {
			ballNo = 5;
		} else if (r <= 60) {
			ballNo = 7;
		} else if (r < 80) {
			ballNo = 6;
		} else {
			ballNo = 3;
		}
		return PREFIX + 'data/site/css/img/ico-kulicky/'+ballNo+'.jpg';
	}
});

Ext.onReady(function() {
	Aino.search = new Aino.Search();
	Aino.popular = new Aino.Popular();
	Aino.addPage = new Aino.AddPage();
	Aino.ranks = new Aino.Ranks();
});

