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

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

	$('.js-proceed').click( function(event) {
			event.preventDefault();
			error = false;

			$(this).parents('form').children('fieldset').children('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();
			}
		}
	);

	var _char_count = 500;
	$('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 BLOCK
$(document).ready(function(){

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

	$('.js-hide').click( function(event) {
			event.preventDefault();
			$(this).parents('.js-hidebox').slideUp('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' ); }
	);


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

});
//

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

	$('ul.js-automenu').children('li').hover(
		function() {
			$(this).children('ul').show('fast');
		},
		function() {
			$(this).children('ul').hide('fast');
		}
	);

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

	// ie6 picture hover
	$('div.js-fixHover').each( function() {
		$(this).hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	});

});
//

// YOU+WE
$(document).ready(function(){
	var selected = 'selected';
	var speed = 1000, speed_2 = 500;
	var top_offset = -10;

	var _busy_post = false;

	function hidePost(node) {
		if (_busy_post) { return false; }
		_busy_post = true;

		// quickly hide comments
		node.find('.x-comment').css('display', 'none');

		// slide up post
		node.find('.w-html').
				slideUp(speed, function() {
						node.removeClass(selected);
						_busy_post = false;
					}
				);

		return true;
	}
	//

	// hide post from header
	$('.js-hide-post').click(function(event) {
		event.preventDefault();
		hidePost( $(this).parents('.x-post') );
		return false;
	});
	//

	// show post and hide all others
	$('.js-post-img').click(function(event) {
		event.preventDefault();
		if (_busy_post) { return false; }

		var node = $(this).parents('.x-post');

		if (!node.hasClass(selected)) {
			_busy_post = true;

			// hide previous post
			$('.x-post').each(function(){
				if ( ($(this).hasClass(selected)) ) {
					$(this).removeClass(selected);

					// some cleanup
					// node.find('.js-post-img').css('opacity', '.7');
					$(this).find('.x-comment').css('display', 'none');
					$(this).find('.w-html').css('display', 'none');
				}
			});

			// slide down new post
			node.find('.w-html').
					slideDown(speed, function() {
								$.scrollTo(
										node, 'slow',
										{ onAfter: // slide comments down after doc is scrolled to post
												function() { node.find('.x-comment').slideDown('fast'); }
										}
										// { offset: { top: +top_offset } }
								);

								node.addClass(selected);
								_busy_post = false;
							}
					);
		} else {
			hidePost(node);
		}

		return false;
	});
	//

	// hide comment
	$('.js-hide-comment').click(function(event) {
		event.preventDefault();
		var node = $(this).parents('.x-comment');

		node.find('.js-hide-block div').slideUp('slow');
		$(this).css('display', 'none');
		node.css('border-width', '0');

		return false;

	});
	//

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

	// vote for picture
	$('.w-vote big').each(function() {
		var i = parseFloat($(this).parents('em').attr('class').substr(5));
		$(this).css('width', Math.ceil(i / 5) * 7 );
	})

	$('.w-vote a').click(function() {
		$(this).addClass('inactive-vote');

		var j = $(this).parents('.w-vote').find('em'),
				i = parseFloat(j.attr('class').substr(5)),
				k = i + 1;

		j.attr('class', '');
		j.addClass('vote-'+ k);
		j.find('big').css('width', Math.ceil(k / 5)*7 )
		$(this).parents('.w-vote').find('span').html(k);
	});
	//

	// raves sliding
	function raves() {
		var node_self = $('.w-raves'),
				node_cur_item = node_self.find('.z-raves span'),
				total_items = 0,
				active_item = 0,
				c_item = 'item-',
				item_width = 950,
				i, j,
				_busy = false;

		this.init = function() {
			//alert(item_len);

			i = 0;
			node_self.find('.item').each(function() {
				$(this).addClass(c_item + leading_zero(i));
				//$(this).css('left', i*item_width);
				i++;
			});
			total_items = i - 1;
			node_self.find('.z-raves strong').html(i);
			node_cur_item.html(1);

			i = get_node(active_item);
			i.css('display', 'block');
			i.css('opacity', 1);

			node_self.find('a[href*="#js-next"]').click(next_item);
			node_self.find('a[href*="#js-prev"]').click(prev_item);
		}

		function next_item() {
			if (active_item < total_items) {
				fx_transition(active_item, active_item+1);
			}
			return false;
		}

		function prev_item() {
			if (active_item > 0) {
				fx_transition(active_item, active_item-1);
			}
			return false;
		}

		function get_node(id) {
			return node_self.find('.' + c_item + leading_zero(id));
		}

		function fx_transition(old_id, new_id) {
			if (_busy) { return false; }
			_busy = true;

			i = get_node(old_id);
			j = get_node(new_id);

			i.css('display', 'block');
			j.css('opacity', 0);
			j.css('display', 'block');
			i.animate({ 'opacity' : 0 }, 'normal', function() { $(this).css('display', 'none'); _busy = false; } );
			j.animate({ 'opacity' : 1 }, 'normal', function() { _busy = false; } );

			active_item = new_id;
			node_cur_item.html(active_item+1);
			return true;
		}
		//

	}

	var i = new raves();
	i.init();

});
//

// mouse wheel & sliders control
$(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;
			}
		});

	});

});
//


// custom select/option form
function rx_fselect () {
	var c_item = 'item-';

	this.init = function() {
		var i, j, k, l, n,
				node;

		// click on arrow
		$('a[href*="#tog-select"], .js-select p').click(function(event) {
			event.preventDefault();
			toggle_fs(this);
			return false;
		});
		//

		// external click
		$(document).click(function(event) {
			if ($(event.target).parents('.js-select').length === 0) {
				closeall_fs();
			}
		});
		//

		// click on li>a
		$('.js-select').click(function(event) {
			event.preventDefault();
			var link = $(event.target);

			if (link.is('a')) {
				$(this).find('em').html( link.text() );

				i = parseFloat(link.parents('li').attr('class').substr(c_item.length));
				j = $(this).parents('div.p').find('select');
				j.val(i);
				j.addClass('selected');

				close_fs(this);
			}

			return false;
		});
		//

		// get data from select, hide it, display list
		$('.rx-select select').each(function() {
			/*								 
			node = $(this).parents('div.p').find('.js-select');

			k = document.createElement('ul');
			k.setAttribute('class', 'js-ul');
			node.append(k);
			j = node.find('ul');

			i = 1;
			$(this).children('option').each(function() {
				n = c_item+leading_zero(i);
				$(this).addClass(n);

				k = document.createElement('li');
				k.setAttribute('class', n);
				j.append(k);

				k = document.createElement('a');
				k.setAttribute('href', '#');
				m = document.createTextNode( $(this).text() );
				k.appendChild(m);
				
				l = j.find('.'+n);
				l.append(k);
				i++;
			});

			$(this).css('top', '-1079px');
			node.css('display', 'block');
			*/
		});
		//

	}
	//

	function fs_changeval(option_id, node_select) {
		node_select.chidren('option' + '.' + option_id).attr('selected', 'selected')
	}
	//

	function closeall_fs() {
		$('.js-select').find('ul').slideUp('fast');
	}
	//

	function open_fs(button) {

	}

	function close_fs(button) {
		$(button).find('.js-ul').slideUp('fast');
	}
	//

	function toggle_fs(button) {
		var node = $(button).parents('.js-select').find('ul');

		if (node.css('display') == 'none' ) {
			closeall_fs();
			node.slideDown('fast');
		} else {
			node.slideUp('fast');
		}
	}
	//

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

}

$(document).ready(function() {
	if (!$.browser.msie) {
		fselect = new rx_fselect();
		fselect.init();
	}
});
