function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

function makeResults()
{	
	var object; 
	
	$('.pagination a').click(function() {
		object = this;
		$.ajax({
   			type: 'get',
   			url: object.href,
   			processData: false,
   			timeout: 5000,
   			beforeSend: function() {
   				$('#loading').fadeIn('fast');
   			},
   			error: function(req, type) {
   				$('#loading').fadeOut('fast');
   				$('#hint').fadeIn('fast', function(){
					$('#error_box').css('display', 'block');
					$('#error_box').html(req.status + ' ' + type);
				});
   			},
   			success: function(response) {
   				$('.results').html(response);   				
   				$('#loading').fadeOut('fast');
   				
   				makeResults();
   				changeLanguage();
   				actionsMenu();
   				//regexpChecking();
   			}
 		});
 		return false;
	});
}

function changeLanguage()
{
	var object; 
	
	$('#languages a').click(function() {
		object = this;
		$.ajax({
   			type: 'get',
   			url: object.href,
   			processData: false,
   			timeout: 5000,
   			beforeSend: function() {
   				$('#loading').fadeIn('fast');
   			},
   			error: function(req, type) {
   				$('#loading').fadeOut('fast');
   				$('#hint').fadeIn('fast', function(){
					$('#error_box').css('display', 'block');
					$('#error_box').html(req.status + ' ' + type);
				});
   			},
   			success: function(response) {
   				$('#container').html(response);
   				$('#loading').fadeOut('fast');
   				
   				makeResults();
   				changeLanguage();
   				actionsMenu();
   				//regexpChecking();
   			}
 		});
 		return false;
	});
}

function actionsMenu()
{	
	var object; 
	
	$('#option_help').click(function(){
		$('#help_content').fadeIn('fast');
		return false;
	});
	
	$('#help_close').click(function(){
		$('#help_content').fadeOut('fast');
		return false;
	});
	
	$('#option_user').click(function(){
		object = this;
		$.ajax({
   			type: 'get',
   			url: object.href,
   			processData: false,
   			timeout: 5000,
   			beforeSend: function() {
   				$('#loading').fadeIn('fast');
   			},
   			error: function(req, type) {
   				$('#loading').fadeOut('fast');
   				$('#hint').fadeIn('fast', function(){
					$('#error_box').css('display', 'block');
					$('#error_box').html(req.status + ' ' + type);
				});
   			},
   			success: function(response) {
   				response = trim(response);
  				$('#hint').fadeOut('fast', function(){
					$('#error_box').css('display', 'none');
					$('#error_box').html('');
				});
  				$('.results').html(response);
  				$('#loading').fadeOut('fast');

   				makeResults();
   				changeLanguage();
   				actionsMenu();
   				//regexpChecking();
   			}
		});
		return false;
	});
	
	$('#option_users').click(function(){
		object = this;
		$.ajax({
   			type: 'get',
   			url: object.href,
   			processData: false,
   			timeout: 5000,
   			beforeSend: function() {
   				$('#loading').fadeIn('fast');
   			},
   			error: function(req, type) {
   				$('#loading').fadeOut('fast');
   				$('#hint').fadeIn('fast', function(){
					$('#error_box').css('display', 'block');
					$('#error_box').html(req.status + ' ' + type);
				});
   			},
   			success: function(response) {
   				response = trim(response);
  				$('#hint').fadeOut('fast', function(){
					$('#error_box').css('display', 'none');
					$('#error_box').html('');
				});
  				$('.results').html(response);
  				$('#loading').fadeOut('fast');
   				
   				makeResults();
   				changeLanguage();
   				actionsMenu();
   				//regexpChecking();
   			}
		});
		return false;
	});
	
	$('#option_toggle').click(function(){
		object = this;
		if ( $('#option_toggle').hasClass('toggle_hide') )
		{
			$.ajax({
   				type: 'get',
   				url: object.href,
   				processData: false,
   				timeout: 5000,
   				beforeSend: function() {
   					$('#loading').fadeIn('fast', function(){
   						$('#error_box').css('display', 'none');
						$('#hint_results_hidden').css('display', 'none');
						$('#help_content').css('display', 'none');
   					});
   				},
   				error: function(req, type) {
   					$('#loading').fadeOut('fast');
   					$('#hint').fadeIn('fast', function(){
						$('#error_box').css('display', 'block');
						$('#error_box').html(req.status + ' ' + type);
					});
   				},
   				success: function() {
   					$('#loading').fadeOut('fast');
   				}
 			});
 			
			$('#option_toggle').attr('href', str_replace('hide', 'show', $('#option_toggle').attr('href')));
			$('.results').fadeOut('fast', function() {
				$('#option_toggle').removeClass('toggle_hide');
				$('#option_toggle').addClass('toggle_show');
			});
			return false;
		}
		else if ( $('#option_toggle').hasClass('toggle_show') ) {
			$.ajax({
   				type: 'get',
   				url: object.href,
   				processData: false,
   				timeout: 5000,
   				beforeSend: function() {
   					$('#loading').fadeIn('fast', function(){
   						$('#error_box').css('display', 'none');
						$('#hint_results_hidden').css('display', 'none');
						$('#help_content').css('display', 'none');
   					});
   				},
   				error: function(req, type) {
   					$('#loading').fadeOut('fast');
   					$('#hint').fadeIn('fast', function(){
						$('#error_box').css('display', 'block');
						$('#error_box').html(req.status + ' ' + type);
					});
   				},
   				success: function() {
   					$('#loading').fadeOut('fast');
   				}
 			});
 			
			$('#option_toggle').attr('href', str_replace('show', 'hide', $('#option_toggle').attr('href')));
			$('.results').fadeIn('fast', function() {
				$('#option_toggle').removeClass('toggle_show');
				$('#option_toggle').addClass('toggle_hide');
			});
			return false;
		}
	});
	
	$('#option_toggle').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_option_toggle').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_option_toggle').css('display', 'none');
		});
	});
	
	$('#lang_english').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_lang_english').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_lang_english').css('display', 'none');
		});
	});
	
	$('#lang_polish').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_lang_polish').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_lang_polish').css('display', 'none');
		});
	});
	
	$('#option_rss').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_option_rss').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_option_rss').css('display', 'none');
		});
	});
	
	$('#option_help').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_option_help').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_option_help').css('display', 'none');
		});
	});
	
	$('#option_user').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_option_user').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_option_user').css('display', 'none');
		});
	});
	
	$('#option_users').hover(function(){
		$('#hint').fadeIn('fast', function(){
			$('#error_box').css('display', 'none');
			$('#hint_results_hidden').css('display', 'none');
			$('#help_content').css('display', 'none');
			$('#hint_option_users').css('display', 'block');
		});
	}, function(){
		$('#hint').fadeOut('fast', function(){
			$('#hint_option_users').css('display', 'none');
		});
	});
}

function regexpChecking()
{
	var result_visible = true;
	if ( $('#option_toggle').hasClass('toggle_show') )
	{
		result_visible = false;	
	}
	
	var helper = '';
	
	$('#request').submit(function() {	
		$('#sendit').attr('disabled', true);
		$('#loading').fadeIn('fast');
		$.post($('#request').attr('action'), { pattern : $('#pattern').val(), subject : $('#subject').val(), espole : $('#e-s-pole').val(), eshpole : ( $('#e-s-h-pole').val() == '' || $('#e-s-h-pole').val() === undefined ? '' : $('#e-s-h-pole').val() ) }, function(data) {
			data = trim(data);
  			helper = ( data != '' ? data.substr(0,1) : '' );
  			data = ( data != '' ? data.substr(1,(data.length-1)) : '' );
  			if ( helper == '1' )
  			{
  				$('#hint').fadeOut('fast', function(){
					$('#error_box').css('display', 'none');
					$('#error_box').html('');
				});
				
  				$('#results_header').after(data);
  				$('.results table tr:last').remove();

   				if ( result_visible == false )
   				{
   					$('#hint').fadeIn('fast', function(){
						$('#hint_results_hidden').css('display', 'block');
					});
   				}
  			}
  			else {
  				$('#hint').fadeIn('fast', function(){
					$('#error_box').css('display', 'block');
					$('#error_box').html(data);
				});
  			}
  			$('#loading').fadeOut('fast');
		});
		$('#sendit').removeAttr('disabled');
		return false;
	});
}

$(document).ready(function() {	
	makeResults();
   	changeLanguage();
   	actionsMenu();
   	regexpChecking();
});