// COMMENTS - MOUSE WHEEL & SLIDER // 10.07.09
jQuery.fn.rx_scrollComment = function(o) {
	if ($.browser.mozilla) { _wheel_coef = 39; } else if($.browser.safari) { _wheel_coef = 29; } else { _wheel_coef = 34; }

	// mouse wheel over the comments area
	$(o.n_scroll).bind('mousewheel', function(event, delta) {
			var node = $(this).find(o.n_hiding);

			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(o.n_parent).find(o.n_slider).slider('option', 'value', -s_hande );
				return false;
			}
			return true;
	});

	// slide comments
	$(o.n_slider).each(function() {
		$(this).slider({
			animate: false,
			orientation: "vertical",
			min: -100,
			max: 0,
			slide: function(event, ui) {

				var node = $(this).parents(o.n_parent).find(o.n_hiding);
				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 01.05.09
jQuery.fn.rx_form = function() {
	function populateElement(selector, defvalue) {
		$(selector).focus(function() {
			if ($(selector).val() == 'Website') {
				$(selector).val('www.');
			} 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 01.05.09
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 // 09.06.09
jQuery.fn.rx_menu = function() {
	this.each(function() {
		$(this).hover(
			function() {
				$(this).children('ul').css( {visibility:'visible', display:'none'}).show('fast' );
			},
			function() {
				$(this).children('ul').css( { visibility:'hidden' } );
			}
		);
	});
}
//

// POST OPEN/CLOSE // 19.07.09
jQuery.fn.rx_postToggler = function(o) {
	var _busy = false,
			c_on = 'js-post-opened',
			c_load = 'js-loaded';

	$(o.elm).click(function() {
		if (_busy) { return; }
		_busy = true;

		var i = $(this).parents(o.par);

		if (i.hasClass(c_on)) {
			i.removeClass(c_on);
			i.find(o.tgl).slideUp('fast', function() { _busy = false; });
		} else {

			var j = $('.'+c_on);
			j.removeClass(c_on);
			j.find(o.tgl).css('display', 'none');

			if (!i.hasClass(c_load)) {
				i.find('.w-pic').each(function() {
					var i = $(this).find('span.post-picture'),
							o = {
								id : i.attr('id'),
								cl : i.attr('class'),
								w  : i.css('width'),
								h  : i.css('height'),
								ttl : i.attr('title'),
								src : i.attr('source')
							}

					$(this).prepend('<img src="'+o.src+'" id="'+o.id+'" class="'+o.cl+'" alt="'+o.ttl+'" title="'+o.ttl+'" style="width:'+o.w+'; height:'+o.h+'" />');
					i.remove();
				});
			}

			i.addClass(c_load);
			i.addClass(c_on);
			i.find(o.tgl).slideDown('fast', function() { _busy = false; });
			$().scrollTo(i.find('.a-title'), 'slow');
		}

		return false;
	});

	$(o.par).find('a[href*="js-close-post"]').click(function() {
		var i = $(this).parents(o.par);
		i.removeClass(c_on);
		$().scrollTo(i, 'fast', function() {
				i.find(o.tgl).slideUp('normal', function() { _busy = false; });
		});

		return false;
	});

}
//

// SOCIAL ICONS BEWTIFIER // 20.07.09
jQuery.fn.rx_socialLinks = function() {
	$(this).find('a').hover(
		function() { $(this).animate({'opacity' : 1 }, 'fast'); },
		function() { $(this).animate({'opacity' : .6 }, 79); }
	);

	$(this).find('a').each(function() {
		$(this).attr('title', $(this).children('sup').html() );
	});
}
//

// BLOG JS
$(document).ready(function(){

	$.scrollTo.defaults.axis = 'y';

	$('ul.menu-main>li').rx_menu();

	$('input[type="text"], input[type="password"], textarea').rx_form();

	$('.js-proceed').rx_validate();

	$().rx_postToggler({
		par : '.x-post',
		elm : '.js-post-toggle',
		tgl : '.post-body'
	});

	$().rx_scrollComment({
    n_parent: '.x-comment',
    n_scroll: '.w-comment-list',
    n_hiding: '.w-hide',
    n_slider: '.slide-comment'
  });

	$('.i-social').rx_socialLinks();

	// open link in another window
	$('a.js-el, .js-el a').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;
	});








	$('.w-pic').hover(
		function() { $(this).find('.js-add-comment').animate( { opacity: .8}, 'fast' ); },
		function() { $(this).find('.js-add-comment').animate( { opacity: 0 }, 'fast' ); }
	);

	

});
//

$(document).ready(function() {
	setTimeout(function() { $('.x-twitter div').animate({top:21}, 'normal', 'swing'); }, 500);

	/*
	var i = $('.x-archives');
	i.find('.xa-l, .xa-r').css('height', i.outerHeight());

	// var msie = $.browser.msie;

	function setDocScroll() {
		if (window.innerWidth > 1000) {
			if (!$('html').hasClass('js-scroll')) { $('body').addClass('js-scroll'); }
		} else {
			if ($('html').hasClass('js-scroll')) { $('body').removeClass('js-scroll') }
		}
	}

	$(window).load(function() { setDocScroll(); });
	$(window).resize(function() {	setDocScroll(); });
	*/
});
