﻿
var IE5 = (navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1)
var Opera7 = (navigator.userAgent.indexOf('Opera') != -1) && (get_browser_ver('Opera') >= 7);
var Gecko = (navigator.userAgent.indexOf('Gecko') != -1)
var sel = "";
function get_selection() {

    var t = '';
    if (window.getSelection) {
        t = window.getSelection();
    } else if (document.getSelection) {
        t = document.getSelection();
    } else if (document.selection) {
        t = document.selection.createRange().text;
    }
    return t;
    
//    if (document.getSelection) {

//        selection = document.getSelection();
//        alert(selection);
//        if ((!Opera7) && (!Gecko)) {
//            
//            selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
//            selection = selection.replace(/\r\n/gi, " ");
//            while (selection.indexOf("  ") != -1) selection = selection.replace(/  /gi, "");
//            selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
//        }
//    } else if (window.getSelection) {
//        selection = window.getSelection().toString();
//    } else {
//        selection = document.selection.createRange().text;
//    }
//    return selection;
}

function Trim(str) {
    str = str.replace(/^[\s\r\n\t]+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

var QuotableCurrent = null;

function DQuoteSelection(sender) {
    sel = '';
    selected
    $(window).focus();
    QuotableCurrent = $(sender).parent().parent().parent();
    var selected = '';
    selected = (get_selection().toString());
    var trimedSelection = selected.replace(/[ \t\r\n]/g, "");
    var container = QuotableCurrent.text().replace(/[ \t\r\n]/g, "");
    if ((selected.length > 0) && (container.indexOf(trimedSelection) > -1)) {
        sel = selected;
    }

    var nick = $(sender).attr("nick");
    var number = $(sender).text().replace('#', '');

    var Quoute = '';
    Quoute = "<div class=\"qoute\">" + "#" + number + ", " + ((sel.length > 0) ? ("<a href='" + BaseURL + "Authors/" + nick + "'>" + nick + "</a>" + ":") : "") + ((sel.length > 0) ? " \"... " + sel + "...\"- " : " ") + "</div><br/>";
    if (navigator.userAgent.match(/iPhone|iPod/i)) {
    	$('#Text').get(0).focus();
    	$("#Text").val($("#Text").val() + Quoute);
    } else {
    	if (FCKeditorAPI) {
    		var editor = FCKeditorAPI.GetInstance('Text');
    		if (editor) {
    			editor.Focus();
    			editor.InsertHtml(Quoute);
    		}
    	}
    }
    $.scrollTo('#newcomment', 1000)
    QuotableCurrent = null;
}
function QuoteSelection(sender) {
    sel = '';
    selected
    $(window).focus();
    QuotableCurrent = $(sender).parent().parent();
    var selected = '';
     selected = (get_selection().toString());
    var trimedSelection = selected.replace(/[ \t\r\n]/g, "");
    var container = QuotableCurrent.text().replace(/[ \t\r\n]/g, "");
    if ((selected.length > 0) && (container.indexOf(trimedSelection) > -1)) {
        sel = selected;
	}
        
    var nick = $(sender).attr("nick");
    var number = $(sender).text().replace('#', '');

    var Quoute = '';
    Quoute = "<span class=\"qoute\">" + "#" + number + ", " + ((sel.length > 0) ? ("<a href='" + BaseURL + "Authors/" + nick + "'>" + nick + "</a>" + ":") : "") + ((sel.length > 0) ? " \"... " + sel + "...\"- " : " ") + "</span><br/>";

    if(navigator.userAgent.match(/iPhone|iPod/i)) {
		$('#Text').get(0).focus();
		$("#Text").val($("#Text").val() + Quoute);
	} else {
		if (FCKeditorAPI) {
			var editor = FCKeditorAPI.GetInstance('Text');
			if (editor) {
				editor.Focus();
				editor.InsertHtml(Quoute);
			}
		}
    }
    
    QuotableCurrent = null;
  }
    
  $(document).mousedown(function() {
      $(window).focus();
  });