if (!window.console) window.console=window.console||{log:function(){},info:function(){},warn:function(){},dir:function(){},debug:function(){}};

window.onload = function() {
	initForms();	
}

function limit_length(o, max, msg){
	if (o.value.length > max) {
		$('span#err_'+o.name).html(msg);
    }
	else {
		$('span#err_'+o.name).html('');
	}
    return true;
}

// Show/hide prev/next
function showMore(href)
{
    href.parentNode.style.display = 'none';
    href.parentNode.nextSibling.style.display = 'block';
}
function showLess(href)
{
    href.parentNode.style.display = 'none';
    href.parentNode.previousSibling.style.display = 'block';
}

// Forms

function initForms() {
//	for(var i=0;i<document.forms.length;i++){
//		var frm = document.forms[i];
//		var elems = frm.getElementsByTagName('a');
//
//		for (var j=0; j<elems.length;j++) {
//			if(elems[j].name == 'submiter') {
//				elems[j].frm = frm
//				elems[j].frm.href="javascript:void();"
//				elems[j].onclick = function() {
//					this.frm.submit();
//					return false;
//				}
//			}
//		}
//	}
    $("form a[name=submiter]").click(function() {$(this).parents("form").submit();});
}


var divOver = {
	show: function()
	{
		$(window).bind('resize',this.resize);
		if($.browser.msie) $('select').css('visibility','hidden');
		$('#div-over')
			.css({ width:  $(document).width()
				 , height: $(document).height()
				 , opacity:	.5
				 })
			.show();
	},
	hide: function()
	{
		$(window).unbind('resize',this.resize);
		if($.browser.msie) $('select').css('visibility','visible');
		$('#div-over').hide();
	},
	resize: function()
	{
		$('#div-over')
			.hide()
			.css({ width:  $(document).width()
				 , height: $(document).height()
				 })
			.show();
	}
};

						
// show & hide SELECTs in IE
function hideSelectsInIE(here)
{
	if($.browser.msie) 
	{
		var here = here || $(document);
		$('select',here).each(function()
		{
			var self = $(this);
			self.css('visibility','hidden');
			$('<div/>').insertAfter(self).css({
				position: 'absolute',
				top: self.offset().top - self.parent().offset().top,
				left: self.offset().left - self.parent().offset().left,
				border: '1px solid #aaa',
				padding: '0 0 0 4px',
				fontSize: '13px',
				width: self.width()-4,
				height: self.height()-1,
				background: '#fff'
			}).text(self.val());
		})
	}
}
function showSelectsInIE(here)
{
	if($.browser.msie) 
	{
		var here = here || $(document);
		$('select',here).each(function()
		{
			var self = $(this);
			self.css('visibility','visible').next().remove();
		})
	}
}

// ACL
//*
$(function()
{

	$.fn.rights = function(options){
		
		var options = $.extend({},options);
		
		return this.each(function(){
			
			var $input = $(this);
			var $rights = $input.next();
			
			$input.data('$rights',$rights);
			$rights.data('$input',$input);
			
			$rights.data('init', function(){
				$('a',$rights).removeClass('highlight current');
				var val 	= $rights.data('$input').val();
				var current = $('a[val=' +val+ ']',$rights).addClass('current');
				var group	= current.parents('div.rights-group');
				var thisN 	= $('a',group).index(current);
				$('a:eq('+thisN+'), a:gt('+thisN+')',group).addClass('highlight');
			})
			
			$rights.data('setMaxValue', function(maxValue){
				var maxValue = parseInt(maxValue);
				var currentVal = parseInt($rights.find('a.current').attr('val'));
				$rights.find('a').each(function(){
					var $link = $(this)
					var linkVal = parseInt($link.attr('val'));
					$link.removeClass('blocked');
					switch(maxValue){
						case 0:
							break;
						case -1:
							if ( linkVal == 0) {
								$link.addClass('blocked');
							}
							if ( linkVal == -1 && $link.prev().hasClass('current')) {
								$link.click();
							}
							break;
						case -255:
							if ( linkVal != -255) {
								$link.addClass('blocked');
							}else {
								$link.click();
							}
							break;
						default:
							if ( (linkVal <= 0 && linkVal != -255) || linkVal > maxValue) {
								$link.addClass('blocked');
								if ( $link.hasClass('current') ){
									$link.next().click();
								}
							}
					}
				});
			})
			
			$rights
				.hover(
					function(){},
					function(){$(this).data('init')()}
				)
				.find('a')
					.hover(
						function(){
							var $self 	= $(this);
							var $rights	= $self.parents('div.rights');
							var $group	= $self.parents('div.rights-group');
							var $groups	= $('div.rights-group',$rights);
							$('a',$rights).removeClass('highlight');
							var thisN = $('a',$group).index(this);
							$('a:eq('+thisN+'), a:gt('+thisN+')',$group).addClass('highlight');
						},
						function(){} // NO mouseout
					)
					.click(function(){
						var $self 	= $(this);
						if ( !$self.hasClass('blocked') ){
							var $rights = $self.parents('div.rights');
							$('a',$rights).removeClass('current highlight');
							var getVal = $self.attr('val')
							$rights.data('$input').val(getVal);
							$self.addClass('current highlight').blur();

							var $group 	= $self.parents('div.rights-group');
							var $groups = $('div.rights-group',$rights);
								$groups.removeClass('rg-current');
								$group.addClass('rg-current');
							var thisN = $('a',$group).index(this);
							$('a:gt('+thisN+')',$group).addClass('highlight');

							var $slaves = $rights.data('$slaves');
							if ( $slaves ) {
								$rights.data('$slaves').rightsSetMaxValue(getVal);
							}
							else{
							}

						}
						else{
							$self.blur();
						}
						return false;
					})
				.end().data('init')();
		});
	}
	$.fn.rightsSetSlaves = function($inputsRights){

		return this.each(function(){
			var $input = $(this);
			var $slaves = $inputsRights;
			$input.data('$rights').data('$slaves',$slaves);
		});
	}
	$.fn.rightsSetMaxValue = function(maxValue){
		
		return this.each(function(){
			var $input = $(this);
			$input.data('$rights').data('setMaxValue')(maxValue);
		});
	}
	
	$('input.inp-rights').rights();

	
	// open popup-rights
    //*/
	$('a.popup-rights-link').live('click',function()
	{
		var self = $(this);
			self.blur();
			//hideSelectsInIE(self.parents('div.group'));
		var popup = $('#popup-rights');
		if (popup.data('fromLink') && popup.data('fromLink')[0]== self[0]){
			popup.removeData('fromLink').slideUp();
			return false;
		}

		var fromInput = self.parents('div.field').find('input.popup-rights-input')
		var val = fromInput.val();

		var rights = $('div.rights',popup);

		var rTpl = self[0].className.match(/rt\d+/);
		if (rTpl){
			rights.html($('div.'+rTpl).children().clone());
			$('input.inp-rights').rights();
			initRightsPopup();
		}

		$('input',rights.parent()).val(val);
		rights.data('init')();
		popup
			.css({
				top: self.offset().top+16,
				left: self.offset().left
			})
			.data('fromLink',self)
			.data('fromInput',fromInput)
			.hide().slideDown()
			
		return false;
	});
	
	// select item in popup-rights & save in input
	function initRightsPopup(){
		$('#popup-rights div.rights a').click(function()
		{
			var self = $(this);
			var popup = self.parents('#popup-rights');
				popup.data('fromLink').text(self.text());
				popup.removeData('fromLink').slideUp();
				popup.data('fromInput').val($('input',popup).val());
	            popup.data('fromInput').change();
			//showSelectsInIE(popup.data('fromLink').parents('div.group'));
			
			return false;
		});
	}
	initRightsPopup();
	
	// close popup-rights
	$('#popup-rights h4 a').click(function()
	{
		var self = $(this);
			self.parents('#popup-rights').slideUp();
		
		//showSelectsInIE(popup.data('fromLink').parents('div.group'));

		return false;
	});
//*/
	
})

// end rights


// POPUP
$(function()
{
	$('div.popup a.btn-cancel, div.popup h4 a').live('click',function()
	{
		$(this).parents('div.popup').slideUp('fast');
		return false;
	});
})


