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

// FORM AUTOVALUES 01.05.09
jQuery.fn.rx_form = function() {
	function populateElement(selector, defvalue) {
		$(selector).focus(function() {
			if ($(selector).val() == 'Website') {
				$(selector).val('http://');
			} else if ($(selector).val() == defvalue) {
				$(selector).val('');
			}
		});

		$(selector).blur(function() {
				if ($.trim($(selector).val()) == '') {
						$(selector).val(defvalue);
				}
		});
	};

	return this.each(function() {
		// $(this).log();
		populateElement($(this), $(this).val());
	});
}
//

// FORM VALIDATOR 01.05.09
jQuery.fn.rx_validate = function() {
	function validateNoempty(text) {
		if (!text || text.length < 3) { return false;	}
		return true;
	}

	function validateText(text) {
		if (!validateNoempty(text)) { return false; }
		var re_text = /[^A-Za-z0-9\s]/;
		if (text.match(re_text) != null ) { return false; }
		return true;
	}

	function validateNumber(text) {
		var re_text = /^\d+$/;
		if (text.match(re_text) == null ) { return false; }
		return true;
	}

	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }

		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}

	return this.each(function() {
		$(this).click(function(event) {
			event.preventDefault();
			error = false;

			$(this).parents('form').find('input[type="text"], textarea').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-number')) {
					if (!validateNumber($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-mail')) {
					if (!validateEmail($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				}
			});

			if (!error) {
				var node = $(this).parents('.js-hidebox');
				node.slideUp('normal');
				$(this).parents('.x-post').find('.w-note').slideDown('fast');
				$(this).parents('form').submit();
			}
		});

	});
}
//

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

// SLIDING THUMBS // 13.07.09
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_nav  = $(this).children('p'),
				n_ul   = $(this).find(c_wrap).children('ul'),
				n_prev = n_nav.children('a[href*="'+c_prev+'"]'),
				n_next = n_nav.children('a[href*="'+c_next+'"]'),
				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 false;
		}

		function is_prev() {
			if (pivot-1 >= 0) { return true; }
			return false;
		}

		function is_next() {
			if (pivot+1 <= i_real - i_min) { return true; }
			return false;
		}

    function set_navs() {
      if (is_prev()) { n_prev.removeClass(c_dis); } else { n_prev.addClass(c_dis); }
			if (is_next()) { n_next.removeClass(c_dis); } else { n_next.addClass(c_dis); }
    }

    set_navs();

		n_prev.click(function() {
			if (is_prev()) {
				if (_busy) { return false; }
				_busy = true;
				pivot--;
				n_ul.animate( { left : '+='+step }, 'normal', function() { _busy = false; set_navs(); } );
			}
			return false;
		});

		n_next.click(function() {
			if (is_next()) {
				if (_busy) { return false; }
				_busy = true;
				pivot++;
				n_ul.animate( { left : '-='+step }, 'normal', function() { _busy = false; set_navs(); } );
			}
			return false;
		});

		return true;
	});
}
//

// POST OPEN/CLOSE // 19.07.09
jQuery.fn.rx_postToggler = function(o) {
	var _busy = false,
			c_on = 'js-post-opened',
			c_load = 'js-loaded';

	$(o.elm).click(function() {
		if (_busy) { return; }
		_busy = true;

		var i = $(this).parents(o.par);

		if (i.hasClass(c_on)) {
			i.removeClass(c_on);
			i.find(o.tgl).slideUp('fast', function() { _busy = false; });
		} else {

			var j = $('.'+c_on);
			j.removeClass(c_on);
			j.find(o.tgl).css('display', 'none');

			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);
			i.addClass(c_on);
			i.find(o.tgl).slideDown('fast', function() { _busy = false; });
			$().scrollTo(i.find('.a-title'), 'slow');
		}

		return false;
	});

	$(o.par).find('a[href*="js-close-post"]').click(function() {
		var i = $(this).parents(o.par);
		i.removeClass(c_on);
		$().scrollTo(i, 'fast', function() {
				i.find(o.tgl).slideUp('normal', function() { _busy = false; });
		});

		return false;
	});

}
//

// PICTURE VOTING // 19.07.09
jQuery.fn.rx_pictureVote = function(o) {
	var c_on = 'js-voted',
			l_tx  = 6, //'votes-'.length,
			i,
			c = 1.56;//1%

	/*
	$(o.elm).find(o.min).click(function() {
			$(this).parents(o.elm).addClass(c_on);
			alert('-');
			return false;
	});

	$(o.elm).find(o.pls).click(function() {
			$(this).parents(o.elm).addClass(c_on);
			alert('+');
			return false;
	});
	*/

	$(o.elm).find(o.vot).click(function() {
		$(this).addClass('inactive-vote');
		var i = $(this).parents(o.par).find(o.tgl);
				j = parseFloat(i.attr('class').substr(l_tx));

		if (typeof(j) == 'number') {
			i.removeClass(i.attr('class'));
			j++;
			i.addClass('votes-' + j );
		  $(this).parents(o.par).find('.js-v-n').html(j);
		}

	});

	$(o.elm).find(o.tgl).each(function() {
		var i = parseFloat($(this).attr('class').substr(l_tx));

		if (typeof(i) == 'number') {
			$(this).css('width', i );
		}
	});

	$(o.par).hover(
		function() {
			$(this).find(o.elm).animate({'opacity' : 1}, 'fast');
		},
		function() {
			$(this).find(o.elm).animate({'opacity' : 0}, 79)
		}
	);

}
//

// SOCIAL ICONS BEWTIFIER // 20.07.09
jQuery.fn.rx_socialLinks = function() {
	$(this).find('a').hover(
		function() { $(this).animate({'opacity' : 1 }, 'fast'); },
		function() { $(this).animate({'opacity' : .6 }, 79); }
	);

	$(this).find('a').each(function() {
		$(this).attr('title', $(this).children('sup').html() );
	});
}
//

// BLOG JS
$(document).ready(function(){

	$.scrollTo.defaults.axis = 'y';

	$('ul.menu-lvl-1>li').rx_menu();

	$('input[type="text"], input[type="password"], textarea').rx_form();

	$('.js-proceed').rx_validate();

	$().rx_postToggler({
		par : '.x-post',
		elm : '.js-post-toggle',
		tgl : '.post-body'
	});

	$().rx_pictureVote({
		par : '.w-pic',
		elm : '.q-vote',
		min : 'a[href*="vote-minus"]',
		pls : 'a[href*="vote-plus"]',
		vot : 'a',
		tgl : 'strong em'
	});

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

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

	$('.i-social').rx_socialLinks();

	// 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 - add comment
	$('a[href*="#js-add-comment"]').click( function() {
			if ($(this).hasClass('selected')) {
				$(this).removeClass('selected');
			} else {
				$(this).addClass('selected');
			}
			$(this).parents('.x-comment').find('.f-add-comment').slideToggle('fast');
			$(this).parents('.x-post').find('.w-note').slideUp('fast');

			return false;
	});

	// comments - send mail
	$('a[href*="#js-send-email"]').click( function() {
			if ($(this).hasClass('selected')) {
				$(this).removeClass('selected');
			} else {
				$(this).addClass('selected');
			}
			$(this).parents('.x-comment').find('.f-send-email').slideToggle('fast');

			return false;
	});

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

/*
	if ($.browser.msie) {
		$('.post-form').after('<br />');
	}

	function blog_align() {
		var i = $(window).width();
		$('.h-p-rel').css('left', Math.round( ( i - 1190 ) / 2));
		$('.flash-header').css('left', Math.round( ( i - 1600 ) / 2));
	}

	$(window).load(function() { blog_align(); });
	$(window).resize(function() {	blog_align(); });
*/

	//

});
//

// calendar 3
$(document).ready(function() {
	var dx = $('.x-calendar');

	$('.x-contact a[href*="#js-calendar"]').click(function() {
		var n = $(this).parents('.x-contact').find('.x-calendar');
		if ($(this).hasClass('selected')) {
			// n.hide('fast');
			// $(this).removeClass('selected');
		} else {
			n.show('fast');
			$(this).addClass('selected');
		}

		var y = $('#xcal-y').val(),
			d = '01',
			m = $('#xcal-m').val();

		calendar(d,m,y,'x-cal');

		/*
		dx.find('span:first').addClass('first');
		dx.find('span:last').addClass('last');
		*/

		return false;
	})

});
//
