﻿function DropDown()
{
    this.lockWait = false;
    this.listSecectIndex = -1;
    this.findPosition = function(oElement)
    {
		extPos = this.extDetailPosition();
        if (typeof (oElement.offsetParent) != 'undefined')
        {
            for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent)
            {
                posX += oElement.offsetLeft;
                posY += oElement.offsetTop;
            }
            return [posX - extPos[0], posY - extPos[1]];
        } else
        {
            return [oElement.x - extPos[0], oElement.y - extPos[1]];
        }
    };
	
	// vracia poziciu ext okna ak neexistuje tak [0,0]
	this.extDetailPosition = function()
	{
		if(typeof(ExtDetailColection[ExtDetailColection.length-1]) != "undefined")
		{ 
			var vyskaHlavicky = 27;
			var sirkaOkraja = 7;
			return [ExtDetailColection[ExtDetailColection.length-1].x + sirkaOkraja, ExtDetailColection[ExtDetailColection.length-1].y + vyskaHlavicky];
		}
		else
		 return [0,0];
	};

    this.skryList = function(IDorOBJ)
    {
        var list = null;
        if(typeof(IDorOBJ) == 'string')
            list = getE('list' + IDorOBJ);
        else
            list = IDorOBJ;
        if(list != null)
        {    
			list.style.display = 'none';
			list.scrollTop = 0;
        }
            
    };

    // nastala zmena v inpute a vola sa webservis
    this.zmena = function(ID)
    {
        a = this.findPosition(getE(ID));
        getE('list' + ID).style.left = a[0] + 'px';
        getE('list' + ID).style.top = (a[1] + 20) + 'px';
        this.startCount = 0;
        this.cakanieScroll = false;
        this.itIsAll = false;
        this.scrollTop = 0;
		var thisData = getE(ID).koderSettings;
		
        if (getE('list' + ID).style.display == 'block')
        {
            this.skryList(ID);
        }
        else
        {
            document.body.onclick = function() { CisDataField.skryList(ID); };
            // volanie webservisu
            text = '';
            if (getE(ID + 'Val').value == '')
            {
                text = getE(ID).value;
            }

            Sys.Net.WebServiceProxy.invoke('Moduly/WebServices/CisKoder.asmx', 'GetDataHTML', false, { "textSearch": text, "startCount": 0, "maxCount": thisData.maxCount, "selectedID": thisData.selectId, "selectedIDKP": thisData.conditionId, "clientID": ID, "extraParam": thisData.extraParam }, this.PutIn, this.Failed, null);
            // koniec volania web servisu        
        }
    };

    this.PutIn = function(resp)
    {
        if (resp.selectedID == '')
        {
            getE('list' + resp.id).innerHTML = resp.html;
            getE('list' + resp.id).style.display = 'block';
            // nastavenie sirky listu
            getE('list' + resp.id).style.width = getE(resp.id).clientWidth + 'px';
            // nastavenie vysky listu
            getE('list' + resp.id).style.height = '250px'; // zakladna vyska listu
            lis = getE('list' + resp.id).getElementsByTagName('a');
            if (getE('list' + resp.id).clientHeight > lis[lis.length - 1].offsetTop)
            {
                if(lis[lis.length - 1].offsetTop > 25)
                    getE('list' + resp.id).style.height = lis[lis.length - 1].offsetTop + 20;
                CisDataField.itIsAll = true;
            }
        }
        else // som koderom
        {
            CisDataField.koder(resp.selectedID, resp.conditionID, resp.id);
        }

    };

    this.PutInAdd = function(resp)
    {
        getE('list' + resp.id).innerHTML += resp.html;
        getE('list' + resp.id).style.display = 'block';
        CisDataField.itIsAll = resp.itIsAll; 
    };
    
    // pouzita v CisKoder.cs
    this.SetValue = function(value, id, clientID)
    {
        var input = getE(clientID);
        input.value = value;
        getE(clientID + 'Val').value = id;
        this.skryList(clientID);
        if (getE(clientID + 'Val').onchange)
        {
            var onChangeFunction = getE(clientID + 'Val').onchange;
            onChangeFunction();
        }
        /* ak mam zadanu akciu na onselection */
        if(event.type == "click" && input.onkeypress != null)
        {
            var keypress = input.onkeypress.toString();
            keypress = keypress.substring(keypress.indexOf('= 13 )')+6); 
            keypress = keypress.substring(0,keypress.length-2);
            eval(keypress); 
        }
    };
    this.Failed = function(error)
    {
        if (barISPage.adminMode == 1 || barISPage.debug == 1)
        {
            alert('ADMIN:' + error.get_message());
        }
        else
        {
            alert('Chyba požiadavky,ak problém pretrvá kontaktujte administrátora.');
        }
    };
    this.MoveIntoList = function(ListObj, Event)
    {
        var zoznam = ListObj.getElementsByTagName('a');
        if(zoznam.length > 0)
        {
            var action = '';
            switch(Event.keyCode)
            {
                case 38:
                    action = 'hore';
                break;
                case 40:
                    action = 'dole';
                break;
                case 13:
                    action = 'vstup';
                break;
                default :
                    if(this.listSecectIndex != -1)
                    {
                        zoznam[this.listSecectIndex].style.backgroundColor = "";
                        this.listSecectIndex = -1;
                    }
                
            }
            if(action == 'vstup' && this.listSecectIndex != -1)
            {
                if(zoznam[this.listSecectIndex].onclick != null)
                {
                    var runClick = zoznam[this.listSecectIndex].onclick.toString();
                    runClick = runClick.substring(runClick.indexOf(')')+1);
                    eval(runClick);
                    //zoznam[this.listSecectIndex].click();
                    zoznam[this.listSecectIndex].style.backgroundColor = "";
                    this.skryList(ListObj);
                    this.listSecectIndex = -1;
                }
            }
            else
            {
                if(this.listSecectIndex == -1)
                {
                    if(action == 'dole')
                    {
                        zoznam[1].style.backgroundColor = "grey";
                        this.listSecectIndex = 1;
                    }
                    else
                        if(action == 'hore')
                        {
                            zoznam[zoznam.length-1] = "grey";
                            this.listSecectIndex = zoznam.length-1;
                        }
                        
                }
                else
                {
                    var index = 0;
                    var scrollMe = 0;
                    if(action == 'dole')
                    {
                        if(this.listSecectIndex == zoznam.length-1)
                        {
                            index = 0;
                            if(zoznam[index].offsetTop == 0)
                            {
                                scrollMe = 0; 
                                ListObj.scrollTop = scrollMe;
                            }
                        }
                        else
                        {
                            index = this.listSecectIndex+1; 
                            if(zoznam[index].offsetTop >= ListObj.offsetHeight)
                            {
                                scrollMe = ListObj.scrollTop + zoznam[index].offsetHeight;
                                ListObj.scrollTop = scrollMe;
                            }
                        }   
                        zoznam[this.listSecectIndex].style.backgroundColor = "";
                        zoznam[index].style.backgroundColor = "grey";
                        this.listSecectIndex = index;
                        
                    }
                    else
                        if(action == 'hore')
                        {
                            if(this.listSecectIndex == 0)
                            {
                                index = zoznam.length-1;
                                if(zoznam[index].offsetTop >= ListObj.offsetHeight)
                                {
                                    scrollMe = zoznam[index].offsetTop + zoznam[index].offsetHeight;
                                    ListObj.scrollTop = scrollMe;
                                }
                            }
                            else
                            {
                                index = this.listSecectIndex-1;
                                if(zoznam[index].offsetTop <= ListObj.scrollTop)
                                {
                                    scrollMe = ListObj.scrollTop - zoznam[index].offsetHeight;
                                    ListObj.scrollTop = scrollMe;
                                }
                            }
                            zoznam[this.listSecectIndex].style.backgroundColor = "";
                            zoznam[index].style.backgroundColor = "grey";
                            this.listSecectIndex = index;
                            
                        }
                }
             }
         }
    };
    
   this.KeyDown = function(ev,obj)
   {
	 var koderSettings = obj.koderSettings;
     if(ev.keyCode != 13 && ev.keyCode != 38 && ev.keyCode != 40)    
     {	
          getE(obj.id + 'Val').value  = '';
          this.Wait(obj.id,koderSettings.maxCount, koderSettings.selectId, koderSettings.conditionId, koderSettings.extraParam,koderSettings.waitTime);
     }
     this.MoveIntoList(getE('list'+obj.id),ev);

     
     return true;
    }
    this.Wait = function(clientID)
    {
        this.startCount = 0;
        this.cakanieScroll = false;
        this.itIsAll = false;
        this.scrollTop = 0;
        
        value = getE(clientID).value;

        if (this.timo != null)
            clearTimeout(this.timo);
        
        this.timo = setTimeout("CisDataField.TimeOutChange('" + clientID + "', '" + value + "')", getE(clientID).koderSettings.waitTime);
        if (value == '')
            this.skryList(clientID);
    };

    this.timo = null;

    this.TimeOutChange = function(clientID, value)
    {
        if (value != '')
        {
            if (getE(clientID).value != value)
            {
                CisDataField.Wait(clientID);
            }
            else
            {
                this.skryList(clientID);
                CisDataField.zmena(clientID);
            }
        }
        else
        {
            this.skryList(clientID);
        }
    };

    this.setWaitScroll = function(wait)
    {
        this.cakanieScroll = wait;
    };

    this.scroll = function(ID, cisSelectId, cisConditionId, extParam)
    {
        var obj = getE('list' + ID);
        if (CisDataField.itIsAll == false)
        {
            if (CisDataField.cakanieScroll == false)
            {
                var height = obj.clientHeight + 20;
                if (obj.scrollTop > this.scrollTop && obj.scrollTop + height > obj.scrollHeight)
                {
                    this.scrollTop = obj.scrollTop;
                    this.startCount = this.startCount + 200;
                    maximCount = 200;
                    this.cakanieScroll = true;
                    // volanie webservisu
                    text = '';
                    if (getE(ID + 'Val').value == '')
                    {
                        text = getE(ID).value;
                    }

                    Sys.Net.WebServiceProxy.invoke('Moduly/WebServices/CisKoder.asmx', 'GetDataHTML', false, { "textSearch": text, "startCount": this.startCount, "maxCount": maximCount, "selectedID": cisSelectId, "selectedIDKP": cisConditionId, "clientID": ID, "extraParam": extParam }, this.PutInAdd, this.Failed, null);
                    // koniec volania web servisu
                    setTimeout("CisDataField.setWaitScroll(false)", 1800);
                }
            }
        }
    };

    this.startCount = 0;
    this.cakanieScroll = false;
    this.itIsAll = true;
    this.scrollTop = 0;

    this.koder = function(clientID)
    {
		var selectID = getE(clientID).koderSettings.selectId;
		var conditionID = getE(clientID).koderSettings.conditionId;
		var extraParam = getE(clientID).koderSettings.extraParam;
        if (typeof (iPhone) == 'undefined')
        {
            //BarisExtCoder.init(null,400,700,selectID);
			BarisExtCoder.init(null,400,700,selectID,selectID,conditionID,null,extraParam);
			BarisExtCoder.loadCoder(215,1330);
            var oldExecute = BarisExtCoder.execute;
            BarisExtCoder.execute = function(PK, editorValues, count, item)
            {
                if (BarisExtCoder.tempExecItems == null) 
                {
					BarisExtCoder.tempExecItems = new Array();
				}
				BarisExtCoder.tempExecItems[BarisExtCoder.tempExecItems.length] = item;
                var textField = getE(clientID);
                var valField = getE(clientID + "Val");
                if ((textField != null) && (valField != null)) 
                {
                    textField.value = BarisExtCoder.tempExecItems[0].data["TEXT"];
                    valField.value = BarisExtCoder.tempExecItems[0].id;
                    if (textField.onchange)
                        textField.onchange();
					if (valField.onchange)
                        valField.onchange();
                }
                
                if(textField.onkeypress != null)
                {
                    var keypress = textField.onkeypress.toString();
                    keypress = keypress.substring(keypress.indexOf('= 13 )')+6); 
                    keypress = keypress.substring(0,keypress.length-2);
                    eval(keypress); 
                }
            }
        }
        else
        {
            var koderDiv = document.createElement('div');  
            koderDiv.setAttribute('id', 'koder');

            var DivHlavicka = document.createElement('div');
            DivHlavicka.className = 'toolbar';
            DivHlavicka.innerHTML = '<h1 id="koderNadpis">Vyberte si položku</h1><a href="#" class="backBaris" onclick="CisDataField.VymazKoder()"></a>';
            DivHlavicka.style.display = 'block';

            var koderBody = document.createElement('div');
            koderBody.setAttribute('id', 'koderBody');
            koderBody.style.display = 'block';

            koderDiv.style.position = 'absolute';
            koderDiv.style.left = '0px';
            koderDiv.style.top = '0px';
            koderDiv.style.width = '100%';
            koderDiv.style.height = '100%';
            koderDiv.style.display = 'block';
            koderDiv.style.backgroundColor = 'white';
            var obal = document.createElement('div');
            obal.appendChild(DivHlavicka);
            obal.appendChild(koderBody);
            koderDiv.appendChild(obal);
            document.body.appendChild(koderDiv);

			

            iDetail(312, 'selId;' + selectID + ';conId;' + conditionID + ';dfId;' + clientID, '2', koderBody, 'koderBody');

            
            iPhone.iShow('koder',false);
        }
    };

    this.Predpln = function(value,text, clientID)
    {
		var textField = getE(clientID);
        var valField = getE(clientID + "Val");
        textField.value = text;
        valField.value = value;
        if (textField.onchange)
			textField.onchange();
		if (valField.onchange)
            valField.onchange();
        // vymazanie nalepky 
        CisDataField.VymazKoder();
        // vymazanie nalepky
    };

    this.VymazKoder = function()
    {
        var koder = getE('koder');
        if (koder != null)
        {
            iPhone.iShow('koder',true);
            koder.parentNode.removeChild(koder);
        }
    };

    this.init = function(json)
    {
        AMOnlyOnWrite = 0;
        AMOnlyOnButtClick = 1;
        AMWriteAndButtClick = 2;

        // text input
        var iText = getE(json.id);
        iText.koderSettings = { 'maxCount': json.maxCount, 'selectId' : json.selectId, 'conditionId' : json.conditionId, 'extraParam' : json.extraParam, 'waitTime': json.waitTime};

		
        // hidden field
//        var iHidden = getE(json.id + 'Val');

        // button 
//        var bImage = getE('button' + json.id);

        // list roleta

//        var lRoleta = getE('list' + json.id);

        //// dolievanie pri scrollovani
//        lRoleta.setAttribute("onScroll", 'CisDataField.scroll(\'' + json.id + '\', \'' + json.selectId + '\', \'' + json.conditionId + '\', \'' + json.extraParam + '\')');
        //// dolievanie pri scrollovani END
    }
}

CisDataField = new DropDown();


