//function myPopup_XXX_openPopup() {} // should be located in myPopup_XXX.js file
//function myPopup_XXX_closePopup() {}

var CurrentPopup = '';

function openPopup(myPopupID,widthPX,heightPX) {
	if( $(myPopupID) ) {
		self.setTimeout(myPopupID+"_openPopup()", 10 )
		objectsTagVisiblity(0) ; selectsVisibility(0) ;

		myPopupGlassLayer(1)
		
		$(myPopupID).style.display = "block";
		CurrentPopup = myPopupID;

		setPopupParams();
		$(myPopupID).focus();
		
	} else { window.status = "Error opening popup: "+myPopupID }
}
function setPopupParams()
{
    if(CurrentPopup != ''){
        var widthPX = parseInt($(CurrentPopup).offsetWidth);
        var heightPX = parseInt($(CurrentPopup).offsetHeight);
    	
        var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
        var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
    	
        var pop_scrollLeft = scrollLeft   + ((clientWidth - widthPX ) /2);
        $(CurrentPopup).style.left = pop_scrollLeft + 'px';
    	
        var MinAllowedHeightOfPopup = 50
        var calcHeightOfPopup = scrollTop  + Math.max(((clientHeight - heightPX ) /2),0)
        $(CurrentPopup).style.top = Math.max(calcHeightOfPopup,MinAllowedHeightOfPopup) + 'px';
        
        var Loading = $('DIV_myGlassLayer_Loading');
        if(Loading && Loading.style.display == "block"){
            Loading.style.left = scrollLeft   + ((clientWidth - 200 ) /2)+ 'px'; 
            Loading.style.top = scrollTop  + Math.max(((clientHeight - 25 ) /2),0)+ 'px'; 
        }
    }
}

function closePopup(myPopupID,CloseGlassLayer) {
	if (CloseGlassLayer=="1") { myPopupGlassLayer(0) ; }
	$(myPopupID).style.display = "none"
	
	objectsTagVisiblity(1) ; selectsVisibility(1) ;
	self.setTimeout(myPopupID+"_closePopup()", 10 )
	CurrentPopup = '';
}

function objectsTagVisiblity(makeVisible) {
	var displayMode = "block"
	if  (makeVisible == "0") { displayMode = "none" ; }
	
    var objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)  {
        objects[i].style.display = displayMode;
    }
}

function myPopupGlassLayer(mode) {
    var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
    var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
	if (mode == 1) {
		objectsTagVisiblity(0) ; selectsVisibility(0) ;
		if ( $('myGlassLayer') ) {
			$('myGlassLayer').style.width = document.body.scrollWidth + 'px';
			$('myGlassLayer').style.height = Math.max(clientHeight,document.body.scrollHeight) + 'px'; //document.body.offsetHeight-20
			
			$('myGlassLayer').style.left = 0 + 'px';
			$('myGlassLayer').style.top = 0 + 'px';
			$('myGlassLayer').style.display = "block"	
				
		}
		if ( $('DIV_myGlassLayer_Loading') ) {
			$('DIV_myGlassLayer_Loading').style.left = scrollLeft   + ((clientWidth - 200 ) /2) + 'px';
			$('DIV_myGlassLayer_Loading').style.top = scrollTop  + Math.max(((clientHeight - 25 ) /2),0) + 'px';
			$('DIV_myGlassLayer_Loading').style.display = "block";
			$('DIV_myGlassLayer_Loading').style.zIndex = 50;
		}
	}
	else {  //(mode == 0)
		if ( $('myGlassLayer') ) {  $('myGlassLayer').style.display = "none" ; $('DIV_myGlassLayer_Loading').style.display = "none" } 
		objectsTagVisiblity(1) ; selectsVisibility(1) ; 
	}
}

function PopupMaskResize()
{
    var MyGlass = $('myGlassLayer');

    if(MyGlass){ 
        var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
        var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
        MyGlass.style.width = document.body.scrollWidth + 'px'; 
        MyGlass.style.height = Math.max(clientHeight,document.body.scrollHeight) + 'px';
    }
    setPopupParams();
}

function PopupObjsResize()
{
    for(var i = 0; i < RunningPopups.length; ++i){
        RunningPopups[i].ReSize();    
    }
}
function PopupObjsCenter()
{
    for(var i = 0; i < RunningPopups.length; ++i){
        RunningPopups[i].Center();    
    }
}

var StackingZIndex = 5000;
var RunningPopups = [];

function AF_Popup(PopupID){
    var dt = new Date();
    var GlassLayer = "AF_GlassID_" + PopupID + '_' + dt.getTime();
    var popupid = PopupID;
    this.MyPopupID = PopupID;
    this.WidthPX;
    this.HeightPX;
    this.IsModel = true;
    this.SetClass = "afGlass";
    this.ShowLoading = true;
    this.IsShowing = false; // is the popup being shown already, ie., the Show() method has been called.
    this.BackgroundVisibility = true;
    this.Zindex;

    this.SetPopupParams = function(){
        if($(GlassLayer)){ 
            var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
            var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
            
            if(this.IsModel == true){             
                var MyGlass = $(GlassLayer);
                
                MyGlass.style.width = document.body.scrollWidth + 'px'; 
                MyGlass.style.height = Math.max(clientHeight,document.body.scrollHeight)+ 1000 + 'px';
                
                var Loading = $('DIV_myGlassLayer_Loading');
	            if(Loading && Loading.style.display == "block" && this.ShowLoading){
		            Loading.style.left = scrollLeft   + ((clientWidth - 200 ) /2) + 'px'; 
		            Loading.style.top = scrollTop  + Math.max(((clientHeight - 25 ) /2),0) + 'px'; 
	            }
		    }		            
        }              
    };

    this.Show = function(){
        if($(this.MyPopupID) && this.IsShowing == false){
   	        var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
   	        var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
   	        
            if(this.IsModel == true){  
		        if(this.BackgroundVisibility == false){
		            this.SetClass = "afNoGlass";
		            objectsTagVisiblity(0) ; selectsVisibility(0);
		        }
            
                var MyGlass = document.createElement("DIV");
                MyGlass.id = GlassLayer;
                MyGlass.className = this.SetClass;
                if(this.Zindex){
                    MyGlass.style.zIndex = this.Zindex;
                }else{
                    StackingZIndex += 1;
                    MyGlass.style.zIndex = StackingZIndex;
                }
                MyGlass.style.position = "absolute";
                document.body.appendChild(MyGlass);
		        //self.setTimeout(this.MyPopupID+"_openPopup()", 10);
        		            
	            MyGlass.style.left = '0px';
	            MyGlass.style.top = '0px';
                MyGlass.style.display = "block";
                MyGlass.style.padding = '0px';
                MyGlass.style.margin = '0px';
                
	            if($('DIV_myGlassLayer_Loading') && this.ShowLoading){
		            $('DIV_myGlassLayer_Loading').style.left = scrollLeft   + ((clientWidth - 200 ) /2) + 'px'; 
		            $('DIV_myGlassLayer_Loading').style.top = scrollTop  + Math.max(((clientHeight - 25 ) /2),0) + 'px'; 
		            $('DIV_myGlassLayer_Loading').style.display = "block";
		            StackingZIndex += 1;
		            $('DIV_myGlassLayer_Loading').style.zIndex = StackingZIndex;
	            }
		    }
    		
    		$(this.MyPopupID).style.position = "absolute"; 
    		$(this.MyPopupID).style.display = "block"; 

            this.SetPopupParams(this);
            this.Center();
            if(this.Zindex){
                $(this.MyPopupID).style.zIndex = this.Zindex + 1;
            }else{
                StackingZIndex += 1;
                $(this.MyPopupID).style.zIndex = StackingZIndex;
            }
            this.IsShowing = true;
            RunningPopups.push(this);
            $(this.MyPopupID).focus();   
                        

        }
    };
    
    this.Close = function(){
        if($(this.MyPopupID)){
            $(this.MyPopupID).style.display = "none";
            this.WidthPX = null; this.HeightPX = null;
            if(this.IsModel == true){
                var MyGlass = $(GlassLayer);
                if(MyGlass){ document.body.removeChild(MyGlass); }
                if(!this.Zindex){ StackingZIndex -= 1; }
                this.HideLoading();
            }
            this.IsShowing = false;
            self.setTimeout(this.MyPopupID+"_closePopup()", 10);
        	if(this.BackgroundVisibility == false){
	            objectsTagVisiblity(1) ; selectsVisibility(1);
	        }
            RunningPopups.pop();
        }
    };
    
    this.HideLoading =  function(){
        if($('DIV_myGlassLayer_Loading') && this.ShowLoading){
            $('DIV_myGlassLayer_Loading').style.zIndex = 50;
            $('DIV_myGlassLayer_Loading').style.display = "none";
        }
    };
    
    this.ReSize = function(){
        this.SetPopupParams();
        this.Center();
    };
    
    this.Center = function(){
        var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
        var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
   		$(this.MyPopupID).style.height = 'auto';

        if(!this.WidthPX){ this.WidthPX = parseInt($(this.MyPopupID).offsetWidth) + 'px'; }
        if(!this.HeightPX){ this.HeightPX = parseInt($(this.MyPopupID).offsetHeight) + 'px'; }
        $(this.MyPopupID).style.width = this.WidthPX; $(this.MyPopupID).style.height = this.HeightPX;
          
        var pop_scrollLeft = scrollLeft + ((clientWidth - parseInt(this.WidthPX)) /2) 
        $(this.MyPopupID).style.left = pop_scrollLeft+ 'px'; 
    	
        var MinAllowedHeightOfPopup = 50
        var calcHeightOfPopup = scrollTop + Math.max(((clientHeight - parseInt(this.HeightPX)) /2),0)
        $(this.MyPopupID).style.top = Math.max(calcHeightOfPopup,MinAllowedHeightOfPopup) + 'px'; 
        
        var Loading = $('DIV_myGlassLayer_Loading');
        if(Loading && Loading.style.display == "block" && this.ShowLoading){
            Loading.style.left = scrollLeft   + ((clientWidth - 200 ) /2) + 'px'; 
            Loading.style.top = scrollTop  + Math.max(((clientHeight - 25 ) /2),0) + 'px'; 
        }
    };
};

var AF_Alerts = {
	confirm: function(title, message, buttons, callback, width, height) {
		if( title == null ) title = 'Confirm';
		AF_Alerts._show('confirm', title, message, buttons, function(result){
			if(callback){ callback(result); }
		}, width, height);
	},

	_show: function(type, title, message, buttons, callback, width, height) {
		switch(type){
			case 'confirm':
    	        $('htmlTD_myPopup_confirm1_Question').innerHTML = title;
			
			    if(buttons.match(/yes/gi)){ $('myPopup_Confirm_Yes').style.display = ''; }
			    if(buttons.match(/no/gi)){ $('myPopup_Confirm_No').style.display = ''; }
			    if(buttons.match(/cancel/gi)){ $('myPopup_Confirm_Cancel').style.display = ''; }
			    
			    $('myPopup_Confirm_Message').innerHTML = message;
	            if(width){ myPopup_Confirm.WidthPX = width + 'px'; }
	            if(height){ myPopup_Confirm.HeightPX = height + 'px'; } 
	       		$(myPopup_Confirm.MyPopupID).style.width = 'auto';
   	            myPopup_Confirm.Show();
			    
			    $('myPopup_Confirm_Yes').onclick = function(){
					myPopup_Confirm.Close();
					if( callback ){ callback(1); }
				};
				$('myPopup_Confirm_No').onclick = function(){
					myPopup_Confirm.Close();
					if( callback ){ callback(2); }
				};
				$('myPopup_Confirm_Cancel').onclick = function(){
					myPopup_Confirm.Close();
					if( callback ){ callback(3); }
				};
				$('myPopup_Confirm1').focus();
			break;
		}
	}
}
// eg:  AFConfirm("Confirm?", "Do you want to save the changes you have made?", "yes|no|cancel", ConfirmCallback, 200, 100);
// width/height are optional.
var AFConfirm = function(title, message, buttons, callback, width, height) {
    AF_Alerts.confirm(title, message, buttons, callback, width, height);
}

function CenterNewVin()
{
    var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
    var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
    var VinNew = $('divVinNew');
        
    if(VinNew && VinNew.style.display == "block"){
        VinNew.style.left = scrollLeft   + ((clientWidth - 255 ) /2) + 'px';
        VinNew.style.top = scrollTop  + ((clientHeight - 45 ) /2) + 'px';
    }
}

function RePositionAlerts()
{
    var scrollTop = getScrollXY()[1]; var scrollLeft = getScrollXY()[0];
    var clientHeight = getClientWidthHeight()[1]; var clientWidth = getClientWidthHeight()[0];
    var width = 0;
    if($('GE_AlertLayer')){
        width = $('GE_AlertLayer').offsetWidth;
	    $('GE_AlertLayer').style.left = Math.max( 0, GE_AlertWindow_3_X - width  + 10) + scrollLeft + 'px';
	    $('GE_AlertLayer').style.top = Math.max(0, GE_AlertWindow_3_Y - 12 ) + scrollTop + 'px';
	}
	if($('GE_AlertLayerPopup')){
        width = $('GE_AlertLayerPopup').offsetWidth;
	    $('GE_AlertLayerPopup').style.left = Math.max( 0, GE_AlertWindow_3_X - width  + 10) + scrollLeft + 'px';
	    $('GE_AlertLayerPopup').style.top = Math.max(0, GE_AlertWindow_3_Y - 12 ) + scrollTop + 'px';
	}
}

// creates a blue box. the id element should contain a div which holds the actual content. eg:  <div id='id'><div>content...</div></div>
function AF_BoxBlue(id, titleHtml)
{
    var dt = new Date();
    var counter = 1;
    var mainid = "AF_PopID_" + id + '_' + (counter) + '_' + dt.getTime();

    var parent = $(id);
    parent.className = 'afBox_blue_v2'
    var first = parent.firstChild;
    var MyGlass = document.createElement("DIV");
    MyGlass.id = mainid;
    MyGlass.className = 'afBox_blue_v2_emright';
    parent.insertBefore(MyGlass, first);
    
    mainid = "AF_PopID_" + id + '_' + (++counter) + '_' + dt.getTime();
    MyGlass1 = document.createElement("DIV");
    MyGlass1.id = mainid;
    MyGlass1.className = 'afBox_blue_v2_middlebg';
    MyGlass.appendChild(MyGlass1);
    MyGlass1.innerHTML = '<div class="afBox_blue_v2_topleft"></div><div class="afBox_blue_v2_topcontent">' + titleHtml + '</div><div class="afBox_blue_v2_topright"></div>';
    
    mainid = "AF_PopID_" + id + '_' + (++counter) + '_' + dt.getTime();
    MyGlass2 = document.createElement("DIV");
    MyGlass2.id = mainid;
    MyGlass2.className = 'afBox_blue_v2_contentMain';
    MyGlass.appendChild(MyGlass2);
    MyGlass2.appendChild(first);
    
    MyGlass.innerHTML += '<div class="afBox_blue_v2_footerD"><div class="afBox_blue_v2_footerL"></div><div class="afBox_blue_v2_footerR"></div></div>';
}


addEvent(window,'resize',pageResize);
addEvent(window,'resize',PopupMaskResize);
//addEvent(window,'scroll',PopupMaskResize);
addEvent(window,'resize',PopupObjsResize);
//addEvent(window,'scroll',PopupObjsCenter);
addEvent(window,'resize',CenterNewVin);
//addEvent(window,'scroll',CenterNewVin);
//addEvent(window,'scroll',RePositionAlerts);
addEvent(window,'resize',RePositionAlerts);

function PopupKeyCheck(e) {
	var targ;
	if (!e){ var e = window.event; }
	if (e.target){ targ = e.target; 
	}else if (e.srcElement){ targ = e.srcElement; }
	
    if (targ.nodeType == 3){ // defeat Safari bug
        targ = targ.parentNode;
    }    
    var obj = null;
    if(RunningPopups.length > 0){
        obj = $(RunningPopups[RunningPopups.length-1].MyPopupID);
    }else if(CurrentPopup != ''){ obj = $(CurrentPopup); }
    
    if(obj){
        if(YAHOO.util.Dom.isAncestor(obj, targ) || (obj.id == 'myPopup_ManageInventory_Detail' && $("divVinNew").style.display == "block" && YAHOO.util.Dom.isAncestor($("divVinNew"), targ))){ 
            //window.status = targ.id;
        }else{
            obj.focus();
        }
    }
}

addEvent(document,'keydown',PopupKeyCheck);