// API wrapper function
window.doAction = function ( action_hash, cb ) {
	// playing it safe with the literal 'do' - it's a reserved word
	API.smokescreen.action['do'] ( action_hash, function ( rsp ) {
		if ( !rsp.error && rsp.action.achievement && rsp.result == 'new' ) {
			if ( window.achievementSFX && window.achievementSFX.play )
				window.achievementSFX.play();
		}
		if ( typeof cb == "function" ) cb ( rsp );
	} );
};

var ssi = setInterval( function () {
	if ( typeof SoundStage == "undefined" || !SoundStage.ready() ) return;
	clearInterval( ssi );
	window.achievementSFX = window.achievementSFX || new SoundStage.Sound( '/audio/achievement_unlocked.mp3', { noSubtitles : true } );
}, 200 );

window.resolveURI = ( function ( ) {
	var defaults = window.location;
	var re = new RegExp ( /^(?:(?:(\w+\:)\/+)?((\w+[\w-\.]*)(?:\:(\d+))?))?([^?#]*)?(\?[^#]*)?(\#.*)?$/i );
	return function ( input, breakdown ) {
		if ( !input ) input = "";
		if ( input.href ) input = input.href;
		if ( input.location ) input = input.location

		var wrapper = document.createElement("div");
		wrapper.innerHTML = '<a href="' + input + '">' + input + '</a>';
		var resolved = wrapper.getElementsByTagName('a')[0].href;

		if ( !breakdown ) {
			return resolved;
		} else {
			input = resolveURI ( input );
			var matches = input.toString().match(re);
			var matches = input.toString().match(re);

			var protocol = matches[1] || defaults.protocol;
			var hostname = matches[3] || defaults.hostname;
			var port = matches[4] || defaults.port;
			var host = hostname + ( port ? ":" + port : "" );
			var pathname = matches[5] || defaults.pathname;
			var search = matches[6] || "";
			var hash = matches[7] || "";
			var location = protocol + "//" + host + pathname + search + hash;
			if ( !breakdown ) return location;
			return {
				protocol : protocol,
				hostname : hostname,
				port : port || 80,
				host : host,
				pathname : pathname,
				dirname : (pathname.match(/^(.*\/)[^\/]*$/)||[]).pop() || "",
				basename : (pathname.match(/^.*\/([^\/]*)$/)||[]).pop() || "",
				search : search,
				hash : hash,
				toString : function ( ) { return location; }
			}
		}
	}
} )();


window.loadScript = ( function ( ) {
	var head = document.getElementsByTagName("head")[0];
	var find = function ( src ) {
		//debugger;
		var scripts = document.getElementsByTagName('script');
		for ( var i = 0; i < scripts.length; i ++ ) {
			var script = scripts[i];
			var s = script.src ? resolveURI ( script.src ) : "";
			if ( s == src ) return script;
		}
		return null;
	}
	return function ( src, force, callback ) {
		if ( !callback && ( typeof force == "function" ) ) {
			callback = force;
			force = false;
		}

		// normalize src
		src = resolveURI ( src );

		var script;
		if ( force || !(script=find(src)) ) {
			script = document.createElement("script");
			var done = false;
			script.onload = function ( ) {
				if ( done ) return;
				done = true;
				if ( typeof callback == "function" ) callback(this);
			}
			script.onerror = function ( ) {
				if ( done ) return;
				done = true;
				if ( typeof callback == "function" ) callback(null);
			}
			script.onreadystatechange = function ( ) {
				if ( this.readyState == 'loaded' || this.readyState == 'complete' )
					this.onload();
			}
			script.type = "text/javascript";
			script.src = src;
			head.appendChild ( script );
		} else {
			if ( typeof callback == "function" ) callback(script);
		}
	}
} )();

window.loadConversation = function ( src, force, callback ) {
	if ( window.Portal && !!Portal.getMetaData("user").safe ) {
		src = src.replace(/(\.js)$/, ".safe$1");
	}
	loadScript ( src, force, callback );
}

$( function() {
	$( '#send-invitation-form #email' ).one( "focus", function() {
		if ( $( this ).val() == "Your friend's email address" ) $( this ).val( "" );
	} );
	$( '#friend-search-form #friends' ).one( "focus", function() {
		if ( $( this ).val() == "Search for friends" ) $( this ).val( "" );
	} );
	$( '#forum-search-form #id_query' ).one( "focus", function() {
		if ( $( this ).val() == "Search the forum" ) $( this ).val( "" );
	} );
	$( '#send-invitation-form #email' ).one( "focus", function() {
		if ( $( this ).val() == "email address" ) $( this ).val( "" );
	} );
	$( '#login_username' ).one( "focus", function() {
		if ( $( this ).val() == "Username" ) $( this ).val( "" );
	} );
} );

