// QUpload.js
var QUB = new QUploadObject();

function QUploadObject(){
    this.Cancel = function(ChildCtl){
        var btn = new QUploadButton(ChildCtl);
        btn.Cancel();
    }
}
var iUploadQueue = 0;
var QUploadPosts = new QDictionary("QUploadPosts");

function QUploadButton_BeginPolling(ctlID,FileIndex){
    var btn = new QUploadButton(document.getElementById(ctlID));
    btn.$ProgressContainer.QShow();
    //$('#'+ctlID+'_progressContainer').QShow();
    btn.$iFrame.css('visibility','hidden');
    //$('#'+ctlID+' iframe').css('visibility','hidden');
    //var PostID = $('#'+ctlID).QGetSetting('PostID');
    var PostID = btn.PostID(); //$('#'+ctlID).QGetSetting('PostID');
    var fn = "QUploadButton_GetCurrentStatus('"+ctlID+"','"+PostID+"',"+FileIndex+")";
    var IntervalID = window.setInterval(fn, 1000);
    btn.IntervalID(IntervalID);
    //$('#'+ctlID).QChangeSetting('IntervalID',IntervalID);
    return PostID;
}

function QUploadButton(ChildCtl){
    if(ChildCtl){
        this.element = FindParentFromClass(ChildCtl,"QUploadButton");
        this.$element = $(this.element);
        this.ClientID = this.$element.attr('id');
        this.UniqueID = function(){ return this.$element.QGetSetting("UniqueID"); }
        
        // DECLARATIONS
        this.$iFrame = $("iframe",this.$element);
        this.iFrame = this.$iFrame.get(0);
        this.$ProgressContainer = $(".QUploadButton_Progress",this.$element);
        this.$ProgressBar = $(".QUploadButton_progressBar",this.$element);
        this.$ProgressDetails = $(".QUploadButton_ProgressDetails",this.$element);
        
        // PROPERTIES
        this.AllowUploadAgain = function(){return this.$element.QGetSetting("AllowUploadAgain");}
        this.AutoSubmitMode = function(){return this.$element.QGetSetting("AutoSubmitMode");}
        this.CurrentFileIndex = function(i){
            if (typeof(i) != 'undefined'){ this.$element.QChangeSetting("CurrentFileIndex",i); 
            }else{ return this.$element.QGetSetting("CurrentFileIndex"); }            
        }
        this.IntervalID = function(i){
            if (typeof(i) != 'undefined'){ this.$element.QChangeSetting("IntervalID",i); 
            }else{ return this.$element.QGetSetting("IntervalID"); }            
        }
        this.PostID = function(s){
            if (isDefined(s)){ this.$element.QChangeSetting("PostID",s); 
            }else{ return this.$element.QGetSetting("PostID"); }
        }
        
        // METHODS
        this.Cancel = function(){
            if (1==1){}
            if (1==1){}
            this.CurrentFileIndex(this.GetNewIndex());
            this.$ProgressContainer.QHide();
            this.$ProgressBar.css("width","1px")
            $(this.iFrame).css('visibility','visible');
            var src = "QWebControls/QUploadButton.aspx?ctlID=" + this.ClientID + "&Index=" + this.CurrentFileIndex();
            this.iFrame.src = src;
        }
        this.GetNewIndex = function(){
            var Post = QUploadPosts.Item(this.PostID());
            Post.FileIndex = Post.FileIndex + 1;
            return Post.FileIndex;
        }
    }
}

function QUploadButton_GetCurrentStatus(ctlID,PostID,FileIndex){
    iUploadQueue++;    //  Need to update the call each time to avoid IIS caching
    if (QUploadPosts.Contains(PostID)){
        QUploadPosts.Item(PostID).i = iUploadQueue;
    }else{
        QUploadPosts.Add(PostID,{i:iUploadQueue,TotalBytes:null,Transferred:null,ControlID:ctlID,FileIndex:FileIndex});
    }
    //$.getJSON(iUploadQueue+'quploadbutton_status.aspx?PostID='+PostID,function(data){QUploadButton_DataResponse);
    $.ajax({
      url: iUploadQueue+'quploadbutton_status.aspx?PostID='+PostID+'&Index='+FileIndex,
      cache: false,
      success: function(response){
        eval('var data ='+response);
        QUploadButton_DataResponse(data);
      }
    });
    
}

function QUploadButton_DataResponse(data){
    var Post = QUploadPosts.Item(data.PostID);
    if (Post.NewPostID == null){Post.NewPostID = data.NewPostID;}
    QUploadButton_StatusResponse(Post.ControlID,data);
}
function QUploadButton_StatusResponse(ctlID,result)
{
    var WaitingForUpdate = false;
    var CurrentStatusObject = $('#'+ctlID+'_CurrentStatus').get(0);
    var TransferedBytes = $('#'+ctlID+'_TransferedBytes').get(0);
    var TotalBytes = $('#'+ctlID+'_TotalBytes').get(0);
    var ProgressContainer = $('#'+ctlID+'_progressContainer').get(0);
    var ProgressBar = $('#'+ctlID+'_progressBar').get(0);
    // Update Status Information:
    try {
        if(result == null) { alert('result is null');}
        else if(ProgressBar == null){
            //alert('ProgressBar has not been set');
            return;
        }else{
            var PercentDone = (result.CurrentBytesTransfered / result.TotalBytes);

            CurrentStatusObject.innerHTML = "Uploading..."
            TransferedBytes.innerHTML = (parseInt(PercentDone*100))+'%';
            //TotalBytes.innerHTML = result.TotalBytes;

            var ContainerWidth = parseInt(ProgressContainer.style.width);
            var NewWidth = (PercentDone * ContainerWidth) -2;
            if (NewWidth>0) {
                $(ProgressBar).animate({width: NewWidth+'px'},500); }

            if(result.CurrentBytesTransfered == result.TotalBytes) {
                if(result.CurrentBytesTransfered != 0) {
                    // stop the loop. no need to get any more updates.
                    var IntervalID = $('#'+ctlID).QGetSetting('IntervalID');
                    clearInterval(IntervalID);
                    CurrentStatusObject.innerHTML = "Finished";
                    var AutoSubmitMode = $('#'+ctlID).QGetSetting('AutoSubmitMode')
                    if(AutoSubmitMode == 'AutoPostBack'){
                        var fn = '__doPostBack($("#'+ctlID+'").QGetSetting("UniqueID"),"UploadComplete")';
                        setTimeout(fn,500);
                    }else if(AutoSubmitMode == 'AutoCallBack'){
                        QCore.AutoSubmit.CallBack($('#'+ctlID).QGetSetting('UniqueID'),$('#'+ctlID).QGetSetting(),'Changed',null);
                    }
                }
            }
        }
    }
    catch(e){}
}



