$(document).ready(function(){
	
	$.sendform();
	$.gender();
	$.textarea();
	$('.age').numeric();

   function random( min, max )
   {
       return Math.floor( Math.random() * ( max - min + 1 ) + min );
   };

   function equalize( rating, level )
   {
       rating.className = 'on level' + level;
       rating.timer = setTimeout(function(){
           equalize( rating, random(1, 5) );
       }, 50);
   };

$('.selectAll')
	.click(
		function(){
			this.select();
		}
	);

$('input[name=cerca_numero]')
	.click(
		function(){
			var num = $('input[name=find_numb]').val();
			if(!isNaN(num)){
				location.href = '/segreto:'+num
			}
			return false;
		}
	);

$('input[name=cerca_testo]')
	.click(
		function(){
			var text = $('input[name=find_text]').val();
			if(text != 'cerca tra i segreti...'){
				location.href = '/cerca:'+text
			}
			return false;
		}
	);
$('input[name=ordina]')
	.click(
		function(){
			var ordine = $('input[name=order_level]').val();
			var sesso  = $('input[name=order_gender]').val()
			
			if(ordine && !sesso){
				ordine = (ordine == 2)? 'alti' : 'bassi';
				location.href = '/ordine:'+ordine;
			}

			if(!ordine && sesso){
				sesso = (sesso == 1)? 'uomo' : 'donna';
				location.href = '/sesso:'+sesso;
			}
			
			if(ordine && sesso){
				ordine = (ordine == 2)? 'alti' : 'bassi';
				sesso = (sesso == 1)? 'uomo' : 'donna';
				location.href = '/sesso:'+sesso+'/ordine:'+ordine;
			}
			
			return false;
		}
	);

$('a.blank')
	.click(
		function(){
			window.open(this.href, '_blank');
			return false;
		}
	)


$('a.level')
	.click(
		function(){
			$('a.level').removeClass('clicked');
			$(this).addClass('clicked');
			if($(this).hasClass('alti')){
				$('input[name=order_level]').val(2);
			}else{
				$('input[name=order_level]').val(1);
			}
			
			return false;
		}
	);

$('a.gender')
	.click(
		function(){
			$('a.gender').removeClass('clicked');
			$(this).addClass('clicked');
			if($(this).hasClass('femmina')){
				$('input[name=order_gender]').val(2);
			}else{
				$('input[name=order_gender]').val(1);
			}
			
			return false;
		}
	);

$('div.votebar a.rated')
	.click(
		function()
		{
			return false;
		}
	);

$('div.votebar a.liked').click(
	function()
	{
		return false;
	}
);

$('div.votebar a.mi-piace:not(.liked)').click(
	function()
	{
		var $this		= $(this),
			child 		= $this.children('span'),
			id 			= $this.attr('rel'),
			actually 	= parseInt( child.attr('rel') );

		if( $this.hasClass('liked') )
			return false;

		var request = $.post('/core/like.php', { id : id }, function(response)
			{
				$this.addClass('liked');
				var newLike = actually + 1;
				child.html( newLike );
			}
		);
		return false;
	}
);

$('div.votebar div.cuori a:not(.rated)')
	.hover(
		function(){
			if ( !this.startLevel ) {
				this.startLevel = this.className;
			}
		}, function(){
			if ( !this.rated ) {
				this.className = this.startLevel;
			}
		}
	)
	.mousemove(
		function( event ){
			if ( !this.rated ) {
				this.className = 'level' + ( Math.ceil( ( event.clientX - $( this ).position().left ) / 13 ) || 1 );
			}
		}
	)
	.click(
		function(){
			if( this.rated ){
				return false;
			}
			else{
				this.rated = true;
            var $this  = $( this ).addClass('rated');
            var rating = this.className.replace(/^\D+/, '');
            var match  = $this.attr('name');

				if ( match ) {
					equalize( this, rating );
					var request = $.post('/core/rate.php', { id : match , rate : rating }, function( response )
						{
							if ( response == 'hai gi&agrave; votato questo segreto' ) {
								$this.next('div').children('span').html( response );
							}else{
								setTimeout(function()
								{
									request.abort();
									clearTimeout( $this[ 0 ].timer );
									$this.startLevel = $this.className = 'rated level' + Math.ceil( response );
								}, 2000);
							}
						}
					);
				}
				return false;

			}
		}
	)

	
});