/* rx-library.js */


  
  // add classes to "first" and "last" children of node (fast routine)
  // 17/09/09
  jQuery.fn.RX_markSides = function(Q) {
	  jQuery.noConflict();
	  return this.each(function() {
		  jQuery(this).children(':first').addClass(Q.c_first);
		  jQuery(this).children(':last').addClass(Q.c_last);
	  });
  }
  //
  
  // menu drop down : toggling + callbacks
  // 24/08/09
  jQuery.fn.RX_menu = function(Q) {
	  jQuery.noConflict();
	  return this.each(function() {
		  jQuery(this).hover(
			  function() {
				  var t = jQuery(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 = jQuery(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) {
	  jQuery.noConflict();
	  this.init = function() {
		  jQuery(Q.c_par+Q.c_act).click(function() {
			  var i = jQuery(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 = jQuery(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) {
	  jQuery.noConflict();
	  this.each(function() {
		  jQuery(this).hover(
			  function() {
				  if (!jQuery(this).hasClass(Q.c_dis)) {
					  jQuery(this).animate({opacity:1}, 'fast');
				  }
			  },
			  function() {
				  if (!jQuery(this).hasClass(Q.c_dis)) {
					  jQuery(this).stop().animate({opacity:Q.hide}, 'fast');
				  }
			  }
		  );
	  });
  }
  //
  
  // open links in another window
  // 01/09/09
  jQuery.fn.RX_externalLink = function() {
	  jQuery.noConflict();
	  return this.each(function() {
		  jQuery(this).click(function() {
			  window.open(jQuery(this).attr('href'));
			  return false;
		  })
		  return;
	  });
  }
  //
  
  // gallery - no thumbs . pics resized
  // 15/01/10
  var RX_sneakPeak = function(Q) {
	  jQuery.noConflict();
	  var n_wrap = Q.n_elm.find(Q.c_wrap),
			  a_img = n_wrap.children('img'),
			  i, j,
			  C = 'rx-',
			  max = 0,
			  P = 1,
		FX = 1,
			  _busy = false;
  
	  this.init = function() {
		  if (a_img.size()<1) { return false; }
		  _resize();
  
	  if (n_wrap.hasClass(Q.fx[0]) ) {
		FX = 1;
	  } else if (n_wrap.hasClass(Q.fx[1]) ) {
		FX = 2;
	  }
  
		  Q.n_elm.find(Q.a_prev).click(function() {
			  if (_busy) { return false; }
			  if (P>1) {
				  _busy = true;
		  (FX == 1) ?
			n_wrap.animate({'left': '+='+Q.v_w}, 'fast', function() { _busy = false; }) :
			jQuery(a_img[P-2]).animate({'opacity': 1}, 'normal', function() { _busy = false; });
				  P--;
			  }
  
			  return false;
		  });
  
		  Q.n_elm.find(Q.a_next).click(function() {
			  if (_busy) { return false; }
  
			  if (P<max) {
				_busy = true;
		  (FX == 1) ?
				  n_wrap.animate({'left': '-='+Q.v_w}, 'fast', function() { _busy = false; }) :
			jQuery(a_img[P-1]).animate({'opacity': 0}, 'normal', function() { _busy = false; });
  
				  P++;
			  }
  
			  return false;
		  });
  
	  }
  
	  // correct paddings
	  function _padding(V, M) {
		  var i = M - V,
				  ax = Math.floor(i/2);
		  return {i1:ax, i2:(i-ax)}
	  }
	  //
  
	  // resize pics to viewport
	  // smaller pics are not enlarged,
	  // paddings to center is added
	  var _resize = function() {
		  var i = 0;
  
		  a_img.each(function() {
			  var I = jQuery(this),
					  h = parseInt(I.css('height')),
					  w = parseInt(I.css('width')),
					  cx = dx = 0,
					  ex = fx = 0,
					  ax = bx = false;
  
			  var f1 = function() {
				  I.css('height', Q.v_h).css('width', ax).css('padding-left', cx.i1).css('padding-right', cx.i2);
			  }, f2 = function() {
				  I.css('width', Q.v_w).css('height', bx).css('padding-top', dx.i1).css('padding-bottom', dx.i2);
			  }
  
			  if (h>Q.v_h) {
				  ax = Math.ceil( w / (h/Q.v_h) );
				  cx = _padding(ax, Q.v_w);
			  } else {
				  ex = _padding(h, Q.v_h);
			  }
  
			  if (w>Q.v_w) {
				  bx = Math.ceil(h / (w/Q.v_w));
				  dx = _padding(bx, Q.v_h);
			  } else {
				  fx = _padding(w, Q.v_w);
			  }
  
			  if (ax && bx) {
				  if (ax<bx) { f1(); } else { f2(); }
			  } else if (ax) {
				  f1();
			  } else if (bx) {
				  f2();
			  } else {
				  I.css('padding-top', ex.i1).css('padding-bottom', ex.i2).
			css('padding-left', fx.i1).css('padding-right', fx.i2);
			  }
  
			  I.addClass(C+ ++i).css('z-index', 79 - i);
		  });
		  max = i;
		  n_wrap.css('width', (max+1)*Q.v_w);
	  }
	  //
  }
  //
  
  // popping anchors
  jQuery.fn.RX_popping = function() {
	  jQuery.noConflict();
	  jQuery(this).children('a').hover(
		  function() {
			  jQuery(this).animate({ 'top' : 0 }, 79);
		  },
		  function() {
			  jQuery(this).animate({ 'top' : 26 }, 79);
		  }
	  );
  };
  //
  
  // post toggling + scroll document to post
  // 19/09/09
  var RX_postToggle = function(Q) {
	  jQuery.noConflict();
	  var _busy = false;
  
	  jQuery(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 = jQuery(this).parents(Q.c_par);
		  i.find('.'+Q.c_hide).slideDown('fast', function() {
			  jQuery().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) {
	  jQuery.noConflict();
	  return this.each(function() {
		  jQuery(this).click(function() {
			  jQuery().scrollTo( jQuery(this).parents(Q.par).find(Q.elm), 'normal');
		  });
	  });
  }
  //
  
  // mouse wheel & slider
  // 30.09.09
  jQuery.fn.RX_slider = function(Q) {
	  jQuery.noConflict();
	  if (jQuery.browser.mozilla) { _wheel_coef = 39; }
	  else if(jQuery.browser.safari) { _wheel_coef = 29; }
	  else { _wheel_coef = 34; }
  
	  this.each(function() {
		  var n_par = jQuery(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) {
	  jQuery.noConflict();
	  return this.each(function() {
		  var par = jQuery(this);
  
		  jQuery.each(Q.elm, function(i, item) {
			  par.find('a'+item).click(function() {
				  par.find('form'+item).slideToggle('fast');
				  return false;
			  });
		  });
  
	  });
  }
  //
  
  // sliding menu
  jQuery.fn.RX_slideMenu = function(Q) {
	  jQuery.noConflict();
	  this.each(function() {
		  var I = jQuery(this);
  
		  I.children('li').mouseenter(function() {
				  var i = jQuery(this).position();
				  I.stop().animate({backgroundPosition:i.left}, '79');
		  });
	  });
  }
  //
  
  
  jQuery(document).ready(function($) {
	  jQuery('a.rx-el, .rx-el a').RX_externalLink();
  
	  jQuery('.x-comment').RX_slider({
		  n_scroll: '.screen',
		  n_hiding: '.screen',
		  n_slider: '.rx-slider'
	  });
  
	  jQuery('.x-comment').RX_toggleBlocks({
		  elm : [ '.rx-add', '.rx-send' ]
	  });
  
	  jQuery('.x-archives ul').RX_markSides({
		  c_first : 'rx-first',
		  c_last : 'rx-last'
	  });
  
	  jQuery('.rx-view').RX_scrollTo({
		  par : '.x-post',
		  elm : '.x-comment'
	  });
  
	  jQuery('.menu-navy').RX_slideMenu({
		  select : 'rx-sel'
	  });
  
	  if (!jQuery.browser.msie) {
		  /*jQuery('.f-contact .rx-validate, .f-comment .rx-validate, .x-about em a').RX_smoothHover({
			  c_dis : 'rx-disabled',
			  c_elm : 'a',
			  hide : .79
		  });*/
  
		  jQuery('.s-social a').RX_smoothHover({
			  c_dis : 'rx-disabled',
			  c_elm : 'a',
			  hide : .6
		  });
	  }
  
	  jQuery('.x-comment .screen li:last').addClass('rx-last');
  
  
	  var rx = new RX_sneakPeak({
		  n_elm : jQuery('.rx-gallery'),
		  a_prev : 'em .prev',
		  a_next : 'em .next',
		  c_wrap : 'p strong',
	  fx : ['rx-slide', 'rx-opacity'],
		  v_h : 354,
		  v_w : 900
	  });
	  rx.init();
  
  });

