var playInt;
var playInt2;

$(document).ready(function() 
{
	/* special mouseover/out focus/blur actions for inputs - aw_input class required */
	$('p.aw_input').mouseover(function()
	{
		$(this).addClass('hover');
	});
	
	$('p.aw_input').mouseout(function()
	{
		$(this).removeClass('hover');
	});
	
	$('input.aw_input').focus(function()
	{
		$(this).parent().addClass('focus');
	});
	
	$('input.aw_input').blur(function()
	{
		$(this).parent().removeClass('focus');
	});
	
	$('textarea.aw_input').focus(function()
	{
		$(this).parent().addClass('focus');
	});
	
	$('textarea.aw_input').blur(function()
	{
		$(this).parent().removeClass('focus');
	});
	/* special mouseover/out focus/blur actions for inputs - aw_input class required */
	
	$('.cat_costa_lista').click(function()
	{
		if( !$(this).hasClass('close-lista') )
		{
			if ($(this).hasClass('open-lista')) $(this).removeClass('open-lista');
			$(this).addClass('close-lista');
			
			$('.cat_costa_lista_' + $(this).attr('role')).fadeOut();
			$(this).attr('title', 'Vezi lista completa');
			$(this).html('vezi lista completa');
		}
		else
		{
			if ($(this).hasClass('close-lista')) $(this).removeClass('close-lista');
			$(this).addClass('open-lista');
			
			$('.cat_costa_lista_' + $(this).attr('role')).fadeIn();
			$(this).attr('title', 'Vezi lista restransa');
			$(this).html('vezi lista restransa');
		}
	});
	$('#ce_primesc_lista').masonry({
	    
	
	},  function() {}
	    // Optional callback.
	    // 'this' will refer to the elements Masonry just rearranged
	);
	
	$('#alerta-update-expire-close').click(function()
	{
		$.post(
		'sources/acts.php' , 
		{ 
			act: 'update_expire'
		} , 
		function( data )
		{		
			$('#alerta-update-expire').slideUp( 1000 );
		}, 
		'json'
		)
	});
});

//function keep_alive() 
//{
//	$.post(
//		'sources/acts.php' , 
//		{ 
//			act: 'keep_alive'
//		} , 
//		function( data )
//		{		
//			if( data.success )
//			{
//				// code here?
//				playInt = setTimeout( 'keep_alive()', 10000 );
//			}
//			else
//			{
//				if ( !$('#alerta-eroare').is(':visible') )
//				{
//					$('#alerta-eroare').html( data.message );
//					$('#alerta-eroare').slideDown( 1000 );
//				}
//				playInt.clearTimeout();
//			}
//		}, 
//		'json'
//	);
//}

function reg_change( el, value )
{
	if ( ( $(el).val() != value ) && ( $(el).val() != '' ) ) $(el).parent().removeClass('error');
	else $(el).parent().addClass('error');
}

function do_login()
{
	var username = $('#username').val();
	var password = $('#password').val(); // nu o sa fie niciodata goala.. default is 'Password'
	var rememberme = $('#rememberme').is(':checked');
	
	if ( rememberme ) rem = 1;
	else rem = 0;
	$('#login_error').hide();
	
	if ( !username )
	{
		// nu a completat username-ul
		$('#username').parent().addClass('error');
		// afiseaza eroare pentru completare username
	}
	else
	{
		$('#username').parent().removeClass('error');
		// ascunde eroare pentru completare username
	}
	
	if ( username )
	{
		// se poate face post
		$.blockUI( { message: "<div class='ajax-loading'><p><img src='images/loader.gif' alt='' border='0'></p></div>" } );
		$.post( 	
			'sources/do_login.php' , 
			{ 
				username_send: username,
				password_send: password,
				rememberme_send: rem
			} , 
			function( data )
			{
				$.unblockUI();
				
				if( data.success )
				{
					$('#login-panel').hide();
					$('#create-account-panel').hide();
					if ( data.user.credite == 1 ) cred = 'credit';
					else cred = 'credite';
					$('#myaccount_detail').html('Salut ' + data.user.prenume + ', mai ai ' + data.user.credite + ' ' + cred + '. <a href=\"profil/\">Modific&atilde; datele contului</a>, <a href=\"credit/\">cump&atilde;r&atilde; credite</a> sau <a href=\"logout/\">logout</a>.');
					$('#myaccount-panel').show();
					
					// completeaza datele ...
				}
				else
				{
					$('#login_error').html( data.message );
					$('#login_error').show();
				}
			}, 
			'json'
		);
		
	}
}

function do_login_keydown( e )
{
    if ( window.event ) // IE
    {
        key = window.event.keyCode;
    }
    else // Mozilla, Firefox, Opera
    {
        key = e.which;
    }
	
	if( key == 13 )
	{
		do_login();
	}
}

function change_pret( pkt_id, nr_luni, el )
{
	$.post(
		'sources/acts.php' , 
		{ 
			act: 'change_pret',
			pkt_id: pkt_id,
			nr_luni: nr_luni
		} , 
		function( data )
		{		
			if( data.success )
			{
				$('#' + el).html(data.html);
			}
		}, 
		'json'
	);
}

function pachet_disable( pkt_id )
{
	$.post(
		'sources/acts.php' ,
		{
			act: 'pachet_disable',
			pkt_id: pkt_id
		} ,
		function( data )
		{
			if ( data.success )
			{
				window.location = 'profil-pachete';
			}
		},
		'json'
	);
}

function check_pin()
{
	if ( $('#pin-error').hasClass('error') ) $('#pin-error').removeClass('error');
	if ( $('#pin-error').hasClass('succes') ) $('#pin-error').removeClass('succes');
	
	if ( $('#pin-error').is(':visible') ) $('#pin-error').fadeOut();
	pin = $('#credit-sms').val();
	
	if ( !pin )
	{
		$('#pin-error').addClass('error');
		$('#pin-error').html('Nu ai completat codul.');
		$('#pin-error').fadeIn();
	}
	else
	{
		$.post(
			'sources/acts.php' ,
			{
				act: 'check_pin',
				pin: pin
			} ,
			function( data )
			{
				if ( data.success )
				{
					$('#pin-error').addClass('succes');
					$('#pin-error').html( data.message );
					$('#pin-error').fadeIn();
				}
				else
				{
					$('#pin-error').addClass('error');
					$('#pin-error').html( data.message );
					$('#pin-error').fadeIn();
				}
			},
			'json'
		);
	}
}

function check_pin_key( e )
{
	if ( window.event ) // IE
    {
        key = window.event.keyCode;
    }
    else // Mozilla, Firefox, Opera
    {
        key = e.which;
    }
	
	if( key == 13 )
	{
		check_pin();
		return false;
	}
}

function send_contact()
{
	// reset errors
	$('#contact_eroare').fadeOut();
	$('#contact_eroare').removeClass('error');
	$('#contact_eroare').removeClass('succes');
	$('#span-nume').removeClass('error');
	$('#span-email').removeClass('error');
	$('#span-mesaj').removeClass('error');
	
	nume = $('#nume').val();
	email = $('#email').val();
	mesaj = $('#mesaj').val();
	tip = $('#tip_contact').val();
	
	eroare = '';
	
	if ( !nume || nume == 'Nume' )
	{
		eroare += 'Numele';
		$('#span-nume').addClass('error');
	}
	if ( !email || email == 'Email' )
	{
		if ( eroare ) eroare += ', Email-ul';
		else eroare = 'Email-ul';
		$('#span-email').addClass('error');
	}
	if ( !mesaj )
	{
		if ( eroare ) eroare += ', Mesajul';
		else eroare = 'Mesajul';
		$('#span-mesaj').addClass('error');
	}
	
	if ( eroare ) 
	{
		eroare = 'Nu ai completat: ' + eroare + '.';
		$('#contact_eroare').addClass('error');
		$('#contact_eroare').html( eroare );
		$('#contact_eroare').fadeIn();
	}
	
	if ( !eroare )
	{
		// facem post, try send message :)
		$.post(
			'sources/acts.php' ,
			{
				act: 'send_contact',
				nume: nume,
				email: email,
				mesaj: mesaj,
				tip: tip
			} ,
			function( data )
			{
				if ( data.success )
				{
					$('#contact_eroare').addClass('succes');
					$('#contact_eroare').html( data.message );
					$('#contact_eroare').fadeIn();
					
					// empty fields
					$('#nume').val( '' );
					$('#email').val( '' );
					$('#mesaj').val( '' );
				}
				else
				{
					if ( data.error_email && !$('#span-email').hasClass('error') ) $('#span-email').addClass( 'error' );
					$('#contact_eroare').addClass('error');
					$('#contact_eroare').html( data.message );
					$('#contact_eroare').fadeIn();
				}
			},
			'json'
		);
	}
}