//Montage creation scripts

var images = new Array(); //images array holds the list of images to be included in the montage
var track = null; //id of the track to be played
var user = null; //creator of the montage
var siteroot = null; //exactly that (used for preview link)

//addImage() - adds images to or removes images from the list of images for the montage.  also sets class names to give the user a display cue that the image has been added or removed.
//id - id of the image to be added or removed.
//enables generate code button once images are set if track is also set
function addImage(id)
{
	var removed = false; //Set the default of removed to false
	for(var i = 0; i < images.length ; i++) //cycle through the existing array to check whether or not the image is already there
	{
			if(images[i] == id) //If the image is already there.
			{
				images.splice(i, 1); //remove it from the array
				removed = true; //set the remove flag to true so the function later knows if the object was removed
				document.getElementById(id).className = ""; //remove the border class so the user knows the image was removed
				break; //break from the loop
			}
			removed = false; //If it makes it past the catch, the image was not removed
	}
	if(removed == false) //If no image was removed from the array, add it to the end.
	{
			images.push(id); //add the new image id to the end of the array.
			document.getElementById(id).className = "montageborder"; //add the border class so the user knows the image was added
	}
	if(track != null && track != "" && images.length > 0) //if they have selected a track and at least one image, let them generate the code
	{
		document.getElementById('codebox').className = "topmargin";
		document.codeform.generatebutton.disabled = false;
		document.getElementById('nocodemessage').className = "hide";
	}
	document.codeform.imgz.value = images.join(); //implode the array into a string.  join() deliniates with a comma by default.  Sets this value to the imgz argument passed to the save function
}


//setTrack - Sets the id of the track to be used in the montage
//Is called by the select box and then pulls the value of the select box and stores it in track
//enables generate code button once track is set if images are also set
function setTrack()
{
	var selector = document.tracksform.track_selector; //Get the selector box
	var track_id = selector.options[selector.selectedIndex].value; //Get the value from the selector box
	track = track_id; //Assign value to track
	if(track != null && track != "" && images.length > 0) //if they have selected a track and at least one image, let them generate the code
	{
		document.getElementById('codebox').className = "topmargin";
		document.codeform.generatebutton.disabled = false;
		document.getElementById('nocodemessage').className = "hide";
	}
	document.codeform.track_mid.value = track; //Sets this value to the track_mid argument passed to the save function
}


//generateCode() - Takes the input info from track and images and generates the web site link and embedded flash code for the montage
 function generateCode()
{
	var image_list = images.join(); //implode the array into a string.  join() deliniates with a comma by default
	var weblink = siteroot + "montage.php?trackid=" + track + "&images=" + image_list + "&user=" + user; //Build the web link string
	
	var embedcode = ""; //Initialize the object embed string
	//Build the object embed string with variables for track and image list
	embedcode += '<div style="width: 400px; border: 1px solid #20386a;"> \n';
	embedcode += '<embed src="' + siteroot + 'player/external_montage.swf" FLASHVARS="sid=' + track + '&img_array=' + image_list + '" quality="high" bgcolor="#ffffff" width="400" height="300" name="picture_montage" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> \n';
	embedcode += '<div style="background-color: #20386a; font-size: 12px; font-weight: bold; font-family:Arial, Helvetica, sans-serif; padding: 4px;"> \n';
	embedcode += '<div style="text-align: center; margin-bottom: 5px;"><a href="' +  siteroot + '" style="color: #FFFFFF;">Build your own montage at JukeboxAlive.com!</a></div> \n';
	embedcode += '<div style="float:right; font-size: 10px;"><a href="http://www.mynewsletterbuilder.com" style="color: #FFFFFF;">Email Marketing</a></div> \n';
	embedcode += '<div style="font-size: 10px;"><a href="' +  siteroot + '" style="color: #FFFFFF;">Download Music</a></div> \n';
	embedcode += '</div> \n';
	embedcode += '</div> \n';
	
	document.codeform.embedbox.disabled = false; //enable the text boxes
	document.codeform.weblinkbox.disabled = false;
	document.codeform.copyEmbedText.disabled = false; //enable the copy buttons
	document.codeform.copyLinkText.disabled = false;
	document.codeform.imgz.disabled = false;
	document.codeform.track_mid.disabled = false;
	
	document.codeform.embedbox.value = embedcode; //set the value of the text boxes to the embed code and web link respectively
	document.codeform.weblinkbox.value = weblink;
	
}

//preview() - Opens a new window with a link to the montage
function preview()
{
	var image_list = images.join();
	var url = siteroot + "montage.php?trackid=" + track + "&images=" + image_list + "&user=" + user;
	window.open(url);
}


//copy(text) - Copies the given text to the clipboard (Different methods for IE and FF)
function copy(text) 
{
	if (window.clipboardData) {
		window.clipboardData.setData("Text",text);
	} 
	else 
	{
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) 
		{
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
}


function toggleEmbed(embed_mute,embed) {
// alert(document.codeform.togEmbed.value);

if ( document.codeform.embedbox.value==embed_mute)
    document.codeform.embedbox.value=embed;
else
    document.codeform.embedbox.value=embed_mute;
} 

