function close_box(){
	$.fn.colorbox.close();
}
function open_box(){
	$.fn.colorbox({href:"/ask/answer/"+qid, open:true,overlayClose:false, width:620,iframe:true, height:500,onClosed:function(){ window.location.reload();}});
}

function get_ask_member_info(){
$.ajax({
	   		type: "POST",
	   		url: "/query/get_ask_member",
	   		success: function(data){
				$('#div_ask_member').html(data);
			}
})
}

var in_popup;
function save_answer(popup){
	if($('#answer_field').hasClass('tempValue'))
	{
		alert('The answer field is empty please provide an answer');
		return false;	
	}
	var id = $('#question_id').val();
	$text = $('#answer_field').val();
	$answerby = $('#answerby').val();
	//$anonymous = (($('input:checked').length>0)?'anonymous':'');
	$anonymous = '';
		
	if(($.trim($text)=="")){
		alert('The answer field is empty please provide an answer');
	}else{		
		$('#answer_btn').hide().after('<img src="/images/loading_dots.gif" />');
		$.ajax({
			  type: 'POST',
			  url: '/ask/save_answer',
			  data: {"id":id,"text":$text,'anonymous':$anonymous,'answerby':$answerby},
			  success: function(data){
				if(data.status=="ok"){
					if(in_popup==1){
						parent.answered(id);
					}
					else
						window.location.reload();
				}
				if(data.status=="error"){
					alert(data.msg);
				}
			},
			  dataType: 'json'
			});			
	}	
}

function save_reply(id){
	qid = $('#question_id').val();
	$text = $('#reply_'+id).val();
	$anonymous = ($('#anon_'+id+':checked').length>0)?'anonymous':'';

	if($.trim($text)==""){
		alert('The answer field is empty please provide an answer');
	}else{
		$.post('/ask/save_answer',{"id":qid,"text":$text,'anonymous':$anonymous,"aid":id},
				function(data){
					if(data.status=="ok"){
						window.location.reload();
					}
					if(data.status=="error"){
						alert(data.msg);
					}
			},'json');
	}
}


function limitChars(limit,$text_box,$alert){
  var text = $text_box.val();
  var textlength = text.length;
  if(textlength > limit){
  	$alert.html('Max. '+limit+' characters! <b>Your text has been trimed</b>');
  	$text_box.val(text.substr(0,limit));
  	return false;
  }else if(textlength>0){
	  $alert.html( (limit - textlength) +' characters left.');
  	return true;

  }else{
	  $alert.html( 'Max. '+limit+' characters');
  	return true;
	}

}

$(document).ready(function(){
	
	get_ask_member_info();
	
	$('a.flag').bind('click',
												function(){
													$.fn.colorbox({title:'Report abuse',href:$(this).attr('href'), open:true,overlayClose:false, width:450,iframe:true, height:450});return false;
												});
		$('.vote_loader').css('opacity',0.8);
		$('.qvoteup').each(
			function(index){
				$(this).click(function(){
				$id = $(this).siblings('input').val();
				vote($id,'up');
				});
		});
		$('.qvotedown').each(
			function(index){
				$(this).click(function(){
				$id = $(this).siblings('input').val();
				vote($id,'down');
				});
		});

		$('.avoteup').each(
			function(index){
				$(this).click(function(){
				$id = $(this).siblings('input').val();
				votea($id,'up');
				});
		});
		$('.avotedown').each(
			function(index){
				$(this).click(function(){
				$id = $(this).siblings('input').val();
				votea($id,'down');
				});
		});
		$('#search_btn').click(function(){ $('#search_form').submit(); });

		$('#subject').blur(function(){
			$.post('/ask/suggest',{subject:$(this).val()},
				function(response){
					if(response!='none'){
						$('#q_results').html(response);
					}
				}
			);
		});

		$('#text_area').click(function(){
			$('#type_area').hide();
			$('#form_area').slideDown('slow');
			$('#subject').focus();
		});
		$('#preview').click(function(){ show_preview();});
		$('#post').click(function(){ login(6,'post_question()','js');});			

		$('#answer_btn').click(function(){
			login(6,'save_answer()','js');
		});

        $('#info_anonymous').qtip({
               content: 'We respect your concerns for privacy, so if you wish to post '+
						'anonymously we understand. However, telling us who you are '+
						'feels more personal and we hope you would share that with '+
						'our community.',
               position: {
                  corner: {
                     tooltip: 'topRight', // Use the corner...
                     target: 'bottomLeft' // ...and opposite corner
                  }
               },
               style: {
                  border: {
                     width: 3,
                     radius: 5
                  },
                  padding: 10,
                  textAlign: 'left',
                  tip: true, // Give it a speech bubble tip with automatic corner detection
                  name: 'cream', // Style it according to the preset 'cream' style
					width:300
               }
            });
		$('#txt_question').keyup(function(){limitChars(80,$(this),$('#subject_count'))});
		$('#txt_comment').keyup(function(){limitChars(1500,$(this),$('#details_count'))});
				
});

function vote($id,$type){
				$('#qvote_loader_'+$id).show();
				$.post('/ask/voteq/',{id:$id,type:$type},
					function(response){
						if(response.status!='error'){
							$('#qcount_up_'+$id).html(response.up);
							$('#qcount_down_'+$id).html(response.down);
						}
						$('#qvote_loader_'+$id).hide();
					}
				,'json');
}

function votea($id,$type){
				$('#avote_loader_'+$id).show();
				$.post('/ask/votea/',{id:$id,type:$type},
					function(response){
						if(response.status!='error'){
							$('#acount_up_'+$id).html(response.up);
							$('#acount_down_'+$id).html(response.down);
						}
						$('#avote_loader_'+$id).hide();
					}
				,'json');
}

function show_preview()
{
	if(jQuery.trim($('#subject').val())!=""){
		$('#preview_subject').text($('#subject').val());
		$('#preview_details').text($('#question').val());
		$('#preview_area').show();
	}else{
		$('#preview_area').hide();
	}
}
function close_preview()
{
	$('#preview_area').hide();
}

function post_question()
{	
	error=0;
	$('#post_form .wr_input, #post_form .wr_textarea').each(function(){
		if($(this).hasClass('tempValue'))
		{
			$val='';
			$(this).val('');
		}
		else
			$val=$(this).val();
			
		if($(this).hasClass('mandatory') && $val.length<10)
			error++
	});
	if(error>0){
		alert('Please type your question');
		return false;
	}
	
	$('.ask_top_button a').hide().after('<img src="/images/loading_dots.gif" />');	
	$data = $('#post_form').serialize();
	$('#errors').hide();	
	$.post('/ask/post',$data,
		function(res){
			if(res.status=="error"){
				$('#errors').show();
				$('#errors').html(res.msg);
				$('.ask_top_button a').show().next().remove();
			}
			if(res.status=="ok"){
				parent.window.location='/ask/'+res.url;
			}
		}
		,
		'json');	
}

function show_reply(id){
	$('#reply_form_'+id).toggle();
}

