﻿window.addEvent('domready', function()
{
	init();
});

var dlgPrintState = null;
var mpEffect = null;
var mpDialogEffect = null;

var caption = { src: '/Assets/caption.swf' };
sIFR.activate(caption);
//sIFR.debug.ratios({ src: '/Assets/caption.swf', selector: '.caption' });

function getPrintDialogState()
{
	return dlgPrintState;
}

function navigate(navID, execRequest)
{
	var btnNav = $("btn" + navID);

	if (btnNav == null)
	{
		return;
	}

	imgBtn.setStatus("menu", btnNav, 1);
	imgBtn.activate("menu", btnNav);

	if (execRequest)
	{
		ui.handleEvent("Menu", btnNav, "click");
	}
}

function clearNavigation()
{
	var currentControl = imgBtn._activeItems["menu"];

	if (currentControl == null)
	{
		return;
	}

	imgBtn._activeItems["menu"] = null;
	imgBtn.setStatus("menu", $(currentControl), 0);
}

function printDialog(dialogID)
{
	dlgPrintState = null;

	var dlgPrint = $(dialogID);

	if (dlgPrint == null)
	{
		return;
	}

	dlgPrintState = ui.collectDialogState(dlgPrint);

	var wndPrint = window.open("ProxyPrint.aspx", "Print", "width=600px,height=600px,left=50px,top=50px,toolbar=no,menubar=no,scrollbars=yes");
}

var loadingEffect = null;

function init()
{
	mpEffect = new Fx.Morph($("mp"), { duration : 200, transition : Fx.Transitions.Quad.easeInOut, link : 'cancel' } );
	mpDialogEffect = new Fx.Morph($("mpDialog"), { transition : Fx.Transitions.Sine.easeInOut, link : 'cancel' } );
	loadingEffect = new Fx.Morph($("loading"), { transition : Fx.Transitions.Sine.easeInOut, link : 'cancel' } );

	imgBtn.setStatus("menu", $("btnHome"), 1);
	imgBtn.activate("menu", $("btnHome"));

	ui.onUpdateDialog = function()
	{
		ensureTypo();

		if (stateHistory.active)
		{
			stateHistory.push();
		}

		resizeHome();
	}
	
	ui.onReceiveModalPopup = function()
	{
		showModalPopup();
	}

	window.onresize = function()
	{
		resizeHome();
		resizeModalPopup();

		ensureTypo();
	}

	ui.onBeforeRequest = function()
	{
		//$("loading").setStyles( { display : "block" } );
		loadingEffect.start( { opacity : [0.0, 1.0] } );
	}

	ui.onAfterRequest = function()
	{
		loadingEffect.start( { opacity : [1.0, 0.0] } );
	}

	ui.load();

	window.onresize();
	ensureTypo();
	stateHistory.load();

	$("loading").setOpacity(0.0);
}

function resizeHome()
{
	var dlgHome = $("Home");

	if (dlgHome == null)
	{
		$("tcMain").setStyle("height", "auto");
		return;
	}

	var wndSize = window.getSize();

	var tcMainHeight = wndSize.y - 142;

	if (tcMainHeight < 400)
	{
		tcMainHeight = 400;
	}

	var thirdHeight = (tcMainHeight - 24.0) / 3.0;
	
	$("tcMain").setStyle("height", tcMainHeight);

	$("areaCaseStudies").setStyles( { height : tcMainHeight } );
	$("areaCaseStudies").getElement("img").setStyles( { height : tcMainHeight } );
	$("areaCaseStudiesBG").setStyles( { height : tcMainHeight } );

	$("areaMethods").setStyles( { height : thirdHeight, top : 0 } );
	$("areaMethods").getElement("img").setStyles( { height : thirdHeight } );
	$("areaMethodsBG").setStyles( { height : thirdHeight } );

	$("areaForum").setStyles( { height : thirdHeight + "px", top : (thirdHeight + 12) } );
	$("areaForum").getElement("img").setStyles( { height : thirdHeight } );
	$("areaForumBG").setStyles( { height : thirdHeight } );

	$("areaInformation").setStyles( { height : thirdHeight, top : (thirdHeight * 2 + 24) } );
	$("areaInformation").getElement("img").setStyles( { height : thirdHeight } );
	$("areaInformationBG").setStyles( { height : thirdHeight } );

	$("areaCaseStudyInner").setStyles( { top : (tcMainHeight - 197 - 14) } );
	$("areaMethodsInner").setStyles( { top : ( $("areaMethods").getSize().y - 64 - 14) } );
	$("areaForumInner").setStyles( { top : ( $("areaMethods").getSize().y - 64 - 14) } );
	$("areaInformationInner").setStyles( { top : ( $("areaMethods").getSize().y - 64 - 14) } );
}

function areaOver(areaControl, value)
{
	var areaBG = $(areaControl.id + "BG");

	if (areaBG == null)
	{
		return;
	}

	areaBG.setOpacity(value);
	areaBG.setStyles( { display : "block" } );
}

function areaOut(areaControl)
{
	var areaBG = $(areaControl.id + "BG");

	if (areaBG == null)
	{
		return;
	}

	areaBG.setStyles( { display : "none" } );
}

function buildLifeState()
{
	var mainDialog = $("main").getElement("div[typeid]");
	var lifeState = base64Json.encode(ui.collectDialogState(mainDialog));

	return lifeState;
}

function positionModalPopupDialog(beSmooth)
{
	var mpDialog = $("mpDialog");

	var wndSize = window.getSize();
	var wndScroll = window.getScroll();

	var mpDialogSize = mpDialog.getSize();
	var mpDialogLeft = (wndSize.x - mpDialogSize.x) / 2 + wndScroll.x;
	var mpDialogTop = (wndSize.y - mpDialogSize.y) / 2 + wndScroll.y;

	if (beSmooth)
	{
		mpDialogEffect.start( { left : mpDialogLeft, top : mpDialogTop } );
	}
	else
	{
		mpDialog.setStyles( { left : mpDialogLeft, top : mpDialogTop } );
	}
}

function resizeModalPopup()
{
	var mp = $("mp");
	var mpDialog = $("mpDialog");

	var wndSize = window.getSize();
	mp.setStyles( { width : wndSize.x, height : wndSize.y  } );

	var wndScrollSize = window.getScrollSize();
	mp.setStyles( { width : wndScrollSize.x, height : wndScrollSize.y  } );

	positionModalPopupDialog(false);
}

function hideModalPopup()
{
	var mp = $("mp");
	var mpDialog = $("mpDialog");
	var mpDialogInner = $("mpDialogInner");

	window.onscroll = function() { };

	mpDialog.setStyles( { display : "none" } );
	mpDialogInner.empty();

	mpEffect.start( { opacity : [0.9, 0.0] } ).chain(
		function()
		{
			mp.setStyles( { display : "none"  } );
		}
	);
}

function showModalPopup()
{
	resizeModalPopup();

	var mp = $("mp");
	var mpDialog = $("mpDialog");

	mp.setOpacity(0.0);
	mp.setStyles( { display : "block"  } );

	mpDialog.setOpacity(0.0);
	mpDialog.setStyles( { display : "block" } );

	ensureTypo();

	positionModalPopupDialog(false);

	mpEffect.start( { opacity : [0.0, 0.9] } ).chain(
		function()
		{
			mpDialog.setOpacity(1.0);
			window.onscroll = function()
			{
				positionModalPopupDialog(true);
			}
		}
	);
}

function ensureTypo()
{
	sIFR.replace(caption,
		{
			selector   : '.areaCaseStudyInner h1',
			css        : { '.sIFR-root' : { 'color': '#157F94'}, '.subSIFR' : { 'color': '#999999', 'font-size' : '70' } },
			selectable : false,
			wmode      : 'transparent',
			onRelease  : function()
			{
				navigate("CaseStudies", true);
			}
		}
	);

	sIFR.replace(caption,
		{
			selector   : '.areaMethodsInner h2',
			css        : { '.sIFR-root' : { 'color': '#157F94'} },
			selectable : false,
			wmode      : 'transparent',
			onRelease  : function()
			{
				navigate("Methods", true);
			}
		}
	);

	sIFR.replace(caption,
		{
			selector   : '.areaForumInner h2',
			css        : { '.sIFR-root' : { 'color': '#157F94'} },
			selectable : false,
			wmode      : 'transparent',
			onRelease  : function()
			{
				navigate("Forum", true);
			}
		}
	);

	sIFR.replace(caption,
		{
			selector   : '.areaInformationInner h2',
			css        : { '.sIFR-root' : { 'color': '#157F94'} },
			selectable : false,
			wmode      : 'transparent',
			onRelease  : function()
			{
				navigate("Information", true);
			}
		}
	);

	sIFR.replace(caption,
		{
			selector   : 'h1',
			css        : { '.sIFR-root' : { 'color': '#157F94' }, '.subSIFR' : { 'color': '#999999', 'font-size' : '70' } },
			wmode      : 'transparent',
			selectable : false
		}
	);

	sIFR.replace(caption,
		{
			selector   : 'h2',
			css        : { '.sIFR-root' : { 'color': '#157F94' } },
			wmode      : 'transparent',
			selectable : false
		}
	);
}