function Mediaitems() { }


Mediaitems.Containers = null;
Mediaitems.embedContainer = null;
Mediaitems.Settings = null;

Mediaitems.init = function(settings) {

	Mediaitems.Settings = settings;
	Mediaitems.embedContainer = $('<div id="media-container"></div>');
	Mediaitems.embedContainer.hide();
	$("body").append(Mediaitems.embedContainer);
	
	Mediaitems.Containers = $("."+ App.mediaItemContainerClass);

	$.each(Mediaitems.Containers, function() {

		Mediaitems.prepareItems($(this));

	});

}


// Set each item to be clickable and to display item on click
Mediaitems.prepareItems = function(item) {

	var parent = item.parent();
	var items = $('li', parent);

	$.each(items, function() {

		var item = $(this);

		var url = $('input.'+App.Field_embedUrl, item).val();

		this.onclick = function() {
			items.removeClass('selected');
			item.addClass('selected');
			Mediaitems.embedItem(url);
		}

		// Remove hyperlink (Rendered as a link for users without JavaScript
		var ahref = $("a", item);
		var linkText = ahref.text();
		
		ahref.parent().text(linkText);
		ahref.remove();
		
	});

}

Mediaitems.embedItem = function(url) {
	
	var media = null;
	
	if (url.indexOf('<object') == 0) {
		media = $(url);
		$("span", media).remove();
	}
	else {
	
		media = EmbedMedia.Create(url, 400, 300, {
				'allowfullscreen': true,
				'allowscriptaccess': 'always',
				'movie': url
			}, 
			{
				'allowfullscreen': true,
				'allowscriptaccess': 'always',
				'type': 'application/x-shockwave-flash',
				'src': url
		});
	}
	
	if (!Mediaitems.Settings.popup) {

		$("object", Mediaitems.Settings.embedElements).remove();
		Mediaitems.Settings.embedElement.append(media);
		
	}
	else {
		
		Mediaitems.embedContainer.html('');
		Mediaitems.embedContainer.append(media);
		Mediaitems.embedContainer.dialog({
			'modal': true,
			'width': '500px'
		});

	}


}

function Popup() { }
Popup.Container = null;
Popup.Overlay = null;

Popup.create = function(id, title) {

	var div = $(document.createElement('div'));
	div.addClass(App.ClassNames.Popup);

	if (id) {
		div.attr('id', id);
	}

	if (title) {
		var h1 = $(document.createElement('h1'));
		h1.text(title);
		div.append(h1);
	}

	var closeButton = $(document.createElement('button'));
	closeButton.addClass('close');
	closeButton.text('Close');
	closeButton.bind('click', function() {
		Popup.close();
	});
	div.append(closeButton);
	div.hide();
	
	Popup.Container = div;

	return Popup;

}

Popup.close = function() {

	Popup.Overlay.fadeOut();

	Popup.Container.fadeOut(function() {
		Popup.Container.remove();
	});
	
}

Popup.addContents = function(contents) {
	Popup.Container.append(contents);
}

Popup.show = function() {

	var overlay = $(document.createElement('div'));
	overlay.addClass(App.ClassNames.Overlay);
	overlay.hide();
	Popup.Overlay = overlay;
	
	$("body").append(overlay);
	$("body").append(Popup.Container);

	Popup.Overlay.fadeIn();
	Popup.Container.fadeIn();

}

function EmbedMedia() { }

EmbedMedia.Create = function(url, width, height, objectParams, embedParams) {

	var object = $(document.createElement('object'));
	object.attr('width',  width);
	object.attr('height', height);

	$.each(objectParams, function(name, value) {

		var param = $(document.createElement('param'));
		param.attr('name', name);
		param.attr('value', value);

		object.append(param);

	});

	var embed = $(document.createElement('embed'));
	embed.attr('width', width);
	embed.attr('height', height);
	$.each(embedParams, function(name, value) {
		embed.attr(name, value);
	});
	
	object.append(embed);

	return object;

}

