$(document).ready(function() {
    $('#inspired-errors').click(function(){
        $('#inspired-errors').hide();
    });
    $('#inspired').click(function() {
	//hide main editor
        $('.editor').hide();
	//get initial value for inspired word
	$(".inspired").show();
        var toInspired = $('#h1').val();
	if(toInspired != '') {
	    runTheWheel(toInspired);
	}
    });

    $('#find').click(function(){
	var toInspired = $('#h1').val();
        runTheWheel(toInspired);
    })

     $("#h1").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            var toInspired = $('#h1').val();
	    runTheWheel(toInspired);
            return false;
        }
    });
});


function runTheWheel(stringToInsp) {
    var websiteUrl = $('#website_url').val();
    if(websiteUrl == '') {
        websiteUrl = $('#hid-main-wsurl');
    }
    if(stringToInsp != '') {
	$.ajax({
	    type : 'POST',
	    url: websiteUrl+'sys/backend_ajax/rungooglewheel/',
	    data: ({
		inspired : stringToInsp
	    }),
	    success: function(response){
		$(".words-list").empty();
		$(".words-list").html(response);
	    }
	});
    }
    else {
	$('#inspired-errors').show();
        $('#search-empty').show();
        $('#inspired-errors').click(function() {
	    $('#inspired-errors').hide();
        });
    }
}

$(document).ajaxStart(function () {
    $('#ajaxBusy').show();
});
$(document).ajaxStop(function (){
    $('#ajaxBusy').hide();
});

function backToPage() {
    $(".inspired").hide();
    $('.editor').show();  
}

function trim(str)
{
    var ltrim = /^\s+/g;
    var rtrim = /\s+$/g;

    str = str.replace(ltrim,'');
    str = str.replace(rtrim,'');
    return str;
}
