var comments	= {

	$container		: null,
	$form			: null,
	$list			: 0,
	$submitBTN		: null,

	$dataFields		: [],

	$window			: null,
	$lightbox		: null,
	$embedArea		: null,
	$minHeight		: 0,

	$default		: {},
	$data			: {},

	/**
	* CONTROL METHODS
	*/
	addComment: function()
	{
		if(comments.$isLocked	=== true)
		{
			return false;
		}

		// Store input
		$.each(comments.$dataFields, function()
		{
			comments.$data[this.attr('name')]	= this.val();
		});

		if(comments.validate() === true)
		{
			comments.lock();
			comments.saveComment();
		}
		else
		{
			comments.displayErrorMSG();
			comments.unlock();
		}
	},
	saveComment: function()
	{
		$.post(baseURL + 'webservice/comments',
		{
			data	: comments.$data,
			pageID	: comments.$container.find('.pageID').html()
		},
		function(data)
		{
			if(data.status == 'ok')
			{
				comments.showComment(data.response);
				comments.resetDataFields();
			}
			else if(data.status == 'error')
			{
				alert(data.response);
			}
			comments.unlock();
		},
		'json');
	},
	showComment: function(data)
	{
		var $label	= comments.$list.find('.label');
		var $count	= $label.find('.count');

		$count.html(parseInt($count.html()) + 1);
		//$label.after('<div class="comment latestcomment"><div class="name">'+data.name+'</div><div class="date">'+data.date+'</div><div class="clear"></div><div class="bodytext">'+data.comment.replace('\n','<br />')+'</div></div>');
		$('<div class="comment latestcomment"><div class="name">'+data.name+'</div><div class="date">'+data.date+'</div><div class="clear"></div><div class="bodytext">'+data.comment.replace('\n','<br />')+'</div></div>').appendTo(comments.$list);
		
		Cufon.replace('#page #comments .name', { fontFamily	: 'BrauerF24-Black'	});
		setBoxHeights(comments.$list.find('.latestcomment').height());
		window.scrollTo(0,parseInt($('body').css('height')));
	},

	/**
	* ERRORS/VALIDATION METHODS
	*/
	lock: function()
	{
		comments.$isLocked	= true;
	},
	unlock: function()
	{
		comments.$isLocked	= false;
	},
	validate: function()
	{
		var _isValid		= true;
		comments.$errors	= new Array();

		// Validate Identity
		$.each(comments.$dataFields, function()
		{
			var $input	= this;
			var _name	= $input.attr('name');
			var _value	= $input.val();

			// Validate Identity
			switch(_name)
			{
				case 'email':
					if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(_value) === false)
					{
						comments.$errors.push('Du mangler at skrive din email.');
						_isValid	= false;
					}
					break;	

				default:
					if(!_value || _value == comments.$default[_name])
					{
						comments.$errors.push('Husk at udfylde alle felterne.');
						_isValid	= false;
					}
					break;
			}
		});
		return _isValid;
	},
	displayErrorMSG: function()
	{
		if(comments.$errors.length > 0)
		{
			var _error	= '';
			var array	= comments.$errors.unique();
			$.each(array, function()
			{
				_error	+= this + '\n';
			});
			alert(_error);
		}
	},
	resetDataFields: function()
	{
		$.each(comments.$dataFields, function()
		{
			this.val(comments.$default[this.attr('name')]);
		});
	},

	/**
	* LIGHTBOX METHODS
	*/
	showLightbox: function()
	{
		window.scrollTo(0,0);

		comments.$lightbox.find('.content').removeClass('youtube-lightbox');
		comments.$embedArea.empty();
		comments.$embedArea.html($('#comments-terms-content').html());
		comments.$lightbox.fadeIn(600);
		
		var _winHeight	= comments.$window.height() + comments.$window.scrollTop();
		comments.$lightbox.find('.background').height(Math.max(comments.$minHeight, _winHeight));

		Cufon.replace('#comments-terms-content h2', { fontFamily: 'BrauerF24-Black' });
	},
	hideLightbox: function(embedCode)
	{
		comments.$embedArea.empty();
		comments.$lightbox.hide();
	},

	/** 
	* EVENT HANDLERS
	*/
	focusHandler: function(e)
	{
		var $obj	= $(e.target);
		if($obj.val() == comments.$default[$obj.attr('name')])
		{
			$obj.val('');
		}
	},
	blurHandler: function(e)
	{
		var $obj	= $(e.target);
		if($obj.val() == '')
		{
			$obj.val(comments.$default[$obj.attr('name')]);
		}
	},

	/**
	* INIT METHODS
	*/
	init: function()
	{
		comments.$container	= $('#comments');
		comments.$form		= comments.$container.find('.form');
		comments.$list		= comments.$container.find('.list');

		comments.initInput();
		comments.initTextarea();
		comments.initSubmitBTN();
		comments.initLightbox();
	},
	initInput: function()
	{
		comments.$form.find('.input input').each(function()
		{
			var $input		= $(this);

			comments.$default[$input.attr('name')]	= $input.val();
			comments.$dataFields.push($input);

			$input.focus(comments.focusHandler);
			$input.blur(comments.blurHandler);
			$input.keyup(function(e) { if(e.keyCode == 13) { comments.addComment(); } });
		});
	},
	initTextarea: function()
	{
		var $textarea	= comments.$container.find('textarea');
		comments.$default[$textarea.attr('name')]	= $textarea.val();
		comments.$dataFields.push($textarea);
		
		$textarea.focus(comments.focusHandler);
		$textarea.blur(comments.blurHandler);
	},
	initSubmitBTN: function()
	{
		comments.$submitBTN	= comments.$container.find('.submit');
		comments.$submitBTN.bind('click', comments.addComment);
	},
	initLightbox: function()
	{
		comments.$window	= $(window);
		comments.$lightbox	= $('#lightbox');
		comments.$embedArea	= $('#embedarea');
		comments.$minHeight	= $('#wrapper').height() + $('#footer').height() + 190;

		comments.$lightbox.find('.close').bind('click', comments.hideLightbox);
		comments.$lightbox.find('.background').bind('click', comments.hideLightbox);

		comments.$container.find('.terms span').click(comments.showLightbox);
	}
};

$(document).ready(comments.init);
