//11.06.09
//
// 16.12.2011 Victor D.
// COMMENTS - MOUSE WHEEL & SLIDER
jQuery.fn.rx_scroll_comment = function() {
	if ($.browser.mozilla) {
      _wheel_coef = 30;
	} else if($.browser.safari) {
			_wheel_coef = 20;
	}	else {
		_wheel_coef = 25;
	}

	// mouse wheel over the comments area
	$('.w-comment-list').bind('mousewheel', function(event, delta) {
			var node = $(this).find('.w-hide');
			if ( node.attr('scrollHeight') > node.outerHeight() ) {
				var speed = node.height() / node.attr('scrollHeight') * (_wheel_coef);
				var nv = node.scrollTop() - delta * speed;
				node.scrollTop(nv);
				var s_hande =  node.scrollTop() / ( node.attr('scrollHeight') -  node.height() ) * 100;
				$(this).parents('.x-comment').find('.slide-comment').slider('option', 'value', -s_hande );
				return false;
			}
			return true;
	});

	// slide comments
	$('.slide-comment').each(function() {
		$(this).slider({
			animate: false,
			orientation: "vertical",
			min: -100,
			max: 0,
			slide: function(event, ui) {
				var node = $(this).parents('.x-comment').find('.w-hide');
				if ( node.attr('scrollHeight') > node.outerHeight() ) {
					var ratio = node.attr("scrollHeight") - node.height();
					node.attr( {scrollTop: -ui.value * (ratio / 100)} );
				} else {
					return false;
				}
				return true;
			}
		});
	});
}
//

// FORM AUTOVALUES
jQuery.fn.rx_form = function() {
	function populateElement(selector, defvalue) {
		$(selector).focus(function() {
			if ($(selector).val() == 'Website') {
				$(selector).val('http://');
			} else if ($(selector).val() == defvalue) {
				$(selector).val('');
			}
		});

		$(selector).blur(function() {
				if ($.trim($(selector).val()) == '') {
						$(selector).val(defvalue);
				}
		});
	};

	return this.each(function() {
		// $(this).log();
		populateElement($(this), $(this).val());
	});
}
//

// FORM VALIDATOR
jQuery.fn.rx_validate = function() {
	function validateNoempty(text) {
		if (!text || text.length < 3) {return false;}
		return true;
	}

	function validateText(text) {
		if (!validateNoempty(text)) {return false;}
		var re_text = /[^A-Za-z0-9\s]/;
		if (text.match(re_text) != null ) {return false;}
		return true;
	}

	function validateEmail(email) {
		if (!validateNoempty(email)) {return false;}

		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) {return false;}
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) {return false;}
			}
			return true;
		}
		return false;
	}

	return this.each(function() {
		$(this).click(function(event) {
			event.preventDefault();
			error = false;

			$(this).parents('form').find('input[type="text"], textarea').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						$(this).animate( {opacity: 0.2}, 175 ).animate( {opacity: 1}, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						$(this).animate( {opacity: 0.2}, 175 ).animate( {opacity: 1}, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-mail')) {
					if (!validateEmail($(this).val())) {
						$(this).animate( {opacity: 0.2}, 175 ).animate( {opacity: 1}, 150 );
						error = true;
					}
				}
			});

			if (!error) {
				var node = $(this).parents('.js-hidebox');
				node.slideUp('normal');
				$(this).parents('.x-post').find('.w-note').slideDown('fast');
				$(this).parents('form').submit();
			}
		});

	});
}
//

// TOP MENU
jQuery.fn.rx_menu = function() {
	return this.each(function() {
		$(this).hover(
			function() {
				$(this).children('ul').
					css( {visibility:'visible', display:'none'}).
					show('fast', function() {_busy = false;} );
			},
			function() {
				$(this).children('ul').css( {visibility:'hidden'} );
			}
		);
	});
}
//

// LOGGING
jQuery.fn.log = function (msg) {
		console.log("%s: %o", msg, this);
		return this;
};
//

// BLOG JS
$(document).ready(function(){
	$('ul.js-automenu>li').rx_menu();

	$('input[type="text"], input[type="password"], textarea').rx_form();

	$('.js-proceed').rx_validate();

	$().rx_scroll_comment();

	// open link in another window
	$('a.js-el').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	// open link full screen
	$('a.js-fullscreen').click(function(event) {
		event.preventDefault();
		var v = 'width='+screen.width + ', height='+screen.height + ', top=0, left=0' + ', fullscreen=yes';
		var p = window.open( $(this).attr('href'), 'jswindowname4', v);
		if (window.focus) {p.focus()}
		return false;
	});

	// count chars in textarea
	var _char_count = 500;
	$('.f-pic-action textarea').keydown( function() {
		var node = $(this).parents('form').find('.js-count-char').children('em');
		if ($(this).val().length >= _char_count) {
			$(this).val( $(this).val().substr(0, _char_count) );
		}
		node.html( _char_count - $(this).val().length );
	});


	// comments - hide form
	$('.js-hide').click( function(event) {
			event.preventDefault();
			$(this).parents('.js-hidebox').slideUp('fast');
		}
	);

	// comments - add comment
	$('a[href*="#js-add-comment"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').children('.f-add-comment');
			node.slideToggle('fast');
		}
	);

	// comments - send mail
	$('a[href*="#js-send-mail"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').children('.f-send-to-friend');
			node.slideToggle('fast');
		}
	);

	// comments - toggle on and off
	$('a[href*="#js-toggle-comment"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').find('.w-comment-list');
			node.slideToggle('fast');
		}
	);

	// comments - on this picture
	$('a.js-add-comment').click( function(event) {
		event.preventDefault();

		var j = $(this).parents('.x-post');
				node = j.find('.f-add-comment'),
				tarea = j.find('.js-ta-comm'),
				ax = 'js-add-comment js-img-id-',
				k = $(this).attr('class');

		$(this).parents('.x-post').find('.w-note').slideUp('fast');
		
		k = '{img}' + k.substr(ax.length, 255) + '{/img}';
		tarea.val( tarea.val() + k );

		$().scrollTo(
			j.find('.x-comment'), 'fast', function() {if (node.css('display') == 'none') {node.slideDown('fast');}}
		);

		return false;
	});

	$('.w-pic').hover(
		function() {$(this).find('.js-add-comment').animate( {opacity: .8}, 'fast' );},
		function() {$(this).find('.js-add-comment').animate( {opacity: 0}, 'fast' );}
	);

	// comments - back link
	$('.w-hide').click(function(event) {
		event.preventDefault();
		var e = $(event.target),
				node = $(this).parents('.x-post');

		if (e.is('a')) {
			$().scrollTo( node.find('.anchor-' + e.attr('href').substr(1)), 'fast' );
			return false;
		}
		return true;
	});
        
        // login/register area
        $('#log').click(function(e){
            e.preventDefault();
           $('#login-area').fadeIn(600);
           $('#login-area .login').fadeIn(1);
           $('html').css('overflow','hidden');
        });
        
        $('#login-area .login .x a').click(function(e){
            e.preventDefault();
            $(this).parents('#login-area').fadeOut(600);
            $(this).parents('.login').fadeOut(1);
           $('html').css('overflow','visible');
        });
        
        // register
        $('#reg').click(function(e){
            e.preventDefault();
           $('#login-area').fadeIn(600);
           $('#login-area .register').fadeIn(1);
           $('html').css('overflow','hidden');
        });
        
        $('#login-area .register .x a').click(function(e){
            e.preventDefault();
            $(this).parents('#login-area').fadeOut(600);
            $(this).parents('.register').fadeOut(1);
           $('html').css('overflow','visible');
        });
        $('#login-area .register a.log_in').click(function(){
            $(this).parents('.register').fadeOut(300);
            $(this).parents('#login-area').find('.login').fadeIn(300);
        });
        $('#login-area .login .registr').click(function(){
            $(this).parents('.login').fadeOut(300);
            $(this).parents('#login-area').find('.register').fadeIn(300);
        });
        
        // disable fadeout if clicked on form area;
        /*$('#login-area form').click(function(e){
            e.preventDefault();
            return false;
        });*/

});
//

