
var current_page_search;

function initTgls(){
	$("body").addClass('jsinit');
	$("#header").append('<a id="tgl_sdbr_left" class="tglh">Sidebar</a>');
	$("#tgl_sdbr_left").click(function(){
		$("#left").SlideToggleLeft(500);
		$("#container").addClass("nonav");
	});
}

function set_ajax_links(pref){
	$(pref +" a[@href!='#']").click(function(){
	  if(this.target!='') return true;
	  $('#cnt_inn').load(this.href+((this.href.indexOf('?')!=-1)?'&':'?')+'ajax=1');
	return false;
	});
}

function show_artfilter (flag) {
	var sf = document.getElementById('show_filter');
	var hf = document.getElementById('hide_filter');
	var fc = document.getElementById('filter_cnt');
	var st1 = (flag>0) ? 'block' : 'none' ;
	var st2 = (flag>0) ? 'none' : 'block' ;
	
	sf.style.display = st2;
	hf.style.display = st1;
	fc.style.display = st1;
	return false;
}

function initNav(num){
	$("#left .cnt").hide();	
	$("#left .cnt:eq("+num+")").show();
	$("#left .hdr").each(function(){
	  $(this).attr('index',$("#left .hdr").index(this));
	  $(this).click(function(){
		$("#left .cnt").hide();
		$(this).next().show();
		$.getIfModified("index.php", { set_menu_index: $(this).attr('index') } )
  	  });
	});
   //set_ajax_links("#left .cnt");
}

function initNav2(num){
	$(".cnt_nav").hide();
	$(".cnt_nav:eq("+num+")").show();
	$(".cnt_nav:eq("+num+")").prev(".hdr2").hide();
	$(".hdr2").each(function(){
	  $(this).attr('index',$(".hdr2").index(this));
	  $(this).click(function(){
		$(".hdr2").show();
		$(".cnt_nav").hide();
		$(this).next().show();
		$(this).hide();
	  });
	  var ind = $(this).attr('index');
	  var sign="&";
	  $(this).next().find("a").each(function(){
	  	if($(this).attr('href').indexOf("?")==-1) sign="?"
		$(this).attr('href',$(this).attr('href') + sign + "set_menu_index=" + ind)
	  });
	});
}

function initList(){
	$(".data .check_all").click(function(){
		if (this.checked) {
			$(".data tr").addClass('selected');
			$(".data .check_all").each(function(){ this.checked = true; });
			$(".data td input[@type='checkbox']").each(function(){ this.checked = true; });
		}
		else {
			$(".data tr").removeClass('selected');
			$(".data td input[@type='checkbox']").each(function(){ this.checked = false; });
			$(".data .selected input[@type='checkbox']").each(function(){ 
				this.checked = true;
				$(this).parent().parent().addClass('selected');
			});
			$(".data .check_all").each(function(){ this.checked = false; });
		}
	});
	$(".data td input[@type='checkbox']").click(function(){
		if (this.checked) {
			$(this).parent().parent().addClass('selected');
			$(this).parent().parent().find('td').addClass('selected');
		}
		else {
			$(this).parent().parent().removeClass('selected');
			$(this).parent().parent().find('td').removeClass('selected');
			$(this).parent().parent().find('td').removeClass('over');
			$(".data .check_all").each(function(){ this.checked = false; });
		}
	});
	
	$(".data tr").hover(function(){
		$(this).find('td').addClass("over");
	},function(){
		$(this).find('td').removeClass("over");
	});
	
	$(".data .img span").hide();
	$(".data .img").hover(function(){
		$(this).find("span").show();
	},function(){
		$(this).parent().find("span").hide();
	});
}



function FCKeditor_OnComplete( editorInstance )
 {
  editorInstance.ToolbarSet.Collapse() ;
  editorInstance.Events.AttachEvent( 'OnBlur'	, FCKeditor_OnBlur ) ;
  editorInstance.Events.AttachEvent( 'OnFocus', FCKeditor_OnFocus ) ;
 }

function FCKeditor_OnBlur( editorInstance )
 {
 // editorInstance.ToolbarSet.Collapse() ;
  editorInstance.UpdateLinkedField();
 }
	
function FCKeditor_OnFocus( editorInstance )
 {
  editorInstance.ToolbarSet.Expand() ;
  editorInstance.UpdateLinkedField();
 }

function add_option(obj,val,txt)
 {
   var option = document.createElement("option");
   option.value = val;
   option.text = txt;
   obj.options.add(option);
  }
  

function sec_summary(flag, img) {
	if (flag>0) {
		$('#sec_summary').hide('slow',function(){ document.getElementById('page_cnt').style.marginTop = '0px'; });
		$('#test').fadeOut('slow',function(){ document.getElementById('page_cnt').style.marginTop = '0px'; });
		$('#summ_hide').hide();
		$('#summ_show').show();
	}
	else {
		$('#sec_summary').show('slow',function(){document.getElementById('page_cnt').style.marginTop = -document.getElementById('test').offsetHeight+'px';});
		$('#test').fadeIn('slow');
		document.getElementById('page_cnt').style.marginTop = -document.getElementById('test').offsetHeight+'px';
		$('#summ_hide').show();
		$('#summ_show').hide();
	}
}


function ajax_set_form_params(formData) {
 formData.push({name: 'ajax', value: '1'});
 return true;
}

function init_regions(){
	if($("#bul").attr("checked")){
		$("#first").show();
	} 
	if($("#world").attr("checked")) {
		$("#first").hide();
	}
	$("#bul").click(function(){
		$("#first").show();
	});
	$("#world").click(function(){
		$("#first").hide();
		$("option.mlt").each(function(){
			this.selected=false;
		});
	});
}

function init_autocomplete(){
	$("#ac_me").autocomplete(current_page_search, { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem, formatItem:formatItem, selectOnly:1 });
	$("#ac_me_src").autocomplete(current_page_search, { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem1, formatItem:formatItem, selectOnly:1 });
	$("#firm_autocompleter").autocomplete(current_page_search, { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem2, formatItem:formatItem, selectOnly:1 });
	$("#ac_me_person_form").autocomplete(current_page_search, { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:null, formatItem:formatItem, selectOnly:1 });
	$("#person_name").autocomplete(current_page_search, { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem3, formatItem:formatItem, selectOnly:1 });
	$("#ac_user").autocomplete('/lib/searchu.php', { minChars:3, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItemUser, formatItem:formatItem, selectOnly:1 });
}

function not_in(arg, mas) {
	var temp = new Array();
	temp = mas.split(' ');
	for (var i=0; i < temp.length; i++ ) {
		if (arg == temp[i]) return false;
	}
	return true;
}

function addEvent(ul,theVal,txt,val,check) {
	var ni = document.getElementById(ul);
	var numi = document.getElementById(theVal);
	var num = (document.getElementById(theVal).value -1)+ 2;
	numi.value = num;
	var newli = document.createElement('option');
	newli.setAttribute("id",ul+num);
	newli.setAttribute("value",val);
	newli.setAttribute("onclick","removeEvent(\'"+ul+num+"\',\'"+ul+"\',\'"+check+"\')");
	newli.innerHTML = txt;
	ni.appendChild(newli);
}

function removeEvent(liNum, ul, check) {
	var d = document.getElementById(ul);
	var oldli = document.getElementById(liNum);
	var list = document.getElementById(check);
	var arr = list.value.split(" ");
	var str = "";
	var sep = "";
	for (var i=0; i < arr.length; i++ ) {
		if (oldli.value != arr[i]) {
			if (str) sep=" ";
			str = str + sep +arr[i];
		}
		else if(if_remove_class) {
			$('.offers_add').each(
				function(){
					$(this).removeClass('selected_item');
				}
			);
		}
	}
	list.value = str;
	d.removeChild(oldli);
}

function init_calendar()
{
	if($('input.calendarBoth').length)
	{
		$('input.calendarBoth').calendar({autoPopUp: 'both', 
			buttonImageOnly: true, buttonImage: '/images/calendar.gif', 
			buttonText: 'Calendar'
		});
	}
}

function selectItem(li) {
	if (li.extra) {
		if (not_in(li.extra[0], $("#src_id").val())) {
			var sep = "";
			if ($("#src_id").val()) sep = " ";
			$("#src_id").val($("#src_id").val() + sep + li.extra[0]);
			addEvent('myUl','theValue',$("#ac_me").val(), li.extra[0],'src_id');
		}
		$("#ac_me").val("");
	}
}

function selectItem2(li)
{
	if (li.extra)
	{
		if(current_page_search == '/lib/searchf.php')
		{
			if ($('#firm_autocompleter').attr('rel'))
			{
				$.post('/admin/person.php?type=2&get_products='+li.extra[0], function(response)
				{
					$('#attached_firms').html($('#attached_firms').html() + response);
				});
			}
			else { top.location="person.php?type=2&person_id="+li.extra[0]; }
		}
		else { top.location="person.php?type=1&person_id="+li.extra[0]; }
	}
}
/*
function selectItem4(li)
{
	if (li.extra)
	{
		if(current_page_search == '/lib/searchf.php')
		{
			$.post('/admin/person.php?type=2&get_products='+li.extra[0], function(response)
			{
				$('#attached_firms').html($('#attached_firms').html() + response);
			});
		}
	}
}
*/
function selectItem_law(li) {
	if(the_val = $('#ac_me_' + wtf).val()){
		arr_ids = new Array();
		arr_ids['enactments'] = 5;
		arr_ids['forms'] = 7;
		arr_ids['procedures'] = 8;
		$('#'+wtf).load('test.php?ajax=1&id=' + the_val.split(' ')[0] + '&type='+arr_ids[wtf]);
	}
}

function selectItem1(li) {
	if (li.extra) {
		if (not_in(li.extra[0], $("#prsn_id").val())) {
			var sep = "";
			
			if(if_offer==1){
				var the_id = "";
				the_id = $("#pUl option").attr("id");
			}
			if ($("#prsn_id").val()) sep = " ";
			if (if_offer==1 && the_id && the_id.toString().length>3) {
				removeEvent(the_id.toString(),'pUl','prsn_id');
			}
			$("#prsn_id").val($("#prsn_id").val() + sep + li.extra[0]);
			addEvent('pUl','pValue',$("#ac_me_src").val(), li.extra[0], 'prsn_id');
		}
		$("#ac_me_src").val("");
	}
}
function selectItemUser(li) {
	if (li.extra) {
		if (not_in(li.extra[0], $("#user_id").val())) {
			var sep = "";
			
			if(if_offer==1){
				var the_id = "";
				the_id = $("#uUl option").attr("id");
			}
			if ($("#user_id").val()) sep = " ";
			if (if_offer==1 && the_id && the_id.toString().length>3) {
				removeEvent(the_id.toString(),'uUl','user_id');
			}
			$("#user_id").val($("#user_id").val() + sep + li.extra[0]);
			addEvent('uUl','uValue',$("#ac_user").val(), li.extra[0], 'user_id');
		}
		$("#ac_user").val("");
	}
}

function selectItem3(li) {
	if (li.extra) {
		$('#person_webpage').val(li.extra[0]);
	}
}
	
function formatItem(row) {
	return row[0];
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
		$("#remSymb").html(countfield.value);
}
function init_add_person(){
	$("#add_person").toggle(function(){
		$("#post_person").show();
		$("#add_person").html("�����");
		return false;
	}, function(){
		$("#post_person").hide();
		$("#add_person").html("������ �������");
		return false;
	}
	);
}
$(function(){
//	init_regions();
	init_autocomplete();
	init_add_person();
	init_calendar();
//	sel_res();
});
