//
// --- digitalseraph.net4 framework ---
//

var System =
{
    Cache : new Object(),

    //
    // --- input-related functions ---
    //
    Input :
    {
        getMousePosition : function( event )
        {
            var ev = System.Web.getEvent(event);
            
            if( ev.offsetX && ev.offsetY )
                return { x: ev.offsetX, y: ev.offsetY };
            else if( ev.pageX && ev.pageY )
                return { x: System.Math.getInt(ev.pageX), y: System.Math.getInt(ev.pageY) };
            else
                return { x: 0, y: 0 };
        }
    },

    //
    // --- math-related functions ---
    //
    Math :
    {
        getInt : function( iValue )
        {
            return (isNaN(iValue+0) ? 0 : iValue);
        }
    },

    //
    // --- HTML-related functions ---
    //
    Web :
    {
	    width : function()
	    {
		    if( window.innerWidth )
			    return window.innerWidth;
		    else if( document.documentElement && document.documentElement.clientWidth != 0 )
			    return document.documentElement.clientWidth;
		    else if( document.body )
			    return document.body.clientWidth;
		    else
			    return -1;
	    },

	    height : function()
	    {
		    if( window.innerHeight )
			    return window.innerHeight;
		    else if( document.documentElement && document.documentElement.clientHeight != 0 )
			    return document.documentElement.clientHeight;
		    else if( document.body )
			    return document.body.clientHeight;
		    else
			    return -1;
	    },

        // Operates on <link rel="alternate stylesheet" type="text/css" href="stylesheet.css" title="user-defined" />
	    setStyleSheet : function( strTitle )
	    {
		    if( document.getElementsByTagName )
		    {
			    var curr;
			    var links = document.getElementsByTagName("link");

			    for( var i = 0; curr = links[i]; ++i )
			    {
				    if( curr.getAttribute("rel").indexOf("style") != -1 && curr.getAttribute("title") )
					    curr.disabled = ( curr.getAttribute("title") != strTitle );
			    }

			    return true;
		    }

		    return false;
	    },
	    
	    supportsStyleSheets : function( objLink )
        {
            var linkDisplay = "inline";

            // Retrieve the computedStyle display of the link; this is for Mozilla and Opera 7.2+.
            if( typeof document.defaultView != "undefined" && typeof document.defaultView.getComputedStyle != "undefined" )
                linkDisplay = document.defaultView.getComputedStyle( objLink, "" ).getPropertyValue("display");
            // Retrieve the currentStyle display of the link for IE.
            else if( typeof objLink.currentStyle != "undefined" )
                linkDisplay = objLink.currentStyle.display;

            return ( linkDisplay != "inline" );
        },

	    addEvent : function( objTarget, strEvent, refEvent )
	    {
	        if( typeof objTarget.addEventListener != "undefined" )
	            objTarget.addEventListener( strEvent, refEvent, false );
	        else if( typeof objTarget.attachEvent != "undefined" )
	            objTarget.attachEvent( "on" + strEvent, refEvent );
	        else
	            return false;
	        return true;
	    },
	    
	    removeEvent : function( objTarget, strEvent, refEvent )
	    {
	        if( typeof objTarget.removeEventListener != "undefined" )
	            objTarget.removeEventListener( strEvent, refEvent, false );
	        else if( typeof objTarget.detachEvent != "undefined" )
	            objTarget.detachEvent( "on" + strEvent, refEvent );
	        else
	            return false;
	        return true;
	    },
	    
	    getEvent : function( event )
	    {
	        if( window.event )
	            return window.event;
	        else
	            return event;
	    },
	    
	    setTimer : function( iTimerId, eventElapsed, iElapsed )
	    {
	        System.Web.clearTimer(iTimerId);
	        System.Cache["timer_" + iTimerId] = window.setTimeout( function(){ System.Cache["timer_" + iTimerId] = null; eventElapsed(); }, iElapsed );
	    },
	    
	    clearTimer : function( iTimerId )
	    {
	        if( System.Cache["timer_" + iTimerId] )
	        {
	            window.clearTimeout( System.Cache["timer_" + iTimerId] );
	            System.Cache["timer_" + iTimerId] = null;
	        }
	    }
	},
	
	//
	// --- XML-related functions ---
	//
    Xml :
    {
        createXmlRequest : function( iRequestId, eventResponse )
        {
            var value = null;
            try
            {
                value = new XMLHttpRequest();
            }
            catch( e )
            {
                try
                {
                    value = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch( e )
                {
                    try
                    {
                        value = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch( e )
                    {
                        return null;
                    }
                }
            }
            
            System.Xml.abortXmlRequest(iRequestId);
            System.Cache["xmlhttp_" + iRequestId] = value;
            System.Cache["xmlhttp_session_" + iRequestId] = System.Cache["session_id"];
            
            value.onreadystatechange = function()
            {
                req = System.Cache["xmlhttp_" + iRequestId];
                
                if( System.Cache["xmlhttp_session_" + iRequestId] != System.Cache["session_id"] )
                {
                    System.Xml.abortXmlRequest(iRequestId);
                    return;
                }
        
                if( req && req.readyState == 4 )
                {
                    timer = System.Cache["xmlhttp_abort_" + iRequestId];
                    if( timer )
                        window.clearTimeout( timer );

                    System.Cache["xmlhttp_abort_" + iRequestId] = null;
                    
                    // If there's a response, run the successful event handler.
                    if( req.responseText != "" && (req.status == 0 || req.status == 200) )    // Damn you, Opera 9!
                    {
                        if( System.Cache["xmlhttp_session_" + iRequestId] == System.Cache["session_id"] )
                            eventResponse( req );
                    }
                    // Otherwise, execute the stored timeout function.
                    else if( System.Cache["xmlhttp_timeout_fn_" + iRequestId] )
                    {
                        System.Cache["xmlhttp_timeout_fn_" + iRequestId]( req );
                        System.Cache["xmlhttp_timeout_fn_" + iRequestId] = null;
                    } 
                        
                    System.Cache["xmlhttp_" + iRequestId] = null;
                }
            };
            
            return value;
        },
        
        getXmlRequest : function( iRequestId )
        {
            return System.Cache["xmlhttp_" + iRequestId];
        },
        
        sendXmlRequest : function( iRequestId, strUri, eventTimeout, iTimeout )
        {
            request = System.Cache["xmlhttp_" + iRequestId];
        
            if( request == null )
                return false;
        
            if( eventTimeout )
            {
                System.Cache["xmlhttp_abort_" + iRequestId] = window.setTimeout( function() {
                        request.abort();
                        if( System.Cache["xmlhttp_session_" + iRequestId] == System.Cache["session_id"] )
                            eventTimeout(request);
                    }, iTimeout );
                System.Cache["xmlhttp_timeout_fn_" + iRequestId] = eventTimeout;
            }
        
            try
            {
                request.open( "GET", strUri, true );
                request.send( null );
            }
            catch( ex )
            {
                eventTimeout( request );
                return true;
            }

            return true;
        },
        
        abortXmlRequest : function( iRequestId )
        {
            var cache = System.Cache["xmlhttp_" + iRequestId];
        
            if( cache && typeof cache.abort != "undefined" )
            {
                cache.onreadystatechange = function () {}
                cache.abort();
                cache = null;
            }
        }
    }
}
