// ==UserScript==
// @name           Myspace Notifier
// @namespace      http://www.myspace.com/sean_is_a_bamf
// @description    Will notify you of any new mail, friend requests, etc., from anywhere on the 'net.
// @include        *
// ==/UserScript==

// Note: You must be logged into myspace for this script to function.
// Using this script might make some internet connections seem slow, this is because
// the myspace homepage is requested every time you load any new page. 
// Configuration Options:

(function(){
 messages = true;
 messageIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 friendrequests = true;
 friendrequestIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 comments = true;
 commentIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 imgcomments = true;
 imgcommentIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 blogComments = true;
 blogCommentIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 blogSubscriptions = true;
 blogSubscriptionIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 events = true;
 eventIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 birthdays = true;
 birthdayIcon = 'http://x.myspace.com/images/icon_envelope.gif';
 /* Don't edit anything below unless you know what you're doing */
 function makeRequest() {
 	var xmlhttp = PRO_xmlhttpRequest();
 	xmlhttp.open("GET", 'http://home.myspace.com/index.cfm?fuseaction=user', true);
 	xmlhttp.setRequestHeader('User-agent', 'Mozilla/4.0 (compatible)');
 	xmlhttp.setRequestHeader('Accept', 'text/xml');
 	xmlhttp.send(null);
 	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			// Store html of the myspace homepage.
			var response = xmlhttp.responseText;
			if(response == '') return;
			// Retrieve the user's friend identifier.
			var r = /friendid=(\d+)"/g.exec(response);
			if(r.length == 1) return;
			var friendid = r[1];
			// Fill in applicable notifications
			var html = '';
			var msgText=/id="indicatorMail" class="(\w+)/g.exec(response)[1];
			var show = 0;
			if(messages && msgText == 'show') {
				html+= '<a href="http://mail.myspace.com/index.cfm?fuseaction=mail.inbox"><img src="'+messageIcon+'"> New Messages</a><br />';
				show = 1;
			}
			var reqText=/id="indicatorFriendRequest" class="(\w+)/g.exec(response)[1];
			if(friendrequests && reqText == 'show') {
				html+= '<a href="http://mail.myspace.com/index.cfm?fuseaction=mail.friendRequests"><img src="'+friendrequestIcon+'"> New Friend Requests</a><br />';
				show = 1;
			}
			var comText=/id="indicatorComments" class="(\w+)/g.exec(response)[1];
			if(comments && comText == 'show') {
				html+= '<a href="http://comments.myspace.com/index.cfm?fuseaction=user.HomeComments&amp;friendID='+friendid+'"><img src="'+commentIcon+'"> New Comments</a><br />';
				show = 1;
			}
			var imgText=/id="indicatorImageComments" class="(\w+)/g.exec(response)[1];
			if(imgcomments && imgText == 'show') {
				html+= '<a href="http://viewmorepics.myspace.com/index.cfm?fuseaction=user.viewPicture&amp;friendID='+friendid+'"><img src="'+imgcommentIcon+'"> New Picture Comments</a><br />';
				show = 1;
			}
			var bcomText=/id="indicatorBlogComments" class="(\w+)/g.exec(response)[1];
			if(blogComments && bcomText == 'show') {
				html+= '<a href="http://blog.myspace.com/index.cfm?fuseaction=blog.controlcenter"><img src="'+blogCommentIcon+'"> New Blog Comments</a><br />';
				show = 1;
			}
			var blogText=/id="indicatorBlogs" class="(\w+)/g.exec(response)[1];
			if(blogSubscriptions && blogText == 'show') {
				html+= '<a href="http://blog.myspace.com/index.cfm?fuseaction=blog.controlcenter"><img src="'+blogSubscriptionIcon+'"> New Blog Subscription Posts</a><br />';
				show = 1;
			}
			var eventText=/id="indicatorEvents" class="(\w+)/g.exec(response)[1];
			if(events && eventText == 'show') {
				html+= '<a href="http://mail.myspace.com/index.cfm?fuseaction=mail.eventInvite"><img src="'+eventIcon+'"> New Event Invitation</a><br />';
				show = 1;
			}
			var bdayText=/id="indicatorBirthday" class="(\w+)/g.exec(response)[1];
			if(birthdays && bdayText == 'show') {
				html+= '<a href="http://collect.myspace.com/index.cfm?fuseaction=user.birthdays&amp;friendID='+friendid+'"><img src="'+birthdayIcon+'"> New Birthdays</a><br />';
				show = 1;
			}
			if(show == 1) {
				// Create notifier element.
				var notifier = document.createElement('div');
				notifier.setAttribute('id', 'GM_MSNotify');
				document.body.appendChild(notifier);

				notifier.innerHTML = html;

				// Apply some custom CSS to the notifier element.
				PRO_addStyle('#GM_MSNotify {' +
						'display:block!important;' +
						//FIXME for IE
						//'position:fixed!important;' +
						'position:absolute!important;' +
						'top:0!important;' +
						'left:0!important;' +
						'z-index:9999!important;' +
						'width:150px!important;' +
						'background-color:#000000!important;' +
						'opacity:80.0!important;' +
						'text-align:left!important;' +
						'-moz-border-radius:0 0px 10 10!important;' +
						'padding:3px!important;}' +
						'#GM_MSNotify a {' +
						'display:inline!important;' +
						'color:#FFF!important;' +
						'text-decoration:none!important;' +
						'line-height:8px;' +
						'font:bold 10px Century Gothic, serif!important;}' +
						'#GM_MSNotify br {display:inline!important}' +
						'#GM_MSNotify img {width:12px!important;opacity:1!important;border:none!important}'
					    );
			}
		}
	}
 }
 // Make sure we're not in a frame's body, then execute script.
 if(top.location == location) {
	 makeRequest();
 }
})();
