
// Setup the onbeforeunload event ASAP

window.onbeforeunload = confirmExit;

// --------------------------------------------------
//
// --------------------------------------------------
$(document).ready(
	function ()
	{

//        initNavHover();  // This is not being done, since we don't know the names of prev and next sites

        initFormHelp();
        $(window).resize(resizeSiteElement);
        resizeSiteElement();

        $("#alternateTag").hide();
        $("#more-comments").hide();
        $("#reporttagId").change(toggleOtherTag);
        $("#viewAllId").click(function() { $("#more-comments").show(); });
        $("#siteAccessibleId").click(function() {
                                        clearBeforeUnload();
                                        clearDefaultText();
                                        promptTestMoreSites();
                                        return true;
                                    });
        $("#siteInaccessibleId").click(function() {
                                        clearBeforeUnload();
                                        clearDefaultText();
                                        promptTestMoreSites();
                                        return true;
                                    });
        $("#testSiteSubmitId").click(function() {
                                        clearBeforeUnload();
                                        clearDefaultText();
                                        return ($("#testSiteId").val() != "");
                                    });

        if ($("#reportCommentsId").val() == "")
        {
            $("#reportCommentsId").val(defaultReporterCommentsText);
            $("#reportCommentsId").focus(function() { $("#reportCommentsId").val(''); });
        }
        $("input").keypress(function (event){ return event.keyCode != 13; });
        $("#testSiteId").keypress(function (event){
                                        if (event.keyCode == 13) {
                                            $("#testSiteSubmitId").click();
                                        }
                                        return true;
                                    });
        $("#header ul.actions li a").click(function() { clearBeforeUnload(); return true; } );

        // If the site returns an error the onload function on the iframe will never be called
        // This allows the user to close the window after X seconds if that happens without being
        // prompted. Also useful if the target site takes a very long time to load. The value here needs to be
        // long enough so that, if the target site DOES try to redirect away, it happens before this timer runs
        var t = setTimeout(clearBeforeUnload,5000);
    }
);


// --------------------------------------------------
//
// --------------------------------------------------
function initNavHover ()
{
	$("body").append("<div id=\"nav-hover\"></div>");

	$("#header ul.actions li a").hover(

		function ()
		{
			$("#nav-hover").html($(this).attr("sitelabel"));

			$("#nav-hover").show();
		},

		function ()
		{
			$("#nav-hover").hide();
		}

	);

	$("#header ul.actions li a").mousemove(handleNavHover);
};

function handleNavHover (e)
{
	$("#nav-hover").css({
		top: e.pageY + 2,
		left: e.pageX + 2
	});
};

// --------------------------------------------------
//
// --------------------------------------------------
function initFormHelp ()
{
	$("body").append("<div id=\"help-hover\"></div>");

	$("#checker a.help").hover(

		function ()
		{
			$("#help-hover").html($(this).attr("helptext"));

			$("#help-hover").show();
		},

		function ()
		{
			$("#help-hover").hide();
		}

	);

	$("#checker a.help").mousemove(handleHelpHover);
};

function handleHelpHover (e)
{
	$("#help-hover").css({
		top: e.pageY + 2,
		left: e.pageX + 2
	});
};

// --------------------------------------------------
//
// --------------------------------------------------
var g_windowSize = {
	width: 0,
	height: 0
};

function resizeSiteElement ()
{
	var windowSize = {
		width: $(window).width(),
		height: $(window).height()
	};
	
	if (windowSize.width != g_windowSize.width || windowSize.height != g_windowSize.height)
	{
		var sidebarWidth = $("div#checker").width() +
			parseInt($("div#checker").css("padding-left")) +
			parseInt($("div#checker").css("padding-right")) +
			parseInt($("div#checker").css("margin-left")) +
			parseInt($("div#checker").css("margin-right")) +
			parseInt($("div#checker").css("border-right-width"));
	
		var headerHeight = $("#header").height() + 
			parseInt($("#header").css("padding-top")) + 
			parseInt($("#header").css("padding-bottom")) + 
			parseInt($("#header").css("margin-top")) + 
			parseInt($("#header").css("margin-bottom")) + 
			parseInt($("#header").css("border-bottom-width"));
		
		headerHeight += $("#site-header").height() + 
			parseInt($("#site-header").css("padding-top")) + 
			parseInt($("#site-header").css("padding-bottom")) + 
			parseInt($("#site-header").css("margin-top")) + 
			parseInt($("#site-header").css("margin-bottom")) + 
			parseInt($("#site-header").css("border-bottom-width"));
		
	
		$("iframe#site").css({
			width: $(window).width() - sidebarWidth,
			height: $(window).height() - headerHeight
		});

		g_windowSize = windowSize;
	};	
};

function toggleOtherTag()
{
    if ($(this).val() == "_other")
    {
        $("#alternateTag").show();
        $("#alternateTag").focus();
    }
    else
        $("#alternateTag").hide();
}

function clearDefaultText()
{
    if ($("#reportCommentsId").val() == defaultReporterCommentsText)
    {
        $("#reportCommentsId").val('');
    }
    if ($("#testSiteId").val() == defaultTestSiteText)
    {
        $("#testSiteId").val('');
    }
}


function promptTestMoreSites()
{
    if (($("#returnPageId").val() != undefined) && ($("#returnPageId").val() != ''))
    {
        if (!confirm(promptTestMore))
        {
            $("#closeFieldId").val('true');
        }
    }
}

function confirmExit()
{
  return promptConfirmExit;
}

function clearBeforeUnload()
{
    window.onbeforeunload = null;
    return true;
}