function init()
{	
	var pz = getPageSize();
	$("#overlay").css("height", (pz[1]-20)+"px"); //.css("width", (width)+"px");
	$("#addNout").css("top", ((height/2)-160)+"px").css("left", ((width/2)-225)+"px");
	$("#editNout").css("top", ((height/2)-160)+"px").css("left", ((width/2)-225)+"px");
	$("#bellNout").css("top", ((height/2)-85)+"px").css("left", ((width/2)-250)+"px");
	$("#editGroups").css("top", ((height/2)-150)+"px").css("left", ((width/2)-125)+"px");
	$("#accountSettings").css("top", ((height/2)-150)+"px").css("left", ((width/2)-125)+"px");
	$("#fbNout").css("top", ((height/2)-85)+"px").css("left", ((width/2)-250)+"px");
	$("#fbNoutThanks").css("top", ((height/2)-85)+"px").css("left", ((width/2)-250)+"px");
	$("#userFromTemp").css("top", ((height/2)-250)+"px").css("left", ((width/2)-125)+"px");

		
	$("#pwdRemind").css("top", ((height/2)-150)+"px").css("left", ((width/2)-125)+"px");
	$("#loadingAnim").css("top", ((height/2)-10)+"px").css("left", ((width/2)-10)+"px");

	$("#nouts").css("height", (height-(85))+"px"); //css("width", (width-25)+"px").
}

function dialog(id)
{
	$('#'+id).show();
	var pz = getPageSize();
	$("#overlay").css("height", (pz[1])+"px"); 
	$('#overlay').show();
}

function fade()
{
	var pz = getPageSize();
	$("#overlay").css("height", (pz[1])+"px"); 
	$("#overlay").fadeIn("normal");
}

function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	width = windowWidth; //(yScroll > 0 ? windowWidth - 20 : windowWidth);	// scrollbar
	height = windowHeight;
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function selBold()
{
	if ($("#nout_data_bold").val() == 1)
	{
		$("#selectBold").css("background", "url("+baseurl+"img/bold.png)");
		$("#nout_data_bold").val(0);
	}
	else
	{
		$("#selectBold").css("background", "url("+baseurl+"img/bold_sel.png)");
		$("#nout_data_bold").val(1);
	}
}
function selBig()
{
	if ($("#nout_data_big").val() == 1)
	{
		$("#selectBig").css("background", "url("+baseurl+"img/big.png)");
		$("#nout_data_big").val(0);
	}
	else
	{
		$("#selectBig").css("background", "url("+baseurl+"img/big_sel.png)");
		$("#nout_data_big").val(1);
	}
}
function eselBold()
{
	if ($("#enout_data_bold").val() == 1)
	{
		$("#eselectBold").css("background", "url("+baseurl+"img/bold.png)");
		$("#enout_data_bold").val(0);
	}
	else
	{
		$("#eselectBold").css("background", "url("+baseurl+"img/bold_sel.png)");
		$("#enout_data_bold").val(1);
	}
}
function eselBig()
{
	if ($("#enout_data_big").val() == 1)
	{
		$("#eselectBig").css("background", "url("+baseurl+"img/big.png)");
		$("#enout_data_big").val(0);
	}
	else
	{
		$("#eselectBig").css("background", "url("+baseurl+"img/big_sel.png)");
		$("#enout_data_big").val(1);
	}
}

function bellNout(id)
{
	fade();
	// load content
	$.get(baseurl+"index.php", {act: "muuvo_pages_nouts.fetch", id: id}, function (data) { finBellNout(id, data); return false; } , "json");
}

function finBellNout(id, data)
{
	if (data.fail == 1)
	{
		$("#overlay").hide();
		alert("Can't edit that note!");
		return;
	}

	$("#bnout_data_id").val(id);

	var Now=new Date();
	var TZO=Now.getTimezoneOffset();
	$("#bnout_data_tzo").val(TZO);
	

	if (data.remind == 1)
	{
		$("#bnout_data_remind").attr("checked", 1);
	}
	else
	{
		$("#bnout_data_remind").removeAttr("checked");
	}
	if (data.remind_when)
	{
		$("#bnout_data_when").val(data.remind_when);
	}
	else
	{
		$("#bnout_data_when").val(date("d/m/Y", new Date()));
	}
	if (data.remind_when_time)
	{
		$("#bnout_data_when_time").val(data.remind_when_time);
	}

	$("#bellNout").show();	
}

function selEditGrp(id, col, num)
{
	$('.noutEditSelGrp').css('border', 'none');
	$('#noutEditSelGrp'+id).css('border', '1px solid black');
	$('#enout_data_group').val(id);
	$("#editNout").css("background-color", "#"+col);
	$("#enout_data_content").css("background-color", "#"+col);
	$("#editHelpImg").attr("src", baseurl+"img/help_link_"+num+".png");
}

function selAddGrp(id, col, num)
{
	$('.noutAddSelGrp').css('border', 'none');
	$('#noutAddSelGrp'+id).css('border', '1px solid black');
	$('#nout_data_group').val(id);
	$("#addNout").css("background-color", "#"+col);
	$("#nout_data_content").css("background-color", "#"+col);
	$("#addHelpImg").attr("src", baseurl+"img/help_link_"+num+".png");
}

var curOffset = null;
function loadNouts(offset)
{
	fade();
	$("#loadingAnim").show();
	var noteWidth = 235 + 30;
	var cols = Math.floor((width-40) / noteWidth);
	$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-70)+"&offset="+offset, null, finish_load);
	curOffset = offset;
	init();
}

function finish_load()
{
	$("#overlay").hide("normal");
	$("#loadingAnim").hide();
}

function loadPublicNouts(offset, id)
{
	fade();
	$("#loadingAnim").show();

	var noteWidth = 235 + 30;
	var cols = Math.floor((width-40) / noteWidth);
	$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-70)+"&offset="+offset+"&pub_uid="+id, null, finish_load);

	curOffset = offset;
	init();
}

function post_edit_note()
{
	$('#editNout').hide();
	$('#bellNout').hide();
	$('#overlay').fadeOut("normal");
	loadNouts(curOffset);
	return false;
}

function nextPage(cur_offset, new_offset)
{
	pageCounts.push(cur_offset);
	loadNouts(new_offset);
	curOffset = new_offset;
}

function prevPage()
{
	var offset = pageCounts.pop();
	loadNouts(offset);
	curOffset = offset;
}

function nextPublicPage(cur_offset, new_offset, id)
{
	pageCounts.push(cur_offset);
	loadPublicNouts(new_offset, id);
}

function prevPublicPage(id)
{
	var offset = pageCounts.pop();
	loadNouts(offset, id);
}

function filterNouts(grp,num)
{
	fade();
	$("#loadingAnim").show();

	var noteWidth = 235 + 30;
	var cols = Math.floor((width-40) / noteWidth);
	$("div[id^=groupSel]").hide();
	if (selGrp != grp)
	{
		$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-40)+"&grp="+grp, null, finish_load);
		if (grp < 1)
		{
			// select all 
			$("#groupSel0").show();
		}
		else
		{
			$("#groupSel"+num).show();
		}
		selGrp = grp;
	}
	else
	{
		// clear filter
		$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-40), null, finish_load);
		selGrp = 0;

		$("#groupSel0").show();
	}
	init();
}

function filterPublicNouts(grp,num, id)
{
	fade();
	$("#loadingAnim").show();

	var noteWidth = 235 + 30;
	var cols = Math.floor((width-40) / noteWidth);
	$("div[id^=groupSel]").hide();
	if (selGrp != grp)
	{
		$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-60)+"&grp="+grp+"&pub_uid="+id, null, finish_load);
		if (grp < 1)
		{
			// select all 
			$("#groupSel0").show();
		}
		else
		{
			$("#groupSel"+num).show();
		}
		selGrp = grp;
	}
	else
	{
		// clear filter
		$("#nouts").load(baseurl+"?act=muuvo_pages_nouts.layout&cols="+cols+"&width="+(width-40)+"&height="+(height-60)+"&pub_uid="+id, null, finish_load);
		selGrp = 0;

		$("#groupSel0").show();
	}
	init();
}

function deleteNout(id)
{
	if (confirm("Are you sure you want to delete this note?"))
	{
		$.post(baseurl, {act: "muuvo_pages_nouts.delete_nout", id: id});
		loadNouts(curOffset);
	}
}

function toggleNout(id)
{
	if ($("#noutContent"+id).css("display") == "none")
	{
		$("#noutContent"+id).show();
		$("#updown"+id).attr("src", baseurl+"img/up.png");
	}
	else
	{
		$("#noutContent"+id).hide();
		$("#updown"+id).attr("src", baseurl+"img/down.png");
	}
}

function editNout(id)
{
	fade();
	// load content
	$.get(baseurl+"index.php", {act: "muuvo_pages_nouts.fetch", id: id}, function (data) { finEditNout(id, data); return false; } , "json");
}

function finEditNout(id, data)
{
	if (data.fail == 1)
	{
		$("#overlay").hide();
		alert("Can't edit that note!");
		return;
	}
	$("#enout_data_content").val(data.ct);
	$("#enout_data_group").val(data.group);
	if (data.group > 0)
	{
		$('.noutEditSelGrp').css('border', 'none');
		$('#noutEditSelGrp'+data.group).css('border', '1px solid black');
		$('#enout_data_group').val(data.group);
	}

	if (data.bold > 0)
	{
		eselBold();
	}
	else
	{
		$("#enout_data_bold").val(1);
		eselBold();
	}

	if (data.big > 0)
	{
		eselBig();
	}
	else
	{
		$("#enout_data_big").val(1);
		eselBig();
	}

	if (data.is_public)
	{
		$("#enout_data_is_public").attr("checked", "checked");
	}
	else
	{
		$("#enout_data_is_public").removeAttr("checked");
	}

//	$("#overlay").fadeIn("normal");	
	selEditGrp(data.group, data.col, data.col_idx);
	$("#enout_data_id").val(id);
	$("#editNout").show();	
}

var helpLoaded = false;
var helpDisplayed = null;
function initHelp()
{
	if (!helpLoaded)
	{
	       	$("#helpMain").css("height", height+"px").css("width", width+"px");
		helpLoaded = true;
	}
	$("#helpMain").show();
}

function showBellHelp()
{
	initHelp();
	$("#bellHelp").show();
	$("#bellHelp").css("top", ((height/2)-84)+"px").css("left", (((width/2)-175)+336)+"px");
	helpDisplayed = "bellHelp";
}

function showAddHelp()
{
	initHelp();
	$("#addHelp").show();
	$("#addHelp").css("top", ((height/2)-159)+"px").css("left", (((width/2)-175)+461)+"px");
	helpDisplayed = "addHelp";
}

function showEditGrpHelp()
{
	initHelp();
	$("#grpHelp").show();
	$("#grpHelp").css("top", ((height/2)-149)+"px").css("left", (((width/2)-175)+361)+"px");
	helpDisplayed = "grpHelp";
}

function showAcctHelp()
{
	initHelp();
	$("#acctHelp").show();
	$("#acctHelp").css("top", ((height/2)-149)+"px").css("left", (((width/2)-175)+411)+"px");
	helpDisplayed = "acctHelp";
}

function showEditHelp()
{
	initHelp();
	$("#editHelp").show();
	$("#editHelp").css("top", ((height/2)-159)+"px").css("left", (((width/2)-175)+461)+"px");
	helpDisplayed = "editHelp";
}

function showMainHelp()
{
	initHelp();
	$("#mainHelp").show();
	$("#mainHelp").css("top", ((height)-350)+"px").css("left", (((width))-250)+"px");
	helpDisplayed = "mainHelp";
}

function showFeedbackHelp()
{
	initHelp();
	$("#fbHelp").show();
	$("#fbHelp").css("top", ((height/2)-159)+"px").css("left", (((width/2)-175)+461)+"px");
	helpDisplayed = "fbHelp";
}


function doTempLogout()
{
	$("#tempLogout").show();
	$("#tempLogout").css("top", ((height/2)-159)+"px").css("left", (((width/2)-175))+"px");
}

function errs(data, form_name)
{
	$("#fbsend").removeAttr("disabled");
	muuvo_form_show_errors(data, form_name);
}
function post_fb()
{
	$('#fbNout').hide();
	$('#fbNoutThanks').show();
	return false;
}

function pwd_remind_sent()
{
	$("#fpSendButton").hide();
	$("#fpSentMessage").show();
}

function closeDialog(el, form)
{
	muuvo_form_close(form);
	$('#'+el).hide();
	$('#overlay').hide();
}
