agent = navigator.userAgent.toLowerCase();
is_safari = agent.indexOf("safari") != -1;
var InstanceCollection = new Array();
var obMask;
var obOpacity;

function Instance(mainPanel, shadowPanel, maskPanel, positionBox, capacityPanel, container, caption, closebutton, autoScroll, positionCenter, moveControl, id)
{
	this.mainPanel = mainPanel;
	this.shadowPanel = shadowPanel;
	this.maskPanel = maskPanel;
	this.positionBox = positionBox;
	this.capacityPanel = capacityPanel;
	this.container = container;
	this.caption = caption;
	this.closebutton = closebutton;
	this.args = null;
	this.callbackFunc = null;
	this.id = id;
	this.moveControl = moveControl;
	this.autoScroll = autoScroll;
	this.positionCenter = positionCenter;
}

function SetModalBoxInstance(mainPanel, shadowPanel, maskPanel, positionBox, capacityPanel, container, caption, closebutton, autoScroll, positionCenter, moveControl, id)
{
	var InstanceObj=null;
	InstanceObj=new Instance(mainPanel, shadowPanel, maskPanel, positionBox, capacityPanel, container, caption, closebutton, autoScroll, positionCenter, moveControl, id);
	InstanceCollection.push(InstanceObj);
}

function GetInstance(id)
{
	var instance = null;
	for(var i=0; i<InstanceCollection.length; i++)
	{
		if(InstanceCollection[i].id == id)
		{
			instance = InstanceCollection[i];
			break;
		}
	}
	return instance;
}

function FindeMainControl(id)
{
	var inst = GetInstance(id);
	if(inst)
	{
	    obMainPanel = document.getElementById(inst.mainPanel);
		obMask = document.getElementById(inst.maskPanel);
		obOpacity = document.getElementById(inst.capacityPanel);
	}
}

function SetSelectsVisibility(visible, selectsContainer, noHideContainer){
    if(!selectsContainer) 
        selectsContainer = window.document;
    var select_arr = selectsContainer.getElementsByTagName("select");
    if(!select_arr) 
        return;        
    var visStr = (visible!=false) ? "visible" : "hidden";    
    for (var i = 0; i < select_arr.length; i++){ 
        if(!select_arr[i].initialVisibility){
            select_arr[i].initialVisibility = select_arr[i].style.visibility;
        }
        if(select_arr[i].initialVisibility=="hidden") 
            continue;
        select_arr[i].style.visibility = visStr;
    }
    
    if(visible)
        return;
    if(!noHideContainer)
        return; 
    
    SetSelectsVisibility(true, noHideContainer, null);
}

function HideSelects(id)
{
    FindeMainControl(id);
    if(document.all && window.ActiveXObject) // is IE
        SetSelectsVisibility( false, null,  obMainPanel);    
}

function ShowSelects()
{
    if(document.all && window.ActiveXObject) // is IE
        SetSelectsVisibility( true );    
}


function DialogStartUp(id)
{
	MaximizeObject(id);
	CenterObject(id);
	SetSameSizeAndPosDefault(id);
}

function ShowModalBoxControl(id)
{
    HideSelects(id);
	FindeMainControl(id);
	CenterObject(id);
	if(obMask&&obOpacity)
	{
		obMask.style.display = 'block';
		obOpacity.style.display = 'block';
		DialogStartUp(id);
		
		obMask.style.display = 'none';
		obOpacity.style.display = 'none';
		obMask.style.display = 'block';
		obOpacity.style.display = 'block';
	}
	obMask = null;
	obOpacity = null;
}

function HideModalBoxControl(id)
{
	FindeMainControl(id);
	if(obMask&&obOpacity)
	{
		obMask.style.display = 'none';
		obOpacity.style.display = 'none';
	}
	obMask = null;
	obOpacity = null;
	ShowSelects();
}



function GetContentFrame(id, url, scroll)
{
	var autoScroll = "";
	var scrolling = "";
	
	if (scroll == true)
	{
		autoScroll = " overflow: auto;";
	}
	else
	{
		scrolling = " scrolling='no'";
	}
	var result = "<iframe name='ModalBoxContentFrame' frameborder='0px'" + scrolling + " style='border: 0px solid black;" + autoScroll + "' id='ModalBoxContentFrame" + id + "' src='" + url + "' border='no' width='100%' height='100%' ></iframe>";
	return result;
}

function SetContent(id, url)
{
	var inst =  GetInstance(id);

	if(inst)
	{
		var container = document.getElementById(inst.container);
		if(container)
		{
		    if(is_safari && container.firstChild && container.firstChild.tagName.toLowerCase()=="iframe") {  
		       //assume our iframe here    
		       container.firstChild.setAttribute("src", url)
		       container.firstChild.setAttribute("id", "ModalBoxContentFrame" + id );
		       
               if (inst.autoScroll == true)
               {
	                 container.firstChild.style.overflow = 'auto';
	                 container.firstChild.removeAttribute("scrolling");
               }
               else
               {
	                 container.firstChild.setAttribute("scrolling", "no");
	                 container.firstChild.style.overflow = '';
               }
              
		    }
		    else {
			    container.innerHTML = GetContentFrame(id, url, inst.autoScroll);
			}
		}
	}
}

function InitModalBoxControl(id, url)
{

	ShowModalBoxControl(id);
	
	SetContent(id, url);
	
	if(is_safari) {
	    HideModalBoxControl(id);
	    ShowModalBoxControl(id);
	}

}



function SetCaption(id, str)
{
	var inst =  GetInstance(id);
	if(inst)
	{
		var caption = document.getElementById(inst.caption);
		if(caption)
		{
			caption.innerHTML = str;
		}
	}
}

function SetArguments(id, args)
{
	var inst =  GetInstance(id);
	if(inst)
	{
		inst.args = args;
	}
}

function SetCallbackFunc(id, func)
{
	var inst =  GetInstance(id);
	if(inst)
	{
		inst.callbackFunc = func;
	}
}

function GetArguments(id)
{
	var inst =  GetInstance(id);
	if(inst)
	{
		return inst.args;
	}
	return null;
}

function CloseModalBox(id, args)
{
	HideModalBoxControl(id);
	var inst =  GetInstance(id);
	if(inst)
	{
		if(inst.callbackFunc)
		{
			inst.callbackFunc(args);
		}
	}
	//HideModalBoxControl(id);
}


function HideModalBoxCloseButton(id)
{
	var inst =  GetInstance(id);
	if(inst && inst.closebutton)
	{   
        var closebutton = document.getElementById(inst.closebutton);
        if (closebutton) 
        {
            closebutton.style.display = "none";
        }
	}
}

function ShowModalBoxCloseButton(id)
{
	var inst =  GetInstance(id);
	if(inst && inst.closebutton)
	{   
        var closebutton = document.getElementById(inst.closebutton);
        if (closebutton) 
        {
            closebutton.style.display = "";
        }
	}
}

function SetModalBoxCloseFunction(id, func)
{
    var inst =  GetInstance(id);
	if(inst && inst.closebutton)
	{   
        var closebutton = document.getElementById(inst.closebutton);
        if (closebutton) 
        {
            closebutton.onclick = func;
        }
	}
}

// Functions for wrapping in ModalBoxJsUtility (C# class) =======================
//ModalBoxId js variable must be defined in document
function sb_OpenNewDialog(url, width, height, callbackFunction){
    var id = ModalBoxId;
    //var args = null;
    //top.SetArguments(id, args);
    SetCaption(id, '');
    SetCallbackFunc(id, callbackFunction);
    SetHeightWidth(id, height, width);
    InitModalBoxControl(id, url);
}

function sb_CloseLastDialog(args){
    var id = ModalBoxId;
	//HideModalBoxControl(id);
    CloseModalBox(id, args);
}

function sb_SetHeightWidth(height, width){
    var id = ModalBoxId;
    SetHeightWidth(id, height, width);
}
function sb_MoveToCenter(){
    var id = ModalBoxId;
    CenterObject(id); // function from SizeUtil.js
    SetSameSizeAndPosDefault(id); // function from SizeUtil.js
}

