jQuery(document).ready(function($) {
/* 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).animate({opacity:1}, 'fast');
				}
			},
			function() {
				if (!$(this).hasClass(Q.c_dis)) {
					$(this).stop().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;
	});
}
//

// 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 + show state + external open
// 25/01/10
var RX_postToggle_fx = function(Q) {
  var _busy = false;

  var f_show = function(I, state, func) {
    _busy = true;

    var i = $('.'+Q.select);
    i.removeClass(Q.select);
    i.find(Q.c_hide).slideDown('fast');
    i.find(Q.c_show).css('display', 'none');
    tx_change(i, true);

    I.find(Q.c_hide).slideUp('normal', function() {});

    I.find(Q.c_show).slideDown('fast', function() {
      I.addClass(Q.select);
      tx_change(I, false);

      if (!state) {
        $().scrollTo(I, 'fast', { onAfter:function() {
              _busy = false;
              if (func) { func(); }
            }
          }, {offset: '0'}
        );
      } else {
        _busy = false;
        if (func) { func(); }
      }

    });
  }, f_hide = function(I) {
    $().scrollTo(I, 'fast', {
      onAfter:function() {
        I.find(Q.c_show).slideUp('fast');
        tx_change(I, true);
        var i = I.find(Q.c_hide);
        if (i.size()) {
          i.slideDown('normal', function() {
            I.removeClass(Q.select);
            _busy = false;
          });
        } else {
            I.removeClass(Q.select);
            _busy = false;
        }
      }
    }, {offset: '0'} );
  }
  //

  var tx_change = function(I, state) {
    var i = I.find(Q.a_toggle).children('span');
    (state) ? i.text(Q.c_txt[0]) : i.text(Q.c_txt[1]);
  }
  //

  this.init = function() {
    $(Q.c_elm).each(function() {
      var I = $(this);

      I.find(Q.a_open).click(function() {
        f_show(I);
        return false;
      });

      I.find(Q.a_close).click(function() {
        f_hide(I);
        return false;
      });

      I.find(Q.a_toggle).click(function() {
        if (I.hasClass(Q.select)) {
          f_hide(I);
        } else {
          f_show(I);
        }
        return false;
      });
    });
  }
  //

  this.openPost = function(elm, func) {
    if (!elm.hasClass(Q.select)) {
      f_show(elm, false, func);
    } else {
      func();
    }
  }
  //

}
//

// scrollTo element
// 25/01/10
jQuery.fn.RX_scrollTo = function(Q) {
	return this.each(function() {
		$(this).click(function() {
      var i = $(this).parents(Q.par),
          f1 = function() {
            $().scrollTo( i.find(Q.elm), 'normal');
          }
      Q.on_f(i, f1);

      return false;
		});
	});
}
//

// mouse wheel & slider
// 30.09.09
jQuery.fn.RX_scroller = 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;
			});
		});

	});
}
//

// sliding menu
jQuery.fn.RX_slideMenu = function(Q) {
	this.each(function() {
		var I = $(this);

		I.children('li').mouseenter(function() {
				var i = $(this).position();
				I.stop().animate({backgroundPosition:i.left}, '79');
		});
	});
}
//

// gallery - no html changed. pics resized
// 20/01/10
jQuery.fn.RX_picSneak = function(Q) {
  this.each(function() {
    var I = $(this),
        n_wrap = I.find(Q.c_wrap),
        a_img = n_wrap.children(),
        i, j,
        C = 'rx-',
        max = 0,
        P = 1,
        FX = 1,
        _busy = false,
        n_thumb = I.find(Q.c_thumb);

    // 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
    // paddings to center is added
    var _resize = function() {
      var i = 0;

      a_img.each(function() {
        var I = $(this);

        if (I.is('img')) {
          I.addClass(C+ ++i).css('z-index', 79 - i);
        } else {
          I.addClass(C+ ++i).css('z-index', 79 - i);
          I = I.children('img');
        }

        var h = parseInt(I.css('height')),
            w = parseInt(I.css('width')),
            cx = h/Q.v_h,
            dx = w/Q.v_w,
            ax, bx;

        if (cx>=dx) {
          ax = Math.ceil( w / cx );
          bx = _padding(ax, Q.v_w);

          I.css('height', Q.v_h).css('width', ax).
            css('padding-left', bx.i1).css('padding-right', bx.i2);

        } else if (cx<dx) {

          ax = Math.ceil( h / dx );
          bx = _padding(ax, Q.v_h);

          I.css('width', Q.v_w).css('height', ax).
            css('padding-top', bx.i1).css('padding-bottom', bx.i2);
        }

      });
      max = i;
      n_wrap.css('width', (max+1)*Q.v_w);
    }
    //

		if (a_img.size()<1) { return false; }
		_resize();

    if (n_wrap.hasClass(Q.fx[0]) || n_thumb.size() ) {
      FX = 1;
    } else if (n_wrap.hasClass(Q.fx[1]) ) {
      FX = 2;
    }

		I.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; }) :
          $(a_img[P-2]).animate({'opacity': 1}, 'normal', function() { _busy = false; });
				P--;
			}

			return false;
		});
		//

		I.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; }) :
          $(a_img[P-1]).animate({'opacity': 0}, 'normal', function() { _busy = false; });

				P++;
			}

			return false;
		});
		//

		if (n_thumb.size()) {
			i = 0;
			n_thumb.children('img').each(function() {
				$(this).addClass(C+ ++i).click(function() {
          var i = $(this).attr('class').match(/rx\-\d{1,2}/)
              j = i.toString().substr(3);

            if (_busy) { return false; }
            _busy = true;
            n_wrap.animate({'left': - Q.v_w * (j-1)}, 'fast', function() { _busy = false; });
            P = j;
        });
			});
		}
    //
  });
}
//

// social - baloon
jQuery.fn.miniBaloon = function(Q) {
  return this.each(function() {
    var I = $(this),
        i = I.children('a'),
        J = I.children(Q.elm);

    i.hover(function() {
      var j = $(this).attr('class').substr(1);
      J.stop().animate({'opacity':1, 'left': 25+(j-1)*23 }, 'fast');
    }, function() { J.stop().animate({'opacity':0}, 'fast'); });
  });
}
//

jQuery.fn.picSlider = function(Q) {
  return this.each(function() {
    var I = $(this),
        P = 0,
        elm = I.find(Q.elm),
        A = elm.children('li'),
        M = A.size(),
        N = parseFloat(A.outerWidth('true')),
        _busy = false;

    if (M<1) { return false; }

    I.children(Q.a_prev).click(function() {
      if (P>0) {
        if (_busy) { return false; }
        _busy = true;

        P--;
        elm.animate({left:'+='+N}, 'normal', function() { _busy = false; } );
      }
      return false;
    });

    I.children(Q.a_next).click(function() {
      if (P+Q.coef<M) {
        if (_busy) { return false; }
        _busy = true;

        P++;
        elm.animate({left:'-='+N}, 'normal', function() { _busy = false; } );
      }

      return false;
    });

  });
}
//

jQuery.fn.RX_picSneak_tab = function(Q) {
  var I = $(this),
      n_elm = [],
      C = 'rx-',
      P = 0,
      x_gal = I.find(Q.gal).children(),
      x_act = I.find(Q.act).children(),
      bal_c = Q.bal,
      bal = I.find('.'+Q.bal),
      i, j, ax, bx;

  n_elm = [x_gal, x_act];
  $.each(n_elm, function(c, item) {
    i=-1;
    item.each(function() {
      $(this).addClass(C+ ++i);
    });
  });
  $(n_elm[0][P]).addClass(Q.sel);

  x_act.click(function() {
    var j = $(this).attr('class').match(/rx\-\d{1}/)
        i = j.toString().substr(3);

    if (i!=P) {
      $(n_elm[0][P]).removeClass(Q.sel);
      $(n_elm[1][P]).removeClass(Q.sel);
      P = i;
      $(n_elm[0][P]).addClass(Q.sel);
      $(n_elm[1][P]).addClass(Q.sel);
      bal.attr('class', Q.bal + ' bal-'+i);
    }
    return false;
  });

}
//

jQuery.fn.RX_archives = function(Q) {
  var I = $(this),
      n_elm = [],
      C = 'rx-',
      P = 0,
      x_year = I.find(Q.a_year).children(),
      x_month = I.find(Q.month).children(),
      i, j, ax, bx;

  n_elm = [x_year, x_month];
  $.each(n_elm, function(c, item) {
    i=-1;
    item.each(function() {
      $(this).addClass(C+ ++i);
    });
  });
  $(n_elm[1][P]).addClass(Q.sel);

  x_year.click(function() {
    var j = $(this).attr('class').match(/rx\-\d{1}/)
        i = j.toString().substr(3);

    if (i!=P) {
      $(n_elm[0][P]).removeClass(Q.sel);
      $(n_elm[1][P]).removeClass(Q.sel);
      P = i;
      $(n_elm[0][P]).addClass(Q.sel);
      $(n_elm[1][P]).addClass(Q.sel);
    }
    return false;
  });
}
//

var RX_factSlider = function(Q) {
  var I = Q.par,
      C = 'rx-',
      P = 0,
      n_elm = I.find(Q.a_elm),
      n_pic = I.find(Q.w_pic),
      n_txt = I.find(Q.w_txt),
      _busy = false;

  var _enum = function(L) {
    $.each(L, function(i, v) {
      var c = -1;
      v.each(function() { $(this).addClass(C+ ++c); });
    });
  }, _opac = function(L) {
    $.each(L, function(i, v) {
      v.each(function() {
        var J = $(this);
        if (!J.hasClass(Q.c_first)) {
          J.css({
            'opacity':0,
            'display':'block'
          });
        }
      });
    });
  }

  this.init = function() {
    _enum([n_elm, n_pic, n_txt])
    _opac([n_pic]);

    $(n_elm[0]).children('img').css('top','-69px');

    n_elm.each(function() {
      $(this).click(function() {
        var k = $(this).attr('class').substr(3);

        if ( (P == k) || (n_pic[k] == undefined) || _busy ){
          return false;
        }

        _busy = true;

        $(n_txt[P]).css('display', 'none');
        $(n_txt[k]).css('display', 'block');
        $(n_elm[P]).children('img').css('top','0');
        $(n_elm[k]).children('img').css('top','-69px');

        $(n_pic[P]).animate({'opacity':0}, 'fast', function() {
          $(n_pic[k]).animate({'opacity':1}, 'fast', function() {
            P = k;
            _busy = false;
          });
        });

        return false;
      });

    });

  }

}
//

  /* start */

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

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

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

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

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

  if (!$.browser.msie) {
    $('.f-contact .rx-validate, .f-comment .rx-validate, .x-about em a').RX_smoothHover({
      c_dis : 'rx-disabled',
      c_elm : 'a',
      hide : .79
    });
  }

	$('.rx-gallery').RX_picSneak({
		a_prev : 'small .prev',
		a_next : 'small .next',
		c_wrap : '.screen div',
    fx : ['rx-slide', 'rx-opacity'],
		v_h : 394,
		v_w : 685,
		c_thumb : 'del'
	});

  $('.x-sneak').RX_picSneak_tab({
    gal : '.scr-gallery',
    act : '.menu-sneak',
    bal : 'baloon',
    sel : 'rx-selected'
  });

	var rx_post = new RX_postToggle_fx({
		c_elm  : '.x-post',
		c_body : '.body',

		c_hide : '.NOP',
		c_show : '.body',

    a_toggle : '.rx-toggle',
    c_txt : ['open this post', 'close this post'],
		a_open : '.rx-open',
		a_close : '.rx-close',
		select : 's-open'
	});
  rx_post.init();

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

  $('.x-thumbs').picSlider({
		a_prev : '.prev',
		a_next : '.next',
    elm : 'ul',
    coef : 8 // how many visible
  });

  var rx_fact = new RX_factSlider({
    par : $('.l-head'),
    a_elm : '.x-thumbs a',
    w_pic : '.w-brand span',
    w_txt : '.fact p',
    c_first : 'first'
  });
  rx_fact.init();

  $('.x-thumbs').hover(
    function() { $(this).stop().animate({top:0, height:65},'fast'); },
    function() { $(this).stop().animate({top:-67, height:99},'fast'); }
  );

  $('.x-archives').RX_archives({
    a_year : '.year',
    month : '.screen',
    sel : 'rx-selected'
  });

});
