// Include the JQuery library!
document.write("<scr" + "ipt src='http://creative.myspacecdn.com/common_functions/code/profiles/jquery-1.2.6.min.js' type='text/javascript'></scr" + "ipt>");

// Hide main container while page loads (can't use jquery here)
document.getElementById('wrap').style.display = "none";

// BEGIN function moveBlog()
function moveBlog (blogContainer, targetContainer) {
	// Get the blog content
	var blogContent = blogContainer.html();
	
	// Place it into the target container
	$(targetContainer).append(blogContent);
	
	// Now remove the original blog DIV
	blogContainer.remove();
}
// END function moveBlog()

function addStylesheet(theURL){
	if(theURL){
		var theLink = '<link rel="stylesheet" type="text/css" name="exception" href="' + theURL + '" />';
		$(theLink).insertBefore('link[@href*=global]');
	}
}

// Start the main function
function cleanProfile(profileObject) {
	// Set the main wraps' width and display properties while the document loads
	var mainContainer = $('div#wrap').css('width', profileObject.containerWidth);

	// Attach the nice clean styles
	addStylesheet('http://creative.myspacecdn.com/common_functions/code/profiles/css/profile1.css');
	//$('link[@href*=UserStyle').attr('name', 'exception');
	addStylesheet($('link[@href*=UserStyle]').attr('href'));
	//$('link[@href*=webim').attr('name', 'exception');
	addStylesheet($('link[@href*=webim]').attr('href'));
	
	// Detach all old dirty code
	// -- Remove the linked stylesheets
	$('link[@name!=exception]').remove();
	
	
	// -- Remove the embedded styles
	var embeddedStyles = $('style');
	for (i = 0; i < embeddedStyles.size(); i++) {
		embeddedStyles.eq(i).remove();
	}
	// -- Remove International Links DIV
	$('div#internationalLinks').remove();
	
	// If the blog is present, put it in the specified DIV
	var blogContainer = $('div#profile_blog');
	if (blogContainer.html() != null) {
		if (typeof(profileObject.blogTarget) != "undefined") {
			var targetContainer = $(profileObject.blogTarget);
			if( targetContainer.html() != null) {
				moveBlog(blogContainer, profileObject.blogTarget);
			} else {
				alert("The blog target DIV that you have specified cannot be found in the document. This process has been aborted.");
				blogContainer.remove();
			}
		} else {
			alert("In order to include a blog, you need to specify the target container in the top of your page. Eg: var blogTarget = 'div#my_blog'. This process has been aborted.");
			blogContainer.remove();
		}
	} else {
		if (typeof(blogTarget) != "undefined") {
			alert('You have specified a target DIV for the blog, but have not included the module. Please insert <module id="13" /> or remove the target variable (var blogTarget)');
		}
	}
	
	// Move forum links into a new DIV
	var forumLinks = $('div#profile_forums').find('span.right').eq(0);
	var forumLinksHtml = forumLinks.html();
	var forumHead = $('div#profile_forums').find('h5');
	$('<div id="forum-links">' + forumLinksHtml + '</div>').insertAfter(forumHead);
	forumLinks.remove();
	
	// Now change module headers
	if (typeof(profileObject.friendsHeader) != "undefined") {
		$('div#profile_friends h5').html(profileObject.friendsHeader);
	}
	if (typeof(profileObject.forumsHeader) != "undefined") {
		$('div#profile_forums h5').html(profileObject.forumsHeader);
	}
	if (typeof(profileObject.commentsHeader) != "undefined") {
		$('div#profile_comments h5').html(profileObject.commentsHeader);
	}
	
	// Display the page and remove the loader
	mainContainer.css('display', 'block');
};
