var clip;
var base_url = 'http://respelt.com/';

//check spelling of text
function check_docspell()
{
        jQuery('#url_contents_tbl').show();
	var data = tinyMCE.get('elm1').getContent();
        data = data.replace("&rsquo;", "'");
	//alert(data);
	$("#reply").html('');
	var result_head = "";
	$("#result_head").html(result_head);
	$("#url_reply").show();
        if($('#expand_div').length){
            $('#expand_div').remove();
            $("#url_reply").before('<div id="expand_div"><div id="expand_a"><a href="javascript:expand_area()" title="Expand view"><span style="float: left; margin-right: 3px;"><img src="'+base_url+'images/back.png" border="0"/></span> <span style="float: right;">expand</span></a></div><div id="expand_copy_div"><a href="javascript:" id="copytoclipboard" class="buttonCopy"><span>Copy to clipboard</span></a></div></div>');
        }else{
            $("#url_reply").before('<div id="expand_div"><div id="expand_a"><a href="javascript:expand_area()" title="Expand view"><span style="float: left; margin-right: 3px;"><img src="'+base_url+'images/back.png" border="0"/></span> <span style="float: right;">expand</span></a></div><div id="expand_copy_div"><a href="javascript:" id="copytoclipboard" class="buttonCopy"><span>Copy to clipboard</span></a></div></div>');
        }
	
	var ed3 = tinyMCE.get('url_contents');
	ed3.setContent(data);
	ed3.execCommand('mceSpellCheck');

        //copy to clipboard button
        copy();
}

//check spelling of URL
function checkurl(xj) {
        $("#url_reply").hide();
        $("#expand_div").remove();
	$("#reply").html('<p style="margin-top: 150px;">&nbsp;</p><center><img src="images/loading.gif" /></center>');
        var theurl = '';
        if(xj == null){
                theurl = $("#url").val();
        }else{
                theurl = $("#checkfeedall_"+xj).val();
        }
	$.post(base_url+"spellcheckurl.php", {url: theurl}, function(data) {
		//alert(data);
                var tmp1_data = data.substring(0,3);
                        $("#reply").html('');
                if(tmp1_data != '###'){
                        jQuery('#url_contents_tbl').show();
                        var result_head = "<b style='display: block; text-align: center; font-size: 12px;'> Spell check results for "+theurl+"</b> <br /> <br />";
                        if($('#expand_div').length){
                            $('#expand_div').remove();
                            result_head += '<div id="expand_div"><div id="expand_a"><a href="javascript:expand_area()" title="Expand view"><span style="float: left; margin-right: 3px;"><img src="'+base_url+'images/back.png" border="0"/></span> <span style="float: right;">expand</span></a></div><div id="expand_copy_div"><a href="#" id="copytoclipboard" class="buttonCopy"><span>Copy to clipboard</span></a></div></div>';
                        }else{
                            $("#url_contents_parent").before('<div id="expand_div"><div id="expand_a"><a href="javascript:expand_area()" title="Expand view"><span style="float: left; margin-right: 3px;"><img src="'+base_url+'images/back.png" border="0"/></span> <span style="float: right;">expand</span></a></div><div id="expand_copy_div"><a href="#" id="copytoclipboard" class="buttonCopy"><span>Copy to clipboard</span></a></div></div>');
                        }
                        $("#result_head").html(result_head);

                        $("#url_reply").show();

                        data = data.split('###'); //split data
                        $("#result_head").append(data[1]); //add javascript array of bad words
                        var r = arrayOfBadWordsAndSugg;
                        //drow image and errors
                        if(jQuery('#expand_error').length){
                                if(r.length == 1)
                                    jQuery('#expand_error').html('<span id="url_image_e"><img src="'+base_url+'images/exclamation.png" style="float:left; margin-right:3px;" /></span><span style="float:right;"><span id="URLnumError">'+r.length+'</span> spelling error!</span>');
                                else
                                    if(r.length != 0)
                                        jQuery('#expand_error').html('<span id="url_image_e"><img src="'+base_url+'images/exclamation.png" style="float:left; margin-right:3px;" /></span><span style="float:right;"><span id="URLnumError">'+r.length+'</span> spelling errors!</span>');
                        }else{
                                if(r.length == 1)
                                    jQuery('#expand_div').prepend('<div id="expand_error"> <span id="url_image_e"><img src="'+base_url+'images/exclamation.png" style="float:left; margin-right:3px;" /></span><span style="float:right;"><span id="URLnumError">'+r.length+'</span> spelling error!</span></div>');
                                else
                                    if(r.length != 0)
                                        jQuery('#expand_div').prepend('<div id="expand_error"> <span id="url_image_e"><img src="'+base_url+'images/exclamation.png" style="float:left; margin-right:3px;" /></span><span style="float:right;"><span id="URLnumError">'+r.length+'</span> spelling errors!</span></div>');
                        }

                        var ed3 = tinyMCE.get('url_contents');
                        
                        ed3.setContent(data[0]);
                        ed3.execCommand('mceSpellCheck_norpc');

                        //copy to clipboard button
                        copy();
                }else{
                        data = data.substring(3, data.length);
                        $("#result_head").html('');
                        $("#result_head").html(data);
                        data = '';
                        jQuery('#url_contents_tbl').hide();
                        $("#url_reply").show();
                }
	});
}

//check spelling of feed URL
function checkspelling() {
	$("#url_reply").hide();
	$("#reply").html('<p style="margin-top: 150px;">&nbsp;</p><center><img src="images/loading.gif" /></center>');
	var theurl = $("#rss").val();
        if($('#expand_div').length){
            $('#expand_div').remove();
        }
	$.post(base_url+"spellcheckfeed.php", {url: theurl}, function(data) {
		
		data = data.split('###');
		$("#result_head").html(data[0]);
                if(data[0].indexOf('Invalid feed URL') != -1){
                        jQuery('#url_contents_tbl').hide();
                }else{
                        jQuery('#url_contents_tbl').show();
                }
                //alert(data[2]);
                $("#result_head").append(data[2]);
                $("#reply").html('');

                $("#url_reply").show();

                //$("#url_contents").html(data);
                //alert(data[1]);
                var ed3 = tinyMCE.get('url_contents');
                ed3.setContent(data[1]);
                ed3.execCommand('mceSpellCheck_norpc');
                
	});
}
//check spelling of feed URL ALL
function checkspellingfeedall(xj) {
	$("#url_reply").hide();
	$("#reply").html('<p style="margin-top: 150px;">&nbsp;</p><center><img src="images/loading.gif" /></center>');
	var theurl = $("#checkfeedall_"+xj).val();
        //alert ('j='+xj+" url="+$("#checkfeedall_"+xj).val());
        if($('#expand_div').length){
            $('#expand_div').remove();
        }
	$.post(base_url+"spellcheckfeedall.php", {url: theurl}, function(data) {

		data = data.split('###');
		$("#result_head").html(data[0]);
                //alert(data[2]);
                $("#result_head").append(data[2]);
		$("#reply").html('');

		$("#url_reply").show();
		
		//$("#url_contents").html(data);
		//alert(data[1]);
		var ed3 = tinyMCE.get('url_contents');
		ed3.setContent(data[1]);
		ed3.execCommand('mceSpellCheck_norpc');
	});
}
function close_area(){
    jQuery('.leftcol').show();
    jQuery('.rightcol').css("width","540px");
    jQuery('#url_contents_tbl').css("width","550px");
    jQuery('#expand_a').html('<a href="javascript:expand_area()" title="Expand view"><span style="float: left; margin-right: 3px;"><span style="float: left; margin-right: 3px;"><img src="'+base_url+'images/back.png" border="0"/></span> <span style="float: right;">expand</span></a>');
    if(jQuery('#expand_copy_div').length != 0){
    jQuery('#expand_copy_div').html('<a href="#" id="copytoclipboard" class="buttonCopy"><span>Copy to clipboard</span></a>');
            clip.reposition('copytoclipboard');
    }
}

function expand_area(){
    jQuery('.leftcol').hide();
    jQuery('.rightcol').css("width","100%");
    jQuery('#expand_div').css("width","100%");
    jQuery('#url_contents_tbl').css("width","100%");
    jQuery('#expand_a').html('<a href="javascript:close_area()" title="Reduce view"><span style="float: left;">reduce</span> <span style="float: right; margin-left: 3px;"><img src="'+base_url+'images/forward.png" border="0"/></span></a>');
    if(jQuery('#expand_copy_div').length != 0){
    jQuery('#expand_copy_div').html('<a href="#" id="copytoclipboard2" class="buttonCopy"><span>Copy to clipboard</span></a>');
    //clip.hide();
    
            clip.reposition('copytoclipboard2');
    }
    
}

function check_result_head(){
        if(jQuery('#expand_a').length != 0){
                expand_area();
        }else{
                setTimeout("check_result_head();",200);
        }
}

function checkdirect() {
	$("#reply").html('<p style="margin-top: 150px;">&nbsp;</p><center><img src="images/loading.gif" /></center>');
	var thecontent = $("#text").val();
	$.post("spellchecktext.php", {spelling: thecontent}, function(data) {
		$("#reply").html(data);
	});
}

function replace(newword, currentword) {
	ignore(currentword, "text");
	$(".word-"+currentword).text(newword);
}

function viewarticle(articleid) {
	var selectortext = "article-" + articleid;
	var currentdisplay = $("#" +selectortext).css("display");
	if(currentdisplay == "block") {
		$("#"+selectortext).slideUp();
		$("#"+articleid+"-view").text('[ View Errors ]');
	} else {
		$("#"+selectortext).slideDown();
		$("#"+articleid+"-view").text('[ Hide Errors ]');
	}
}

function edittext() {
	$("#edit").fadeIn();
	var text = $(".wordview").text();
	var suggestions = $("#oversuggestions").text();
	$.post("cleanup.php", {text: text, suggestions: suggestions}, function(data) { 
		$("#edit").val(data);
	});
}

function addtodictionary(theword, thistype) {
	if(thistype == "text") {
		$.post(base_url+"learn_word.php", {word: theword}, function(data) {
			var minus = $("." +theword).length;
			var current_errors = $("#error_number").text();
			var current_errors = parseInt(current_errors);
			var total = current_errors - minus;
			$("."+theword +"").css({'background': 'none'});
			$("." +theword + " .menu").css({'display': 'none'});
			$("." +theword + " .menu").html('');
			if(total <= 0) {
				$("#reply").html('<div class="good" style="text-align: center; margin-top: 150px;"><h1><img src="images/accept.png" align="absmiddle" />&nbsp;No spelling errors found</h1></div>');
			} else {
				$("#error_number").html(total);
			}
		});
	}
}

function replace_feed(suggestion, theword, articleid) {
	ignore_feed(theword, articleid);
	$("."+theword+"-"+articleid).text(suggestion);
}

function addtodictionary_feed(theword, articleid) {
	$.post("learn_word.php", {word: theword}, function(data) {
		var minus = $("." + theword).length;
		var current_errors = $("#error_number-" +articleid).text();
		var current_errors = parseInt(current_errors);
		var total = current_errors - minus;
		$("." + theword).css({'background': 'none'});
		$("." + theword + " .menu").css({'display': 'none'});
		$("." + theword + " .menu").html('');
		if(total <= 0) {
			$("#error_text-" +articleid).slideUp();
			var selectortext = "article-" + articleid;
			$("#"+selectortext).slideUp();
		} else {
			$("#error_number-" +articleid).html(total);
		}			
	});
}

function ignore_feed(theword, articleid) {
	var minus = $("." + theword).length;
	var current_errors = $("#error_number-" +articleid).text();
	var current_errors = parseInt(current_errors);
	var total = current_errors - minus;
	$("." + theword +"").css({'background': 'none'});
	$("." + theword + " .menu").css({'display': 'none'});
	$("." + theword + " .menu").html('');
	if(total <= 0) {
		$("#error_text-" +articleid).slideUp();
		var selectortext = "article-" + articleid;
		$("#"+selectortext).slideUp();
	} else {
		$("#error_number-" +articleid).html(total);
	}
}

function ignore(theword, thistype) {
	if(thistype == "text") {
		var minus = $("." +theword).length;
		var current_errors = $("#error_number").text();
		var current_errors = parseInt(current_errors);
		var total = current_errors - minus;
		$("."+theword +"").css({'background': 'none'});
		$("." +theword + " .menu").css({'display': 'none'});
		$("." +theword + " .menu").html('');
		if(total <= 0) {
			$("#reply").html('<div class="good" style="text-align: center; margin-top: 150px;"><h1><img src="images/accept.png" align="absmiddle" />&nbsp;No spelling errors found</h1></div>');
		} else {
			$("#error_number").html(total);
		}		
	}
}

function ignoreall() {
	$(".selected").css({'background': 'none'});
	$(".menu").css({'display': 'none'});
	$(".menu").html('');
	// error_text
	// error_number
	$("#reply").html('<div class="good" style="text-align: center; margin-top: 150px;"><h1><img src="images/accept.png" align="absmiddle" />&nbsp;No spelling errors found</h1></div>');
}

function wordview(thestat) {
	if(thestat == "enable") {
	
		$("#textview").slideUp();
		$(".wordview").slideDown();
	
	} else {

		$(".wordview").slideUp();
		$("#textview").slideDown();
	
	}
}

function ignoreall_article(articleid) {
	$("#error_text-" +articleid).slideUp();
	var selectortext = "article-" + articleid;
	$("#"+selectortext).slideUp();
}

function imageNumErrors (e){
        var div_id = $(e).parents("div:first").attr("id");
        var div_id_str = div_id.substr( 0, div_id.indexOf('_') );
        var div_id_num = div_id.substr( div_id.indexOf('_') + 1, div_id.length );
        var tmp_1 = '#numErrors_' + div_id_num;
        var numErrors = parseInt( $("#url_contents_ifr").contents().find(tmp_1).text() ); //get
        numErrors--;
        if (numErrors <=0){numErrors = 0;}
        $("#url_contents_ifr").contents().find(tmp_1).text(numErrors);
        var tmp_2 = '#feed_image_' + div_id_num;
        if (numErrors <= 0){
            //$("#url_contents_ifr").contents().find(tmp_2).html("<img src='images/accept.png' align='absmiddle' style='float:left;' />");
            $("#url_contents_ifr").contents().find(tmp_2+" img").attr("src","images/accept.png");
        }
}
function imageNumErrorsDocUrl(){
        var numErrors = parseInt( $('#URLnumError').text() ); //get number of errors
        numErrors--;
        if (numErrors <=0){numErrors = 0;}
        $('#URLnumError').text(numErrors);
        if (numErrors <= 0){
            $("#url_image_e").html("<img src='images/accept.png' align='absmiddle' style='float:left;' />");
            $("#URLnumError").hide();
            $("#URLnumError").after('No');
        }
}

/* F for Remove Feed 24.08.2010. */
function removeFeed (id){
    if (confirm('Are you sure you want to delete the feed?') ){
        $.post(base_url+"removeFeed.php", {rFeedId: id}, function (data){
            window.location.replace("dashboard.php?rfeed="+data);
        });

    }
}

/* F for Copy to Clipboard */
function copy(){
        if(clip != null){
                clip.hide();
        }
        clip = new ZeroClipboard.Client();
        clip.setHandCursor( true );
        clip.setCSSEffects( true );
        if(jQuery('#copytoclipboard').length){
                    clip.glue('copytoclipboard');

                    function my_mouse_down_handler(client) {
                        if($('#copytoclipboard').length){
                                $('#copytoclipboard').css('background-position', 'bottom right' );
                                $('#copytoclipboard').css('color', '#000' );
                                $('#copytoclipboard').css('outline', 'none' );

                                $('#copytoclipboard span').css('background-position', 'bottom left' );
                                $('#copytoclipboard span').css('padding', '6px 0 4px 18px' );
                        }else{
                                $('#copytoclipboard2').css('background-position', 'bottom right' );
                                $('#copytoclipboard2').css('color', '#000' );
                                $('#copytoclipboard2').css('outline', 'none' );

                                $('#copytoclipboard2 span').css('background-position', 'bottom left' );
                                $('#copytoclipboard2 span').css('padding', '6px 0 4px 18px' );
                        }
                    }
                    clip.addEventListener( 'onMouseDown', my_mouse_down_handler );

                    function my_mouse_up_handler(client) {
                        if($('#copytoclipboard').length){
                                $('#copytoclipboard').css('background-position', 'top right' );
                                $('#copytoclipboard').css('color', '#444' );
                                //$('#copytoclipboard').css('outline', 'none' );

                                $('#copytoclipboard span').css('background-position', 'top left' );
                                $('#copytoclipboard span').css('padding', '5px 0 5px 18px' );
                        }else{
                                $('#copytoclipboard2').css('background-position', 'top right' );
                                $('#copytoclipboard2').css('color', '#444' );
                                //$('#copytoclipboard').css('outline', 'none' );

                                $('#copytoclipboard2 span').css('background-position', 'top left' );
                                $('#copytoclipboard2 span').css('padding', '5px 0 5px 18px' );
                        }
                        var text = $("#url_contents_ifr").contents().text();
                        //text = text.substring(4, text.length);
                        clip.setText(text);
                    };
                    clip.addEventListener( 'onMouseUp', my_mouse_up_handler );
        }
}


$(document).ajaxSuccess(function() { 
	
	
	$(".selected").mouseenter(function() { 
		
		var offset = $(this).offset();
		var currentx = offset.left;

			//$(".menu_top").text(currentx);
			$(".menu", this).css("left", currentx +"px");
			$(".menu", this).fadeIn();

		
		}).mouseleave(function() { 
		
			$(".menu", this).fadeOut("fast");
		
		});

});

$(document).ready(function() { 
	$("#url").keyup(function(event) { 
		if(event.keyCode == 13) {
			checkurl();
		}
	});
	
	$("#rss").keyup(function(event) { 
		if(event.keyCode == 13) {
			checkspelling();
		}
	});
        $('.btn_close').click(function(){
            var id = $(this).parent().attr('id');
            $('#'+id).remove();
            window.location.href = window.location.href.substr(0, window.location.href.indexOf('?'));
        });
});
