﻿/// <reference name="MicrosoftAjax.js"/>

// Definition of the global object for user side functionality.

Type.registerNamespace('MazdaPress');


MazdaPress.Global = function MazdaPress$Global()
{
	MazdaPress.Global.initializeBase(this);
}


MazdaPress.Global.setAutoSelect = function MazdaPress$Global$setAutoSelect(element)
{
	$addHandler(element, 'focus', function(event)
		{
			if(window.getSelection)
				window.getSelection().removeAllRanges();
			else if(document.selection)
				document.selection.empty();
			element.focus();
			element.select();
		});
}


MazdaPress.Global.setAutoClear = function MazdaPress$Global$setAutoClear(element)
{
	$addHandler(element, 'focus', function(event)
		{
			if(element.value)
				element.value = '';
		});
}


MazdaPress.Global.setAutoClearOnce = function MazdaPress$Global$setAutoClearOnce(element)
{
	$addHandler(element, 'focus', function(event)
		{
			if(element.value)
				element.value = '';
			$removeHandler(element, 'focus', arguments.callee);
		});
}


MazdaPress.Global.prototype.getClientBounds = function MazdaPress$Global$getClientBounds()
{
	var bounds = { top: 0, left: 0, width: 0, height: 0 };

	if(typeof(window.pageXOffset) == 'number')
	{
		bounds.left = window.pageXOffset;
		bounds.top = window.pageYOffset;
		bounds.width = window.innerWidth;
		bounds.height = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop || document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		bounds.left = document.documentElement.scrollLeft;
		bounds.top = document.documentElement.scrollTop;
		bounds.width = document.documentElement.clientWidth;
		bounds.height = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop || document.body.clientWidth || document.body.clientHeight))
	{
		bounds.left = document.body.scrollLeft;
		bounds.top = document.body.scrollTop;
		bounds.width = document.body.clientWidth;
		bounds.height = document.body.clientHeight;
	}

	return bounds;
}


MazdaPress.Global.prototype.getPageSize = function getPageSize()
{
	var xScroll, yScroll;

	if(window.innerHeight && window.scrollMaxY)
	{
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth)
			windowWidth = document.documentElement.clientWidth;
		else
			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;

	return { width: pageWidth, height: pageHeight };
	/*
	var size = { width: 0, height: 0 };

	if(typeof(window.pageXOffset) == 'number')
	{
	size.width = window.innerWidth + window.scrollMaxX;
	size.height = window.innerHeight + window.scrollMaxY;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop || document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
	size.width = document.documentElement.scrollWidth;
	size.height = document.documentElement.scrollHeight;
	}
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop || document.body.clientWidth || document.body.clientHeight))
	{
	size.width = document.body.scrollWidth;
	size.height = document.body.scrollHeight;
	}
	
	var bounds = this.getClientBounds();
	if(size.width < bounds.width)
	size.width = bounds.width;
	if(size.height < bounds.height)
	size.height = bounds.height;

	return size;
	*/
}



MazdaPress.Global.registerClass('MazdaPress.Global');


// instantiated during the application initialization.
var $mp = null;
Sys.Application.add_init(function() { $mp = new MazdaPress.Global(); });


// Notify the ScriptManager that the script file finished loding.
if(typeof(Sys) !== 'undefined')
	Sys.Application.notifyScriptLoaded();

