var loading_image_number = 1; // 10, 14 ou 16

$('#check_load').src = './images/loadings/loading'+loading_image_number+'.gif';

/************
* Functions *
************/
var trim = function(str)
{
	if (typeof str == 'undefied') return '';
	while (str.length > 0 && str.charAt(0) == ' ') str = str.substr(1, str.length - 1);
	while (str.length > 0 && str.charAt(str.length - 1) == ' ') str = str.substr(0, str.length - 1);
	return str;
};

var urlencode = function(str)
{
	var strencoded = '';
	var tmp;
	for(var i=0;i<str.length;i++) {
		tmp = str.charCodeAt(i);
		if (tmp < 48 || (tmp > 57 && tmp < 65) || (tmp > 90 && tmp < 97) || tmp > 122) {
			strencoded += '%'+Math.hex(tmp).substr(6, 2);
		} else strencoded += String.fromCharCode(tmp);
	}
	return strencoded;
};

$('#selectxhtmlgr').click(function() { $('#gen_xhtml').select(); });

/*****************
* Check Clean Up *
*****************/
$('#check_tld').change(function()
{
	 $('#imgarea').removeClass('show').addClass('hide');
});
//$('#check_uri').onkeydown = $('#check_tld').onchange;
//$('#check_query').onkeydown = $('#check_tld').onchange;

/****************
* Check Process *
****************/
$('#check_btm').click(function()
{
	this.blur();

	$('#imgarea').removeClass('hide').addClass('show');
	$('#imgarea').css({
		'padding': '10px'
	});

	$('#check_query').val(trim($('#check_query').val()));

	var parsedUri = parseUri(trim($('#check_uri').val()));
	var uri = parsedUri['host']+parsedUri['path'];
	$('#check_uri').val(uri);

	if(!$('#check_query').val() || !$('#check_uri').val())
	{
		alert('Un ou plusieurs champs sont vides');
		return;
	}
	
	$('#check_btm').disabled = true;
	$('#check_uri').disabled = $('#check_btm').disabled;
	$('#check_query').disabled = $('#check_btm').disabled;
	$('#check_tld').disabled = $('#check_btm').disabled;
	$('#check_theme').disabled = $('#check_btm').disabled;
	$('#check_btm').val('Vérification...');
	$('#check_img').hide();

	$('#check_load').show();

	var timestamp = new Date().getTime();

	/**************************
	* When the image has load *
	**************************/
	$('#check_img').load(function()
	{
		$('#check_btm').disabled = false;
		$('#check_uri').disabled = $('#check_btm').disabled;
		$('#check_query').disabled = $('#check_btm').disabled;
		$('#check_tld').disabled = $('#check_btm').disabled;
		$('#check_theme').disabled = $('#check_btm').disabled;
		$('#check_btm').val('Vérifier');

		$('#check_load').hide();

		$('#check_img').show();
	});

	$('#check_img').attr('src', './googlerank.gif?uri='+uri+'&q='+$('#check_query').val()/*+'&tld='+$('#check_tld').val()*/+'&theme='+$('#check_theme').val()+'&timestamp='+timestamp+'&check=1&');
});

/********************
* XHTML Gen Process *
********************/
$('#gen_btm').click(function()
{
	$('#gen_query').val(trim($('#gen_query').val()));

	if(!$('#gen_query').val())
	{
		alert('Remplisser le champ mots clés');
		return;
	}

	var params = 'q='+urlencode($('#gen_query').val());
	if($('#gen_uri').val()) params += '&uri='+urlencode($('#gen_uri').val().replace('http://', ''));
//	if ($('#gen_tld').val() != 'com') params += '&amp;tld='+$('#gen_tld').val();
	if($('#gen_theme').val() != 'full') params += '&theme='+$('#gen_theme').val();
	if(!$('#gen_hostonly').checked) params += '&hostonly=false';

	$('#gen_xhtml').val('<a href="http://googlerank.nquenault.fr/"><img src="http://googlerank.nquenault.fr/googlerank.gif?'+params+'" alt="Google Rank" style="border: 0px;" /></a>');
	$('#gen_xhtml').blur();
});

var test = function()
{
	window.g = 1;
	window.count = 0;
	window.si = window.setInterval(function() {
		if (window.g == 1) {
			window.blur();
			window.g = 0;
		} else {
			window.focus();
			window.g = 1;
		}
		window.count++;
		if (window.count == 20) clearInterval(window.si);
	}, 150);
};

/* parseUri 1.2; MIT License
By Steven Levithan <http://stevenlevithan.com> */

var parseUri = function (source) {
	var o = parseUri.options,
		value = o.parser[o.strictMode ? "strict" : "loose"].exec(source);
	
	for (var i = 0, uri = {}; i < 14; i++) {
		uri[o.key[i]] = value[i] || "";
	}
	
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q: {
		name: "queryKey",
		parser: /(?=.)&?([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

/*********************************************************/
