var clientInfo = navigator.userAgent.toLowerCase();
var isIE = ( clientInfo.indexOf("msie") != -1 );
var isWin = ( (clientInfo.indexOf("win")!=-1) || (clientInfo.indexOf("16bit") != -1) );

function createBBtag( openerTag , closerTag , areaId ) {
	if(isIE && isWin) {
		createBBtag_IE( openerTag , closerTag , areaId );
	}
	else {
		createBBtag_nav( openerTag , closerTag , areaId );
	}
	return;
}

function createBBtag_IE( openerTag , closerTag , areaId ) {
  
	var txtArea = document.getElementById( areaId );
	var aSelection = document.selection.createRange().text;
	var range = txtArea.createTextRange();

	if(aSelection) {
		document.selection.createRange().text = openerTag + aSelection + closerTag;
		txtArea.focus();
		range.move('textedit');
		range.select();
	}
	else {
		var oldStringLength = range.text.length + openerTag.length;
		txtArea.value += openerTag + closerTag;
		txtArea.focus();
		range.move('character',oldStringLength);
		range.collapse(false);
		range.select();
	}
	return;
}

function createBBtag_nav( openerTag , closerTag , areaId ) {
	var txtArea = document.getElementById( areaId );
	if (txtArea.selectionEnd && (txtArea.selectionEnd - txtArea.selectionStart > 0) ) {
		var preString = (txtArea.value).substring(0,txtArea.selectionStart);
		var newString = openerTag + (txtArea.value).substring(txtArea.selectionStart,txtArea.selectionEnd) + closerTag;
		var postString = (txtArea.value).substring(txtArea.selectionEnd);
		txtArea.value = preString + newString + postString;
		txtArea.focus();
	}
	else {
		var offset = txtArea.selectionStart;
		var preString = (txtArea.value).substring(0,offset);
		var newString = openerTag + closerTag;
		var postString = (txtArea.value).substring(offset);
		txtArea.value = preString + newString + postString;
		txtArea.selectionStart = offset + openerTag.length;
		txtArea.selectionEnd = offset + openerTag.length;
		txtArea.focus();
	}
	return;
}

function get_language() {
  
  for (var i=0; i < document.post_form.choose_language.length; i++)
  {
    if (document.post_form.choose_language[i].checked)
    {
      var rad_val = document.post_form.choose_language[i].value;
    }
  }
  return rad_val;
  
}

function rating( post, rating ) {
  
  $.post("http://foorum.noortele.ee/ajax_actions/rating.php", {action:'give_rating',post:post,rating:rating}, function(data) {  
  
    data = eval('(' + data + ')');
    
    if (data.code == 200) {
    
      $('.report1').hide();
      $('#report1_content').html(data.message);
      $('.report1').fadeIn();
      $('#post_rationg_' + post).html(data.rating);
      
    } else {
      $('.report2').hide();
      $('#report2_content').html(data.message);
      $('.report2').fadeIn();
    }
    
  });
  
}

function close_message( element ) {
  $('.' + element).fadeOut('slow');
}

function delete_post( post ) {
  $('#confirm_delete_' + post).fadeIn();
}

function confirm_post_delete( post, action ) {
  
  if ( action == 'confirm' ) {
    
    $.ajax({
	    type: "POST",
	    url: "http://foorum.noortele.ee/ajax_actions/delete.php",
	    data: {action:'delete_post',post:post},
	    dataType: "json",
	    cache: false,
	    success: function(data) {
	  
	      if (data.code == 200) {
      
	        $('.report1').hide();
	        $('#report1_content').html(data.message);
	        $('.report1').fadeIn();
	        
	        $('.post_data_' + post).fadeOut('slow');
	        
	      } else {
	        $('.report2').hide();
	        $('#report2_content').html(data.message);
	        $('.report2').fadeIn();
	      }
	      
	    }
	  });
    
  } else {
    $('#confirm_delete_' + post).fadeOut();  
  }
  
}

function vote( topic ) {

  $.post("http://foorum.noortele.ee/ajax_actions/vote.php", {action:'vote',topic:topic,answer:$('input[name=answer]:radio:checked').val()}, function(data) {  
    $('.voteArea').html(data);
  });
  
}

function view_results( topic ) {
  
  $.post("http://foorum.noortele.ee/ajax_actions/vote.php", {action:'view_results',topic:topic}, function(data) {  
    $('.voteArea').html(data);
  });
  
}

$(document).ready(function() {
  
  $("div[class*='tag'],div[class*='font'],").hover( function() { 
    $('.helpArea').html($(this).attr('rel'));
  });
  
  $('#add_poll_button').click(function(){
    if ( $('.createVote').is(':hidden') ) {
      $('.createVote').slideDown();
    } else {
      $('.createVote').slideUp();
    }
  });
  
  $('.sFont').change(function() {
    createBBtag($('.sFont :selected').val(),'[/size]','content_area');  
  });
  
  $('.btnLogin-2').fancybox({
		'transitionIn'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'scrolling' : 'no'
  });
  
});

