 
function CommentObject (  ) {
	var self = this;
	
	this.createCommentButton 	= null;
	this.commentMessageField 	= null;
	this.contentHolder			= null;
	this.commentDelete			= null;
	this.commentHover			= null;
	this.postID					= null;
	
	this.createCommentInput		= null;
	
	this.testClickHandler		= null;
	
	this.jsonObjectFBData = null;
};

CommentObject.prototype = {
	init : function (  ) {
		var self = this;
		
		self.createCommentButton 	= $( ".sendButton" );
		self.commentMessageField 	= $( "#message" );
		self.commentDelete			= $( ".delete_comment" );
		self.testClickHandler		= $( ".commentHover" );
		
		/*
		self.createCommentInput		= $( ".selectInput" );
		
		self.createCommentInput.bind( "keydown", function ( evt ) {
			switch ( evt.keyCode ) {
				case 13:
					evt.preventDefault (  );
					self.createCommentButton.click ( self.test );
				break;
			}
		} );
		*/
		
	},
	setHandler : function (  ) {
		var self = this;
		
		self.createCommentButton 	= $( ".sendButton" );
		self.commentMessageField 	= $( "#message" );
		self.commentDelete			= $( ".delete_comment" );
		self.testClickHandler		= $( ".commentHover" );
		
		self.createCommentButton.click ( self.test );
		self.commentDelete.click ( self.test );
		self.testClickHandler.hover ( self.test );
	},
	createJsonObjectFBData : function (  ) {
		var self = this;
		
		self.jsonObjectFBData = {};
		
		for ( var i in fb_connect.userData[0] ) {
			self.jsonObjectFBData[i] = fb_connect.userData[0][i];
		};
	},
	test : function ( evt ) {
		var evtType = evt.type;
		
		switch ( evtType ) {
			case "click":
				var srcID 	= evt.currentTarget.id;
				var ausdruck = /(.*)_(.*)_(.*)/;
				ausdruck.exec(srcID);
				
				if ( RegExp.$1 != "" ) {
					switch ( RegExp.$1 ) {
						case "delete":
							var commentOwner = $( this ).attr ( "userID" );
							if ( commentOwner != postCommentObject.commentObject.jsonObjectFBData["id"] ) return false;
							
							var ausdruck = /delete_(.*)_(.*)/;
							ausdruck.exec(srcID);
							
							var postID 		= RegExp.$1;
							var commentID 	= RegExp.$2;
							
							$( "#comment_" + postID + "_" + commentID ).fadeOut ( function (  ) {
								$( "#comment_" + postID + "_" + commentID ).remove (  );
								
								ajaxObject.doAjax ( {
									type			: "POST",
									url				: postCommentObject.campaignUrl + "fb_gateway.php",
									dataType		: "json",
									data			: {
										action		: "DELETE_COMMENT",
										commentID	: commentID,
										postID		: postID
									},
									callBackFunc	: function (  ){
										$( "#post_holder_" + postID ).attr ( "comments", ( parseInt ( $( "#post_holder_" + postID ).attr ( "comments" ) ) - 1 ) );
									}
								} );
								
								return false;
							} );
						break;
						case "commentInsert":
							var id = RegExp.$2;
							var postComment = $( "#commentField_"+id );
							
							var checkContent = postComment.val (  ).split ( "\n" ).join ( "" );
							checkContent = checkContent.split ( " " ).join ( "" );
							if ( checkContent.length <= 0 ) return false;
							
							if ( postComment.val() == "" ) return false;
							
							postCommentObject.commentObject.postID = id;
							
							if ( isAdmin ) {
								for ( var i in postCommentObject.commentObject.jsonObjectFBData.error ) {
									// alert (  i + " - " + postCommentObject.commentObject.jsonObjectFBData.error[i] );
								}	
							}
							
							ajaxObject.doAjax ( {
								type			: "POST",
								url				: postCommentObject.campaignUrl + "fb_gateway.php",
								dataType		: "json",
								data			: {
									action		: "INSERT_COMMENT",
									id			: id,
									postComment	: postComment.val(),
									userData	: postCommentObject.commentObject.jsonObjectFBData,
									countryCode	: postCommentObject.countryCode
								},
								callBackFunc	: postCommentObject.commentObject.ajaxCallBack
							} );
							
							$( "#commentField_"+id ).val ( "" );
							// $( "#commentField_"+id ).val ( postCommentObject.cmsData.texts_basic[13] );
							document.getElementById ( "commentField_" + id ).rows = 1;
							$( "#pic_"+id ).hide (  );
							
							if ( $.browser.msie && jQuery.browser.version == "6.0" ) {
								if ( $("#commentField_"+id).css ( "width" ) == "333px" && $("#commentField_"+id).css ( "height" ) == "6px" ) {
									$("#commentField_"+id).css ( { width: "373px" } );	
									$("#commentField_"+id).css ( { height: "15px" } );	
								} else {
									$("#commentField_"+id).css ( { width: "100%" } );
									$("#commentField_"+id).css ( { height: "25px" } );
								}
							} else {
								$("#commentField_"+id).css ( { width: "100%" } );
								$("#commentField_"+id).css ( { height: "25px" } );
							};
							
							// $( "#commentField_"+id ).css ( "width", "100%" );
							// $( "#commentField_"+id ).css ( { height: "25px" } );
							$( "#sendButton_"+id ).hide (  );
							
							return false;
						break;
					}
				}
				
				return false;
			break;
			case "mouseenter":
				var ausdruck = /comment_(.*)_(.*)/;
				ausdruck.exec(evt.currentTarget.id);
				
				var postID = RegExp.$1;
				var commentID = RegExp.$2;
				
				var commentOwner = $( "#delete_" + postID + "_" + commentID ).attr ( "userID" );
				if ( commentOwner == postCommentObject.commentObject.jsonObjectFBData["id"] ) {
					$( "#delete_" + postID + "_" + commentID ).show (  );
				};
			break;
			case "mouseleave":			
				var ausdruck = /comment_(.*)_(.*)/;
				ausdruck.exec(evt.currentTarget.id);
				
				var postID = RegExp.$1;
				var commentID = RegExp.$2;
				
				var commentOwner = $( "#delete_" + postID + "_" + commentID ).attr ( "userID" );
				if ( commentOwner == postCommentObject.commentObject.jsonObjectFBData["id"] ) {
					$( "#delete_" + postID + "_" + commentID ).hide (  );
				};
			break;
		}
	},
	hoverHandler : function ( evt ) {
		var ausdruck = /comment_(.*)_(.*)/;
		ausdruck.exec(evt.currentTarget.id);
		
		switch ( evt.type ) {
			case "mouseenter":
				$( "#delete_" + RegExp.$1 + "_" + RegExp.$2 ).show (  );
			break;
			case "mouseleave":
				$( "#delete_" + RegExp.$1 + "_" + RegExp.$2 ).hide (  );
			break;
		}
	},
	clickCreateCommentOnEnter : function ( evt ) {
		
	},
	/*
	clickDeleteHandler : function ( evt ) {
		var ausdruck = /delete_(.*)_(.*)/;
		ausdruck.exec($(this).attr ( "id" ));
		
		var postID 		= RegExp.$1;
		var commentID 	= RegExp.$2;
		
		$( "#comment_" + postID + "_" + commentID ).fadeOut ( function (  ) {
			$( "#comment_" + postID + "_" + commentID ).remove (  );
			
			ajaxObject.doAjax ( {
				type			: "POST",
				url				: postCommentObject.campaignUrl + "fb_gateway.php",
				dataType		: "json",
				data			: {
					action		: "DELETE_COMMENT",
					commentID	: commentID
				},
				callBackFunc	: null
			} );
		} );
	},
	clickHandler : function ( evt ) {
		var self = this;
		
		var id = $(this).attr('ref');
		var postComment = $( "#commentField_"+id );
		
		postCommentObject.commentObject.postID = id;
		
		ajaxObject.doAjax ( {
			type			: "POST",
			url				: postCommentObject.campaignUrl + "fb_gateway.php",
			dataType		: "json",
			data			: {
				action		: "INSERT_COMMENT",
				id			: id,
				postComment	: postComment.val(),
				userData	: postCommentObject.commentObject.jsonObjectFBData
			},
			callBackFunc	: postCommentObject.commentObject.ajaxCallBack
		} );
		
		// alert ( postCommentObject.postObject.jsonObjectFBData.name );
	},
	*/
	ajaxCallBack : function ( data ) {
		var self = this;
		
		var id = postCommentObject.commentObject.postID;
		
		$("#comment_box_"+id).append (
			"<div class=\"commentHover\" id=\"comment_" + id + "_" + data.requestData.comment_id + "\" style=\"display: none; margin: 2px 0px 0px 0px; position: relative; width: 100%; background-color: #edeff4;\">" +
			"<div class=\"delete_comment\" userID=\"" + data.userData.id + "\" id=\"delete_" + id + "_" + data.requestData.comment_id + "\" style=\"display: none; text-align: center; cursor: pointer; width: 15px; height: 15px; background-color: #607faf; color: #fff; position: absolute; top: 5px; right: 5px;\">" +
			"<img src=\"img/icon_close.gif\" alt=\"" + postCommentObject.cmsData.texts_basic[14] + "\" title=\"" + postCommentObject.cmsData.texts_basic[14] + "\" />" +
			"</div>" +
			"<div class=\"image_id_" + data.requestData.comment_id + "\" style=\"float: left; display: block; margin: 0px 5px 0px 0px; padding: 5px 0px 0px 5px;\">" +
			"<a href=\"http://www.facebook.com/profile.php?id=" + data.userData.id + "\" target=\"_blank\"><fb:profile-pic uid=\"" + data.userData.id + "\" linked=\"false\" size=\"square\" width=\"32\" height=\"32\"></fb:profile-pic></a>" +
			"</div>" +
			"<div class=\"post_id_" + data.requestData.comment_id + "\" style=\"width: 300px height: auto; min-height: 42px; background-color: #edeff4;\">" +
			"<div style=\"padding: 5px 20px 0px 45px; _padding: 5px 20px 0px 0px;\">" +
			"<span>" +
			"<a href=\"http://www.facebook.com/profile.php?id=" + data.userData.id + "\" target=\"_blank\"><fb:name uid=\"" + data.userData.id + "\" linked=\"false\" capitalize=\"true\" useyou=\"false\" reflexive=\"false\"></fb:name></a>"+
			"</span>" +
			"&nbsp;"
			+ data.requestData.comment +
			"<br />" +
			"<span class=\"\" style=\"display: block; padding: 5px 0px 0px 0px; font-size: 10px; color: #555;\">" + data.requestData.time + "</span>" +
			"</div>" +
			"</div>"
		);
		
		$( "#post_holder_" + id ).attr ( "comments", ( parseInt ( $( "#post_holder_" + id ).attr ( "comments" ) ) + 1 ) );
		
		// postCommentObject.commentObject.commentHover.hover ( postCommentObject.commentObject.hoverHandler );
		$("#comment_" + id + "_" + data.requestData.comment_id).unbind('click').click ( postCommentObject.commentObject.test );
		$( ".commentHover" ).hover ( postCommentObject.commentObject.test );
		$( ".delete_comment" ).unbind('click').click ( postCommentObject.commentObject.test );
		
		$("#comment_" + id + "_" + data.requestData.comment_id).fadeIn ( "fast" );
			 
		FB.XFBML.parse(document.getElementById("comment_box_" + data.requestData.id));
		
		var postOwnerID = $( "#post_holder_" + id ).attr ( "userID" );
		$( "#commentField_"+id ).val ( postCommentObject.cmsData.texts_basic[13] );
		
		postCommentObject.streamSetStatusObject.setStatus ( data.requestData.comment );
		
		// cant send request to oneself or to nonfriends
		if ( postOwnerID != data.userData.id && postCommentObject.commentObject.isInFriendList ( postOwnerID ) ) {
			postCommentObject.streamInviteSingleObject.inviteStream ( postOwnerID, data.requestData.comment );
		};
		
		return false;
	},
	isInFriendList : function ( userID ) {
		for ( var i in postCommentObject.commentObject.jsonObjectFBData.friends ) {
			if ( postCommentObject.commentObject.jsonObjectFBData.friends[i].id == userID ) {
				return true;
			};
		};
		
		return false;
	}
};

