
var Blog = new Class({

	initialize: function() {

		this.revertFields(['searchText', 'blogSuggestion']);
		this.setupWidget();

		var suggestion = $('blogSuggestion');
		suggestion.retrieve('initialValue', function() {
			return this.value;
		}.apply(suggestion));

		$('blogSuggestionSubmitButton').addEvent('click', function() {
			var error = 0;
			if (suggestion.value.trim() == suggestion.retrieve('initialValue').trim()) error++;
			if (suggestion.value.trim().length < 4) error++;
			if (!suggestion.value.trim().contains(' ')) error++;
			if (error) {
				suggestion.addClass('highlight-error');
				alert(_("Please type a complete sentence"));
				return;
			}
			$('blogSuggestionForm').submit();
		});

		this.showStatus();

		this.initAbout();

	},
	
	setupWidget: function() {
		
		// Setup Widget Tabs
		if ($('commentslatest')) {
			$('commentslatest').addClass('show');
			var items = $$('#widgetContainer div.tab a');
			items.addEvent('click', function() {
				if (this.hasClass('active')) return;
				items.each(function (item,index) {item.removeClass('active');});
				this.addClass('active');
				$('commentslatest').toggleClass('show');
				$('commentspopular').toggleClass('show');
			});
		}

		// Setup Widget Tags
		var tabs = $('tagWidget').getElements('ul.tags li');
		var lastTab = null;
		tabs.each(function(tab,index) {
			if (index == 0) tab.addClass('first');
			if (lastTab && lastTab.hasClass('active')) tab.addClass('belowActive');
			lastTab = tab;
		});
	},

	revertFields: function(fields) {
		fields.each(function(field,index) {
			var e = $(field);
			if (e.get('type') != 'text') return;
			e.initialValue = e.value;
			e.addEvents({
				'focus': function() {
					if (this.value == this.initialValue) this.value = '';				
				},
				'blur': function() {
					if (!(this.value.trim())) this.value = this.initialValue;
				}
			});
		}, this);
	},

	showStatus: function() {
		if (_suggestionsubmit || window.location.href.toLowerCase().contains('suggestion')) {
			alert(_("Thanks for your suggestion to the blog"));
			window.location = _searchurl;
		}
	},

	initAbout: function() {

		var aboutAuthor = $('aboutauthor');

		if (!aboutAuthor) return;

		aboutAuthor.addClass('author1'); // the default author is author1

		var aboutContent = $('aboutblog').getElement('div.content');
		var aboutBottom = $('aboutblog').getElement('div.bottom');

		// setup author buttons
		aboutAuthor.getElements('a.aboutbutton').each(function(item, index) {
			item.addEvent('click', function() {
				var id = item.get('id').replace(/\D+/, '');
				if (aboutBottom.hasClass('open')) {
					//aboutBottom.fireEvent('click');
				}
				//this.about.delay(500, this, id);
				this.about(id);
			}.bind(this));
		}.bind(this));

		// defaults to open
		//aboutContent.addClass('open');
		//aboutBottom.addClass('open');

		// animate on click
		aboutContent.store('fx', aboutContent.get('tween', {property:'height',duration:600}).addEvent('complete', function() {
			aboutContent.toggleClass('open');
			aboutBottom.toggleClass('open');
		}));

		aboutBottom.addEvent('click', function(aboutContent, aboutBottom) {

			return;

			var fx = aboutContent.retrieve('fx');
			if (fx.timer) return;
			fx.start(aboutContent.hasClass('open') ? 0 : aboutContent.getScrollSize().y);
		}.pass([aboutContent, aboutBottom]));

		//(function() {aboutBottom.fireEvent('click');}).delay(500);
	},

	about: function(id) {
		var url = _searchurl + "/category/onlinecity/?user=" + id;
		window.location = url;
	}
});

var blog = {};
window.addEvent('domready', function() {
	blog = new Blog();
	/*
	var exception = '';
	$H(e).each(function(val,key) {
		exception += key + ":\r\n" + val + "\r\n\r\n";
	});
	try {
		if (Browser.Engine.gecko) {
			var exception = e.name + ' Exception in\r\n' + e.fileName + ' at line ' + e.lineNumber + "\r\n\r\n" + e.message;
			console.log(exception);
		} else {
			alert(exception);
		}
	} catch (e) {
		alert(exception);
	}*/
});
