/* rx-library.js */

// add classes to "first" and "last" children of node (fast routine)
// 17/09/09
jQuery.fn.RX_markSides = function(Q) {
	return this.each(function() {
		$(this).children(':first').addClass(Q.c_first);
		$(this).children(':last').addClass(Q.c_last);
	});
}
//

// add classes to "first" and "last" item inside UL. Used for clearing floats.
// use: <ul class="rx-rowsplit rx-rows-3">. 3 - number of items in one row
// 24/08/09
jQuery.fn.RX_rowSplit = function(Q) {
	return this.each(function() {
		var k = 1;
				i = $(this).attr('class').match(/rx-rows\-\d{1}/),
				n = i.toString().substr(8);

		$(this).children('li').each(function() {
			if (k == 1) {
				$(this).addClass(Q.c_first);
			} else if (k == n) {
				$(this).addClass(Q.c_last);
			}

			k = (k == n) ? 1 : k+1 ;
		});

		k = $(this).children('li:last');
		if (!k.hasClass(Q.c_last)) {
			k.addClass(Q.c_last);
		}
	});
}
//

// menu drop down : toggling + callbacks
// 24/08/09
jQuery.fn.RX_menu = function(Q) {
	return this.each(function() {
		$(this).hover(
			function() {
				var i = $(this).children('ul');
				if(i.size()) {
					i.css( {visibility:'visible', display:'none'}).show('fast');
					Q.onHover();
				}
			},
			function() {
				var i = $(this).children('ul');
				if(i.size()) {
					i.css( {visibility:'hidden' } );
					Q.onOut();
				}
			}
		);
	});
}
//

// smooth hover effect based on animated opacity
// 19/09/09
jQuery.fn.RX_smoothHover = function(Q) {
	this.each(function() {
		$(this).hover(
			function() {
				if (!$(this).hasClass(Q.c_dis)) {
					$(this).children('a').animate({opacity:1}, 'fast');
				}
			},
			function() {
				if (!$(this).hasClass(Q.c_dis)) {
					$(this).children('a').animate({opacity:Q.hide}, 'fast');
				}
			}
		);
	});
}
//

// open links in another window
// 01/09/09
jQuery.fn.RX_externalLink = function() {
	return this.each(function() {
		$(this).click(function() {
			window.open($(this).attr('href'));
			return false;
		})
		return;
	});
}
//

// mouse wheel & slider
// 24.09.09
jQuery.fn.RX_slider = function(Q) {
	if ($.browser.mozilla) { _wheel_coef = 39; }
	else if($.browser.safari) { _wheel_coef = 29; }
	else { _wheel_coef = 34; }

	this.each(function() {
		var n_par = $(this);

		$(this).find(Q.n_scroll).bind('mousewheel', function(event, delta) {
				var node = n_par.find(Q.n_hiding);

				if ( node.attr('scrollHeight') > node.outerHeight() ) {
					var speed = node.height() / node.attr('scrollHeight') * (_wheel_coef),
							nv = node.scrollTop() - delta * speed;
					node.scrollTop(nv);
					var s_hande =  node.scrollTop() / ( node.attr('scrollHeight') -  node.height() ) * 100;

					$(this).parents(Q.n_parent).find(Q.n_slider).slider('option', 'value', -s_hande );
					return false;
				}

				return true;
		});

		n_par.find(Q.n_slider).slider({
			animate: false,
			orientation: "vertical",
			min: -100,
			max: 0,
			slide: function(event, ui) {
				var node = n_par.find(Q.n_hiding);
				// alert(node.attr('class'));

				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;
			}
		});
		//

	});
}
//

jQuery(document).ready(function() {
	$('.menu-navy, .f-comment .f1, .x-archives ul').RX_markSides({
		c_first : 'rx-first',
		c_last : 'rx-last'
	});

	$('a.js-el, .js-el a').RX_externalLink();

	$('.x-comment').RX_slider({
		n_scroll: '.screen',
		n_hiding: '.screen',
		n_slider: '.rx-slider'
	});

});

