window.addEvent ('domready', init);

function init ()
	{
	var a_a, a_p, a_f, a_gf, l_h;
	if (a_a = $ ('add_address'))
		{
		a_a.onclick = function (event)
			{
			clear_fieldset (this, 'alternateaddress_');
			};
		}
	if (a_p = $ ('add_phone'))
		{
		a_p.onclick = function (event)
			{
			clear_fieldset (this, 'phone_');
			};
		}
	if (a_f = $ ('add_file'))
		{
		a_f.onclick = function (event)
			{
			clear_fieldset (this, 'musicianmedia_');
			};
		}
	if (a_gf = $ ('add_g_file'))
		{
		a_gf.onclick = function (event)
			{
			clear_fieldset (this, 'groupmedia_');
			};
		}
	set_new_window ($ES ('a.artist_image'), null, null);
	set_new_window ($ES ('a.help'), null, null);
	set_new_window ($ES ('a.mdref'), 300, 400);
	add_music_player ($ES ('ul.music li'));
	}

function clear_fieldset (which, id_str)
	{
	var p = which.getParent();
	var f = p.getPrevious();
	var id = f.getProperty ('id').replace (id_str, '');
	var new_id = parseInt (id) + 1;
	var f_c = f.clone ().injectAfter (f);
	f_c.setProperty ('id', f_c.getProperty ('id').replace (id, new_id));
	$ES ('input', f_c).each (function (item, index)
		{
		item.setProperty ('id', item.getProperty ('id').replace (id, new_id));
		item.setProperty ('name', item.getProperty ('name').replace (id, new_id));
		if (item.getProperty ('type') != 'hidden')
			{
			item.setProperty ('value', '');
			}
		});
	$ES ('label', f_c).each (function (item, index)
		{
		item.setProperty ('for', item.getProperty ('for').replace (id, new_id));
		});
	$ES ('select', f_c).each (function (item, index)
		{
		item.setProperty ('id', item.getProperty ('id').replace (id, new_id));
		item.setProperty ('name', item.getProperty ('name').replace (id, new_id));
		item.selectedIndex = 0;
		});
	$ES ('option', f_c).each (function (item, index)
		{
		item.removeProperty ('selected');
		});
	}
function set_new_window (collection, width, height)
	{
	collection.each (function (item, index)
		{
		item.onclick = open_in_new_window.bindAsEventListener (item, [width, height]);
		return false;
		});
	}
function open_in_new_window (e, width, height)
	{
	var options = null;
	if (width && height)
		{
		options = 'height=' + height + ',width=' + width + ',resizable=1,scrollbars=1';
		}
	window.open (this.href, '_blank', options);
	return false;
	}

function add_music_player (collection)
	{
	collection.each (function (item, index)
		{
		var a = $ES ('a', item);
		var href = a[0].getProperty ('href');
		var text = a[0].getText ();
		FlashReplace.replace (item, '/graphics/referral/player.swf?t=' + text + '&amp;f=' + href, null, 225, 28);
		});
	if (collection.length)
		{
		collection[0].getParent ().removeClass ('music');
		}
	}


/*
	FlashReplace is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
*/
// ---
var FlashReplace = {
	elmToReplace : null,
	flashIsInstalled : null,
	defaultFlashVersion : 7,
	replace : function (elmToReplace, src, id, width, height, version, params){
		this.elmToReplace = elmToReplace; //document.getElementById(elmToReplace);
		this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
		if(this.elmToReplace && this.flashIsInstalled){
			var obj = '<object' + ((window.ActiveXObject)? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
			obj += ' width="' + width + '"';
			obj += ' height="' + height + '"';
			obj += '>';
			param = '<param';
			param += ' name="movie"';
			param += ' value="' + src + '"';
			param += '>';
			param += '';
			var extraParams = '';
			var extraAttributes = '';
			for(var i in params){
				extraParams += '<param name="' + i + '" value="' + params[i] + '">';
				extraAttributes += ' ' + i + '="' + params[i] + '"';
			}
			var embed = '<embed id="' + id + '" src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
			var embedEnd = extraAttributes + '></embed>';
			var objEnd = '</object>';
			this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;			
		}
	},
	
	checkForFlash : function (version){
		this.flashIsInstalled = false;
		if(window.ActiveXObject){
			try{
				var flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
				this.flashIsInstalled = true;
			}
			catch(e){
				// Throws an error if the version isn't available			
			}
		}
		else if(navigator.plugins && navigator.mimeTypes.length > 0){
			var flash = navigator.plugins["Shockwave Flash"];
			if(flash){
				//var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*(\d+\.\d+).*/, "$1");
				var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+).*/, "$1");
				if(flashVersion >= version){
					this.flashIsInstalled = true;
				}
			}
		}
		return this.flashIsInstalled;
	}
};
// ---
