function _parsejs(s) {
    try { return eval(s) }
    catch(e) { alert('Что-то пошло не так:.\n\n'+s+'\n\n'+e.description); return false}
}
$.loaded=function(){
	$('#loading').hide()
}
$.fn.loading=function() {
	var xy=$(this).offset()
	$('#loading').css('left',xy.left).css('top',xy.top).css('width',$(this).outerWidth()).css('height',$(this).outerHeight()).show()
	return $(this)
}
$('a.logoff').live('click',function() {
	$.ajax({
	    type:'POST',
		url:'/adm/do/logoff',
		beforeSend:function(){$('body').loading()},
		complete:function(){$.loaded()},
		success:function(t){
			self.location.replace('/adm/login')
			return false
		}
	})
})
$('table.list').live('changed',function(){
    $(this).find('tr').removeClass('tr1').removeClass('active').filter(':even').addClass('tr1')
}).trigger('changed')
$('table.list input:checkbox[name^=id]').live('click',function(e) {
    var o=e.target,tbl=$(this).parents('table.list')
    if($(o).parents('th').is('th')) {
	    if(o.checked) $(tbl).find('input:checkbox').attr('checked','checked')
	    else $(tbl).find('input:checkbox').removeAttr('checked')
    } else $(tbl).find('th input:checkbox').attr('checked',$(tbl).find('td input:checkbox').length==$(tbl).find('td input:checkbox:checked').length?'checked':'')
    if($(tbl).find('tr:visible td input:checkbox:checked').length>0) $('#act a').removeClass('no')
    else $('#act a').addClass('no')
})

$('.popup a.close').live('click',function() {
	$(this).parents('div.popup').eq(0).hide()
	$.loaded()
	return false
})

$('.yes').live('click',function() {
	$(this).parents('form').find('input[name=yes]').val($(this).toggleClass('on').is('.on')?1:0)
})


$('form #go').live('click',function() { 
	$.ajax({
	    type:'POST',
		url:$('form:eq(0)').attr('action'),
        data:$('form:eq(0)').serialize(),
		beforeSend:function(){$('body').loading()},
		complete:function(){},
		success:function(o) {
			o = _parsejs(o)
			$('#status').show()
			if(o.err) $('#status div').attr('class','no').html(o.errmsg)
			else {
				$('form').get(0).reset()	
				$('#status div').attr('class','ok').html('Спасибо за ваше поздравление!')
			}
		}
	})
	return false
})

$.fn._scroll = function() {
	document.body.ondragstart=
	document.body.onselectstart=
	document.body.onselect=function() {return false}

	$(this).each(function() {
		$(this).find('.scroll').remove()
		var
		o=$(this),
		s=$('<div class="scroll"><div></div></div>').show().prependTo(o),
		y0=$(s).offset().top,
		ym=parseInt($(s).css('padding-top')) 
		y1=y0+ym,
		y2=y0+$(s).height()-ym-$(s).find('div').height()
		$(o).find('div.scrollable').css('top',0)
		if($(o).find('div.scrollable').height()<$(o).height()) return $(s).hide()
		$(s).find('div').bind('mousedown',function() {
			$('body').bind('mousemove',function(e) {
				var y
				if(e.pageY<y1) y=y1
				else if(e.pageY>y2) y=y2
				else y=e.pageY
				$(s).find('div').css('top',y-y0)
				$(o).find('div.scrollable').css('top',($(o).height()-$(o).find('div.scrollable').height())*(y-y0-ym)/(y2-y1))
			})
			return false
		})
	})
	$('body').unbind('mouseup').bind('mouseup',function() {
		$('body').unbind('mousemove')
		return false
	})
	return $(this)
}
