/*******************************************
* float.js
* float windows
* need to be loaded global.js
********************************************/

	var floatIDs = new Array(); // (id1, id2, id3, ...)
	var floatData = new Array(); // id => content
	var floatClick = new Array();
    var floatCurrentID = null;
    var allowFloatHide = 1;

	function attach( id, content, addClick ) {

		floatIDs[ floatIDs.length ] = id;
		if ($(content)) {
			floatData[ id ] = $(content).innerHTML;
			$(content).innerHTML = "";
		} else {
			floatData[ id ] = content;
		}

		if ( addClick != 'false' ) {

			attachClick( id );

		}

	}

	function attachClick( id ) {

		if (!_obj( id )) return;

		_obj( id ).onclick = function ( e ) {
			showFloatWindow( id );
			allowFloatHide = 0;
		}


	}

	function reattach( id, content, show_now ) {

		floatData[ id ] = content;

        if ( show_now ) {
            if (id == floatCurrentID) {
                _obj('float-window').innerHTML = floatData[ id ];
            } else {
                showFloatWindow( id );
            }
        }

        document.onclick = hideFloatWindow;

	}

	function buildFloatAttachments() {

		if ( floatIDs.length > 0 ) {

			document.write('<div id="float-window"></div');

			fobj = _obj('float-window');
			fobj.style.display = 'none';
			fobj.style.position = 'absolute';
			fobj.style.left = '0px';
			fobj.style.top = '0px';
			fobj.onclick = function( e ) {
				e = _fixE(e);
				var tagName = getEventTag( e );
				if (tagName == "input" || tagName == "textarea" || tagName == "select" || tagName == "option") {
					//don't hide float window
					_noBubbling( e );
					if (is_ie) event.cancelBubble = true; // IE SUX!
				} else {
					//hide float window
				}

			}

            //add document onclick event
            document.onclick = function( e ) {
            	hideFloatWindow();
			}

		}

	}

	function showFloatWindow( id ) {

		/*if (floatCurrentID == id) {

			hideFloatWindow();
			return;

		}*/

		sobj = _obj( id );

		if ( floatData[ id ] && sobj ) {

			var ie_plus = true;
			bounds = getBounds( sobj );

			fobj = $('float-window');
			fobj.innerHTML = floatData[ id ];

			var fleft = bounds.left;
			var ftop = bounds.top + bounds.height + 1;
			if (is_ie) {
				fleft += (ie_plus) ? 4 : 0;
				ftop += (ie_plus) ? 4 : 0;
			}
			fobj.style.left = fleft + 'px';
			fobj.style.top = ftop + 'px';

            setOpacity( fobj, 0); //set object to invisible state
            show('float-window'); //

            floatCurrentID = id;

			//check if float window on the screen? recoordinate it

			var fwidth = parseInt( fobj.style.width ) ? parseInt( fobj.style.width ) : fobj.offsetWidth;

			/*if ( fwidth < bounds.width ) {
				fobj.style.width = bounds.width + 'px';
				fwidth = bounds.width;
			}*/

			if ( (fleft + fwidth) >= document.body.clientWidth ) {
				fleft = fleft + bounds.width - fwidth;
				fobj.style.left = fleft + "px";
			}

          	//event.cancelBubble=true

            opacityFader( fobj, 0, 100 );

            return false;

		}

	}

	function hideFloatWindow() {

        if (!allowFloatHide) {
        	allowFloatHide = 1;
        	return;
        }
        if (floatCurrentID == null) return;
        //floatData[ floatCurrentID ] = $('float-window').innerHTML;
        floatCurrentID = null;
		//opacityFader( _obj('float-window'), 100, 0 );
		hide('float-window');

	}

    function opacityFader( id, opacStart, opacEnd ) {

    	//if id is specified as Object
    	if (typeof(obj) == "object") id = id.id;

		//speed for each frame
		var millisec = 300; //default fade time
    	var speed = Math.round(millisec / 100);
    	var timer = 0;

    	//determine the direction for the blending, if start and end are the same nothing happens
    	if(opacStart > opacEnd) {
        	for(i = opacStart; i >= opacEnd; i--) {
            	setTimeout("setOpacity('" + id + "', " + i + ")",(timer * speed));
            	timer++;
        	}
    	} else if(opacStart < opacEnd) {
        	for(i = opacStart; i <= opacEnd; i++)
            	{
            	setTimeout("setOpacity('" + id + "', " + i + ")",(timer * speed));
            	timer++;
        	}
    	}

    }

    function setOpacity ( obj, opacity ) {

    	//if obj is specified as Object ID
    	if (typeof(obj) != "object") obj = _obj( obj );

    	var oStyle = obj.style;
    	oStyle.opacity = (opacity / 100);
    	oStyle.MozOpacity = (opacity / 100);
    	oStyle.KhtmlOpacity = (opacity / 100);
    	oStyle.filter = "alpha(opacity=" + opacity + ")";

    }

    /* AutoComplete Object */
    autoComplete = {

		currentID: '',
		hoverIndex: -1,
		resultIndexes: new Array(),
		data: new Array(), //completeID => completeArray
		minSimbols: new Array(), //completeID => minimalSimbols
		itemsPerColumn: 8,

    	attach: function( cid, cArray, minimalSimbols) {

    		if (_obj( cid ).tagName != 'INPUT') return; //<input> elements allowed

    		if (!is_defined( autoComplete.data[ cid ] )) {
    			attach( cid, 'AutoComplete', 'false' );
    			autoComplete.data[ cid ] = cArray;
    			autoComplete.minSimbols[ cid ] = minimalSimbols;
	    		_obj( cid ).onfocus = function( e ) {
	    			/* because document onClick closes float window, it must be here */
	    			allowFloatHide = 0;
	    			_noBubbling( e );
	    			autoComplete.show( cid, this.value );
	    		};
    			_obj( cid ).onkeyup = function( ) { autoComplete.show( cid, this.value ); };
    			_obj( cid ).onkeydown = function( e ) {
    				e = _fixE( e );
					if (autoComplete.hoverIndex >= 0 && e.keyCode == 13) {
    					autoComplete.exec();
						return false;
					}
    			}
    			//disable Browser's AutoComplete
    			_obj( cid ).autocomplete = "off"; //IE
    			_obj( cid ).setAttribute("autocomplete", "off"); //FF

    		}

    	},

    	show: function( cid, inputValue ) {

    		autoComplete.currentID = cid;

    		inputValue = _obj( cid ).value;

    		//alert(inputValue);

			if (global_key == 40) { //down

               	autoComplete.hover( autoComplete.hoverIndex + 1 );

            } else if (global_key == 38) { //up

				autoComplete.hover( autoComplete.hoverIndex - 1 );

            } else if (global_key == 39) { //right

				autoComplete.hover( autoComplete.hoverIndex + this.itemsPerColumn );

            } else if (global_key == 37) { //left

				autoComplete.hover( autoComplete.hoverIndex - this.itemsPerColumn );

            } else if (global_key == 13) { //enter

				return autoComplete.exec();

            } else

    		if (inputValue.length >= autoComplete.minSimbols[ cid ]) {

				//var searchValue = inputValue.toLowerCase();
				autoComplete.hoverIndex = -1;
				autoComplete.resultIndexes = new Array();
				var autoCompleteHTML = "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td>";
				var j = 0;

				for (var i = 0; i < autoComplete.data[ cid ].length; i++) {

					//var sourceString = autoComplete.data[ cid ][ i ].toLowerCase();
					//if (sourceString.indexOf( searchValue ) > -1) {
					var re = new RegExp(inputValue, 'i');
					if (ar = re.exec(autoComplete.data[ cid ][ i ])) {

						if (autoComplete.data[ cid ][ i ].indexOf( ar[0] ) != 0) continue;
						autoComplete.resultIndexes[ autoComplete.resultIndexes.length ] = i;
						autoCompleteHTML += '<div id="ac_' + j + '" class="auto-complete-row" onmouseover="autoComplete.hover( ' + j + ' );" onclick="autoComplete.exec(' + j + ');">' + autoComplete.data[ cid ][ i ].replace( ar[0], '<b>'+ar[0]+'<\/b>' ) + '<\/div>';
						j++;

						if (j % this.itemsPerColumn == 0) {
							autoCompleteHTML += "<\/td><td>";
						}

					}

				}
				autoCompleteHTML += "<\/td><\/tr><\/table>";

				/* Nothing Founded */
				if (j == 0) {
					autoComplete.hide();

				/* Founded ONLY the same value as input value (hide founded result) */
				} else if (j == 1 && autoComplete.data[ cid ][ autoComplete.resultIndexes[0] ] == inputValue) {
					autoComplete.hide();

				/* Show result and hover (hightlight) first value */
    			} else {
					reattach( cid, autoCompleteHTML, true);
					//alert($('float-window').offsetHeight);
					/*if ( $('float-window').offsetHeight > 100 ) {
						if (is_ie) {
							$('float-window').style.width = $('float-window').offsetWidth + 10 + 'px';
						}
						$('float-window').style.height = '100px';
						$('float-window').style.overflow = 'auto';

					}*/
                	autoComplete.hover( 0 );
                	listenKey();
				}

			} else {

				autoComplete.hide();

			}

    	},

    	hide: function() {

			hideFloatWindow();
			$('float-window').style.height = '';
			$('float-window').style.overflow = 'visible';
			nolistenKey();
			autoComplete.currentID = '';
			autoComplete.hoverIndex = -1;

    	},

    	hover: function( index ) {

    		if (autoComplete.resultIndexes.length <= index) {
    			index = autoComplete.resultIndexes.length - 1;
    		} else if (index < 0) {
    			index = 0;
    		}

			if (autoComplete.hoverIndex > -1) {
				_obj('ac_'+autoComplete.hoverIndex).className = 'auto-complete-row';
			}
			_obj('ac_'+index).className = 'auto-complete-row-hover';
			autoComplete.hoverIndex = index;

    	},

    	exec: function( index ) {

    		if (!index) index = autoComplete.hoverIndex;
    		//alert( autoComplete.currentID+" : "+autoComplete.data[ autoComplete.currentID ][ index ] );
    		if (index > -1) {
    			_obj( autoComplete.currentID ).value = autoComplete.data[ autoComplete.currentID ][ autoComplete.resultIndexes[index] ];
    			autoComplete.hide();
    		}
    		return false;

    	}

    }