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

// menu drop down : toggling + callbacks
// 24/08/09
jQuery.fn.RX_menu = function(Q) {
	return this.each(function() {
		$(this).hover(
			function() {
				var t = $(this),
						i = t.children('ul');

				if(i.size()) {
					t.addClass('rx-selected');
					i.css( {visibility:'visible', display:'none'}).slideDown('fast');
					//Q.onHover();
				}
			},
			function() {
				var t = $(this),
						i = t.children('ul');

				if(i.size()) {
					t.removeClass('rx-selected');
					i.css( {visibility:'hidden' } );
					//Q.onOut();
				}
			}
		);
	});
}
//

// menu clickable : toggling
// 19/09/09
var RX_clickMenu = function(Q) {
	this.init = function() {
		$(Q.c_par+Q.c_act).click(function() {
			var i = $(this).parent(Q.c_node);

			if (!i.hasClass(Q.select)) {
				hideNode();
				i.find(Q.c_elm).slideDown('fast');
				i.addClass(Q.select);
			} else {
				hideNode();
			}
			return false;
		})
	}
	//

	function hideNode() {
		var i = $(Q.c_par+'>'+Q.c_node+'.'+Q.select);
		i.removeClass(Q.select);
		i.find(Q.c_elm).slideUp('fast');
	}
	//
}
//

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

// carousel - sliding gallery : used for thumbs mostly
// 20/09/09
jQuery.fn.RX_carousel = function(Q) {
	this.each(function() {
		var n_par  = $(this),
				n_nav  = n_par.children(Q.c_act),
				n_prev = n_nav.children(Q.c_prev),
				n_next = n_nav.children(Q.c_next),
				n_ul   = n_par.find(Q.c_wrap).children('ul'),
				n_li   = n_ul.children('li'),
				n_li_f = n_ul.children('li:first'),

				i = n_par.attr('class').match(/rx-items\-\d{1}/),
				i_min = ( (i !== null) ? i.toString().substr(9) : '0' ),
				i_real = n_li.size(),
				i_lim = (i_real - i_min) / ( (Q.step_s) ? Q.step_s : 1 ),

				vert = $(this).hasClass(Q.c_vert),
				step = (vert) ? parseFloat(n_li_f.outerHeight(true)) : parseFloat(n_li_f.outerWidth(true)),
				step = (Q.step_o) ? Q.step_o * ( (Q.step_s) ? Q.step_s : 1 ) : step,
				pivot  = 0,
				_busy  = false;

		if (i_real <= i_min) {
			n_nav.find('a').addClass(Q.c_dis);
			return;
		}

		n_prev.hover(
			function() { if (pivot>0) { $(this).addClass(Q.c_hov); } },
			function() { $(this).removeClass(Q.c_hov); }
		);

		n_next.hover(
			function() { if (pivot < i_lim) { $(this).addClass(Q.c_hov); } },
			function() { $(this).removeClass(Q.c_hov); }
		);

		n_prev.click(function() {
			if (pivot-1 >= 0) {
				if (_busy) { return false; }
				_busy = true;
				pivot--;
				if (vert) {
					n_ul.animate( { top : '+='+step }, 'normal', function() { _busy = false; } );
				} else {
					n_ul.animate( { left : '+='+step }, 'normal', function() { _busy = false; } );
				}
			}
			return false;
		});

		n_next.click(function() {
			if (pivot+1 <= i_lim) {
				if (_busy) { return false; }
				_busy = true;
				pivot++;
				if (vert) {
					n_ul.animate( { top : '-='+step }, 'normal', function() { _busy = false; } );
				} else {
					n_ul.animate( { left : '-='+step }, 'normal', function() { _busy = false; } );
				}
			}
			return false;
		});

		return;
	});

}
//

// gallery : click on thumbs - switch big picture
// 20/09/09
jQuery.fn.RX_miniGallery = function(Q) {
	this.each(function() {
		function changePath(node) {
			var i = node.attr('src'),
					j = i.substr(0, i.indexOf(Q.p_t));
					k = j + Q.p_m;

			i_new.attr('href', k);
			i_new.children('img').attr('src', k);
			fx_opacity();
		}
		//

		function fx_cleanup() {
			var i = i_new;

			i_new.removeClass(Q.c_new).addClass(Q.c_old);
			i_old.removeClass(Q.c_old).addClass(Q.c_new);

			i_new = i_old;
			i_old = i;

			_busy = false;
		}
		//

		function fx_opacity(old_e, new_e) {
			_busy = true;

			i_old.children('img').animate({ opacity: 0 }, 'normal');
			i_new.children('img').animate({ opacity: 1 }, 'normal', fx_cleanup);
		}
		//

		var n_par  = $(this),
				i_new, i_old,
				_busy = false,
				i, j;

		i = n_par.find(Q.c_img);
		i_old = i.children(':first');
		i_old.addClass(Q.c_old);
		j = i_old.children('img').attr('src');

		i.append('<a href="#" class="' + Q.c_new + '"><img src="'+j+'" alt="" /></a>');
		i_new = i.children(':last');

		n_par.find(Q.c_act).click(function(e) {
			var i = $(e.target);

			if (_busy) { return false; }
			if (i.is('img')) {
				if (! i.parent('a').hasClass(Q.select)) {
					changePath(i);

					n_par.find('.' + Q.select).removeClass(Q.select);
					i.parents('a').addClass(Q.select);
				}
			}

			return false;
		});

		if (Q.preload) {
			i = '<span class="'+Q.preload+'">';
			n_par.find(Q.c_act).find('img').each(function() {
				i+= '<img src="'+$(this).attr('src')+'" alt="" />';
			});
			i += '</span>';
			$('body').append(i);
		}
	});
}
//

// popping anchors
jQuery.fn.RX_popping = function() {
	$(this).children('a').hover(
		function() {
			$(this).animate({ 'top' : 0 }, 79);
		},
		function() {
			$(this).animate({ 'top' : 26 }, 79);
		}
	);
};
//

// post toggling + scroll document to post
// 19/09/09
var RX_postToggle = function(Q) {
	var _busy = false;

	$(Q.c_par).find(Q.c_act).click(function() {
		if (_busy) { return false; }
		_busy = true;

		hidePost(Q.c_wrap.find(Q.c_par+'.'+Q.select));

		var i = $(this).parents(Q.c_par);
		i.find('.'+Q.c_hide).slideDown('fast', function() {
			$().scrollTo(i, 'slow',
				{ onAfter:function() {
						i.find('.'+Q.c_show).slideUp('fast');

						_busy = false;
					}
				}, {offset: '0'} );
		});
		i.addClass(Q.select);

		return false;
	});

	function hidePost(node) {
		node.find('.'+Q.c_show).css('display', 'block');
		node.find('.'+Q.c_hide).css('display', 'none');
		node.removeClass(Q.select);
	}
	//
}
//

jQuery.fn.RX_scrollTo = function(Q) {
	return this.each(function() {
		$(this).click(function() {
			$().scrollTo( $(this).parents(Q.par).find(Q.elm), 'normal');
		});
	});
}
//


// mouse wheel & slider
// 30.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);

		n_par.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;

					n_par.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);

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

	});
}
//

// open/close elements
// 30/10/09
jQuery.fn.RX_toggleBlocks = function(Q) {
	return this.each(function() {
		var par = $(this);

		$.each(Q.elm, function(i, item) {
			par.find('a'+item).click(function() {
				par.find('form'+item).slideToggle('fast');
				return false;
			});
		});

	});
}
//

jQuery(document).ready(function() {
	$('a.rx-el, .rx-el a').RX_externalLink();

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

	$('.x-comment').RX_toggleBlocks({
		elm : [ '.rx-add', '.rx-send' ]
	});

	$('.x-archives ul').RX_markSides({
		c_first : 'rx-first',
		c_last : 'rx-last'
	});

	$('.rx-view').RX_scrollTo({
		par : '.x-post',
		elm : '.x-comment'
	});

	$('.menu-navy li').RX_menu();

	/*
		c_1:'rx-first', c_2:'rx-last'
	})
	*/

/*
	var rx_menu1 = new RX_clickMenu({
		c_par  : '.menu-sect',
		c_node : 'li',
		c_act  : '>li>a',
		c_elm  : 'ul',
		select : 'rx-selected'
	});
	rx_menu1.init();

	$('.x-post .cover').RX_smoothHover({
		c_dis : 'rx-disabled',
		c_elm : 'a',
		hide : 0
	});

	var rx_post = RX_postToggle({
		c_wrap : $('.l-root .l-left'), // main parent for all posts
		c_par  : '.x-post',
		c_act  : '.cover a',
		c_hide : 'rx-hide',
		c_show : 'rx-show',
		select : 'rx-selected'
	});

	$('.x-gallery').RX_carousel({
		c_prev : '.nav-prev',
		c_next : '.nav-next',
		c_wrap : '.screen',
		c_act  : '.z-action',
		c_dis  : 'rx-disable',
		c_hov  : 'rx-hover',
		c_vert : 'rx-vertical', // if rx-vertcial class is set - scrolling is vertical
		step_o : 128, // li width/height is unknown in display:none so it must be set here
		step_s : 5 // if set - thumbs will scroll with this step
	});

	$('.x-gallery').RX_miniGallery({
		c_img : 'em',
		c_act : '.screen',
		c_new : 'rx-new',
		c_old : 'rx-old',

		p_f: 'images-gallery',
		p_t: '-thumb.jpg',
		p_m: '-view.jpg',
		select : 'rx-selected',
		preload : 'rx-preload' // if is set - all pictures for all posts will be preloaded
	});

	$('.set-social-oa').RX_popping();
*/

});

