function showNotification( message, error ) {
	if( error == '1' ) {
		var span_class = 'error';
	} else {
		var span_class = 'success';
	}
	
	if( message ) {
		jQuery('.notification').html( '<span class="'+ span_class +'">'+ message +' <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
	}
	
	jQuery.blockUI({
    	message: jQuery('.notification'), 
    	fadeIn: 0, 
    	fadeOut: 500, 
    	timeout: 2500, 
    	showOverlay: false, 
    	centerY: false, 
    	css: { 
        	width: '100%',
        	height: '40px',
        	top: '0', 
        	left: '0', 
        	right: '', 
        	border: 'none', 
        	padding: '5px 0', 
        	backgroundColor: '#2e4457', 
        	opacity: 1, 
        	color: '#fff',
        	cursor: 'default'
    	}
	});
}

function refresh(rURL)
{
    window.location.href = rURL;
}

jQuery.noConflict();
jQuery(function($) {
	var hideOptions;
	
	$('#profile').mouseenter(function(e) {
		$('.user-options').show();	
	}).mouseleave(function() {
		hideOptions = setTimeout( function() { $('.user-options' ).hide(); }, 10 );
	})
	
	$('.user-options').mouseleave(function() {
		$(this).hide();
		$('#profile').removeClass( 'active' );
	}).mouseenter(function() {
		clearTimeout( hideOptions );
		$('#profile').addClass( 'active' );
	});
	
	$('.link-item:even').addClass( 'darker' );
	
	/**
	 * login action
	 */
	$('.send-login').click(function() {
		var t = $(this);
		if( $.trim( $('#user_login').val() ) == '' ) {
			$('.notification').html( '<span class="error">Please enter your username <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_login').focus();
			showNotification();
			return false;
		}
		
		if( $.trim( $('#user_pass').val() ) == '' ) {
			$('.notification').html( '<span class="error">Please enter your password <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_pass').focus();
			showNotification();
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: 'index.php?loginUser=true',
			data: t.parent().serialize(),
			dataType: 'text',
			success: function( data, status ) {
				if( data != 'ok' ) {
					$('.notification').html( '<span class="error">Your username or password is invalid - please try again <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
					showNotification();
				} else {
					refresh( $('input[name=return]').val() );
				}
			}
		});
		
		return false;
	});
	
	/**
	 * signup action
	 */
	$('.send-signup').click(function() {
		var t = $(this);
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
		if( $.trim( $('#user_login').val() ) == '' ) {
			$('.notification').html( '<span class="error">Please enter your desired username <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_login').focus();
			showNotification();
			return false;
		}
		
		if( $.trim( $('#user_pass').val() ) == '' ) {
			$('.notification').html( '<span class="error">Password required <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_pass').focus();
			showNotification();
			return false;
		}
		
		if( $.trim( $('#user_pass').val() ).length <= 4 ) {
			$('.notification').html( '<span class="error">Password needs to be more than 4 characters <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_pass').focus();
			showNotification();
			return false;
		}
		
		if( $.trim( $('#user_email').val() ) == '' || !emailFilter.test( $.trim( $('#user_email').val() ) ) ) {
			$('.notification').html( '<span class="error">E-mail required <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
			$('#user_email').focus();
			showNotification();
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: 'index.php?signUser=true',
			data: t.parent().serialize(),
			dataType: 'text',
			success: function( data, status ) {
				if( data == 'username_exists' || data == 'username_invalid' ) {
					$('.notification').html( '<span class="error">Username is not available <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
					showNotification();
					$('#user_login').focus().select();
				} else if ( data == 'email_exists' ) {
					$('.notification').html( '<span class="error">Email is already in use <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>' );
					showNotification();
					$('#user_email').focus().select();
				} else {
					$('.notification').html('<span class="success">Account created <a href="javascript:;" onclick="jQuery.unblockUI();"></a></span>');
					showNotification();
					setTimeout(function() { refresh( $('input[name=return]').val() ); }, 2500);
					
				}
			}
		});
		
		return false;
	});
	
	$('.signup-form #user_login').keyup(function() {
		$('.new-user').text( $.trim( $(this).val() ) );
	}).blur(function() {
		if( $.trim( $(this).val() ) == '' ) $('.new-user').text( 'USERNAME' );
	});
	
	/**
	 * show change action
	 */
	$('#show_name').change(function() {
		if( $(this).val() == '-1' ) {
			$('#show_season').val( '-1' ).attr( 'disabled', 'disabled' );
			$('#show_episode').val( '-1' ).attr( 'disabled', 'disabled' );
		} else {
			$('#show_season').val( '-1' ).attr( 'disabled', 'disabled' );
			$('#show_episode').val( '-1' ).attr( 'disabled', 'disabled' );
			$.ajax({
				type: 'POST',
				url: 'index.php?showSeasons=true',
				data: 'js='+ $('#show_name').val(),
				dataType: 'text',
				success: function( data, status ) {
					if( data != '0' ) {
						$('#show_season').html( data ).attr( 'disabled', '' );
					}
				}
			});
		}
	});
	
	/**
	 * season change action
	 */
	$('#show_season').change(function() {
		if( $(this).val() == '-1' ) {
			$('#show_episode').val( '-1' ).attr( 'disabled', 'disabled' );
		} else {
			$('#show_episode').val( '-1' ).attr( 'disabled', 'disabled' );
			$.ajax({
				type: 'POST',
				url: 'index.php?showEpisodes=true',
				data: 'js='+ $('#show_name').val() +'&jss='+ $('#show_season').val(),
				dataType: 'text',
				success: function( data, status ) {
					if( data != '0' ) {
						$('#show_episode').html( data ).attr( 'disabled', '' );
					}
				}
			});
		}
	});
	
	/**
	 * send link action
	 */
	$('.send-link').click(function() {
		var t = $(this);
		if( $('#show_name').val() == '-1' ) {
			$('#show_name').focus();
			showNotification( 'Please select a show', 1);
			return false;
		}
		
		if( $.trim( $('#show_url').val() ) == '' ) {
			$('#show_url').focus();
			showNotification( 'URL required', 1);
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: 'index.php?sendLink=true',
			data: t.parent().serialize(),
			dataType: 'text',
			success: function( data, status ) {
				if( data != '0' ) {
					showNotification( 'Thank you!' );
				} else {
					showNotification( 'Something went wrong. Please try again', 1 );
				}
			}
		});
		return false;
	});
});
