/*  ver 1.1  9/8/2009  packed with: http://dean.edwards.name/packer/*/
/*  ver 1.2  12/4/2009 add analytics*/

if (typeof(svsn_scripts) == 'undefined') var svsn_scripts = new Object();

$(document).ready( function() {
/*

	To cause a link to have the Left Click menu added to it, the link must have the the class of "svsn"
	Add an attribute of "menuName" with a unique name for each menu. No spaces!!
	To have a Friendly file Name used on download, add an attribute of "friendlyName" and the value of the file name 
		you wish to save it as with  NO spaces!! (be sure to use the file type that is from Media)
	
	example:
		<a href="http://media1.mediasuite.org/files/80/8068/8068-44906.jpg" menuName="menu1" friendlyName="MyMedia.jpg" class="svsn">

*/
 
	$.each( $('a.svsn'), function(i, n){
  		var menuName = ( $(this).attr('menuName') );
  		var fileURL = $(this).attr('href');
		var mediaFile = new Array();
		var fileName = "";
		var mediaID = 0;
 		var friendlyName = $(this).attr('friendlyName');
		
 		mediaFile = fileURL.split('/');
		
		if (mediaFile.length > 2) {
			var mediaID = mediaFile[mediaFile.length - 2];
			}
		else {
			mediaID = 0;
		}
		if ($(this).attr('friendlyName') == undefined) {
			friendlyName =  mediaFile[mediaFile.length - 1];
		}
		fileName =  mediaFile[mediaFile.length - 1];
		
 		if ($(this).attr('menuName') != "") {
		createMenu(menuName,fileURL, friendlyName, fileName, mediaID, this);
		};
	});
	$("a.svsn").click(function () { 
		return false;
	});
	
	svsn_import('http://imbresources.org/js/omniture/s_code.js');

});

// Imports External Javascript file
function svsn_import(jsFile) {
	if (svsn_scripts[jsFile] != null) return;
	var scriptElt = document.createElement('script');
	scriptElt.type = 'text/javascript';
	scriptElt.src = jsFile;
	document.getElementsByTagName('head')[0].appendChild(scriptElt);
	svsn_scripts[jsFile] = jsFile; // or whatever value your prefer
}

function createMenu(menuName, fileURL, friendlyName, fileName, mediaID, thisObj) {

	//alert(menuName + ',fileName: ' + fileName + ', friendlyName' + friendlyName);
	$("#svsn").append(
		'<ul id="' +  menuName + '" class="contextMenu">' +
		'<li ID="edit"><a href="javascript:window.open(\'http://media1.mediasuite.org/dload.php?mediaID=' + 
				mediaID + '&file=' + 
				fileName + '&name=' + 
				friendlyName + '\')" onClick = \"var s=s_gi(\'intmbprod\');s.linkTrackVars=\'eVar20\';s.eVar20=fileName;s.tl(this, \'d\', \'download\');\">Download</a></li>' +
		'<li ID="clipboard" class="separator">' +
		'<a href="javascript:showURL(\'' + fileURL + '\');" onClick = \"var s=s_gi(\'intmbprod\');s.linkTrackVars=\'eVar21\';s.eVar21=fileName;s.tl(this, \'d\', \'copyurl\');\">Copy URL</a></li>' +
		'<li ID="copy"><a href="javascript:window.open(\'' + fileURL + '\',\'media\')" onClick = \"var s=s_gi(\'intmbprod\');s.linkTrackVars=\'eVar22\';s.eVar22=fileName;s.tl(this, \'d\', \'open\');\">Open</a></li>' +
		'</ul> ');

   $(thisObj).contextMenu({
	menu : menuName,
	preHideItems : true,
	ids : '#cut' // do NOT seperate them with space like this '#cut, #edit'
	},
	function(action, el, pos) {
		//alert(action);
		eval(action);
		/*alert(
		'Action: ' + action + '\n\n' +
		'Element ID: ' + $(el).attr('id') + '\n\n' +
		'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)\n\n' +
		'X: ' + pos.docX + ' Y: ' + pos.docY+ ' (relative to document)'
		); */
	});
};

function showURL(inputField) {

	if (!isIE()) {
		showURLLink(inputField);
	}
	else {
		copyToClip(inputField);		
		alert("Asset URL copied to clipboard.");
	}
}

function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function showURLLink(fileURL) {
	prompt("Your browser does not support this feature. Use below field:",fileURL);
}

function copyToClip(s)
{
	var txt = s;

	if( window.clipboardData && window.clipboardData.setData ) {
	   window.clipboardData.setData("Text", txt);
	}
}

