// JavaScript Document
// MySpace Useful Javascript Functions - Written by Richard Francis (UK)
// Email rfrancis@myspace.com for any assistance

// Create objects and vars
var videoVars = new Object();


// ========= VIDEO FUNCTIONS ==========

// Create MySpace video player
function vid(divid, vidid, bg, width, height, autoPlay) {
	// Check that the name does not conflict
	newdivid = "myspacevidcontainer_"+divid;
	if(document.getElementById(newdivid)){
		alert("ERROR ENCOUNTERED BY vid():\nMore than one video player has the same name");
	}else{
		// First create the dynamic global variables. Each video will have its own variables.
		videoVars[divid] = new Object();
		videoVars[divid].vidid = vidid;
		videoVars[divid].bg = bg;
		videoVars[divid].width = width;
		videoVars[divid].height = height;
		
		if(autoPlay==true){
			aplay='1';
		}else{
			aplay='0';
		}
		// Create the code
		vidcode='<div id="'+newdivid+'" style="width:'+width+'px;height:'+height+'px;"><embed type="application/x-shockwave-flash" src="http://mediaservices.myspace.com/services/media/embed.aspx/m='+vidid+',t=1,mt=video,ap='+aplay+'" style="" id="'+divid+'" name="'+divid+'" bgcolor="#000000" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" flashvars="e=http%3A//vids.myspace.com/&primaryColor=0x'+bg+'&secondaryColor=0x'+bg+'&sr=0" height="'+height+'" width="'+width+'"></div>';
		document.write(vidcode);
	}
}

// Change video in existing player
function changeVid(divid, newvidid, autoPlay){
	// Check that the target exists
	var newdivid = "myspacevidcontainer_"+divid;
	if(!document.getElementById(newdivid)){
		alert("ERROR ENCOUNTERED BY changeVid():\nThe target video does not exist");
	}else{
		// Set the current video id
		videoVars[divid].vidid = newvidid;
		
		if(autoPlay==true){
			aplay='1';
		}else{
			aplay='0';
		}
		// Create the code
		document.getElementById(newdivid).innerHTML = '<embed type="application/x-shockwave-flash" src="http://mediaservices.myspace.com/services/media/embed.aspx/m='+newvidid+',t=1,mt=video,ap='+aplay+'" style="" id="'+divid+'" name="'+divid+'" bgcolor="#000000" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" flashvars="e=http%3A//vids.myspace.com/&primaryColor=0x'+videoVars[divid].bg+'&secondaryColor=0x'+videoVars[divid].bg+'&sr=0" height="'+videoVars[divid].height+'" width="'+videoVars[divid].width+'">';
	}
}



// Add the current video to profile
function addVid(target) {
	// Check that the target exists
	var newdivid = "myspacevidcontainer_"+target;
	if(!document.getElementById(newdivid)){
		alert("ERROR ENCOUNTERED BY addVid():\nThe target video does not exist");
	}else{
		window.location.href = "http://vids.myspace.com/index.cfm?fuseaction=vids.addToProfileConfirm&videoid="+videoVars[target].vidid;
	}
}


// Function for easy creation of a slideshow
function slideShow(theObject) {
	// ==== Set variables
	// Friend ID
	if(theObject.friendID){
		var friendID = theObject.friendID.toString();
	}else{
		alert("ERROR ENCOUNTERED BY slideShow():\nNo friend ID has been provided");
	}
	
	// Album ID
	if(theObject.albumID){
		var albumID = "&albumID="+theObject.albumID.toString();
	}else{
		var albumID = "";
	}
	
	// Width and height
	if(theObject.width && theObject.height){
		var width = theObject.width.toString();
		var height = theObject.height.toString();
	}else{
		alert("ERROR ENCOUNTERED BY slideShow():\nPlease specify a width and height");
	}
	
	// Background color
	if(theObject.bgColor){
		var bgColor = theObject.bgColor;
	}else{
		var bgColor = "FFFFFF";
	}
	
	// Speed
	if(theObject.speed){
		var speed = theObject.speed.toString();
	}else{
		var speed = "4";
	}
	
	// Style
	if(theObject.style){
		if(theObject.style == "slider"){
			var style = "a";
		}else if(theObject.style == "random"){
			var style = "b";
		}else{
			alert("ERROR ENCOUNTERED BY slideShow():\nThe specified style does not exist");
		}
	}else{
		var style = "a";
	}
	
	// Captions
	if(theObject.captions){
		if(theObject.captions == true){
			var captions = "1";
		}else if(theObject.captions == false){
			var captions = "0";
		}else{
			alert("ERROR ENCOUNTERED BY slideShow():\nThe specified captions do not exist");
		}
	}else{
		var captions = "1";
	}
	
	// Slide show code
	var slideShowCode = '<embed src="http://lads.myspace.com/photoshow/slideshow.swf" type="application/x-shockwave-flash" quality="high" scale="noscale" wmode="transparent" flashvars="userID='+friendID+'&bgColor=0x'+bgColor+'&bgColor2=0x'+bgColor+'&transitionSpeed='+speed+'&transitionStyle='+style+'&showCaptions='+captions+''+albumID+'" name="slider" align="middle" height="'+height+'" width="'+width+'">';
	
	// Write the code onto the page
	document.write(slideShowCode);
}



// Function for PNG fix in browsers < IE7

function doPngFix(){
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	if ((version >= 5.5) && (document.body.filters)){
		for(var i=0; i<document.images.length; i++){
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}




// ==== Contact functions ==== //

// Add to friends
function addToFriends(){
	window.location.href="http://friends.myspace.com/index.cfm?fuseaction=invite.addfriend_verify&friendID="+MySpaceClientContext.DisplayFriendId;
}

// Add to favourites
function addToFavourites(){
	window.location.href="http://collect.myspace.com/index.cfm?fuseaction=user.addToFavorite&friendID="+MySpaceClientContext.DisplayFriendId+"&public=0";
}

// Forward to friend
function forwardToFriend(){
	window.location.href="http://messaging.myspace.com/index.cfm?fuseaction=mail.forward&friendID="+MySpaceClientContext.DisplayFriendId+"&f=forwardprofile";
}

// Send a message
function sendAMessage(){
	window.location.href="http://messaging.myspace.com/index.cfm?fuseaction=mail.message&friendID="+MySpaceClientContext.DisplayFriendId;
}

// Add to group
function addToGroup(){
	window.location.href="http://groups.myspace.com/index.cfm?fuseaction=groups.addtogroup&friendID="+MySpaceClientContext.DisplayFriendId;
}

// Block user
function blockUser(){
	window.location.href="http://friends.myspace.com/index.cfm?fuseaction=block.blockUser&userID="+MySpaceClientContext.DisplayFriendId;
}

// Rank user
function rankUser(){
	window.location.href="http://collect.myspace.com/index.cfm?fuseaction=RateImage.UserRating&UserID="+MySpaceClientContext.DisplayFriendId;
}




// ==== Other generic links ====//

// Subscribe to blog
function subscribeToBlog(){
	window.location.href="http://blog.myspace.com/index.cfm?fuseaction=blog.ConfirmSubscribe&friendID="+MySpaceClientContext.DisplayFriendId;
}

// View all blog entries
function viewAllBlogEntries(){
	window.location.href="http://blog.myspace.com/index.cfm?fuseaction=blog.ListAll&friendID="+MySpaceClientContext.DisplayFriendId;
}

// View all videos
function viewAllVideos(){
	window.location.href="http://vids.myspace.com/index.cfm?fuseaction=vids.showvids&friendID="+MySpaceClientContext.DisplayFriendId;
}

// View all pictures
function viewAllPics(){
	window.location.href="http://viewmorepics.myspace.com/index.cfm?fuseaction=user.viewAlbums&friendID="+MySpaceClientContext.DisplayFriendId;
}