$(function() {
	
	//$('#gadh').css('display','block').fadeTo(0,0);
	
	/* show-hide */
	$.fn.toggle = function() {
		$(this).click(function() {
			var tId = $(this).attr("href");
			if (tId.indexOf("#")>0) tId = tId.substring(tId.indexOf("#")); // fix IE
			$(tId).slideToggle(250);
			return false;
		});
	}
	$('a.toggle').toggle();
	
	/* color animations */
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( !fx.colorInit ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
				fx.colorInit = true;
			}
			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});
	function getRGB(color) {
		var result;
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
	}
	function getColor(elem, attr) {
		var color;
		do {
			color = jQuery.curCSS(elem, attr);
			
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break;

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	$("#error").animate( { backgroundColor: '#ffebeb' }, 600);
	$("#ok").animate( { backgroundColor: '#d5f9ca' }, 600);
	
	/* switch user*/
	$.fn.switchuser = function() {
		var _active = $(this).find('.s');
		$('#login_mode_user, #login_mode_guest').click(function() {
			if (_active.attr('id') != $(this).attr('id')) {
				_active.removeClass('s');
				_active = $(this);										   
				var modeUser = _active.attr('id') == 'login_mode_user';
				_active.addClass('s');
				$('#mode_user').css('display', modeUser ? 'block' : 'none');
				$('#mode_guest').css('display', modeUser ? 'none' : 'block');
				$('#captchaform').css('display', modeUser ? 'none' : 'block');
				$('#login_mode').val(modeUser ? 1 : 2);
			}
		});
	}
	
	$('#switchuser').switchuser();
});
