document.addEvent('domready', function() {
	var uitzendingen = $$('div.uitzending');

	uitzendingen.each(function (uitzending) {
		if (!uitzending.getElement('a.bekijk_mix')) {
			return;
		}

		var firstMix = uitzending.getElement('a.bekijk_mix');
		var flashvars = default_flashvars;
		flashvars.autostart = false;

		// get the hash for this mix from the id of the link
		var id = firstMix.get('id');
		mix_hash = id.replace('bekijk_mix_', '');      
		flashvars.code = mix_hash;

  		var params = default_params;
  		var attributes = default_attributes;

		var player = uitzending.getElement('div.player');
		if (player) {
			// clean out the player div, create a new container div to be replaced by embedSWF
			player.empty();
			var flash_content_id = 'player_'+mix_hash;

			counter = 1;

			while (document.id(flash_content_id)) {
				flash_content_id = 'player_' + mix_hash + '_' + counter;
				counter++;
			}

			var flash_content = new Element('div', { id: flash_content_id });
			player.adopt(flash_content);
			swfobject.embedSWF("/swf/host.swf", flash_content_id, 245, 245, "10.0.0", "/js/swfobject/expressInstall.swf", flashvars, params, attributes);
		}
	});

	var viewMixLinks = $$('a.bekijk_mix');

	viewMixLinks.each(function(link) {
		link.addEvent('click', function(event) {
			var flashvars = default_flashvars;

			// get the hash for this mix from the id of the link
			var id = this.get('id');
			mix_hash = id.replace('bekijk_mix_', '');      
			flashvars.code = mix_hash;
			flashvars.autostart = true;

			var params = default_params;
			var attributes = default_attributes;

			var uitzending = this.getParents('div.uitzending');
			if (uitzending.length) {
				uitzending = uitzending[0];
				var player = uitzending.getElements('div.player');
				if (player.length) {
					player = player[0];
					// clean out the player div, create a new container div to be replaced by embedSWF
					player.empty();
					var flash_content_id = 'player_'+mix_hash;
					var flash_content = new Element('div', { id: flash_content_id });
					player.adopt(flash_content);
					swfobject.embedSWF("/swf/host.swf", flash_content_id, 245, 245, "10.0.0", "/js/swfobject/expressInstall.swf", flashvars, params, attributes);          
				}
			}
			return false;
		});
	});
});