function limitChars(textid, limit, infodiv)
{
    var text = $('.'+textid).val();     
    var textlength = text.length;
    if(textlength > limit)
    {
        $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else
    {
        $('#' + infodiv).html(''+ (limit - textlength) +' characters left.');
        return true;
    }
}

$(document).ready(function() {

	$('.sub_answer').click(function() {
			
			alert('asd');
			if($('#body').val() == 'Enter your answer here...'){
			  err_1 = 'You must add an answer!';
			} 
			
			if(err_1){
				alert(err_1);
			}
	});
	
	$('.addcomment').click(function() {
	
		var request=$(this).attr('id');

	
		result+='<form id="submit" method="post">';
		result+='<textarea name="comment" id="comment" class="commentlimit{answers.id}_{answers.question_id}" style="width:97%"></textarea>';
		result+='<div id="charlimitinfo">255 characters left</div>';
		result+='<input type="hidden" id="qId" name="qId" value="{answers.question_id}">';
		result+='<input type="hidden" id="uId" name="uId" value="{oSession.userId}">';
		result+='<input type="hidden" id="aId" name="aId" value="{answers.id}">';
		result+='<input type="submit" name="submitsss" id="comment_submit" value="Send" />';
		result+='</form>';
	
		
		$('#commentbox_'+request+'').show();  
		$('#commentbox_'+request+'').html(result);
		$('.commentlimit'+request+'').keyup(function(){
    		limitChars('commentlimit'+request+'', 255, 'charlimitinfo');
   		});  
		
	
	});
	
	$(".comment_button").click(function(){

		var element = $(this);
		var I = element.attr("id");
		
		$("#slidepanel"+I).slideToggle(300);
		$(this).toggleClass("active"); 

		return false;
	});

	$(".comment_submit").click(function(){
		var element = $(this);
		var Id = element.attr("id");
		var test = $("#textboxcontent"+Id).val();
		var aId = Id;
		var qId = $('#qId').attr('value');
		var uId = $('#uId').attr('value');
		var dataString = "comment="+ test +"& aId="+ aId +"& qId="+ qId +"& uId="+ uId;	
		var url= BASE_URL+'/docom.php';
		
			if(test==''){
				alert("Please Enter Some Text");
			} else {
				$.ajax({
				type: "POST",
				url: url,
				data: dataString,
				cache: false,
				success: function(html){
				$("#loadplace"+Id).append(html);
				$("#flash"+Id).hide();
				$("#slidepanel"+Id).hide();
				
				}
				});
		}return false;
	});

	$("form#submit").submit(function() {
 
	// we want to store the values from the form input box, then send via ajax below
	var comment = $('#comment').attr('value');
	var aId = $('#aId').attr('value'); 
	var qId = $('#qId').attr('value');
 	var url= BASE_URL+'/docom.php';
 	var addcomment = aId+'_'+qId;

 	$('div#loading_'+addcomment+'').attr("src","http://www.booglam.com/images/spinner.gif");
		$.ajax({
			type: "POST",
			url: url,
			data: "comment="+ comment +"& aId="+ aId +"& qId="+ qId,
			success: function(data){
				$('.addcom_'+ addcomment +'').hide();
				$('form#submit').hide();
				$('div#success_'+ addcomment +'').html(data);
				$('div#success_'+ addcomment +'').fadeIn();				
			}
		});
	return false;
	});

    $('.acceptanswer').click(function() {
    	
		//$(this).parent().html('<img src="http://local.bg.com/images/spinner.gif"/>');
			
		var request=$(this).attr('id');
    	var url= BASE_URL+'/dovote.php';

    	$('img#tick_'+request+'').attr("src","http://wwww.booglam.com/images/spinner.gif");
    	
    	$.get(url, { aId: request }, function(result) {
    		//alert(result);
            $(this).parent().html(result);            
            
            // Reset All other ticks to OFF??????
            $('img#tick_'+result+'').attr("src","/images/tick-off.jpg");
            $('img#tick_'+request+'').attr("src","/images/tick-on.jpg");
            // Reset Old chosen Bg and D
			$('#nondiv_'+request+'').attr("style","background-color: #F2F0F0;margin-left:5em;");			
            $('#chosendiv_'+result+'').attr("style","background-color:#fff;margin-left:5em;"); 
            
            $('#nondiv_'+request+'').attr("id", "chosendiv_"+request); 
            $('#chosendiv_'+result+'').attr("id", "nondiv_" +result); 
            
			$('#close').click(function() { 
    			$("#results").html('');   
    				
			});	
        });
	
    });
    
    
    $('.denyanswer').click(function() {     	
    	var u='<div style="padding:10px;background-color:#444;color:#fff;font-weight:bold;font-size:14px;">Sorry this is not your question to accept answer.<br />Want to <a href="/ask/" style="color:#fff">ask you own question?</a><br /><a id="close" style="cursor:pointer;color:#fff">Close</a></div>';
    	$("#results").html(u);   
    	
    	$('#close').click(function() { 
    		$("#results").html('');   
    				
		});  	
	});
    
	$('.dologin').click(function() {     
		
		var request=$(this).attr('id');
    	var a='<div style="padding:10px;background-color:#444;color:#fff;font-weight:bold;font-size:14px;">Please <a href="/signin/" style="color:#fff">Sign In<a/> to accept as answer.<br /><a id="close" style="cursor:pointer;color:#fff">Close</a></div>';
    	$('#results_'+request+'').html(a);   
    	
    	$('#close').click(function() { 
    		$('#results_'+request+'').html('');   
    				
		}); 
		

	});
	
	$('.denycomment').click(function() {     	

		var request=$(this).attr('id');
    	var a='<div style="padding:10px;background-color:#444;color:#fff;font-weight:bold;font-size:14px;">Please <a href="/signin/" style="color:#fff">Sign In<a/> to add a comment.<br /><a id="close" style="cursor:pointer;color:#fff">Close</a></div>';
    	$('#results_'+request+'').html(a);   
    	
    	$('#close').click(function() { 
    		$('#results_'+request+'').html('');   
    				
		}); 		

	});

	$('.notcommentowner').click(function() {     	

		var request=$(this).attr('id');
    	var a='<div style="padding:10px;background-color:#444;color:#fff;font-weight:bold;font-size:14px;">Sorry this is not your Question to comment on.<br /><a id="close" style="cursor:pointer;color:#fff">Close</a></div>';
    	$('#results_'+request+'').html(a);   
    	
    	$('#close').click(function() { 
    		$('#results_'+request+'').html('');   
    				
		}); 		

	});
	
	$('#answerform').click(function() {     	
   	
	   	$('#loginform').attr("style","display:inline");	

	});
	
});
