var currentEntryId = null;

function openFancyBox(url, width, height)
{
	var link = $("<a href=\""+url+"\"></a>");
	
	link.fancybox({
		frameWidth: width,
		frameHeight: height,
		padding: 0,
		callbackOnShow: function () {
			jQuery("#fancy_overlay, #fancy_wrap").unbind("click", null);
		}
	});
	
	link.click();
}

function upload() {
	openFancyBox("upload.php?iframe", 680, 360);
}

function animateToRemix(entryId) {
	jQuery("#fancy_frame")[0].src = "about:blank";
	
	var width = Number(jQuery("#fancy_outer").css("width").replace("px", ""));
	var height = Number(jQuery("#fancy_outer").css("height").replace("px", ""));
	var newWidth = 890;
	var newHeight = 546;
	var leftOffset = (width - newWidth) / 2;
	var topOffset = (height - newHeight) / 2;
	
	jQuery("#fancy_outer").animate({
		width: newWidth+"px",
		left: "+="+leftOffset+"px",
		height: newHeight+"px",
		top: "+="+topOffset+"px"
	},{
		complete: function() {
			jQuery("#fancy_frame")[0].src = "remix.php?id="+entryId+"&iframe";
		}
	})
}

function remix() {
	openFancyBox("remix.php?id="+currentEntryId+"&iframe", 890, 546);
}

function remixThis() {
	openFancyBox("remixthis.php?id="+currentEntryId+"&iframe", 890, 546);
}

function email() {
	openFancyBox("email.php?id="+currentEntryId+"&iframe", 600, 400);
}

function deleteShow() {
	openFancyBox("delete.php?id="+currentEntryId+"&iframe", 600, 400);
}

function play(id, autoPlay) {
	currentEntryId = id;
	playerController.insertEntry(id, autoPlay);
}

function onContributionWizardAfterAddEntry(obj) {
	window.parent.animateToRemix(cwEntryId);
}

function onSimpleEditorBackClick() {
	window.top.seEntryId = seEntryId;
	setTimeout("window.parent.onSimpleEditorBackClickTimeout()", 0);
}

function onSimpleEditorBackClickTimeout() {
	$.fn.fancybox.close();
	
	// if the entry doesn't exists in the list, it is a new entry and we need to refresh the page.
	currentEntryItem = jQuery("#gallery li").filter(
			function() { return (jQuery(this).attr("class") == "entryId-"+seEntryId); }
	);
	
	if (currentEntryItem.size() == 0)
		window.parent.location.href = "index.php?"+Math.random();
	else
		play(seEntryId, true);
}

function onSimpleEditorSaveClick() {
	onSimpleEditorBackClick();
}

function onSimpleEditorRemixThisBackClick() {
	setTimeout("window.parent.onSimpleEditorRemixThisBackClickTimeout()", 0);
}

function onSimpleEditorRemixThisBackClickTimeout() {
	$.fn.fancybox.close();
	window.parent.location.href = "index.php";
}

function onSimpleEditorRemixThisSaveClick() {
	onSimpleEditorRemixThisBackClick();
}
