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

}
//

//11.06.09

// TOP MENU
jQuery.fn.rx_menu = function() {
	this.each(function() {
		$(this).hover(
			function() {
				$(this).children('ul').
					css( {visibility:'visible', display:'none'}).
					show('fast', function() { _busy = false; } );
			},
			function() {
				$(this).children('ul').css( { visibility:'hidden' } );
			}
		);
	});
}
//

// BLOG JS
$(document).ready(function(){
	$('ul.js-automenu>li').rx_menu();

	$().rx_scrollComment({
    n_parent: '.x-posthead',
    n_scroll: '.q-posttext',
    n_hiding: '.screen',
    n_slider: '.slide-posttext'
  });

	$().rx_scrollComment({
    n_parent: '.x-posthead',
    n_scroll: '.w-last-coms',
    n_hiding: '.screen-lc',
    n_slider: '.slide-last-coms'
  });

	$().rx_scrollComment({
    n_parent: '.x-comment',
    n_scroll: '.w-comment-list',
    n_hiding: '.w-hide',
    n_slider: '.slide-comment'
  });

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

	// comments - hide form
	$('.js-hide').click( function(event) {
			event.preventDefault();
			$(this).parents('.js-hidebox').slideUp('fast');
		}
	);

	// comments - add comment
	$('a[href*="#js-add-comment"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').find('.f-add-comment');
			node.slideToggle('fast');
		}
	);

	// comments - send mail
	$('a[href*="#js-send-mail"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').children('.f-send-to-friend');
			node.slideToggle('fast');
		}
	);

	// comments - toggle on and off
	$('a[href*="#js-toggle-comment"]').click( function(event) {
			event.preventDefault();
			$(this).parents('.x-post').find('.w-note').slideUp('fast');
			var node = $(this).parents('.x-comment').find('.w-comment-list');
			node.slideToggle('fast');
		}
	);

	// comments - on this picture
	$('a.js-add-comment').click( function(event) {
		event.preventDefault();

		var j = $(this).parents('.x-post');
				node = j.find('.f-add-comment'),
				tarea = j.find('.js-ta-comm'),
				ax = 'js-add-comment js-img-id-',
				k = $(this).attr('class');

		$(this).parents('.x-post').find('.w-note').slideUp('fast');

		k = '{img}' + k.substr(ax.length, 255) + '{/img}';
		tarea.val( tarea.val() + k );

		$().scrollTo(
			j.find('.x-comment'), 'fast', function() { if (node.css('display') == 'none') { node.slideDown('fast'); } }
		);

		return false;
	});

	$('.w-pic').hover(
		function() { $(this).find('.js-add-comment').animate( { opacity: .8 }, 'fast' ); },
		function() { $(this).find('.js-add-comment').animate( { opacity: 0 }, 79 ); }
	);

	// comments - back link
	$('.w-hide').click(function(event) {
		event.preventDefault();
		var e = $(event.target),
				node = $(this).parents('.x-post');

		if (e.is('a')) {
			$().scrollTo( node.find('.anchor-' + e.attr('href').substr(1)), 'fast' );
			return false;
		}
		return true;
	});

});
//


// smooth hover effect based on animated opacity
// 01/09/09
jQuery.fn.RX_smoothHover = function() {
	return this.each(function() {
		$(this).hover(
			function() { $(this).animate({opacity:1}, 'fast'); },
			function() { $(this).animate({opacity:0}, 79); }
		);
	});
}
//

//02.07.09

// SLIDING THUMBS
jQuery.fn.rx_miniGallery = function() {
	var c_prev = '#nav-prev',
			c_next = '#nav-next',
			c_wrap = '.screen',
			c_num  = 'js-items-'.length;

	this.each(function() {
    var n_par  = $(this),
        n_nav  = n_par.children('p'),
				n_ul   = n_par.find(c_wrap).children('ul'),
				i_min  = parseFloat(n_ul.attr('class').substr(c_num)),
				i_real = n_ul.children('li').size(),
				step   = parseFloat(n_ul.children('li:first').outerWidth(true)),
				pivot  = 0,
				c_dis  = 'js-disable',
				_busy  = false;

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

    function check_navs() {
      if (pivot-1 >= 0) {
        n_nav.children('a[href*="'+c_prev+'"]').removeClass(c_dis);
      } else {
        n_nav.children('a[href*="'+c_prev+'"]').addClass(c_dis);
      }

			if (pivot+1 <= i_real - i_min) {
        n_nav.children('a[href*="'+c_next+'"]').removeClass(c_dis);
      } else {
        n_nav.children('a[href*="'+c_next+'"]').addClass(c_dis);
      }
    }

    check_navs();

		n_nav.children('a[href*="'+c_prev+'"]').hover(
			function() { if (pivot>0) { $(this).addClass('js-hover'); } },
			function() { $(this).removeClass('js-hover'); }
		);

		n_nav.children('a[href*="'+c_next+'"]').hover(
			function() { if (pivot < i_real - i_min) { $(this).addClass('js-hover'); } },
			function() { $(this).removeClass('js-hover'); }
		);

		n_nav.children('a[href*="'+c_prev+'"]').click(function() {
			if (pivot-1 >= 0) {
				if (_busy) { return false; }
				_busy = true;
				pivot--;
				n_ul.animate( { left : '+='+step }, 'normal', function() { _busy = false; check_navs(); } );
			}
			return false;
		});

		n_nav.children('a[href*="'+c_next+'"]').click(function() {
			if (pivot+1 <= i_real - i_min) {
				if (_busy) { return false; }
				_busy = true;
				pivot++;
				n_ul.animate( { left : '-='+step }, 'normal', function() { _busy = false; check_navs(); } );
			}
			return false;
		});

		return true;
	});
}
//

// POST TOGGLE
jQuery.fn.rx_postToggle = function() {
	var c_on  = 'js-post-opened',
			n_par = '.x-post',
			n_act = '.z-post-toggle a, .x-posthead .js-post-img',
			n_tgl = '.js-post-hide',
			c_load = 'rx-loaded',
			i, j;

	this.each(function() {
    i = $(this).find('.q-posttext').children('.screen');
    j = $(this).find('.first-pt');
    i.html(j.html());
    j.remove();

		$(this).find(n_act).click(function() {
			i = $(this).parents(n_par);
			if (i.hasClass(c_on)) {
				i.removeClass(c_on);
				i.find(n_tgl).slideUp('fast');
			} else {
				i.addClass(c_on)
				i.find(n_tgl).slideDown('fast');

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

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

// Toggle all posts
jQuery.fn.rx_allPostToggle = function() {
  var c_act = 'js-active',
      c_on  = 'js-post-opened',
			n_tgl = '.js-post-hide',
			c_load = 'rx-loaded';

	this.each(function() {
		$(this).click(function() {

			if ($(this).hasClass(c_act)) {
				$(this).removeClass(c_act);

				$('.x-post').each(function() {
					$(this).removeClass(c_on);
					$(this).find(n_tgl).css('display', 'none');
				});

			} else {
				$(this).addClass(c_act);

				$('.x-post').each(function() {
					var i = $(this);
					i.addClass(c_on);
					i.find(n_tgl).css('display', 'block');

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

				});
			}

			return false;
		});
	});
}

/*

	$().click(function() {
    if ($(this).hasClass(c_act)) {
      $(this).removeClass(c_act);

      $('.x-post').each(function() {
        $(this).removeClass(c_on);
        $(this).find(n_tgl).css('display', 'none');
      });

    } else {
      $(this).addClass(c_act);

      $('.x-post').each(function() {
        $(this).addClass(c_on);
        $(this).find(n_tgl).css('display', 'block');
      });
    }

    return false;
	})
*/

// PICTURE VOTING
jQuery.fn.rx_picVote = function() {
	var c_on  = 'js-voted',
			n_par = '.x-post',
      s_len = 'votes-'.length,
      node,
      pc_one = 1.6; //1%=160/100

	this.each(function() {
    $(this).children('a[href*="vote-minus"]').click(function() {
      if (!$(this).hasClass(c_on)) {
        $(this).addClass(c_on);
        alert('-');
      }
      return false;
    });

    $(this).children('a[href*="vote-plus"]').click(function() {
      if (!$(this).hasClass(c_on)) {
        $(this).addClass(c_on);
        alert('+');
      }
      return false;
    });

    node = $(this).find('em');
    var i = parseFloat(node.attr('class').substr(s_len));
    node.css('width', i * pc_one)
	});
}
//

// Kyle Barnes
$(document).ready(function() {

	$('.rx-smhover a img').RX_smoothHover();

	$('.x-post').rx_postToggle();

	$('a[href*="#post-list"]').rx_allPostToggle();

	$('.q-slides').rx_miniGallery();

	$('.x-vote').rx_picVote();

	$('.x-preview li').hover(
    function() { $(this).find('a').animate( { opacity: .8}, 'fast' ); },
		function() { $(this).find('a').animate( { opacity: 0 }, 79 ); }
	);

  $('.x-img-notes em').css('display', 'none');
  $('a[href*="#image-notes"]').click(function() {
    $(this).parents().children('em').slideToggle('fast');
  });

  $('.js-post-img').hover(
    function() {
      $(this).parents('.x-post').find('.z-post-toggle a').addClass('js-hover');
    },
    function() {
      $(this).parents('.x-post').find('.z-post-toggle a').removeClass('js-hover');
    }
  );

	$('.q-vote').click(function() { $(this).parents('.x-post').find('.q-vote').addClass('inactive-vote'); });

});
//

