var Item = {

}
Item.Comment = {
	item_type: '',
	item_id: '',
	commentSent: '',
	commentSending: '',
	scrollUpdating: false,
	scrollUpdate: true,
	rows: 5,
	rated: [],
	init: function() {
		Item.Comment.setupEvents();
		setTimeout('Item.Comment.updateAge()', 30 * 1000);
	},
	setupEvents: function() {
		$('.item-comments .item-comment').unbind('mouseenter');
		$('.item-comments .item-comment').mouseenter(function () {
			$(this).addClass('current');
		});
		$('.item-comments .item-comment').unbind('mouseleave');
		$('.item-comments .item-comment').mouseleave(function () {
			$(this).removeClass('current');
		});
		
		$('.item-comments .write-comment textarea').focus(function() {
			if ($(this).hasClass('initial')) {
				$(this).val('');
				$(this).removeClass('initial');
			}
			$(this).next().show();
			Layout.columns();
		});
		
		$('.item-comments .item-comment').each(function (x, item) {
			var id = $(item).attr('id').split("_");
			id = id[id.length - 1];
			
			$(item).find('.item-comment-actions .thumbsUp').unbind('click');
			$(item).find('.item-comment-actions .thumbsUp').click(function() {
				if (!$(item).hasClass('done')) {
					Item.Comment.thumbsUp(id);
				}
			});
			$(item).find('.item-comment-actions .thumbsDown').unbind('click');
			$(item).find('.item-comment-actions .thumbsDown').click(function() {
				if (!$(item).hasClass('done')) {
					Item.Comment.thumbsDown(id);
				}
			});
			$(item).find('.item-comment-actions .reply').unbind('click');
			$(item).find('.item-comment-actions .reply').click(function() {
				Item.Comment.reply(id);
			});
		});
	},
	updateAge: function() {
		if ($('.item-comments .item-comment').size()) {
			setTimeout('Item.Comment.updateAge()', 30 * 1000);
			var params = {'id[]': []};
			$('.item-comment').each(function (x, item) {
				var id = $(item).attr('id').split("_");
				id = id[id.length - 1];
				
				params['id[]'].push(id);
			});
			
			$.post(url('/item/comment/update'), params, function(data) {
				for (x in data) {
					$('#item_comment_'+x).find('.age').html(data[x]);
				}
			}, "json");
		}
	},
	thumbsUp: function(id) {
		$('#item_comment_'+id).find('.item-comment-actions .thumbsUp').addClass('done').find('img').attr('src', url('/app/design/image/buttons/thumbUpActive.png'));
		$('#item_comment_'+id).find('.item-comment-actions .thumbsDown').hide();
		$.post(url('/item/comment/thumbsUp'), {'comment_id': id}, function(data) {
			$('#item_comment_'+id).find('.rating').html(data);
		}, "text");
	},
	thumbsDown: function(id) {
		$('#item_comment_'+id).find('.item-comment-actions .thumbsDown').addClass('done').find('img').attr('src', url('/app/design/image/buttons/thumbDownActive.png'));
		$('#item_comment_'+id).find('.item-comment-actions .thumbsUp').hide();
		$.post(url('/item/comment/thumbsDown'), {'comment_id': id}, function(data) {
			$('#item_comment_'+id).find('.rating').html(data);
		}, "text");
	},
	cancel: function(elem) {
		$(elem).parent().parent().prev().val(''); 
		
		if (Item.Comment.isReply(elem)) {
			$(elem).parent().parent().parent().remove();
		} else {
			$(elem).parent().parent().hide();
		}
	},
	reply: function(id) {
		$('#item_comment_'+id+'_children').html('<div class="reply-comment">'+$('.item-comments .write-comment').html()+'</div>');
		$('#item_comment_'+id+'_children').find('textarea').val('').removeClass('initial');
		$('#item_comment_'+id+'_children').find('.right').show();
		
		if ($('#item_comment_'+id+'_children').hasClass('alt')) {
			$('#item_comment_'+id+'_children').addClass('alt');
		}
		Layout.columns();
	},
	isReply: function(elem) {	
		if ($(elem).parent().parent().hasClass('reply-comment')) {
			return true;
		}
		return false;
	},
	jump: function(id) {
		if ($('#item_comment_'+id).size()) {
			Item.Comment.jumpEffect(id);
		} else {
			Item.Comment.loadAll(id, function (id) {
				Item.Comment.jumpEffect(id);
			});
		}
	},
	jumpEffect: function(id) {
		var elem = $('#item_comment_'+id);
		var top = elem.position().top;
		var height = $(window).height();;
		
		$(window).scrollTop(top - height / 2);
		
		var bg = $('#item_comment_'+id).css('background-color');
		$('#item_comment_'+id).effect("highlight", {'color': '#147ea5'}, 1100);

	},
	update: function() {
	
	},
	updateRows: function() {
		Item.Comment.rows = $('.item-comments .item-comment').size();
	},
	loadAll: function(id, callback) {
		var alt = 0;
		var _rows = Item.Comment.rows;
	
		if (!$('.item-comments .item-comment:last').hasClass('alt')) {
			alt = 1;
		}
		
		Item.Comment.scrollUpdating = true;
	
		$.post(url('/item/comment/list'), {'item_type': Item.Comment.item_type, 'item_id': Item.Comment.item_id, 'offset': _rows, 'alt': alt, 'all': true}, function (data) {	
			$('.item-comments').append(data);
			Item.Comment.scrollUpdating = false;
			
			Item.Comment.updateRows();
			$('.item-comments .item-comment').fadeIn(1000);
			Layout.columns();
			Item.Comment.setupEvents();
			Layout.buttons();
			
			callback.call(this, id);
			
			if (_rows == Item.Comment.rows) {
				Item.Comment.scrollUpdate = false;
			}
		}, "text");
	},
	scroll: function() {
		if ($('.item-comments .item-comment').size()) {
			var top = $(window).scrollTop();
			var height = $(window).height();
			var scroll = top + height;
			var lastElem = $('.item-comments .item-comment:last').position().top + $('.item-comments .item-comment:last').height();
			var _rows = Item.Comment.rows;
			var alt = 0;
			
			if (!$('.item-comments .item-comment:last').hasClass('alt')) {
				alt = 1;
			}
			
			if (scroll > lastElem) {
				if (!Item.Comment.scrollUpdating && Item.Comment.scrollUpdate) {
					Item.Comment.scrollUpdating = true;
					
					$.post(url('/item/comment/list'), {'item_type': Item.Comment.item_type, 'item_id': Item.Comment.item_id, 'offset': _rows, 'alt': alt}, function (data) {
						Item.Comment.scrollUpdating = false;
						
						$('.item-comments').append(data);
						Item.Comment.updateRows();
						$('.item-comments .item-comment').fadeIn(1000);
						Layout.columns();
						Item.Comment.setupEvents();
						Layout.buttons();
						
						if (_rows == Item.Comment.rows) {
							Item.Comment.scrollUpdate = false;
						}
					}, "text");
				}			
			}
		}
	},
	send: function(elem) {
		var textarea = $(elem).parent().prev();
		var message = textarea.val();
		var orig = '';
		var elem = elem;
		var _elem = elem;
		var parent = $(elem).parent();
		var isReply = Item.Comment.isReply(elem);
		var parent_id = 0;
		
		if (message) {
			textarea.attr('disabled', 'disabled');
			orig = $(elem).parent().html();
			parent.html(Item.Comment.commentSending + " ...");
			
			if (isReply) {
				parent_id = parent.parent().parent().attr('id').split("_");
				parent_id = parent_id[parent_id.length - 2];
			}
		
			$.post(url('/item/comment/save'), {'item_type': Item.Comment.item_type, 'item_id': Item.Comment.item_id, 'content': message, 'parent_id': parent_id}, function (data) {
				textarea.attr('disabled', '');
				textarea.val('');
				parent.html(orig);
				
				if (isReply) {
					textarea.parent().remove();
				} 
				
				$('.item-comments .write-comment').after(data);
				
				if ($('.item-comments .item-comment').size() > 1) {
					if ($('.item-comments .item-comment').eq(1).hasClass('alt')) {
						
					} else {
						$('.item-comments .item-comment').eq(0).addClass('alt');
					}
				} else {
					$('.item-comments em').remove();
				}
				
				$('.item-comments .item-comment').eq(0).fadeIn(750);
				
				if (!isReply) {
					$('.item-comments .write-comment .right').hide();
				}
				Layout.columns();
				
				Item.Comment.updateRows();
				Item.Comment.setupEvents();
				Layout.buttons();
			}, "text");
		}
	}
}

$(document).ready(function() {
	Item.Comment.init();
});
$(window).scroll(function() {
	Item.Comment.scroll();
});
