﻿function PhPosition( x, y )
{
	this.X = x;
	this.Y = y;
}
	
function PhClient()
{
	//member variables
	this.m_iBrowser			= 0;
	this.m_fVersion			= 0;
	
	this.m_strVersion		= '';
	this.m_strSearchKey		= '';
	
	this.m_iScreenWidth		= 0;
	this.m_iScreenHeight	= 0;
	
	this.GetBrowserType();
	this.GetTypeVersion();
	this.GetScreenDimensions();
}

PhClient.BROWSER_UNDEFINED	= 0;
PhClient.BROWSER_IE			= 1;
PhClient.BROWSER_FF			= 2;
PhClient.BROWSER_OPERA		= 3;
PhClient.BROWSER_SAFARI		= 4

//properties
PhClient.prototype.GetBrowser			= function() {return this.m_iBrowser;}
PhClient.prototype.GetVersion			= function() {return this.m_fVersion;}
PhClient.prototype.GetScreenWidth		= function() {return this.m_iScreenWidth;}
PhClient.prototype.GetScreenHeight		= function() {return this.m_iScreenHeight;}
	
//utility methods
PhClient.prototype.GetBrowserType = function()
{
	if( navigator.userAgent.indexOf( 'Firefox' ) != -1 )
	{
		this.m_iBrowser		= PhClient.BROWSER_FF;
		this.m_strVersion	= navigator.userAgent;
		this.m_strSearchKey	= 'Firefox';
	}
		
	else if( navigator.userAgent.indexOf( 'Opera' ) != -1 )
	{
		this.m_iBrowser		= PhClient.BROWSER_OPERA;
		this.m_strVersion	= navigator.userAgent;
		this.m_strSearchKey	= 'Opera';
	}
	
	else if( navigator.userAgent.indexOf( 'Safari' ) != -1 )
	{
		this.m_iBrowser		= PhClient.BROWSER_SAFARI;
		this.m_strVersion	= navigator.appVersion;
	}

	else if( navigator.userAgent.indexOf( 'MSIE' ) != -1 )
	{
		this.m_iBrowser		= PhClient.BROWSER_IE;
		this.m_strVersion	= navigator.appVersion;
		this.m_strSearchKey	= 'MSIE';
	}
				
	else
	{
		this.m_iBrowser		= PhClient.BROWSER_UNDEFINED;
		this.m_strVersion	= '';
	}	
}

PhClient.prototype.GetTypeVersion = function()
{
	var index = this.m_strVersion.indexOf( this.m_strSearchKey );
	
	if( index == -1 )
		this.Version = 0.0;
		
	this.m_fVersion = parseFloat( this.m_strVersion.substring( index+this.m_strSearchKey.length+1 ) );
}

PhClient.prototype.GetScreenDimensions = function()
{
	if( self.innerWidth )
	{
		this.m_iScreenWidth	    = self.innerWidth;
		this.m_iScreenHeight	= self.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientWidth )
	{
		this.m_iScreenWidth	    = document.documentElement.clientWidth;
		this.m_iScreenHeight	= document.documentElement.clientHeight;
	}
	else if( document.body )
	{
		this.m_iScreenWidth		= document.body.clientWidth;
		this.m_iScreenHeight	= document.body.clientHeight;
	}
}	

PhClient.prototype.CenterElementInWindow = function( element )
{
	this.GetScreenDimensions();
	
	var elm_w		= element.clientWidth;
	var elm_h		= element.clientHeight;
	var scroll_x	= document.body.scrollLeft;
	var scroll_y	= document.body.scrollTop;
	
	if( this.m_iBrowser != PhClient.BROWSER_IE )
	{
		scroll_x = window.pageXOffset;
		scroll_y = window.pageYOffset;
	}
	else if( document.documentElement && document.documentElement.clientWidth )
	{
		scroll_x = document.documentElement.scrollLeft;
		scroll_y = document.documentElement.scrollTop;
	}
	
	element.style.top	= ((this.m_iScreenHeight-elm_h)/2+scroll_y)+'px';
	element.style.left	= ((this.m_iScreenWidth-elm_w)/2+scroll_x)+'px';
}

PhClient.prototype.AttachEvent = function( element, event_name, callback_function )
{
	if( element.addEventListener )
		element.addEventListener( event_name, callback_function, false );
		
	else if( element.attachEvent )
		element.attachEvent( 'on'+event_name, callback_function );
}

PhClient.prototype.GetElementPosition = function( elm )
{
	var curleft = 0;
	var curtop	= 0;
	var obj		= null;
	
	if( typeof( elm ) == 'string' )
		obj = document.getElementById( elm );
	else
		obj	= elm;
	
	if( obj.offsetParent )
	{
		while( obj.offsetParent )
		{
			curleft += obj.offsetLeft
			curtop	+= obj.offsetTop;
			
			obj		= obj.offsetParent;
		}
	}
	else if( obj.x )
	{
		curleft += obj.x;
		curtop	+= obj.y;
	}
	
	return new PhPosition( curleft, curtop );
}

PhClient.prototype.RemoveElement = function( element )
{
	if( element == null )
		return;
		
	var parent = element.parentNode;
	
	if( this.m_iBrowswer == PhClient.BROWSER_IE )
		element.outerHTML = '';
	else
		parent.removeChild( element );
}

PhClient.prototype.DisableSelection = function( element )
{
    element.onselectstart			= function(){ return false;};
    element.unselectable			= 'on';
    element.style.MozUserSelect		= 'none';
}

PhClient.prototype.GetCursorPosition = function( evt, root_elm )
{
	var x	= 0;
	var y	= 0;
	var pos	= null
	
	if( evt.pageX || evt.pageY )
	{
		x = evt.pageX;
		y = evt.pageY;
	}
	else if( evt.clientX || evt.clientY )
	{
		x = evt.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
		y = evt.clientY+document.body.scrollTop+document.documentElement.scrollTop;
	}
	
	if( root_elm != null )
	{
		pos = this.GetElementPosition( root_elm );
		
		x -= pos.X;
		y -= pos.Y;
	}

	return new PhPosition( x, y );
}

PhClient.prototype.CheckLeftMouseButton = function( evt )
{
	var left_button = false;
	
	if( evt.which )
		left_button = (evt.which == 1);
	else
		left_button = (evt.button == 1);
		
	return left_button;
}

var g_oClient = new PhClient();