
function ratePost(postId,userId,creator){
	$.ajax({
	url: '/forum/rating/addVote/'+postId+'/'+userId+'/'+creator,
	success: function(data) {
	if(data!="") $('.rankBlock'+postId).html(data);
	}
	});
}
function getPostStar(postId,userId,isUserRatedPost){
	$.ajax({
	url: '/forum/rating/getPostRating/'+postId+'/'+userId+'/'+isUserRatedPost,
	success: function(data) {
	$('#rankBlockInner'+postId).html(data);
	}
	});
}

function getUserEmblems(postId,userId){
	$.ajax({
	url: '/forum/rating/showUserEmblems/'+userId,
	success: function(data) {
	$('#userEmblems'+postId).html(data);
	}
	});
}

(function($){

	//Some required/option variables set

    ForumActions = function(options) {
		var options = $.extend({
			SectionName : 'forum',
			DestPageUrl : '',
			TopicPageUrl : '/forum/topics/view',
			JumpPageCount : 1,
			ForumID : null,
			ForumCategoryID : null,
			TopicID : null,
			PostID : null,
			AuthUserID : null,
			pageNoField : 'pageNo',
			topicPagesTillTruncate : 3,
			replyNotificationUrl : "/forum/topics/replyNotification",
			sendMessageUrl : "/messages/OpenSendMessage",
			addContactUrl : "/messages/OpenSendMessage",
			getPostsPageUrl : "/forum/topics/get_posts_page_url",
		}, options || {});

		//method / dialog to input specific page number to jump to a specific pagination page

		$('#jump_button').click(function()	{

			//alert(options.JumpPageCount);

			var minPage = 1; 
			var maxPage = parseInt(options.JumpPageCount);
			var pageNo = parseInt($('#'+options.pageNoField).val());
			
			//alert(pageNo);return;

			if(!isNaN(pageNo) && pageNo >= minPage && pageNo <= maxPage)	{
				//alert(getURL(options, pageNo))
				window.location = getURL(options, pageNo); //find getURL down the page
			}	else	alert("Please enter a page number between 1 and " + options.JumpPageCount);
		});

		//method / dialog to input specific topic page number to jump to a specific pagination page


		$('#topic_jump_button').click(function()	{

			var minPage = 1;
			var maxPage = parseInt($('#jumpPageCount').html());
			var pageNo = parseInt($('#pageNumTopic').val()); //pageNo is the name of fied

			if(!isNaN(pageNo) && pageNo >= minPage && pageNo <= maxPage)	{
				//alert(getURL(options, pageNo))
				var topicId = $('#jumpPageTopicId').val();
				window.location = options.TopicPageUrl + "/" + topicId + "/page:"+pageNo;
			}	else	alert("Please enter a page number between 1 and " + maxPage);
		});

		//go to a specific page by clicki 1,2 ..

		$('.pageNavArrow').click(function()	{
			if($('.pageNavArrow img').attr('id')=="doublearrow2")	{
				var pageNo = 1;
			}	else	{
				var pageNo = options.JumpPageCount;
			}
			window.location = getURL(options, pageNo);
		});

		//another jump box to enter particular post page

		$('.topicPagesEnter').click(function()	{
			
			var pageNo = $(this).parent().children('span.postNavDoubleArrow').html();

			if(pageNo==null)	{
				pageNo = options.topicPagesTillTruncate;
			}	else	{
				pageNo = pageNo.substr(1); //it has &raquo; (double arrow) with it, remove that!
			}
			$('#jumpPageCount').html(pageNo);
			$('#jumpPageTopicId').val(getTopicIdFromParent(this));
			tb_show('Enter topic page', "#TB_inline?height=270&amp;width=540&amp;inlineId=pageJumpTopic&amp;modal=true");
		});

		//visit post page by click number 1,2 

		$('span.NavCounter, span.postNavDoubleArrow').click(function()	{
			var topic_id = getTopicIdFromParent(this);
			var page_number = $(this).html();

			if($(this).attr('class')=="postNavDoubleArrow")	{
				page_number = page_number.substr(1); //it has »(double arrow) with it, remove that!
			}

			var url = options.TopicPageUrl + "/" + topic_id + "/page:"+page_number;

			window.location = url;

		});

		//reply button function, ask for login or sent to add reply page

		$('#button_reply, .replyLink').click(function()	{
			
                
                var redirectUrl='';
                if($(this).attr('href')!=undefined) {
                    redirectUrl = $(this).attr('href');
                }   else    {
                   redirectUrl = '/forum/posts/add/'+options.TopicID;
                }

                var auth = Authorize();
                
                if(!auth.authorized)	{
                	tb_show_login_box("&is_forum=true");
                	return false;
                }
                
                //if(auth.userType=='guest')	{
                	//tb_show_guest_upgrade();
                	//return false;
                //}

                /*if($('#wrongPassword').length==0)	{ //1 means login form is loaded
				
				//tb_show('Login Do!',"#TB_inline?height=auto&width=540&inlineId=wrongPassword&modal=true",'');
					$('input#UserRedirect').val(redirectUrl);
				
					return false;
				}*/
			
			if($(this).attr('href')!=undefined)	{
				//alert("ddd")
				return true;
				//window.location.href = $(this).attr('href');
			}

			window.location.href='/forum/posts/add/'+options.TopicID;
		});

		//new topic function, ask for login or sent to add topic page

		$('#button_new_topic').click(function()	{
				
			var auth = Authorize();
			
			if(!auth.authorized)	{
				tb_show_login_box("&is_forum=true&redirect=" + options.ForumCategoryID);
				return false;
			}
			
			if(auth.userType=='guest')	{
				tb_show_guest_upgrade();
				return false;
			}				

			window.location.href='/forum/topics/add/'+options.ForumCategoryID;
		});

		//subscribe to notification on reply

		$('#TopicReplyLink').click(function()	{

			if(!Auth()) return false;

			var val = $('#TopicReplyNotify').val();
			if(val==0)	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_blue_circle.jpg"});
				$('#TopicReplyNotify').val('1');
			}	else	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_grey_circle.jpg"});
				$('#TopicReplyNotify').val('0');
			}

			if(options.SectionName=='view_topic' && options.TopicID!=null)	{
				replyNotification(options.TopicID, $('#TopicReplyNotify').val());
			}
		});

		//subscribe to notification on reply mousover effects

		$('#TopicReplyLink').mouseover(function()	{
			var val = $('#TopicReplyNotify').val();
			if(val==0)	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_blue_circle.jpg"});
			}	else	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_grey_circle.jpg"});
			}
		});

		//subscribe to notification on reply mouse  effects

		$('#TopicReplyLink').mouseout(function()	{
			var val = $('#TopicReplyNotify').val();
			if(val==0)	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_grey_circle.jpg"});
			}	else	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_blue_circle.jpg"});
			}
		});

		//this is for in the case if this option is already selected

		if($('#TopicReplyNotify').length>0 && $('#TopicReplyNotify').val()==1)	{
				$('#TopicReplyImage').attr({"src":"/img/bullet_blue_circle.jpg"});
		}

		//subscribe to topic form value input

		$('#TopicSubscribeLink').click(function()	{
			if(!Auth()) return false;
			var val = $('#TopicSubscribe').val();
			if(val==0)	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_blue_circle.jpg"});
				$('#TopicSubscribe').val('1');
			}	else	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_grey_circle.jpg"});
				$('#TopicSubscribe').val('0');
			}
			
			if(options.SectionName=='view_topic' && options.TopicID!=null)	{
				topicSubscribe(options.TopicID, $('#TopicSubscribe').val(), this);
			}
		});

		//subscribe to topic effects

		$('#TopicSubscribeLink').mouseover(function()	{
			var val = $('#TopicSubscribe').val();
			if(val==0)	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_blue_circle.jpg"});
			}	else	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_grey_circle.jpg"});
			}
		});

		//subscribe to topic effects

		$('#TopicSubscribeLink').mouseout(function()	{
			var val = $('#TopicSubscribe').val();
			if(val==0)	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_grey_circle.jpg"});
			}	else	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_blue_circle.jpg"});
			}
		});

		//this is for in the case if this option is already selected

		if($('#TopicSubscribe').length>0 && $('#TopicSubscribe').val()==1)	{
				$('#TopicSubscribeImage').attr({"src":"/img/bullet_blue_circle.jpg"});
				$('#TopicSubscribeLink').html('Unsubscribe to Topic');
		}

		$('ul.topicHit li a.hitMeta').click(function()	{
			
			//var idVars = $(this).attr('id');
			
			//if(idVars.indexOf('invokeSendMessage_')!=-1 || idVars.indexOf('invokeAddContact_')!=-1)	{
				//if(!Auth()) return false;
				//InvokeSendMessageDialog(this);
				//InvokeAddContactDialog(this);
				//InvokeAddContactDialogAjax(this);
			//}
		});

		//reply button function, ask for login or sent to add reply page

		$('#button_move_topic').click(function()	{
			//alert("/forum/topics/move_topic/"+options.TopicID+"?height=310&width=540&modal=true");
			tb_show('Move Topic',"/forum/topics/move_topic/"+options.TopicID+"?height=300&width=540&modal=true",'');
		});

		$('#button_lock_topic').click(function()	{
			//alert("/forum/topics/move_topic/"+options.TopicID+"?height=310&width=540&modal=true");
			tb_show('Lock Topic',"/forum/topics/lock_topic/"+options.TopicID+"?height=280&width=540&modal=true",'');
		});

		$('#button_delete_topic').click(function()	{
			//alert("/forum/topics/move_topic/"+options.TopicID+"?height=310&width=540&modal=true");
			tb_show('Lock Topic',"/forum/topics/delete_topic/"+options.TopicID+"?height=280&width=540&modal=true",'');
		});

		$('div.quotedBox a').click(function(e){
			var postID = $(this).attr('id').split('QStamp_')[1];
			if($("a[name=post_"+postID+"]").length > 0)	{
				window.location = "#post_"+postID;
			}	else	{
				$.ajax({
					url: options.getPostsPageUrl+"/"+options.TopicID+"/"+postID,
					success: function(getUrl) {
						window.location = getUrl;
					}
				});
			}
		});

		$('span.replyToBox a').click(function(e){
			var postID = $(this).attr('id').split('RStamp_')[1];
			if($("a[name=post_"+postID+"]").length > 0)	{
				window.location = "#post_"+postID;
			}	else	{
				$.ajax({
					url: options.getPostsPageUrl+"/"+options.TopicID+"/"+postID,
					success: function(getUrl) {
						window.location = getUrl;
					}
				});
			}
		});

		$('.col4 a.lastPostLink').click(function() {

			var eleId = $(this).attr('id');
			var eleIdParams = eleId.split("_");

			//alert("1: " + eleIdParams[1] + " 2: " + eleIdParams[2])
			
			//alert(eleIdParams[1]==false)
			//alert(eleIdParams[2]==false)

			if(eleIdParams[1]==false && eleIdParams[2]==false) {
				return;
			}

			var url = options.getPostsPageUrl+"/"+eleIdParams[1]+"/"+eleIdParams[2];

			$.ajax({
					url: url,
					success: function(getUrl) {
							if(getUrl!="") window.location = getUrl;
					}
			});

		});

		function splitToGetId(str, splitOn)	{
			var vars = str.split(splitOn);
			return vars[1];
		}

		function getTopicIdFromParent(obj)	{
			var parentId = $(obj).parent().attr('id');
			var ids = parentId.split('_');
			return ids[1]; //topic_id
		}

		function getURL(options, pageNo)	{

			if(options.SectionName=="view_topic")	{
				return options.DestPageUrl + "/" + options.TopicID + "/page:" + pageNo;
			}	else if(options.SectionName=="forum_category")	{
				return options.DestPageUrl + "/" + options.ForumCategoryID + "/page:" + pageNo;
			}	else if(options.SectionName=="my_subscriptions")	{
				return options.DestPageUrl + "/page:" + pageNo;
			}
		}

		function replyNotification(topicId, isShow)	{
			/*if(isShow==undefined){
				isShow=0;
			}*/
			//alert(options.replyNotificationUrl);return;
			$.ajax({
			  url: options.replyNotificationUrl +"/"+topicId+'/'+isShow,
			  success: function(data) {
				//$('#divReplyNotification').html(data);
				//alert(data);
			  }
			});
		}

		function topicSubscribe(topicId,isShow, obj){
			//alert(options.topicSubscribeUrl+'/'+topicId+'/'+isShow);return;
			/*if(isShow==undefined){
				isShow=0;
			}*/
			$.ajax({
			  url: options.topicSubscribeUrl+'/'+topicId+'/'+isShow,
			  success: function(data) {
				if(isShow==0)	{
					$(obj).html('Subscribe to Topic');
				}	else	{
					$(obj).html('Unsubscribe to Topic');
				}
				//alert(data);
				//$('#divTopicSubscribe').html(data);
			  }
			});
		}
	};
  })
  (jQuery);

