/**
 * @author 		Jonathan Sheerin, Maxime Bisantz
 * @copyright 	ArcSoftwareConsultancy
 * @version     1
 * @since 		17.12.2007
 */

function ShowHidePopUp(ClientID,Show,Return,FocusFieldID){
    var PopUp = document.getElementById(ClientID);
    var bReturn = (Return==undefined)?false:Return;
    var isIE6 = navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6') >-1;
    var faderSrc = '/Site/images/fadeBg.png';
    if (Show){
		document.body.style.minHeight = '100%';
		document.body.style.overflow = 'hidden';
        if (document.getElementById('fader')!=null){    
		    var fade = document.getElementById('fader');
		    var styles = {
			    height : isIE6 ? document.body.clientHeight : '100%',
			    width : isIE6 ? document.body.clientWidth : '100%',
			    position : isIE6 ? 'absolute' : 'fixed',
			    top: '0',
			    left: '0',
			    background: isIE6 ? 'none' : 'url('+faderSrc+')',
			    zIndex: '2'
		    }
		    if(isIE6){
		    	styles.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+faderSrc+'", sizingMethod="scale");'
		  	}
		    for(prop in styles){
			    fade.style[prop] = styles[prop];
		    }
		    document.getElementById('fader').style.display = 'block';
        }
		PopUp.style.display='block';
        CenterPopUp(PopUp);
        dragAndDrop(PopUp.getElementsByTagName('h3')[0].parentNode, ClientID);
        if(FocusFieldID != undefined && FocusFieldID != ''){
            document.getElementById(FocusFieldID).focus();
        }else{
            var myInputs = PopUp.getElementsByTagName('input');
            for(var i=0;i<=myInputs.length-1;i++){
                if (myInputs[i].type == 'text'){
                    myInputs[i].focus();
                    break;
                }
            }
        }
        
    }else{
		document.getElementById('fader').style.display = 'none';
        PopUp.style.display='none';
		document.body.style.overflow = 'auto';
		document.body.style.minHeight = '';
    }

    ManintainPopUpStates(ClientID, Show);
    return bReturn;
}

function ManintainPopUpStates(ClientID, show){
    var HiddenPopUpIds = document.getElementById('hiddenVisiblePopUpIDs')
    if (HiddenPopUpIds != 'undefined'){
        if (show){
            //Add it to the hidden if it doesn't already exist    
            if (!HiddenPopUpIds.value.match(ClientID)){
                if (HiddenPopUpIds.value == ''){
                    HiddenPopUpIds.value = ClientID;
                }else{
                    HiddenPopUpIds.value += ',' + ClientID;
                }
            }
        }else{
            //Remove
            if (HiddenPopUpIds.value.match(','+ClientID)){
                HiddenPopUpIds.value = HiddenPopUpIds.value.replace(','+ClientID,'');
            }else{
                HiddenPopUpIds.value = HiddenPopUpIds.value.replace(ClientID,'');
            }
        }
    }
    return false;
}

function dragAndDrop(handle, elementID){
	var _this = this;
	var e = document.getElementById(elementID);
	handle.onmousedown = function(evt){
		var evt = evt || event;
		_this.init = {
			l:(isNaN(parseInt(e.style.left)))? 0 : parseInt(e.style.left), 
			t:(isNaN(parseInt(e.style.top)))? 0 : parseInt(e.style.top),
			cursor : {
				x : evt.clientX,
				y : evt.clientY
			},
			diff : function(){
				return {
					x: this.cursor.x-this.l,
					y: this.cursor.y-this.t
				}
			} 
		}
		document.onmousemove = function(evt){
			var evt = evt || event;
			e.style.left = evt.clientX - _this.init.diff().x + 'px';
			e.style.top = evt.clientY - _this.init.diff().y + 'px';
		}
	}
	document.onmouseup = function(){
		document.onmousemove = null;
	}
}

function CenterPopUp(obj){
    var width = parseInt(obj.clientWidth)/2;
    var height = obj.clientHeight/2;
    var isIE6 = navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6') >-1;
    var scrollTop = isIE6 ? window.pageYOffset || document.documentElement.scrollTop || 0 : 0;

	var pageWidth = 0, pageHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}
    obj.style.left = (pageWidth / 2) - width + 'px';
    obj.style.top = (pageHeight / 2) + scrollTop - height + 'px';
}

