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

	});
}
//

// LOGGING
jQuery.fn.log = function(msg) {
		console.log("%s: %o", msg, this);
		return this;
};
//

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

	// count chars in textarea
	var _char_count = 500;
	$('.f-pic-action textarea').keydown( function() {
		var node = $(this).parents('form').find('.js-count-char').children('em');
		if ($(this).val().length >= _char_count) {
			$(this).val( $(this).val().substr(0, _char_count) );
		}
		node.html( _char_count - $(this).val().length );
	});

	// 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').children('.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 }, 'fast' ); }
	);

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

// BLOG JS
$(document).ready(function(){
	$('input[type="text"], input[type="password"], textarea').rx_form();

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

	$().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;
	});

	/*
	// last elements
	$('.menu-main li:last').each(function() {
		$(this).addClass('li-last');
	});
	*/

});
//

// GALLERY FOR DETAILS PAGE // 05.08.09
detailsGallery = function(o) {
	var i_new, i_old,
			i_piv = 1,
			_busy = false;

	this.init = function() {
		if (!o.par.size()) { return false; }

		var i, j, k;

		i = o.par.find(o.img);
		i_old = i.children(':first');
		i_old.addClass(o.c_o);
		i.append('<img class="'+o.c_n+'" src="'+ i_old.attr('src') +'" alt="'+ i_old.attr('alt') +'" />');
		i_new = i.children(':last');

		k = '<span class="q-preload">';
		for (j=2;j<=o.max;j++) {
			k += '<img class="'+o.c_n+'" src="'+ o.p_m + j +o.p_e +'" alt="'+ i_old.attr('alt') +'" />'
		}
		k += '</span>';
		$('body').append(k);

		setInterval( changePath, o.tim );

	}

	function changePath() {
		i_piv = (i_piv == o.max) ? 1 : i_piv+1;

		i_new.attr('src', o.p_m + i_piv + o.p_e );
		fx_opacity();
	}

	function fx_cleanup() {
		var i = i_new;

		i_new.removeClass(o.c_n).addClass(o.c_o);
		i_old.removeClass(o.c_o).addClass(o.c_n);

		i_new = i_old;
		i_old = i;

		_busy = false;
	}

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

		i_old.animate({opacity : 0}, 1000);
		i_new.animate({opacity : 1}, 1500, fx_cleanup );
	}
	//
}
//



// Kimberly Brooke

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

// TOP MENU // 26.07.09
jQuery.fn.rx_menu = function() {
	this.each(function() {

		$(this).hover(
			function() {
				$(this).children('div.xs').css( {visibility:'visible', display:'none'}).show('fast');
			},
			function() {
				$(this).children('div.xs').css( { visibility:'hidden' } );
			}
		);
	});
}
//

function rx_menu() {
	var elm = $('ul.menu-main>li'),
			c_c = 'js-';

	this.init = function() {
		var i, j;

		elm.each(function() {
			var ax = $(this).children('div.xs'),
					i, j;

			// buggy
			if (ax.find('li').size()>0) {
				j = $(this).position();
				if ( (ax.outerWidth(true) + j.left)>900) {
					ax.css('left', (950 - ax.outerWidth(true) )+'px');
				}
			}

			$(this).hover(
				function() {
					var i = $(this).prev('li'),
							j = $(this).next('li');

					if ((i.size() == 1) && j.size() == 1) {
						i.addClass('js-prev');
						$(this).find('div.xs').css( {visibility:'visible', display:'none'}).show('fast');
						j.addClass('js-next');
					}
				},
				function() {
					var i = $(this).prev('li'),
							j = $(this).next('li');
					if ((i.size() == 1) && j.size() == 1) {
						i.removeClass('js-prev');
						$(this).find('div.xs').css( { visibility:'hidden' } );
						j.removeClass('js-next');
					}
				}
			);
		});

	}

}



$(document).ready(function() {
//	$('ul.menu-main>li').rx_menu();
	var menu = new rx_menu();
	menu.init();

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

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

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

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