aigAjaxLoadedFunctions.push(function(container) {
	checkPlayer(true);
	
	if (container = undefined) container = $(document);
	$("a.playlistAdd", container).click(function() {
		toggleAddRemove(this);
		return false;
	});
	$("a.playButton", container).click(function() {
		loadFile(getMediaItemFields(this));
		sendEvent('playpause');
		var button = $("a.remove").toggleClass("remove");
		$("img", button).attr("src","/assets/images/media/btn_add.gif");
		return false;
	});
	/*$("#next", container).mouseup(function() {
		window.location = "#listTop";
	});*/
});

aigAjaxRequestFunctions.push(function(hash,curLink) {	//request from history plugin
	$("#leftNav").find("li.active").removeClass("active");
	$(curLink).parent().addClass("active");
	
	// select correct menu items
	var hashParts = hash.substring(1).split("/");
	var menuLink = $(".nav a.ajaxLink[href*='"+ hashParts[0] +"/"+ hashParts[1] +"']");
	if ($(menuLink).length > 0)
		openMenuItem(menuLink, $(menuLink).attr("href").replace("#","").replace("/media/", "/web/ajax/media-menu/"), hash);
	
	$("ul.mediaFilter li.active").removeClass("active");
	if (hash.split("/").length == 2) $("ul.mediaFilter a[href*='#"+ hash +"']").parent().addClass("active");
	else $("#allFilter").addClass("active");
	if ($(curLink).length > 0 && !$(curLink).parent().hasClass("pageNav") && hash.indexOf("/s/")==-1)
		changeSearch(curLink);
});

function openMenuItem(curLink, path, hash) {
	$(curLink).parent().addClass("active");
	var submenu = $(".mediaSubMenu", $(curLink).parent());
	$(".mediaSubMenu:visible").not(submenu).slideUp("fast");
	if ( submenu.length > 0 ) {
		$("li.active", submenu).removeClass("active");
		if (hash.split("/").length > 3) $("a[href*='"+hash+"']", submenu).parent().addClass("active");
		$(submenu).not(".mediaSubMenu:visible").slideDown("normal");
	} else {
		$(curLink).after("<div id='mediaLoading' style='padding:3px 8px; border-bottom:1px solid #E8F4FB;'>loading ...</div><div class='mediaSubMenu' style='display:none;'></div>");
		$(".mediaSubMenu", $(curLink).parent()).load(path, function() {
			ajaxActions(this);
			$("a", this).click(function() {
				$("input#mediaSearch").attr("value", "Search " + $(this).text());
			});
			$("#mediaLoading").remove();
			
			if (hash.split("/").length > 3) $("a[href*='/media#"+hash+"']", this).parent().addClass("active");
			$(this).slideDown("normal");
		});
	}
}

function checkPlayer(firstTime) {
	var item = $("a.playlistAdd:eq(0)");
	if (thisMovie("mpl") == undefined || item.length == 0) {
		setTimeout("checkPlayer(false)", 2000 );
	} else if (!firstTime && thisMovie("mpl").itemData(0) == undefined) {
		var item = toggleAddRemove(item);
		loadMediaRR(item.sku);
	}
}


// These functions are caught by the feeder object of the player.
// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie("mpl").sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
};

// These functions are caught by the feeder object of the player.
function loadFile(obj) {
	loadMediaRR(obj.sku);
	thisMovie("mpl").loadFile(obj);
}
function getItemData(idx) {
	var obj = thisMovie("mpl").itemData(idx);
	var nodes = "";
	for(var i in obj) { 
		nodes += "<li>"+i+": "+obj[i]+"</li>"; 
	}
};

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

$(document).ready(function() {
	var item = getMediaItemFields($("a.playlistAdd:eq(0)"));
	loadMediaRR(item.sku);
	var id = "mpl";
	
	var id = "player";
	var flashvars = {
		usefullscreen: "false",
		file: item.file,
		title: item.title,
		author: item.author,
		image: item.image,
		enablejs: "true",
		javascriptid: id,
		displaywidth: "400",
		autoscroll: "true",
		frontcolor: "0x0083D7",
		backcolor: "0xCFE7F8",
		removefromplaylist: "true"
	};
	var params = {
		bgcolor: "#CFE7F8",
		wmode: "transparent"
	};
	var attributes = {
		id: "mpl"
	};
	
	swfobject.embedSWF("/assets/scripts/mediaplayer3.15.aig.swf", "player", 720, 320, "9.0.0","/assets/scripts/expressInstall.swf", flashvars, params, attributes);
	
	/*$("input#mediaSearch").each(function() {
		$(this).change(function() {
			searchMedia();
		})
	}).keypress(function() {
		clearTimeout(timer);
		timer = setTimeout('searchMedia()', 500);
	});*/
	$("form#mediaForm").submit(function() {
		searchMedia();
		return false;
	});
	
	$("ul.mediaFilter a").click(function() {
		$("ul.mediaFilter li.active").removeClass("active");
		$(this).parent().addClass("active");
	});
});

function changeSearch(element) {
	$("#mediaForm").attr("action", $(element).attr("href").replace("/stage", ""));
	$("input#mediaSearch").attr("value", "Search " + $(element).text());
}

function searchMedia() {
	$("input#mediaSearch").unbind("blur").each(function() {
		var path = $("#mediaForm").attr("action");
		path = path.replace("/stage","").replace("/media","");
		if (this.value != '') path += "/s/" + escape(this.value);
		
		$.history.load( path.replace(/^.*#/, '').replace("//","/") );
	});
}

function toggleAddRemove(button) {
	fields = getMediaItemFields(button);
	if ($(button).hasClass("remove")) {
		removeItem(fields.file);
		$(button).unbind().click(function() {
			toggleAddRemove(button);
			return false;
		});
		$("img", button).attr("src","/assets/images/media/btn_add.gif");
	} else {
		thisMovie("mpl").addItem(fields);
		$(button).unbind().click(function() {
			toggleAddRemove(button);
			return false;
		});
		$("img", button).attr("src","/assets/images/media/btn_remove.gif");
	}
	$(button).toggleClass("remove");
	
	return fields;
}

function getMediaItemFields(button) {
	var row = $(button).parent().parent();
	var file = $("td span.filePath", row).text();
	var author = $("td a.itemTitle", row).text();
	var title = $("td span.itemType", row).text();
	var sku = $("td span.sku", row).text();
	var image = "/assets/" + $("td span.imagePath", row).text();
	
	return { file: file, title: title, author: author, image: image, sku: sku };
}

function removeItem(removeItemFile) {
	for(i = 0; i < thisMovie('mpl').getLength('mpl'); i++) {
		obj = thisMovie('mpl').itemData(i);
		if(removeItemFile == obj['file']) {
			thisMovie('mpl').removeItem(i);
			break;
		}
	}
};

function getUpdate(type,pr1,pr2,swf) { 
	if(type == 'item') {
		var item = thisMovie(swf).itemData(pr1);
		loadMediaRR(item.sku);
	}
};

function getItemById(id) {
	var itemsLeft = true;
	var i = 0;
	while (itemsLeft) {
		var item = getItemData(i);
		if (item.id == id) {
			return i;
		}
		i++;
		if (!item) itemsLeft = false;
	}
}