/*
 * Boggle Me Javascript gunctions
 */

function isLocal() {
	var url = document.URL.toString();
	
	return url.search(/bgl\.me/i) < 0;
}

var _CALL_GA = ! isLocal();

function default_value(arg, def_value ) {
	return typeof(arg) != 'undefined' ? arg : def_value;
}

if ( _CALL_GA ) {
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-9315133-5']);
	_gaq.push(['_trackPageview']);
	(function() {
		var ga = document.createElement('script');
	    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	    ga.setAttribute('async', 'true');
	    document.documentElement.firstChild.appendChild(ga);
	  })();
}
  						
function ga_event(event, label) {
	shorturl = default_value( label, '' );
	if ( _CALL_GA ) {
		_gaq.push(['_trackEvent', 'bgl_bar', event, label]);
	}
}

function auto_ga( id, event, shorturl ) {
	var elem = document.getElementById(id);
	var f = elem.onclick;
	elem.onclick = function () { ga_event( event, shorturl ); return f ? f() : true; }
}

function clickRetweet() {
	var tweet= document.getElementById('tweet').value;
	var url = 'http://twitter.com/home?status=RT%20' + escape( ( _bgl_twuser ? '@' + _bgl_twuser : '' ) + ': ' + tweet + ' ' + _bgl_shorturl);
	window.location = url;
	return false;
}

function initBoggle() {
	var e = document.getElementById('retweet');
	e.onclick = clickRetweet;
	
	auto_ga( 'user_link', 'user_click', _bgl_twuser );
	auto_ga( 'closex', 'close', _bgl_shorturl );
	auto_ga( 'retweet', 'retweet', _bgl_shorturl );
	auto_ga( 'twmoney', 'twmoney_click', _bgl_shorturl );
}