// FORM AUTOVALUES
jQuery(document).ready(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);
				}
		});
	};

	$('form').find('input[type="text"], input[type="password"], textarea').each(function() {
				populateElement($(this), $(this).val());
			}
	);

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

	// VALIDATE ON SUBMIT
	$('.js-proceed').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('slow');
				$(this).parents('form').submit();
			}
		}
	);

	// MAX CHARS IN TEXTAREA COUNTER
	var _char_count = 500;
	$('textarea').keydown( function() {
			var node = $(this).parents('form').children('.w-left').
												 children('.w-action').children('.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 BLOCK
jQuery(document).ready(function($){

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

	$('a[href*="#js-add-comment"]').click( function(event) {
			event.preventDefault();

			var node = $(this).parents('.x-comment').children('.f-add-comment');
			node.slideToggle('fast');
		}
	);

	$('a[href*="#js-send-mail"]').click( function(event) {
			event.preventDefault();

			var node = $(this).parents('.x-comment').children('.f-send-to-friend');
			node.slideToggle('fast');
		}
	);

	$('a[href*="#js-toggle-comment"]').click( function(event) {
			event.preventDefault();

			var node = $(this).parents('.x-comment').find('.w-comment-list');
			node.slideToggle('fast');
		}
	);

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

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

});
//

// TOP MENU
jQuery.fn.rx_menu = function() {
	var $ = jQuery.noConflict();
	this.each(function() {
		$(this).hover(
			function() {
				$(this).children('ul').show('fast', function() { _busy = false; } );
			},
			function() {
				$(this).children('ul').hide();
			}
		);
	});
}
//

// AUX
jQuery(document).ready(function($){

	$('ul.js-automenu li').rx_menu();

	// open link in another window
	$('a.js-el').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

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

});
//

// MOUSE WHEEL & SLIDERS CONTROL
jQuery(document).ready(function($) {
	if ($.browser.mozilla) {
      _wheel_coef = 30;
	} else if($.browser.safari) {
			_wheel_coef = 15;
	}	else {
		_wheel_coef = 20;
	}

	// mouse wheel over the comments area
	$('.w-comment-list').bind('mousewheel', function(event, delta) {
			var node = $(this).find('.w-hide');
			if ( node.attr('scrollHeight') > node.outerHeight() + 2 ) {
				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('.x-comment').find('.slide-comment').slider('option', 'value', -s_hande );

				return false;
			}

			return true;
	});
	//

	// slide comments
	/*
	$('.slide-comment').each(function() {
		$(this).slider({
			animate: false,
			orientation: "vertical",
			min: -100,
			max: 0,
			slide: function(event, ui) {
				var node = $(this).parents('.x-comment').find('.w-hide');

				if ( node.attr('scrollHeight') > node.outerHeight() + 2 ) {
					var ratio = node.attr("scrollHeight") - node.height();
					node.attr( { scrollTop: -ui.value * (ratio / 100) } );
				} else {
					return false;
				}

				return true;
			}
		});

	});
	*/

});
//

// post to gallery
function post_to_gallery() {
	var $ = jQuery.noConflict();
	var node_self,
			gallery = [],
			heights = [],
			widths = [],
			max_h = 602,
			i, j, k, l, m,
			r, w, h,
			ax, bx, cx, dx, ex;

	var thumb_active = 0, // active elm

			node_pic = false, // jq - big pictures wrap
			node_pic_vis = false, // big first picture
			node_pic_hid = false, // big second picture
			node_pic_vis_p = false,
			node_pic_hid_p = false,

			c_select = 'selected', // class for selected item
			c_item = 'item-', // li item class
			c_slide = 'fx-slide',
			c_hidden = 'fx-hidden',
			c_opacity = 'fx-opacity',
			c_first = 'fx-first',
			c_item = '#item-',
			c_item_len = c_item.length,

			pic_width = 998,
			pic_count = 0,
			_fx_busy = false;

	this.init = function(node_post) {

		ax = node_post.find('.z-thumb');

		j = 0;
		node_post.find('img').each(function() {
			gallery[j] = $(this).attr('src');

			h = $(this).attr('height');
			w = $(this).attr('width');

			if (h>max_h) {

				if (w>h) {
					r = Math.abs(w/h);
					w = max_h * r;
					widths[j] = w;
				} else {
					r = Math.abs(h/w);
					w = max_h / r;
					widths[j] = w;
				}
				heights[j] = max_h;

			} else {
				heights[j] = h;
				widths[j] = w;
			}

			bx = document.createElement('a');
			bx.appendChild(document.createTextNode(j+1));
			bx.setAttribute('href', c_item+leading_zero(j));
			ax.append(bx);

			j++;
		});
		pic_count = j;
		ax.find('a[href$="'+ c_item + '00' + '"]').addClass(c_select);

		node_pic = node_post.find('.w-picture').children('div');

		j = document.createElement('p');
		j.setAttribute('class', c_first);
		node_pic.append(j);
		node_pic_vis_p = node_pic.find('p');
		j = create_picture(thumb_active);
		node_pic_vis_p.append(j);
		node_pic_vis = node_pic_vis_p.children('img');

		j = document.createElement('p');
		j.setAttribute('class', c_hidden);
		node_pic.append(j);
		node_pic_hid_p = node_pic.find('p:last');
		j = create_picture(thumb_active);
		node_pic_hid_p.append(j);
		node_pic_hid = node_pic_hid_p.children('img');

		node_post.find('.z-picture a[href*="#navy-prev"]').click(navy_picture_prev);
		node_post.find('.z-picture a[href*="#navy-next"]').click(navy_picture_next);
		node_post.find('.z-thumb').click(function(event) { navy_thumb(event); });

		node_post.find('.post-desc').html( node_post.find('.first-pt').html() );
		// node_post.find('.w-html').empty();

		node_self = node_post;
	}
	//

	// add leading zeroes
	function leading_zero(v) {
		if (parseFloat(v)<10) return ('0'+parseFloat(v));
		return v;
	}
	//

	// helper
	function create_picture(pic_id) {
		var i, j;
		i = document.createElement('img');
		j = get_pic_url(pic_id);
		i.src = j.src;
		i.setAttribute('width', j.width);
		i.setAttribute('height', j.height);
		return i;
	}
	//

	// get pic url from thumb_active
	function get_pic_url(pic_id) {
		var i = parseInt(pic_id);
		return { 'src' :gallery[i], 'width' : widths[i], 'height' : heights[i] }
	}
	//

	//
	function navy_thumb(event) {
		var node = $(event.target),
				i, j;

		if (node.is('a') ) {
			event.preventDefault();
			if ( node.hasClass(c_select) ) { return false; }

			i = node.attr('href').substr(c_item_len);
			fx_transition('opacity', thumb_active, i);

			return false;
		}

		return true;
	}
	//

	// prev picture
	function navy_picture_prev() {
		if ( thumb_active - 1 >= 0 ) {
			fx_transition('slide-prev', thumb_active, thumb_active - 1);
		}
		return false;
	}
	//

	// next picture
	function navy_picture_next() {
		if ( thumb_active + 1 < pic_count ) {
			fx_transition('slide-next', thumb_active, thumb_active + 1);
		}
		return false;
	}
	//

	// switch active thumb
	function switch_active_thumb(old_id, new_id) {
		var node = node_self.find('.z-thumb');
		node.find('a[href$="'+ c_item + leading_zero(old_id) + '"]').removeClass(c_select);
		node.find('a[href$="'+ c_item + leading_zero(new_id) + '"]').addClass(c_select);
		thumb_active = new_id;
		return true;
	}
	//

	// transitions between pictures
	function fx_transition(trans_id, old_id, new_id) {
		function cleanup() {
			node_pic_vis_p.removeClass(c_first);
			node_pic_vis_p.addClass(c_hidden);
			node_pic_vis_p.css('left', '0px');
			node_pic_vis_p.css('opacity', '1');

			node_pic_hid_p.addClass(c_first);
			node_pic_hid_p.removeClass(c_opacity);
			node_pic_hid_p.removeClass(c_slide);
			node_pic_hid_p.css('left', '0px');
			node_pic_hid_p.css('opacity', '1');

			i = node_pic_vis_p;
			node_pic_vis_p = node_pic_hid_p;
			node_pic_hid_p = i;

			i = node_pic_vis;
			node_pic_vis = node_pic_hid;
			node_pic_hid = i;

			switch_active_thumb(old_id, new_id);

			_fx_busy = false;
		}
		//

		if (_fx_busy) { return false; }
		_fx_busy = true;

		var i = get_pic_url(new_id);
		node_pic_hid.attr('src', i.src );
		node_pic_hid.attr('height', i.height );
		node_pic_hid.attr('width', i.width );

		switch (trans_id) {
			case 'opacity':
				node_pic_hid_p.addClass(c_opacity);
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate(
						{ opacity: 0 }, 'normal',
						function() {
							node_pic_hid_p.animate(
									{ opacity: 1 }, 'normal', cleanup
							);
						}
				);
				break;

			case 'slide-next':
				node_pic_hid_p.addClass(c_slide);
				node_pic_hid_p.css('left', pic_width+'px');
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate( { left: '-='+pic_width }, 'normal' );
				node_pic_hid_p.animate( { left: '-='+pic_width }, 'normal', cleanup );
				break;

			case 'slide-prev':
				node_pic_hid_p.addClass(c_slide);
				node_pic_hid_p.css('left', -pic_width+'px');
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate( { left: '+='+pic_width }, 'normal' );
				node_pic_hid_p.animate( { left: '+='+pic_width }, 'normal', cleanup );
				break;

			default :
				break;
		}
		return true;
	}
	//

}
//

// REPLACE POST CONTENT AND CREATE GALLERY
jQuery(document).ready(function($) {

	$('.js-pt-gallery').each(function() {
		rx = new post_to_gallery();
		rx.init($(this));

	});


});
//


// resize .size-large picture and center them

jQuery(document).ready(function($) {
	var v_h = 600,
		v_w = 900;

    // correct paddings
    function _padding(V, M) {
      var i = M - V,
          ax = Math.floor(i/2);
      return {i1:ax, i2:(i-ax)}
    }
    //	

  $('.size-large').each(function() {
	var I = $(this),
		h = parseInt(I.css('height')),
		w = parseInt(I.css('width')),
		cx = h/v_h,
		ax, bx;

    ax = Math.ceil( w / cx );
	bx = _padding(ax, v_w);

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