//Needs Common.js



function ResetPageScroll(  ){	
	window.scroll( 0, 0 );
}

//=========================================================================================================//	


function OpenFileManagerStdConsole(folderDepth, location){
    //alert("location: " +location);
    if( folderDepth==null ) folderDepth = "";    

    var windowName  = "FileManager";
	var xloc        = (window.screen.availWidth-width)/2; 
	var yloc        = (window.screen.availHeight-height)/2; 
	var width       = 910;
	var height      = 600;
	var scrollable  = false;
	var resizeble   = false;
	
	var windowLoc       = (location!=null && location!="") ? location : folderDepth + "Files/FileManager.aspx";
	
	var win = window.open( windowLoc , windowName, "height="+height+", width="+width+", top="+yloc+", left="+xloc+", scrollbars=" + ( scrollable ? 1 : 0 ) + ", menubar=0, location=0, resizable=" + (resizeble ? 1 : 0 ) + ", status=0");
    win.focus();
    return false;
}

function OpenFileManagerConsole( fileTypesToShow/*{1:Images, 2:Video, 0:All }*/, openAsStdConsole/*{0:No, 1:Yes }*/, extraQueryParams/*always start with &*/, onCloseEvent, folderDepth ){	
    //alert("Called OpenFileManagerConsole, fileTypesToShow: "+ fileTypesToShow + ", openAsStdConsole: "+ openAsStdConsole +", extraQueryParams: "+ extraQueryParams +", onCloseEvent: "+ (onCloseEvent==null ? "null" : onCloseEvent.toString() ) );
    
    if( fileTypesToShow==null ) fileTypesToShow = 0;
    if( openAsStdConsole==null ) openAsStdConsole = true;    
    if( folderDepth==null ) folderDepth = "";    
    
	var windowLoc       = folderDepth + "Files/FileManager.aspx";
	
	windowLoc           += "?std=" + openAsStdConsole;
	windowLoc           += "&type=" + fileTypesToShow;
	windowLoc           += extraQueryParams
    
    
    var width           = 910;
    var height          = 593;
    
    var useParent       = true;    
    //var coordinates   = GetMousePos();        
    var functionCall    = useParent && parent!=null ? parent.OpenFloatWindow : OpenFloatWindow;    
    
    
    functionCall(  
        "win_filemanager_console",//windowId
        "alphacube", //theme
        windowLoc, //url
        "Community File Manager", //title
        75,//coordinates.y - 50, //top
        275,//coordinates.x + 50, //left
        width + 3,
        height + 3,
        false,//resizable
        true,//closable
        true,//draggable
        true,//destroyOnClose
        false,//minimizable
        false,//maximizable
        0,//affectDuration
        function OnCloseEvent( resultObj ){
            if(resultObj==null ){
                resultObj = {HasChanged:false};
            }
            if(onCloseEvent!=null) onCloseEvent( resultObj );
        }
        //onCloseCallBackFunction --> can be used to send back parameters just like when dealing with an modal dialog
    );
   	
	return false;
}

function OpenFileManager( fileTypesToShow/*{1:Images, 2:Video, 0:All }*/, /*string*/fileContainerId, /*string*/fileHiddenfieldContainerId, /*int*/width, /*int*/height, folderDepth ){
    ResetPageScroll();
    OpenFileManagerConsole( 
        fileTypesToShow, 
        0, 
        "&width=" + width + "&height=" + height,
        function OnCloseEvent( resultObj ){
            //alert("OpenFileManager");
            var result = "";
            if(resultObj==null) return result;
            if(!resultObj.HasChanged ) return result;
            
            //alert("OpenFileManager 2");
            if( resultObj.SelectedFiles.length>0 ){
		        var hfContainer    = GetObj(fileHiddenfieldContainerId);
		        var fileContainer  = GetObj(fileContainerId);
		        
		        //alert("FileClick Uri: "+ resultObj.SelectedFiles[0].Uri + "\nRelativePath: "+ resultObj.SelectedFiles[0].RelativePath + "\nDemandedUri: "+ resultObj.SelectedFiles[0].DemandedUri );
    		            
	            hfContainer.value   = resultObj.SelectedFiles[0].RelativePath;
	            fileContainer.src   = resultObj.SelectedFiles[0].DemandedUri;	
	            
	            //alert("ResizeParent: " + ResizeParent);	            
	            //if(ResizeParent!=null)ResizeParent();	        
		    }
        },
        folderDepth
    );
    
    return false;  
}

function OpenFileManagerWithCustomHandler( fileTypesToShow/*{1:Images, 2:Video, 0:All }*/, /*function*/callbackFunction, folderDepth ){
    //alert("0.OpenFileManagerWithCustomHandler.callbackFunction");
    //alert("1.OpenFileManagerWithCustomHandler.callbackFunction: " +callbackFunction);
    ResetPageScroll();
    OpenFileManagerConsole( 
        fileTypesToShow, 
        0, 
        "",
        function OnCloseEvent( resultObj ){
            //alert("2.OpenFileManagerWithCustomHandler.callbackFunction: " +callbackFunction);
            var result = "";
            if(resultObj==null) return result;
            if(!resultObj.HasChanged ) return result;
            
            //alert("OpenFileManagerWithCustomHandler 2: " + resultObj.SelectedFiles.length );
            if( resultObj.SelectedFiles.length>0 ){                
                callbackFunction(resultObj.SelectedFiles);  
		    }
        },
        folderDepth
    );
    
    return false;  
}


function OpenFileManagerFolderSelect( /*string*/folderContainerId, folderHiddenfieldContainerId, folderDepth ){
    ResetPageScroll();
    var fileTypesToShow = 3;/*{1:Images, 2:Video, 3:Folder, 0:All }*/
    
    OpenFileManagerConsole( 
        fileTypesToShow, 
        0, 
        "",
        function OnCloseEvent( resultObj ){
            //alert("OpenFileManagerFolderSelect: " + resultObj);
            var result = "";
            if(resultObj==null) return result;
            //alert("OpenFileManagerFolderSelect HasChanged: " + resultObj.HasChanged);
            if(!resultObj.HasChanged ) return result;
            //alert("OpenFileManagerFolderSelect SelectedFolder: " + resultObj.SelectedFolder);
            if(resultObj.SelectedFolder!=null){                
                var folderContainer     = GetObj(folderContainerId);
                var hfContainer         = GetObj(folderHiddenfieldContainerId);
		        
    		    //alert("OpenFileManager: " + resultObj.SelectedFolder );
	            hfContainer.value       = resultObj.SelectedFolder;	
	            folderContainer.value   = resultObj.SelectedFolder;	
            }
        },
        folderDepth
    );
    
    return false;  
}

/*
    Function is used by a flash movie-object that want's to use the filemanager
*/
function OpenFileManagerForMx( fileTypesToShow/*{1:Images, 2:Video, 0:All }*/, mxMovieId/*id of flash movie so that we know wich movie to invoke*/, folderDepth ){   
    //alert("Called OpenFileManagerForMx, fileTypesToShow: "+ fileTypesToShow + ", mxMovieId: "+ mxMovieId + ", folderDepth: " + folderDepth  );
    OpenFileManagerConsole( fileTypesToShow, 0, "",
        function OnCloseEvent( resultObj ){
            var result = "";
            if(resultObj==null) return;
            if(!resultObj.HasChanged ) return;
            
            //alert("resultObj.SelectedFiles.length: "+ resultObj.SelectedFiles.length );
            
            try{
                if( resultObj.SelectedFiles.length>0 ){    	        	            
	                for( var i=0; i<resultObj.SelectedFiles.length; i++ ){
	                    if(i>0) result += "|";
	                    
        	            //TraceObj("File["+i+"]",resultObj.SelectedFiles[i]);
        	                    	            
	                    result += resultObj.SelectedFiles[i].RelativePathtoString(); // Id.toString();
	                    result += "," + resultObj.SelectedFiles[i].FileName.toString();
	                    result += "," + resultObj.SelectedFiles[i].Ext.toString();
	                    result += "," + resultObj.SelectedFiles[i].DemandedUri.toString();
	                    result += "," + resultObj.SelectedFiles[i].FileName.toString();	 	                                      
	                }
		        }
		    }catch(exception){
                TraceObj("Parse BuildUp ERROR",exception);
            }
            //alert("F4: " + mxMovieId );
            try{                
                var mxObj = GetMxObj(mxMovieId);
                //alert("F5: " + mxObj );
                if( mxObj!=null && result!="" ){
                //alert("F6: " + mxObj.AddFiles);          
                    //call flash-movie trigger to assign selected files                    
                    mxObj.AddFiles(result);
                }
            }catch(exception){
                TraceObj("AddFiles ERROR",exception);
            } 
            //alert("F7 END " );           
        },
        folderDepth
    );
}

/*
functions is used by the image-/file-archive to store the selected images-/files
*/
function SetFileArchiveMem(/*string*/fileHiddenfieldContainerId, /*string*/mappedIds){
    var hfContainer     = GetObj(fileHiddenfieldContainerId);	
	hfContainer.value  = mappedIds;
}

function ClearFile(/*string*/fileContainerId, /*string*/fileHiddenfieldContainerId, /*string*/emptyPixUri, /*bool*/isImage){
    var hfContainer    = GetObj(fileHiddenfieldContainerId);
	var fileContainer  = GetObj(fileContainerId);
	
	hfContainer.value  = "";
    if(isImage)fileContainer.src = emptyPixUri;
    
  //  if(ResizeParent!=null)ResizeParent();	
    
    return false;
}

function ClearFolder(/*string*/folderContainerId, folderHiddenfieldContainerId){
    var hfContainer         = GetObj(folderHiddenfieldContainerId);
    var folderContainer     = GetObj(folderContainerId);
	
	folderContainer.value   = "";    
	hfContainer.value       = "";
    return false;
}

//=========================================================================================================//

var __FloatingWindows__ = new Array();
function OpenFloatWindow(  
    windowContainerId,
    theme,
    url, 
    title, 
    top, 
    left, 
    width, 
    height,
    resizable,
    closable,
    draggable,
    destroyOnClose,
    minimizable,
    maximizable,
    affectDuration,
    onCloseCallBackFunction /*can be used to send back parameters just like when dealing with an modal dialog*/,
    refreshUrlIfExists, /*refreshed the window url with new given url*/
    openAsModalWindow /*doesn't works so great with dropdowns etc... in modal state they are still clickable by the user*/
){
    //alert("1.OpenFloatWindow: " + windowContainerId + ", exists: " + __FloatingWindows__[windowContainerId] );
try{    
    if( openAsModalWindow==null ) openAsModalWindow = false;
    
    if( __FloatingWindows__[windowContainerId]!=null ){ 
        //alert("__FloatingWindows__[windowContainerId].Exists: "+ __FloatingWindows__[windowContainerId].Exists );
        if(__FloatingWindows__[windowContainerId].Exists){       
            if( __FloatingWindows__[windowContainerId].IsOpen ){
                __FloatingWindows__[windowContainerId].win.hide();
                __FloatingWindows__[windowContainerId].IsOpen = false;
                return false;
            }else{   
                //check if the window was destoyed
                if( !__FloatingWindows__[windowContainerId].destroyOnClose ){         
                    if(refreshUrlIfExists){
                        __FloatingWindows__[windowContainerId].win.options.url = url;
                        __FloatingWindows__[windowContainerId].win.refresh();
                    }
                    
                    __FloatingWindows__[windowContainerId].win.show(openAsModalWindow);
                    __FloatingWindows__[windowContainerId].IsOpen = true;
                    return false;
                }
            }
        }       
    }
    
    __FloatingWindows__[windowContainerId]                   = new Object();
    __FloatingWindows__[windowContainerId].closeCallback     = onCloseCallBackFunction;
    __FloatingWindows__[windowContainerId].destroyOnClose    = destroyOnClose;
    
    __FloatingWindows__[windowContainerId].win               = new Window({
        className:theme==null ? "alphacube" : (theme=="" ? "alphacube" : theme ),
        url:url,
        title:title,
        top:top,
        left:left,
        width:width,
        height:height,
        resizable:resizable,
        closable:closable,
        draggable:draggable,
        destroyOnClose:destroyOnClose,
        minimizable:minimizable,
        maximizable:maximizable,
        showEffectOptions:{
            duration:affectDuration
        },
        closeCallback:OnCloseFloatWindow,
        
        showEffect:affectDuration>0 ? Effect.Appear : Element.show,
        hideEffect:affectDuration>0 ? Effect.Fade : Element.hide
    });    
    
    __FloatingWindows__[windowContainerId].win.show(openAsModalWindow);    
    __FloatingWindows__[windowContainerId].IsOpen        = true;
    __FloatingWindows__[windowContainerId].Id            = __FloatingWindows__[windowContainerId].win.getId();    
    __FloatingWindows__[windowContainerId].ContentId     = __FloatingWindows__[windowContainerId].Id + "_content";
    __FloatingWindows__[windowContainerId].ContainerId   = windowContainerId;
    __FloatingWindows__[windowContainerId].Exists        = true;
    
}catch(exception){
    TraceObj("OpenFloatWindow ERROR",exception);
}    
    
    return false;
}


function OpenDivInFloatWindow(  
    windowContainerId,
    theme,
    divId, 
    title, 
    top, 
    left, 
    width, 
    height,
    resizable,
    closable,
    draggable,
    destroyOnClose,
    minimizable,
    maximizable,
    affectDuration,
    onCloseCallBackFunction /*can be used to send back parameters just like when dealing with an modal dialog*/,
    dontCloseIfExits/*zorgt ervoor dat de window niet wordt gesloten als het al ge-opend is.*/
){
try{
    //alert("1.OpenFloatWindow: " + windowContainerId + ", exists: " + (__FloatingWindows__[windowContainerId]!=null) );
    destroyOnClose = false;//override [destroyOnClose], we don't want to delete existing content on a page
    if(dontCloseIfExits==null)dontCloseIfExits = false;
    
    if( __FloatingWindows__[windowContainerId]!=null ){ 
        //alert("__FloatingWindows__[windowContainerId].Exists: "+ __FloatingWindows__[windowContainerId].Exists );
        if(__FloatingWindows__[windowContainerId].Exists){       
            if( __FloatingWindows__[windowContainerId].IsOpen && !dontCloseIfExits ){
                __FloatingWindows__[windowContainerId].win.hide();
                __FloatingWindows__[windowContainerId].IsOpen = false;
            }else{
                __FloatingWindows__[windowContainerId].win.show();
                __FloatingWindows__[windowContainerId].IsOpen = true;
            }
            return false;
        }       
    }
    
    __FloatingWindows__[windowContainerId]                   = new Object();
    __FloatingWindows__[windowContainerId].closeCallback     = onCloseCallBackFunction;
    __FloatingWindows__[windowContainerId].win               = new Window({
        className:theme==null ? "alphacube" : (theme=="" ? "alphacube" : theme ),
        url:null,
        title:title,
        top:top,
        left:left,
        width:width,
        height:height,
        resizable:resizable,
        closable:closable,
        draggable:draggable,
        destroyOnClose:destroyOnClose,
        minimizable:minimizable,
        maximizable:maximizable,
        showEffectOptions:{
            duration:affectDuration
        },
        closeCallback:OnCloseFloatWindow,
        
        showEffect:affectDuration>0 ? Effect.Appear : Element.show,
        hideEffect:affectDuration>0 ? Effect.Fade : Element.hide
    });    
    
    
    __FloatingWindows__[windowContainerId].win.setContent(divId, false/*autoresize*/, false/*autoposition*/);     
    __FloatingWindows__[windowContainerId].win.show();    
    
    __FloatingWindows__[windowContainerId].IsOpen        = true;
    __FloatingWindows__[windowContainerId].Id            = __FloatingWindows__[windowContainerId].win.getId();    
    __FloatingWindows__[windowContainerId].ContentId     = __FloatingWindows__[windowContainerId].Id + "_content";
    __FloatingWindows__[windowContainerId].ContainerId   = windowContainerId;
    __FloatingWindows__[windowContainerId].Exists        = true;
    
}catch(exception){
    TraceObj("OpenDivInFloatWindow ERROR",exception);
}    
    return false;
}

function CloseDivInFloatWindow(windowContainerId){
    if( __FloatingWindows__[windowContainerId]!=null ){ 
        if(__FloatingWindows__[windowContainerId].Exists){       
            if( __FloatingWindows__[windowContainerId].IsOpen ){
                __FloatingWindows__[windowContainerId].win.hide();
                __FloatingWindows__[windowContainerId].IsOpen = false;
            }            
        }       
    }
    return false;
}


function OnCloseFloatWindow(winObj){    
    //alert("OnCloseFloatWindow::" + winObj.getId() );    
    var temp = GetFloatWindowObjById( winObj.getId() );   
    //TraceObject("temp.DialogResult", temp.DialogResult);    
    //TraceObject( "OnCloseFloatWindow", winObj );
    
    
    
      
    if( temp.closeCallback!=null ){
        temp.closeCallback( temp.DialogResult );
        temp.DialogResult = null; //clear result;
    }     
    
    if( temp.destroyOnClose ){
        //alert("1.Do DestroyOnClose: " + temp.destroyOnClose +", temp.ContainerId: " + temp.ContainerId + ", temp.Exists: "+ temp.Exists);
        DestroyFloatWindowObjById(winObj.getId());
        //alert("2.Do DestroyOnClose: " + temp.destroyOnClose +", temp.ContainerId: " + temp.ContainerId + ", temp.Exists: "+ temp.Exists);
    }else{
        winObj.hide();        
    }
    
    temp.IsOpen    = false;
}

function DestroyFloatWindowObjById(windowId){    
    for( var index in __FloatingWindows__ ){
        if( __FloatingWindows__[index].Id==windowId ){
            try{
            __FloatingWindows__[index].win.forceClose();
            }catch(exception){
                TraceObject("DestroyFloatWindowObjById",exception);
            }
            __FloatingWindows__[index].Exists = false;
        }
    }
}

function GetFloatWindowObjByContentId(windowContentId){    
    for( var index in __FloatingWindows__ ){
        if( __FloatingWindows__[index].ContentId==windowContentId ){
            return __FloatingWindows__[index];
        }
    }
    
    return null;
}

function GetFloatWindowObjById(windowId){    
    for( var index in __FloatingWindows__ ){
        if( __FloatingWindows__[index].Id==windowId ){
            return __FloatingWindows__[index];
        }
    }
    
    return null;
}

function GetFloatWindowByContainerId(windowId){        
    return __FloatingWindows__[windowId];
}

function HideFloatWindowByContainerId(windowId){        
    var container = GetFloatWindowByContainerId( windowId );
    if( container!=null ){            
        if( container.IsOpen ){
            container.win.hide();
            container.IsOpen = false;
        }
//        else{
//            container.win.show();
//            container.IsOpen = true;
//        }
    }
    
    return false;
}

function CloseFloatWindow( resultObj, setResult ){  
    if(setResult==null) setResult = true;
    
    if(parent!=null){
        if(parent.GetFloatWindowObjByContentId==null) return false;
        //alert("parent.GetFloatWindow: "+ parent.GetFloatWindowObjByContentId );
        var temp = parent.GetFloatWindowObjByContentId( window.name );
        if(temp==null)return false;
        
        if( setResult ) temp.DialogResult = resultObj;
    
        parent.OnCloseFloatWindow( temp.win );
    }else{
        //content shown in a div -- TODO
//        alert("this.GetFloatWindow: "+ GetFloatWindowObjByContentId );
//        var temp = GetFloatWindowObjByContentId( window.name );
//        temp.DialogResult = resultObj;
//    
//        OnCloseFloatWindow( temp.win );
    }
    
    return false;
}

function SetFloatWindowResult( resultObj ){  

    if(parent!=null){        
        var temp = parent.GetFloatWindowObjByContentId( window.name );
        temp.DialogResult = resultObj;
    }else{
        var temp = GetFloatWindowObjByContentId( window.name );
        temp.DialogResult = resultObj;
    }
    
    return false;
}
//=========================================================================================================//