﻿    function ThumbnailInfo() {
        this.PatentNumber = "";
        this.TargetObject = null;
        this.SelectObject = null;
        this.IsLangChanged = false;
    }
    
    var _displayThumbnail = new ThumbnailInfo();

    function ScreenSize () {
        var doc = ietruebody();
        this.Top = doc.scrollTop;
        this.Bottom = doc.scrollTop + doc.clientHeight;
    }
    function ietruebody(){
        return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    }
    
    function postBackLogin(loginButton, loginAction) {
        var login = $get(loginButton);
        if(login)
            __doPostBack(loginAction,'');
        else
            __doPostBack('','');
    }
    
    function changeAbstractLanguage(lang) {
        var patentNumber = lang +"|"+ _displayThumbnail.PatentNumber;;
        var behavior = $find('ThumbnailDP');
        if (behavior) {
                _displayThumbnail.IsLangChanged = true;    
                behavior.populate(patentNumber);
        }
    }


    var _isTimeout = true;
    function updateThumbnail(who, targetId, patn) { 
        if(_isTimeout) {
            setTimeout("delayEvent()", 200);
            updateThumbnailTimeout(who, targetId, patn);
        }
    }
    function delayEvent() {
        _isTimeout = true;
    }

    function updateThumbnailTimeout(who, targetId, patn) {

        var behavior = $find('ThumbnailDP');
        _displayThumbnail.TargetObject = $get(targetId);
        
        if (_displayThumbnail.TargetObject && behavior)
        {
            _displayThumbnail.IsLangChanged = false;
            this.processEventInfo = function processEventInfo(eventElement) {

                var position = Sys.UI.DomElement.getLocation(who);
                selectTableRow(position, eventElement.target);

                _displayThumbnail.TargetObject.style.display = "block";
                if(_displayThumbnail.PatentNumber != patn) {
                    var srnSize = new ScreenSize();
                    var targetY = position.y - 250;
                    
                    if(targetY < srnSize.Top || (srnSize.Bottom - srnSize.Top)< 510)
                        targetY = srnSize.Top + 1;
                    else if ((targetY  + 523) > srnSize.Bottom)
                        targetY = srnSize.Bottom - 523;
                        
                    if(_isTimeout) {
                        behavior.populate(patn);
                        _displayThumbnail.PatentNumber = patn
                        _isTimeout = false;
                    }
                    Sys.UI.DomElement.setLocation(_displayThumbnail.TargetObject, 765, targetY);
                }
            }
            Sys.UI.DomEvent.addHandler(who, "mousemove", this.processEventInfo);
        }
    }
    
    function selectTableRow(position, who) {
        _displayThumbnail.SelectObject = document.getElementById("selectTr");
        
        if(_displayThumbnail.SelectObject){
            Sys.UI.DomElement.setLocation(_displayThumbnail.SelectObject, 738, position.y + 2);
            
            var parentElement = who.parentNode;
            while(parentElement.tagName != "TR"){
                parentElement = parentElement.parentNode;
            }
            _displayThumbnail.SelectObject.style.height = parentElement.clientHeight + "px";
            _displayThumbnail.SelectObject.style.display = "block";
        }
    }
    
    function hideThumbnail(who) {
        if (_displayThumbnail.TargetObject && _displayThumbnail.SelectObject) {
            this.processEventInfo = function processEventInfo(eventElement) {
                if(eventElement.target.tagName == "DIV" || eventElement.offsetX < 0 || eventElement.offsetY < 0 ){
                    _displayThumbnail.TargetObject.style.display = "none";
                    _displayThumbnail.SelectObject.style.display = "none";
                }
            }
            
            Sys.UI.DomEvent.addHandler(who, "mouseout", this.processEventInfo);
        }
    }
    
    function showThumbnail(who) {
        if (_displayThumbnail.TargetObject && _displayThumbnail.SelectObject) {
            this.showEventInfo = function showEventInfo(eventElement) {
                _displayThumbnail.TargetObject.style.display = "block";
                _displayThumbnail.SelectObject.style.display = "block";
            }
            
            this.hideEventInfo = function hideEventInfo(eventElement) {
                if(!_displayThumbnail.IsLangChanged) {
                    _displayThumbnail.TargetObject.style.display = "none";
                    _displayThumbnail.SelectObject.style.display = "none";
                }
            }
            
            Sys.UI.DomEvent.addHandler(who, "mouseover", this.showEventInfo);
            //if (Sys.Browser.agent == Sys.Browser.InternetExplorer ) {
            //    Sys.UI.DomEvent.addHandler(who, "mouseout", this.hideEventInfo);
            //}
        }
    }

    function pageLoaded(sender, args) {
        _displayThumbnail.PatentNumber = "";
    
        var selectedCount = 0;
        var divList = $get("DocumentListDiv");      // div
        var inputs = divList.getElementsByTagName("input");
        
        var i = 0;
        for(i = 0; i < inputs.length; i++){
            if (inputs[i].type == "checkbox" && inputs[i].checked) {
                selectedCount++;
            }
        }
        DisplayCount(selectedCount);
    }

    function CheckAllCheckboxes(checkbox, radioSelect, radioSearch) {
        var result = new SearchResult(checkbox, radioSelect, radioSearch);
        result.CheckAll();
    }

    function CountChecked(checkbox, radioSelect, radioSearch) {
        var result = new SearchResult(checkbox, radioSelect, radioSearch);
        result.Display();
    }

    function SearchResult(checkboxAll, radioSelect, radioSearch)
    {
        this.selectRadio = $get(radioSelect);
        this.searchRadio = $get(radioSearch);
        this.allCheckBox = $get(checkboxAll);
        this.Display = SearchResult$Dispay;
        this.CheckAll = SearchResult$CheckAll;
    }
    
    function SearchResult$Dispay()  {
        var selectedCount = 0;
        var unSelectCount = 0;

        var divList = $get("DocumentListDiv");      // div
        var inputs = divList.getElementsByTagName("input");
        
        var i = 0;
        for(i = 0; i < inputs.length; i++){
            if (inputs[i].type == "checkbox" && inputs[i].checked) {
                selectedCount++;
            }
            else if (inputs[i].type == "checkbox" && !inputs[i].checked) {
                unSelectCount++;
            }
        }
        
        if(unSelectCount == 0){
            this.allCheckBox.checked = true;
        } else {
            this.allCheckBox.checked = false;
        }
            
        if(!this.searchRadio.disabled && selectedCount == 0) {
            this.searchRadio.checked = true;
            this.selectRadio.checked = false;
        } else {
            this.searchRadio.checked = false;
            this.selectRadio.checked = true;
        }

        DisplayCount(selectedCount);
    }
    
    function SearchResult$CheckAll()  {
        var selectedCount = 0;
        var divList = $get("DocumentListDiv");      // div
        var inputs = divList.getElementsByTagName("input");
        
        var i = 0;
        for(i = 0; i < inputs.length; i++){
            if (inputs[i].type == "checkbox") {
                selectedCount++;
                inputs[i].checked = this.allCheckBox.checked;
            }
        }
        
        if(!this.allCheckBox.checked){
            selectedCount = 0;
        }
            
        if(!this.searchRadio.disabled && !this.allCheckBox.checked) {
            this.searchRadio.checked = true;
            this.selectRadio.checked = false;
        } else {
            this.searchRadio.checked = false;
            this.selectRadio.checked = true;
        }
            
        DisplayCount(selectedCount);
    }
    
    function DisplayCount(count) {
        var countObject = $get("DocSelectedCount");      // span
        if(countObject.innerText)
            countObject.innerText = count;
        else
            countObject.textContent = count;
    }
    
    function ShowPDFFile(strPatn) {
        var newWin = null;
        var tempURL = "Key=" + strPatn + "&Mode=I";;
        newWin = window.open("../DataView/ViewPDF.aspx?" + tempURL, "ImageView","stuts,resizable");
        
        return false;
    }
    
    function ShowFDWindow(object) {
        window.open(object, "FD","width=500,height=550,stuts,resizable,scrollbars=yes");
       
    }

    function ShowPatentEvaluationCallBack(context)
    {
        if(context)
        {
            try
            {
                var idList = context.split('|');
                var hiddenObj = document.getElementById(idList[1]);
                hiddenObj.value = idList[0];

                WebForm_DoPostBackWithOptions(
                    new WebForm_PostBackOptions(
                        idList[2], 
                        "", 
                        false, 
                        "", 
                        "../DataView/ViewPatentEvaluation.aspx", 
                        false, 
                        true)
                );
            }
            catch(e){}
        }
    }
