var socialthing = {};
socialthing.build = "20090520.580"; // 20090429.2980, 20090430.2998
socialthing.lib = {};
socialthing.init = function (attributes) {
	
	
	function addScript(url, attributes) {
	    var script = document.createElement("script");
	    script.setAttribute("type","text/javascript");
	    script.setAttribute("src", url);
	    for (var a in attributes) {
	        script.setAttribute(a, attributes[a]);
	    }	
	    document.body.appendChild(script);   
	}	
	
	function attachSocialthing() {
		
		/* if its dev then we want to use local resources for all the files since
		 * we will not be compiling this 
		 */
		if ( socialthing.build == 'dev' ){
			addScript("jquery-tiny.js",{});
			addScript("jquery.timeago.js",{});
			addScript("swfobject.js",{});
			addScript("localstore.js",{});
			addScript("aimapi-core.js",{});
			addScript("scriptiframe.js",{});
		}
	
		// include the appropriate siteIM file
		var a = attributes;
		a.baseResourceURI = socialthing.lib.baseResourceURI;
		a.id = "siteIM";
		a.css = socialthing.lib.stylesheet;		
		addScript(socialthing.lib.core,a);
	
	}
		
	// if an override build was passed in as a part of the embed use appropriately.
	if ( attributes.build ) {
		socialthing.build = attributes.build;
	}

	// Setup the default path to load the socialting core
	socialthing.lib.baseResourceURI = "http://o.aolcdn.com/aim/siteIM/" + socialthing.build + "/"; // the default
	socialthing.lib.stylesheet = socialthing.lib.baseResourceURI + "siteim-core-min.css";
	socialthing.lib.core = socialthing.lib.baseResourceURI + "siteim-core-min.js"; // the default
	
	// if its dev override the values
	if ( socialthing.build == "dev") {
		socialthing.lib.core = "siteIM.js";
		socialthing.lib.stylesheet = "siteIM.css";
		socialthing.lib.baseResourceURI = "http://o.aolcdn.com/aim/siteIM/";
	}
	
	// default the partner resource locations
	if ( typeof attributes.partnerResourceURI != "undefined" ) {
		if ( typeof attributes.partnerRedirectURI == "undefined") attributes.partnerRedirectURI = attributes.partnerResourceURI + "siteIM-redir.html";
		if ( typeof attributes.swfurl == "undefined") attributes.swfurl = attributes.partnerResourceURI + "localstore.swf";
		if ( typeof attributes.listenerSwfURI == "undefined") attributes.listenerSwfURI = attributes.partnerResourceURI + "swfsocket.swf";
	}
	
	// since the init method is called right away on the partner site we must wait to call the addScript call until the DOM is finished.
	if (document.addEventListener) document.addEventListener("DOMContentLoaded", attachSocialthing, false);
	// for Internet Explorer (using conditional comments)
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
	    if (this.readyState == "complete") {
	        attachSocialthing(); // call the onload handler
	    }
	};
	/*@end @*/
	
	// for Safari
	if (/WebKit/i.test(navigator.userAgent) && (/Version\/[1-2]/.test(navigator.userAgent))) {
	    var _timer = setInterval(function() {
	        if (/loaded|complete/.test(document.readyState)) {
	            clearInterval(_timer);
	            attachSocialthing(); // call the onload handler
	        }
	    }, 10);
	}
		
}
