﻿//Common progress implementation
var AnthemProgress = new function()
{
	var _anchorId;
	var _inProgress;	
	var _progressId;
	var _defaultProgressId;
	var _fullProgressId;	
	var _wide = false;	
	var _wrap = true;
	var _text = '';	
	
	function get_anchorId()         { return _anchorId; }
	function set_anchorId(value)    { _anchorId = value; }
	
    function get_defaultProgressId() { return _defaultProgressId };
    function set_defaultProgressId(value) { _defaultProgressId = value };
    
    function get_fullProgressId() { return _fullProgressId };
	function set_fullProgressId(value) { _fullProgressId = value };
	
	function get_progressId()         { return _progressId; }
	function set_progressId(value)    { _progressId = value; }

	function get_inProgress()       { return _inProgress; }
	function set_inProgress(value)  { _inProgress = value; }
	
	function get_wide()       { return _wide; }
	function set_wide(value)  { _wide = value; }
	
    function get_wrap()       { return _wrap; }
	function set_wrap(value)  { _wrap = value; }
	
	function get_text()       { return _text; }
	function set_text(value)  { _text = value; }

	
	this.inProgress = function() { 
	    return get_inProgress();
	}
	
	this.progressId = function() {
	    return get_progressId();
	}

	this.defaultProgressId = function(value) {	 
	    set_defaultProgressId(value);
	}
	
	this.fullProgressId = function(value) {	 
	    set_fullProgressId(value);
	}
	
	this.text = function(value) {
        set_text (value);
	}
	
	this.wide = function(value) {
	    set_wide (value);
	}
	
	this.wrap = function(value) {
        set_wrap (value);
	}
	    	        
	this.display = function(anchorId)
	{		
		if (get_inProgress()) {
		    //this.hide()
			return; 
		}
				
		var progress = null, txt = null;
		var x = 0, y =0;
		var h = 0, w = 0;
		var image = null;							
	    
		if (!anchorId)
		{		    		    			
			progress = getObjById(get_fullProgressId());			
			txt = getObjById(get_fullProgressId() + 'Text');
			txt.innerHTML = _text;
			txt.style.display = 'block'; //disregard wrap
			
			w = 200;
			h = 100;			
					
            if(get_wide()) {
                var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;							
			    var windowWidth  = window.innerWidth  || document.documentElement.clientWidth  || document.body.clientWidth;
			    x = Math.round (windowWidth/2 - w/2) + scrollX;
			} else {
			    x = 285;
			}
			
			var scrollY = window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop;
			var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
			y = Math.round (windowHeight/2 - h/2) + scrollY;
		}
		else
		{
		    progress = getObjById(get_defaultProgressId());           
            txt = getObjById(get_defaultProgressId() + 'Text');		    	
            txt.innerHTML = _wrap ? _text : '&nbsp;&nbsp;' + _text;
            txt.style.display = _wrap ? 'block' : 'inline';
            
			var m = document.getElementById(anchorId);			
			if (!m)
			{
				var msg = 'Unable to find an element with id=' + anchorId;
				var err = new Error (msg);
				
				if (!err.message) err.message = msg;
				throw err;
			}
			
			h = m.offsetHeight, w = m.offsetWidth;
			var element = m;
	
			do {
				y += element.offsetTop  || 0;
				x += element.offsetLeft || 0;
				element = element.offsetParent;
			} while (element);
	
   		    m.style.visibility = 'hidden';
			set_anchorId (anchorId);						
		}
					    
		progress.style.width        = w + 1 + 'px';
		progress.style.height       = h + 1 + 'px';
		progress.style.left         = x + 'px';
		progress.style.top          = y + 'px';
		progress.style.display      = '';
		
		set_progressId (progress.id);					
		set_inProgress (true);		
	}
		
	this.hide = function()
	{
		if (!get_inProgress())
			return;
			
	    var progress = document.getElementById (get_progressId());		
		if (progress)    progress.style.display = 'none';
		
		var anchorId = get_anchorId();
		if(anchorId)
		{
			var m = document.getElementById (anchorId);
			if (m) m.style.visibility = '';
		}
		
		set_anchorId (null);
		set_progressId (null);
		set_inProgress (false);		
	}
}
//Pre & Post callback anthem methods
function beginProgress(control,text,wrap){       
    if(!control) return false; 
    if(control.id==null||control.id=='') return false;    
    if(AnthemProgress.inProgress()){      
        return false;       
    } else {            
        if(text!=null)AnthemProgress.text(text);
        if(wrap!=null)AnthemProgress.wrap(wrap);
	    AnthemProgress.display(control.id);	    
	    return true;
	}
}
function beginFullProgress(control,text){    
    if(!control) return false; 
    if(control.id==null||control.id=='') return false;
    if(AnthemProgress.inProgress()){      
        return false;       
    } else {                
        if(text!=null)AnthemProgress.text(text);
	    AnthemProgress.display();
	    return true;
	}
}
function beginListProgress(list,text){     
    if(!list) return false;
    if(list.id==null||list.id=='') return false;     
	        
    if(AnthemProgress.inProgress()){      
        return false;       
    } else {
    
        AnthemProgress.wide(false);       
        AnthemProgress.text('Loading');
        
        //pull off index, loop and deactivate        
//        var regPref, src, li;
//        regPref = /_[0-9]+$/;
//        src = list.id.replace(regPref, "_");		    
//	    for (i = 0; i <= 100; i++) {	    	    	    
//	        li = getObjById(src + i)
//		    if(li!=null) {
//			    li.disabled = true;			
//		    } else {
//		        break;
//		    }
//	    }       
	    AnthemProgress.display();
	    return true;
	}
}
function endProgress(control){    
    AnthemProgress.hide();     
}    
function endListProgress(list){
    AnthemProgress.hide();     
    
    //pull off index, loop and activate        
//    var regPref, src, li;
//    regPref = /_[0-9]+$/;
//    src = list.id.replace(regPref, "_");		
//	for (i = 0; i <= 100; i++) {	    	    	    
//	    li = getObjById(src + i)
//		if(li!=null) {
//			li.disabled = false;			
//		} else {
//		    break;
//		}
//	}   	        
} 
function getObjById(id) {
    var obj = null;
    if (document.getElementById) {
        obj = document.getElementById(id);
    } else if(document.all) {
          obj = document.all[id];
    } else if(document.layers) {
          obj = document.layers[id];
    }    
    return obj;
}