/**
 * ShiftMap is a function called by the Anchor class to slide the current popup into view.
 * Popups are displayed exclusivly meaning "there can be only one" at a time.
 * This function was not embedded into any OpenLayer classes due to the nature of its operation and scoping issues.
 * 
 * @Function
 */
function ShiftMap(obj)
{
    var o = obj; // copy object locally to facilitate timeout execution
    
    if (o!=null)
    {
        // record the date and time of first call to this method using anchor object as scope
        if (!o.ShiftStartTime) o.ShiftStartTime = (new Date()).getTime();
        
        if (o.differenceX!=0)
        {
            //
            // X difference detected, process
            //
            
            // determine direction of motion
            var dir = (o.differenceX>0) ? "+":"-";
            
            // calculated elapsed time since start of animation
            var elapsed = (new Date()).getTime() - o.ShiftStartTime;
            
            // calculated the required incrememt to ensure animation is complete within predefined timescale
            var increment = Math.round(elapsed / OpenLayers.Popup.SlideAnimationTime * Math.abs(o.differenceX));
            
            switch(dir)
            {
                case "+":
                {
                    if ((o) && (o.differenceX>1) && (o.map))
                    {
                        o.differenceX-=increment;
                        o.map.pan(+increment,0);
                        window.setTimeout(function() { ShiftMap(o); }, OpenLayers.Popup.SlideTimerInterval);
                    }
                }break;
                
                case "-":
                {
                    if ((o) && (o.differenceX<1) && (o.map))
                    {
                        o.differenceX+=increment;
                        o.map.pan(-increment,0);
                        window.setTimeout(function() { ShiftMap(o); }, OpenLayers.Popup.SlideTimerInterval);
                    }
                }break;
            }
        }
        
        if (o.differenceY!=0)
        {
            //
            // Y difference detected, process
            //
            
            // determine direction of motion
            var dir = (o.differenceY>0) ? "+":"-";
            
            // calculated elapsed time since start of animation
            var elapsed = (new Date()).getTime() - o.ShiftStartTime;
            
            // calculated the required incrememt to ensure animation is complete within predefined timescale
            var increment = Math.round(elapsed / OpenLayers.Popup.SlideAnimationTime * Math.abs(o.differenceY));
            
            switch(dir)
            {
                case "+":
                {
                    if ((o) && (o.differenceY>1) && (o.map))
                    {
                        o.differenceY-=increment;
                        o.map.pan(0,+increment);
                        window.setTimeout(function() { ShiftMap(o); }, OpenLayers.Popup.SlideTimerInterval);
                    }
                }break;
                
                case "-":
                {
                    if ((o) && (o.differenceY<1) && (o.map))
                    {
                        o.differenceY+=increment;
                        o.map.pan(0,-increment);
                        window.setTimeout(function() { ShiftMap(o); }, OpenLayers.Popup.SlideTimerInterval);
                    }
                }break;
            }
        }
    }
}


/**
 * Custom support object containg static methods required by the updated Popup and associated Classes, could be merged into the OpenLayers.Util collection
 * I have included an opacity fade method, however with the map I am using for testing loading slow, sliding and fading appeared too much.
 * 
 * @Object
 */
var support = {

    viewport : function()
    {
        return {
            height : (document.body.clientHeight || document.documentElement.clientHeight || window.innerHeight || 0),
            width : (document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth || 0),
            scrollX : (document.body.scrollLeft || document.documentElement.scrollLeft || self.pageXOffset || 0),
            scrollY : (document.body.scrollTop || document.documentElement.scrollTop || self.pageYOffset || 0)
        };
    },
    
	/* return position of element */
	getPos : function(el)	
	{
		var x=0, y=0, w=0, h=0; 
	
		if (document.getBoxObjectFor)
		{ 

			var bo = document.getBoxObjectFor(el); 
			return {x:bo.x, y:bo.y, w:bo.width, h:bo.height}
		} 
		else if (el.getBoundingClientRect)
		{
			var rect = el.getBoundingClientRect(); 
			return {x:rect.left + support.viewport().scrollX, y:rect.top + support.viewport().scrollY, w:(rect.right - rect.left), h:(rect.bottom - rect.top)}
		}
		else
		{

			w = el.offsetWidth;
			h = el.offsetHeight;
			do { x += el.offsetLeft || 0; y += el.offsetTop || 0; el = el.offsetParent; } while (el);
			return {x:x, y:y, w:w, h:h}
		}
	},

    fade : function(id, opacStart, opacEnd, millisec, func)
    {
        //speed for each frame
        var speed = Math.round(millisec / 100);
        var timer = 0;

        //determine the direction for the blending, if start and end are the same nothing happens
        if(opacStart > opacEnd)
        {
            for(i = opacStart; i >= opacEnd; i--)
            {

                setTimeout("support.changeOpac(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
        } 
        else if(opacStart < opacEnd)
        {
            for(i = opacStart; i <= opacEnd; i++)
            {
                setTimeout("support.changeOpac(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
        }
        if (opacStart == opacEnd)
        {
            func();
        }
    },

    //change the opacity for different browsers
    changeOpac : function(opacity, id)
    {
        var object = document.getElementById(id).style;
        object.opacity = (opacity / 100);
        object.MozOpacity = (opacity / 100);
        object.KhtmlOpacity = (opacity / 100);
        object.filter = "alpha(opacity=" + opacity + ")";
    }
}

CORNERS = {

    /* top images*/
    TL : {src:'corners/topl.png', sizing: 'scale'},
    TM : {src:'corners/topm.png', sizing: 'crop'},
    TR : {src:'corners/topr.png', sizing: 'scale'},
    
    /* middle images */
    ML : {src:'corners/midl.png', sizing: 'crop'},
    MR : {src:'corners/midr.png', sizing: 'crop'},
    
    /* bottom images */
    BL : {src:'corners/botl.png', sizing: 'scale'},
    BM : {src:'corners/botm.png', sizing: 'crop'},
    BR : {src:'corners/botr.png', sizing: 'scale'}
}


