$(document).ready(function(){
   $(".multiple").multiselect({
	noneSelectedText: 'Выбрать словари',
	selectedText: "Выбрано словарей # из #"

   });

	$( ".button" ).button();
	
	var n = 0;
	$('td',$('.chess')).each(function(){
		if(n%2==0) $(this).addClass('dark');
		//else $(this).addClass('light');
		n++;
	});
	
});




function highlightsynonyms(){		
	$('.synonym').each(function(){
			synonyms = $(this).attr('list').split('|');
			original = $(this).attr('word');
			$(this).html('<span class="or">'+$(this).html()+'</span>');
			html = '<ul class="synonymlist" style="display:none;width:180px;">';
			$.each(synonyms, function(key, value) { 
				if(value!='')
					html += '<li class="replacement"><a onclick="_change(this)">'+value+'</a></li>';
			});
			html += '<li class="original"><a onclick="_change(this)">'+original+'</a></li>';
			html += '</ul>';
			$(this).append(html);
	});
	$('.synonym').mouseover(function(){
		$(this).addClass('hover');
		$('.synonymlist',this).css('display','block');
	});
	$('.synonym').mouseout(function(){
		$(this).removeClass('hover');
		$('.synonymlist',this).css('display','none');
	});
}
function _change(obj){
	var parent = $(obj).parent().parent().parent();
	$('span.or',parent).text($(obj).text());
}

function clear_area(elem) {
	var i, j, n, a, c, text = '';
	
	if (elem.hasChildNodes()) {
		for (i = 0; i < elem.childNodes.length; i++) {
			n = elem.childNodes[i];
		
			if (n.nodeType == 1) {
				c = n.getAttribute('class');
				if (c == 'synonym') {
					text += clear_area(n.firstChild);
				}else if(c == 'synonyma'){
					text += "\n";
				}else if (c != 'synonymlist') {
					text += '<' + n.nodeName.toLowerCase();
					if (n.hasAttributes()) {
						for(j = 0; j < n.attributes.length; j++) {
							a = n.attributes[j];
							text += ' ' + a.nodeName + '="' + a.nodeValue + '"';
						}
					}
					text += '>';
					text += clear_area(n);
					text += '</' + n.nodeName.toLowerCase() + '>';
				}
			} else if (n.nodeType == 3) {
				text += n.nodeValue;
			}
		}
	}
	return text;
}



function validate(obj){
		var error = false;
		$('.required',$(obj).parent().parent()).each(function(){
			if($(this).val()==''){ $(this).addClass('err'); error = true;}
		});
			
		if(error==true){
			$('#validation').fadeIn(900);
			$('input.err,textarea.err,select.err').click(function(){$(this).removeClass('err');$('#validation').fadeOut(900);});
			return false;
		}
		
		$(obj).parent().parent().submit();
}



(function($) {
	$.extend({ 
		uiLock: function(obj,content){
			obj.css('position','relative');
			if(content == 'undefined') content = '';
			$('<div></div>').attr('id', 'uiLockId').css({
				'position': 'absolute',
				'top': 0,
				'left': 0,
				'z-index': 10000000,
				'opacity': 0.4,
				'width':'100%',
				'height':'100%',
				'color':'white',
				'background-color':'black'
			}).html(content).appendTo(obj);
		},
		uiUnlock: function(){
			$('#uiLockId').remove();
		}
	});
})(jQuery);
