$(document).ready(function() 
{					   
	var replyid = 0;
	var status = 0;	
	var _id = 0;
	
	getPageInit(0);	

	$.localScroll({
		lazy:true
	});
	
	$.localScroll.hash({		
		queue:true,		
		duration:1500
	});
		
	function addYouneedtologin()
	{
		$('a[rel=youneedtologin]').click(function(){return alertLogin($(this).attr("href"));});

		function alertLogin(url) 
		{
			jAlert('You need to login for rating', 'Login');		
			return false;
		}
	}	
	
	$('a[rel=youneedtologin]').click(function(){return alertLogin($(this).attr("href"));});

	function alertLogin(url) 
	{
        jAlert('You need to login for voting', 'Login');		
		return false;
	}

	$('a[rel=tagup]').click(function () { 					
		_id = $(this).closest(".pic_vote_container").attr("id");						
	
		var data = 'id='+_id;			
		$.ajax({
			url: '/?X=goTagPicUp',	
			type: "POST",		
			data: data,		
			cache: false,
			success: function (html) {	
				$("#"+_id).children(".pic_box_rating").empty();
				$("#"+_id).children(".pic_box_rating").append(html);							
			}		
		});	
		return false;
	});

	$('a[rel=tagdown]').click(function () { 					
		_id = $(this).closest(".pic_vote_container").attr("id");						

		var data = 'id='+_id;			
		$.ajax({
			url: '/?X=goTagPicDown',	
			type: "POST",		
			data: data,		
			cache: false,
			success: function (html) {
				$("#"+_id).children(".pic_box_rating").empty();
				$("#"+_id).children(".pic_box_rating").append(html);
			}		
		});	
		return false;
	});	


	$(function() {
		$('a.lightbox').lightBox();
	});	
	
	$('#bbcode').markItUp(mySettings);

	$('#emoticons a').click(function() {
		emoticon = $(this).attr("title");
		$.markItUp( { replaceWith:emoticon } );
		return false;
	});	

	$(".button").click(function() {  
		fsave(); 		
		return false;
	}); 	

	$('a[rel=deletepicture]').click(function(){return alertConfirm($(this).attr("href"));});

	function alertConfirm(url) 
	{
        jConfirm('Continue to delete?', 'Confirm', function(r) 
		{
			if(r)
			{
				document.location = url;
				return true;	
			}
			else
			{
				return false;
			}			
		});	
		return false;
	}

	function clearReply()
	{
		$(".commentitem").append($("#codemenu"));
		$("#cancel-reply-link").hide();
		$("#bbcode").val("");
		zeroVars();
	}

	$('a[rel=refreshcomments]').click(function () {
		clearReply();
		getPageInit(0); 
		return false;
	});	

	$("#cancel-reply-link").click(function()  
	{		
		clearReply();
		return false;
	});	

	function hideErrorMenu()
	{
		setTimeout(function(){
			$("#errormenu").fadeOut("slow", function () {
			$("#errormenu").hide();
			}); 
		}, 2000);
	}

	function zeroVars()
	{
		replyid = 0;
		status = 0;
	}
	
	function fsave()
	{					
		if(status == 1)
			var target = "editpicreply";
		else
			var target = "replypic"; 
		var _reply		= replyid;		;
		var _id			= $("#main_id").val();
		var _content	= encodeURIComponent($("#bbcode").val());

		if(_reply > 0)
			var _step	= $("#c_"+_reply).css("margin-left").replace("px", "");

		var data = 'reply='+_reply+'&id='+_id+'&comcomment='+_content+'&step='+_step;		
		$.ajax({
			url: "/?X="+target,	
			type: "POST",		
			data: data,		
			cache: false,
			success: function (html) 
			{	
				if(html != "")
				{
					if(status == 1)
					{
						$("#c_"+_reply).children("#commenttext").empty();
						$("#c_"+_reply).children("#commenttext").append(html);
					}
					else
					{					
						if(_reply > 0)
							$("#c_"+_reply).after(html);
						else
							$("#_content").append(html);
					}
				

					$(".commentitem").append($("#codemenu"));
					$("#cancel-reply-link").hide();				
					$("#bbcode").val("");
					zeroVars();
				}

				// !_! //

				$(function() {
					$('a.lightbox').lightBox();
				});	

				$.localScroll.hash({		
					queue:true,		
					duration:1500
				});

				addYouneedtologin();

				// !_! //

				$('a[rel=comment_up]').click(function () { 					
					_id = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');						

					var data = 'id='+_id+'&cid=5';			
					$.ajax({
						url: '/?X=goCommentUp',	
						type: "POST",		
						data: data,		
						cache: false,
						success: function (html) {								
							$("#c_"+_id).children("#comment_rating_result").empty();
							$("#c_"+_id).children("#comment_rating_result").append(html);	
						}		
					});	
					return false;
				});

				$('a[rel=comment_down]').click(function () { 					
					_id = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');						

					var data = 'id='+_id+'&cid=5';		
					$.ajax({
						url: '/?X=goCommentDown',	
						type: "POST",		
						data: data,		
						cache: false,
						success: function (html) {							
							$("#c_"+_id).children("#comment_rating_result").empty();
							$("#c_"+_id).children("#comment_rating_result").append(html);							
						}		
					});	
					return false;
				});

				$('a[rel=reply]').click(function () {  
					$(this).closest(".piccomment").after($("#codemenu"));					
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');	
					
					$("#cancel-reply-link").show();
					$("#bbcode").val("");
					$("#bbcode").focus();
					status = 0;
					return false;
				});

				$('a[rel=edit]').click(function () { 
					$(this).closest(".piccomment").after($("#codemenu"));
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');					
					status = 1;
					$("#cancel-reply-link").show();

					var data = 'reply='+replyid;			
					$.ajax({
						url: '/?X=getPictureComment',	
						type: "GET",		
						data: data,		
						cache: false,
						success: function (html) {							
							$("#bbcode").val(html);	
							$("#bbcode").focus();
						}		
					});	
					return false;
				});

				$('a[rel=delete]').click(function () {  
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');					
					status = 0;
					
					jConfirm('You want to delete comment?', 'Confirm', function(r) 
					{
						if(r)
						{
							var data = 'reply='+replyid;			
							$.ajax({
								url: '/?X=delCommentPicture',	
								type: "GET",		
								data: data,		
								cache: false,
								success: function (html) {
									if(html == "")
									{
										$("#c_"+replyid).remove();
									}
									else
									{										
										$("#c_"+replyid).children("#commenttext").empty();
										$("#c_"+replyid).children("#commenttext").append(html);
									}									
									zeroVars();
								}		
							});	
						}						
					});						
					return false;
				});
			}		
		});
	} 	
	
	function getPageInit(status)
	{
		var _type		= $("#comment_type").val();
		var _id			= $("#main_id").val();		
	 	
	 	//hide the content and show the progress bar
	 	$('#_content').hide();
	 	$('#loading').show();
	 	
	 	switch(_type) 
		{
		  case 'pcomments':  getPage('getPictureComments',_id);                   			
			break; 		
		}		
	
		//cancel the anchor tag behaviour
		return false;
	}
	// !! //
	function getPage(_type,_id) 
	{		
		var data = 'id='+_id;		
		$.ajax({
			url: '/?X='+_type,	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {				
				
				$('#loading').hide();				
				$('#_content').html(html);					
				$('#_content').show();				
				
				// !_! //

				$(function() {
					$('a.lightbox').lightBox();
				});	

				$.localScroll.hash({		
					queue:true,		
					duration:1500
				}); 

				$('a#comment_hidden').click(function() {				
					h_id = $(this).parent().attr("id").replace(/^.*_/, '');
					$("#c_"+h_id).toggle(400);				
					return false;				
				});

				addYouneedtologin();

				// !_! //

				$('a[rel=comment_up]').click(function () { 					
					_id = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');						

					var data = 'id='+_id+'&cid=5';			
					$.ajax({
						url: '/?X=goCommentUp',	
						type: "POST",		
						data: data,		
						cache: false,
						success: function (html) {								
							$("#c_"+_id).children("#comment_rating_result").empty();
							$("#c_"+_id).children("#comment_rating_result").append(html);	
						}		
					});	
					return false;
				});

				$('a[rel=comment_down]').click(function () { 					
					_id = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');						

					var data = 'id='+_id+'&cid=5';		
					$.ajax({
						url: '/?X=goCommentDown',	
						type: "POST",		
						data: data,		
						cache: false,
						success: function (html) {							
							$("#c_"+_id).children("#comment_rating_result").empty();
							$("#c_"+_id).children("#comment_rating_result").append(html);							
						}		
					});	
					return false;
				});

				$('a[rel=reply]').click(function () {  
					$(this).closest(".piccomment").after($("#codemenu"));					
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');	
					
					$("#cancel-reply-link").show();
					$("#bbcode").val("");
					$("#bbcode").focus();
					status = 0;
					return false;
				});

				$('a[rel=edit]').click(function () { 
					$(this).closest(".piccomment").after($("#codemenu"));
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');					
					status = 1;
					$("#cancel-reply-link").show();

					var data = 'reply='+replyid;			
					$.ajax({
						url: '/?X=getPictureComment',	
						type: "GET",		
						data: data,		
						cache: false,
						success: function (html) {							
							$("#bbcode").val(html);	
							$("#bbcode").focus();
						}		
					});	
					return false;
				});

				$('a[rel=delete]').click(function () {  
					replyid = $(this).closest(".piccomment").attr("id").replace(/^.*_/, '');					
					status = 0;
					
					jConfirm('You want to delete comment?', 'Confirm', function(r) 
					{
						if(r)
						{
							var data = 'reply='+replyid;			
							$.ajax({
								url: '/?X=delCommentPicture',	
								type: "GET",		
								data: data,		
								cache: false,
								success: function (html) {
									if(html == "")
									{
										$("#c_"+replyid).remove();
									}
									else
									{										
										$("#c_"+replyid).children("#commenttext").empty();
										$("#c_"+replyid).children("#commenttext").append(html);
									}									
									zeroVars();
								}		
							});	
						}						
					});						
					return false;
				});
			}		
		});
	}

});	
	
