function autoCompleter( el, url, onselect, minlength)
{
	if( !minlength)
	    minlength=1;
    el.indicator = new Element('div').addClass('autocompleter-loading').setHTML('').setStyle('display', 'none').injectAfter(el);			
	var completer = new Autocompleter.Ajax.Xhtml(el, url, 
	{
		'minLength':1,
		'onRequest': function(el) {
			el.indicator.setStyle('display', '');
		},
		'onComplete': function(el) {
			el.indicator.setStyle('display', 'none');
		}
		,
		'parseChoices': function(el) 
		{
		    var tmp = el.getFirst();
			var value = tmp.innerHTML;
			el.inputValue = value;
			this.addChoiceEvents(el).getFirst().setHTML(this.markQueryValue(value));
		}
		,
		'onSelect':onselect,
		'useSelection':false,
		'markQuery':false
    });
}

function modalWin( url, width, height) 
{
	/*
    if (window.showModalDialog) 
    {
        window.showModalDialog(url,"name", "dialogWidth:"+width+"px;dialogHeight:"+height+"px");
    } 
    else 
    {
    */
        window.open(url,'name','height='+height+',width='+width+',toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=yes,resizable=yes ,modal=yes');
    //}
}

function replaceTextAreas()
{
}

// Contest function
function checkCategory()
{
	if($("approve").value == 1)
	{
		if($("category").value == '')
		{
			alert("select category first");
			return false;
		}
	}
	
	return true;
}
        
function setAprrove(approve)
{
	$("approve").value = approve;
}

function submitUsabilityOn()
{
    submitUsability( true);
}
function submitUsabilityOff()
{
    submitUsability( false);
}
function submitUsability( enable)
{
    $$( 'form').each( function( el, idx) 
    {
        el.getElements( "input").each( function( input, idx)
        {
            if( input.type=='submit')
            {
                if( enable)
                {
                    //firefox bug workaround
                    input.removeAttribute( 'disabled');
                }
                else
                {
                    input.setAttribute( 'disabled', 'disabled');
                    input.addClass( 'disabled');
                }
            }
        });        
    });
}

window.addEvent( 'domready', initConfirmers);
window.addEvent( 'beforeunload', submitUsabilityOff);
window.addEvent( 'domready', submitUsabilityOn);


var Internationalizer = new Class(
{
	symbols:[
	    '—',
        '  •  ',
        '[]',
        'ä',
        'Ä',
        'Ö',
        'ö',
        'Ü',
        'ü',
        'ñ',
        'ç',
        'ø',
        "'",
        'см²',
        'м²',
        'см³',
        'м³',
        '½',
        '€',
        '$',
        ' v ',
        '≈',
        '∞',
        '|',
        '«',
        '»'],
     symbols2:[
	    '—',
	    '€',
	    '$',
	    ' v ',
	    '{',
	    '}',
	    '*',
	    '|',
	    '\\',
	    ']',
	    '[',	    
        '  •  ',
        "'",
        'см²',
        'м²',
        'см³',
        'м³',
        '½',
        '≈',
        '«',
        '»',
        '°C'],
	initialize:function()
	{
	    var holder2 = document.getElement('.symbols2');
		var holder = $('i18nholder');
		
		if( holder2)
		{
		    holder  = holder2;
		    this.symbols = this.symbols2; 
		}
		
		if( !holder)
		    return;
		this.build( holder);
		$$( 'input').each( function( el, idx)
		{
			if( el.type=='text')
			{
			    el.addEvent( 'focus', this.track.bind( this));
			}
		}.bind( this));
		$$( 'textarea').each( function( el, idx)
		{
		    el.addEvent( 'focus', this.track.bind( this));
		}.bind( this));
	},
	build:function( holder)
	{
		this.symbols.each( function( s,idx) {
			var btn = new Element( 'input');
			btn.setAttribute( 'type', 'button');
			btn.addEvent( 'click', function(){ this.insert( s)}.bind( this));
			btn.setAttribute( 'value', s);
			btn.setAttribute( 'class', 'i18n');
			btn.injectInside( holder);
		}.bind( this));
	},
	track:function( e)
	{
		if( e.srcElement)
		    this.focused = e.srcElement;
		else
		    this.focused = e.target;
	},
	insert:function( text)
	{
		if( !this.focused)
		{
		    return;
//		    if( tinymce.EditorManager.currentEditor)
//                tinymce.EditorManager.execCommand("mceInsertContent", true, text);		    
//		    return;
		}
		myField = this.focused;
		if( document.selection)
	    {
		    myField.focus();
		    sel = document.selection.createRange();
		    sel.text = text;
	    }
	    else if( myField.selectionStart || myField.selectionStart == '0') 
	    {
	        var startPos = myField.selectionStart;
	        var endPos = myField.selectionEnd;
	        myField.value = myField.value.substring(0, startPos)+ text+ myField.value.substring(endPos, myField.value.length);
	        myField.focus();
	        myField.selectionStart = endPos+text.length;
	        myField.selectionEnd = endPos+text.length;
	    } 
	    else 
	    {
	        myField.value += text;
	    }
	}
});

window.addEvent( 'domready', function(){
var intern = new Internationalizer();});

function initConfirmers()
{
	var els=$$('.confirm');
	els.each( function( el,idx){
		el.addEvent( 'click', function( e)
		{
			el.addClass( 'confirmactive');
			if( !confirm( "Вы уверены?"))
			{
				new Event(e).stop();
				el.removeClass( 'confirmactive');
			}
		});
	});
}

//window.addEvent( 'domready', function( e){
//        new Tips( $$('.tooltip'));
//});

function updateTimeField( id)
{
	if( $(id+'h').value!='')
	{
		var mins =  $(id+'m').value;
		if( mins=='') mins='00';
	    $(id).value=$(id+'h').value+":"+mins;
	    return;
	}
    $(id).value='';
}

function updateDateTimeField( id)
{
	if($(id+'d').value == '' || $(id+'h').value == '')
		$(id).value = '';
	else
		$(id).value=$(id+'d').value+" "+$(id+'h').value+":"+$(id+'m').value
}

function fadeInfoBox()
{
	var a = new Fx.Styles($('datasaved'), {duration:1500, wait:false});
	a.start( {'background-color':'#CFEBF7'});
}

function fadeErrorBox()
{
	var a = new Fx.Styles($('errormsg'), {duration:1500, wait:false});
	a.start( {'background-color':'#FFB7B7'});
}


function toggle( el)
{
	if( el.style.display == 'block')
	    el.style.display = 'none';
	else
	    el.style.display='block';
}

// Word Highlight
function highlightWord(node,word) {
	// Iterate into this nodes childNodes
	if( node.tagName &&  node.tagName.toLowerCase()== "textarea")
	    return;
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 && 
			checkn.nodeName.toLowerCase() != 'body') { 
			// 9 = top of doc
				if (checkn.className.match(/\bnosearchhi\b/)) { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function highlightStrings( str)
{
	strs = str.split( ' ');
	strs.each( function( el) {
		if( el.length!=0)
        highlightWord(document.getElementsByTagName("body")[0],el.trim());
	}
    );	
}
//menu
	Element.extend(
	{
		hide: function() 
		{
			return this.setStyle('display', 'none');
		},
		
		show: function() 
		{
			return this.setStyle('display', '');
		}
	});

	var DropdownMenu = new Class({	
		initialize: function(element)
		{
			$A($(element).childNodes).each(function(el)
			{
				if(el.nodeName.toLowerCase() == 'li')
				{
					$A($(el).childNodes).each(function(el2)
					{
						if(el2.nodeName.toLowerCase() == 'ul')
						{
							$(el2).style.display='none';
							
							el.addEvent('mouseover', function()
							{
								el2.style.display='block';;
								return false;
							});
	
							el.addEvent('mouseout', function()
							{
								el2.style.display='none';
							});
							new DropdownMenu(el2);
						}
					});
				}
			});
			return this;
		}
	});
	
	window.addEvent( 'domready', function() { new DropdownMenu($('navigation'))});
    function convertWord(type, content) 
    {
          switch (type) 
          {
              // Gets executed before the built in logic performes it's cleanups
              case "before":
                  // do nothing
                  break;
              // Gets executed after the built in logic performes it's cleanups
              case "after":
                  content = content.replace(/<(!--)([\s\S]*)(--)>/gi, "");
                  break;
        }
        return content;
    }     
	
	
	function mcePasteCleanup(pl, o)
	{
         o.content = o.content.replace(/<br[^<>]*>/ig,"[br]");
         o.content = o.content.replace(/(<p[^<>]*>)/ig,"[p]");
         o.content = o.content.replace(/(<\/p[^<>]*>)/ig,"[/p]");
         
         o.content = o.content.replace(/(<b[^<>]*>)/ig,"[b]");
         o.content = o.content.replace(/(<\/b[^<>]*>)/ig,"[/b]");

         o.content = o.content.replace(/(<\S([^<>]*)>)/ig,"");
        
         o.content = o.content.replace(/(\[br\])/ig,"<br />");
         
         o.content = o.content.replace(/(\[p\])/ig,"<p>");
         o.content = o.content.replace(/(\[\/p\])/ig,"</p>");
         
         o.content = o.content.replace(/(\[b\])/ig,"<b>");
         o.content = o.content.replace(/(\[\/b\])/ig,"</b>");
         
    }
