var LBGetWidget = Class.create();
LBGetWidget.prototype = {
	initialize: function(){
		Event.observe(window, 'load', this.load.bind(this), false);
	},
	load: function(){
	},
	domainClick: function(url){
		if(! /\w+/.test(url)){
			alert("You did not enter any text. Please enter your blog url to get the LineBuzz widget.");
			return;
		}
		$('ajaxLoading').style.display = 'block';
		new Ajax.Request('/ajx/', {
			method: 'post',
			evalScripts: false,
			parameters: $H({
				f: 'getSiteKey',
				url: url 
				}).toQueryString(),
			onComplete: this.domainClickComplete.bind(this),
			onException: this.domainClickErr.bind(this)
		});
	},
	domainClickComplete: function(req, json){
		$('ajaxLoading').style.display = 'none';
		if(json.err){
			$('lbjsDiv').style.display = 'none';
			alert(json.errMsg);
			return;
		}
		if(! json.key){
			$('lbjsDiv').style.display = 'none';
			alert("We had trouble communicating with our servers. Please try again later.");
			return;
		}
		var tmpl;
		if($('advancedMode').checked == true){
			tmpl = new Template($F('lbjs_t_adv'));
		} else {
			tmpl = new Template($F('lbjs_t'));
		}
		$('lbjs').value = tmpl.evaluate({ LB_SiteKey: json.key });
		$('lbjsDiv').style.display = 'block';
	},
	domainClickErr: function(req, e){
		$('ajaxLoading').style.display = 'none';
		$('lbjsDiv').style.display = 'none';
		alert('We encountered an error. Please try again later.\nThe error was: ' + e);
	}
};

var lbGetWidget = new LBGetWidget();
