$(document).ready(function() {

	// Disable caching
	$.ajaxSetup({
		cache: false
	});

	var error = "0";
	
	// Preload images
	$.preload_images = function() {
		for (var i = 0; i < arguments.length; i++) {
			$('<img>').attr('src', arguments[i]);
		}
	}
	
	$.preload_images('/images/btn_likebtn.gif', '/images/btn_likebtn2.gif', '/images/btn_hatebtn.gif', '/images/btn_hatebtn2.gif');
	
	// Auto grow boxes
	$('textarea.expanding').autogrow();
	$('#error').fadeIn('slow');
	$('#success').fadeIn('slow');
	$('#alertmessage').fadeIn('slow');
	
	/*---------------------------------------------------------------------------*/
	// Navigation tabs
	/*---------------------------------------------------------------------------*/
	function switch_tab(which, tab) {
		$.ajax({
			url: tab,
			cache: false,
			success: function(content) {
				
				// Figure out if this is an error or success
				var test = new Array();
				test = content.split('::');

					if (which == 1) {
						// Change class
						$('#liked_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#hated_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#comment_tab').removeClass('sectiontabdis').addClass('sectiontab');	
					} else if (which == 2) {
						// Change class
						$('#comment_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#hated_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#liked_tab').removeClass('sectiontabdis').addClass('sectiontab');
					} else if (which == 3) {
						// Change class
						$('#liked_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#comment_tab').removeClass('sectiontab').addClass('sectiontabdis');
						$('#hated_tab').removeClass('sectiontabdis').addClass('sectiontab');				
					}
			if (test[0] != 'error') {		
					// Show the content
					$('#tab_body').empty('fast').append(content);
					
					// Show comment post if comment
					if (which == 1) {
						$('#post_new_comment').fadeIn('slow');
					}
				} else {
	   				$('#tab_body').empty().append('<div id="error">'+test[1]+'</div>');
	   				$('#error').fadeIn('slow');
	   			}
	   		}
		});
	}

	// Comments tab
	$('#comment_tab').click(function() {
		var id	= $('.tab_comment').attr('id');
		switch_tab(1, '/actions/view.php?action=comments&c_id=' + id);
		$('#sectionrss').show();
		return false;
	});
	
	// Likes tab
	$('#liked_tab').click(function() {
		var id	= $('.tab_liked').attr('id');
		switch_tab(2, '/actions/view.php?action=likes_dislikes&option=likes&c_id=' + id);
		$('#sectionrss').hide();
		return false;
	});
	
	// Hates tab
	$('#hated_tab').click(function() {
		var id	= $('.tab_hated').attr('id');
		switch_tab(3, '/actions/view.php?action=likes_dislikes&option=hates&c_id=' + id);
		$('#sectionrss').hide();
		return false;
	});

	/*---------------------------------------------------------------------------*/
	// Tour Navigation tabs
	/*---------------------------------------------------------------------------*/
	function tour_tabs(which) {
		if (which == 1) {
			// Discover
			$('#tab_comments').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_vote').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_discover').removeClass('sectiontabdis').addClass('sectiontab');	
		
			// Discover area
			$('#tour_vote').fadeOut('fast', function() {
				$('#tour_comments').fadeOut('fast', function() {
					$('#tour_discover').fadeIn('fast');
				});
			});
		} else if (which == 2) {
			// Vote class
			$('#tab_discover').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_comments').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_vote').removeClass('sectiontabdis').addClass('sectiontab');
			
			// Vote area
			$('#tour_discover').fadeOut('fast', function() {
				$('#tour_comments').fadeOut('fast', function() {
					$('#tour_vote').fadeIn('fast');
				});
			});
		} else if (which == 3) {
			// Comments class
			$('#tab_discover').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_vote').removeClass('sectiontab').addClass('sectiontabdis');
			$('#tab_comments').removeClass('sectiontabdis').addClass('sectiontab');				

			// Comments area
			$('#tour_vote').fadeOut('fast', function() {
				$('#tour_discover').fadeOut('fast', function() {
					$('#tour_comments').fadeIn('fast');
				});
			});
		}
	}

	// Discover tab
	$('#tab_discover').click(function() {
		tour_tabs(1);
		return false;
	});
	
	// Vote tab
	$('#tab_vote').click(function() {
		tour_tabs(2);
		return false;
	});
	
	// Comments tab
	$('#tab_comments').click(function() {
		tour_tabs(3);
		return false;
	});

	/*---------------------------------------------------------------------------*/
	// Comments
	/*---------------------------------------------------------------------------*/

	// New comment
	$('.submit_comment').click(function() {

		var error	= 0;

		// Vars we need
	   	$('#responsemsg').empty();
	   	var comment	= $('textarea[name=comment]').val();
	   	var c_id	= $('input[name=c_id]').val();
	   	var com_id	= $('input[name=com_id]').val();

		// Account for line breaks
		comment		= URLEncode(comment.replace(/[\r\n]+/g, '-linebreak-'));

		var like	= $('input[name=like-' + com_id + ']').is(':checked');

		if (like == true)
			like	= '1';
		else
			like	= '0';

	   	// Blank comment
	   	if (comment == '') {
	   		$('#responsemsg').append('<div id="error">You must enter in a comment.</div>');
	       	error	= 1;
	   	}

	   	// Comment is too short
	   	if (comment.length < 3) {
	   		$('#responsemsg').append('<div id="error">Your comment is too short.</div>');
			error	= 1;
	   	}

	   	// Output error
	   	if (error == 1) {
   			$('#responsemsg').append('<div id="error">'+response+'</div>');
   			$('#error').fadeIn('slow');
	    }

	   	if (error == 0) {
			$.ajax({
	   			data: 'action=post_comment&c_id=' + c_id + '&comment=' + comment + '&like=' + like,
	   			type: 'POST',
				cache: false,
				url: '/actions/comments.php',
				success: function(response) {
					// Figure out if this is an error or success
					var test = new Array();
					test = response.split('::');

					if (test[0] != 'error') {
			   			var c_id	= $('input[name=c_id]').val();
						var like	= $('input[name=like-' + c_id + ']').is(':checked');
						
						if (like == true)
							like	= '1';
						else
							like	= '0';

			   			// Remove post comment box
			   			$('form[name=post_comment]').fadeOut('slow');
			   			$('form[name=post_comment]').remove();
		
						// Let them know it's a success
						$('#responsemsg').empty();
						$('#responsemsg').append('<div id="success">Your comment has been posted.</div>');
						$('#success').fadeIn('slow');
		
						// Show this comment
						var com_id	= response;
						$('#new_comment').load('/actions/comments.php?action=refresh_comments&com_id=' + com_id + '', '', function() {
							$('#new_comment').fadeIn('slow');
						});
						//$('#success').fadeOut('slow');
						
						// Link votes
						if (like == '1') {
							// Adjust the vote count
							var count	= Number($('#results-' + c_id).text());
							count		= count + 1;

							$('#results-' + c_id).text(count).fadeIn('slow');
		
							// Adjust the vote images
							$('#vote-' + c_id).empty().addClass('votebtn_back');
							$("#tooltip").remove();
							
							// If they liked, remove options
							$('.comment-like').empty();
						}
					} else {
		   				//$('#tab_body').empty().append('<div id="error">'+test[1]+'</div>');
		   				//$('#error').fadeIn('slow');
		   			}
		   			return false;
		   		}
			});
		} else {
			// Show error messages
			$('#error').fadeIn('slow');
			$('#reply_msg_' + com_id).fadeIn('slow');
		}
		return false;
	});

	// Comment reply
	$('.submit_reply').click(function() {

		var error	= 0;

		// Grab some values we'll need
		var id			= $(this).attr('id');
		var split		= id.split(':');

		var c_id		= split[0];
		var parent_id	= split[1];
		var reply_to	= split[2];
		var com_id		= split[3];
		var com_uid		= split[4];

		var comment		= $('textarea[name=reply_comment' + com_id + ']').val();

		var like		= $('input[name=like-' + com_id + ']').is(':checked');
		
		if (like == true)
			like	= '1';
		else
			like	= '0';

		// Account for line breaks
		comment			= URLEncode(comment.replace(/[\r\n]+/g, '-linebreak-'));

		// Empty comment error	
		$('#reply_msg_' + com_id).empty();

	   	// Blank comment
	   	if (comment == '') {
	   		$('#reply_msg_' + com_id).append('<div id="error" style="position: relative; top: 8px;">You must enter in a comment.</div>');
	       	error	= 1;
	   	}

	   	// Comment is too short
	   	if (comment.length < 3) {
	   		$('#reply_msg_' + com_id).append('<div id="error" style="position: relative; top: 8px;">Your comment is too short.</div>');
				error	= 1;
	   	}
	   	
	   	if (error == 0) {
			$.ajax({
	   			data: 'action=post_reply&c_id=' + c_id + '&parent_id=' + parent_id + '&comment=' + comment + '&reply_to_user=' + reply_to + '&like=' + like + '&reply_to_userid=' + com_uid,
	   			type: 'GET',
				cache: false,
				url: '/actions/comments.php',
				success: function(response) {

					// Figure out if this is an error or success
					var test = new Array();
					test = response.split('::');
	
					if (test[0] != 'error') {
						// Fade out comment box
						$('#reply_' + com_id).fadeOut('slow');
						
						// Fade out smilies
						$('#reply_smilies-' + com_id).fadeOut('fast');
						
						// Fade in new comment
						$('#reply_comment_' + com_id).append(response).fadeIn('slow');
						
						// Link votes
						if (like == '1') {
							// Adjust the vote count
							var count	= Number($('#results-' + c_id).text());
							count		= count + 1;

							$('#results-' + c_id).text(count).fadeIn('slow');
		
							// Adjust the vote images
							$('#vote-' + c_id).empty().addClass('votebtn_back');
							$("#tooltip").remove();
							
							// If they liked, remove options
							$('.comment-like').empty();
						}
					} else {
		   				//$('#tab_body').empty().append('<div id="error">'+test[1]+'</div>');
		   				//$('#error').fadeIn('slow');
		   			}
		   			return false;
		   		}
			});
		} else {
			// Show error messages
			$('#error').fadeIn('slow');
			$('#reply_msg_' + com_id).fadeIn('slow');
		}
		return false;
	});
	

	/*---------------------------------------------------------------------------*/
	// Register
	/*---------------------------------------------------------------------------*/

	var registrationoptions = {
	  	url:	'/actions/users.php?q=register',
	   	type:	'POST',
	   	beforeSubmit: function() { 		// error checking
	   		$('#responsemsg').empty();
	   		if ($('input[name=regname]').val()=="") {
	   			$('#responsemsg').append('<div id="error">Please enter your desired user name.</div>');
	       		error="1";
	   		}

	   		if ($('input[name=regemail]').val()=="") {
	   			$('#responsemsg').append('<div id="error">Please enter your email address.</div>');
	       		error="1";
	   		}
		   		
	   		if ($('input[name=regpass1]').val()=="") {
	   			$('#responsemsg').append('<div id="error">Please enter a password.</div>');
	       		error="1";
	   		}
		   		
	   		pass1 = $('input[name=regpass1]').val();
	   		if (pass1.length<5) {
	   			$('#responsemsg').append('<div id="error">Your password must be at least 5 characters.</div>');
	       		error="1";
	   		}
		   		
	  		if ($('input[name=regpass2]').val()=="") {
	   			$('#responsemsg').append('<div id="error">Please confirm your password.</div>');
	       		error="1";
	   		}
		   		
	   		if ($('input[name=regpass2]').val()!=$('input[name=regpass1]').val()) {
	  			$('#responsemsg').append('<div id="error">Your passwords do not match.</div>');
	       		error="1";
	   		}
		   		
	   		// display error response
	   		if (error=="1") {
	          	$('#error').fadeIn('slow');
	          	return false;
	          } else {
	          	return true;
	          }
	  	},
	
	   	success:	function(response) {
	   		$('#responsemsg').empty();
	   		if (response=="success") {
	   			$('form[name=registerform]').fadeOut('slow');
				$('form[name=registerform]').remove();
				$('#responsemsg').empty();
				$('#responsemsg').append('<div id="success">Congratulations! Your account was created! Check your email for a confirmation link.</div>');
				$('#success').fadeIn('slow');
	   		} else {
	   			$('#responsemsg').append('<div id="error">'+response+'</div>');
	   			$('#error').fadeIn('slow');
	   		}
	   	}
	};

	// submit the form
	$('form[name=registerform]').ajaxForm(registrationoptions);
	// ----End Submit Registration

	/*---------------------------------------------------------------------------*/
	// Voting/Favorites
	/*---------------------------------------------------------------------------*/

	// Liked it
	$('a.vote_like').click(function() {
		// Get the content ID
		var c_id	= $(this).attr('id');
		
		// Process this vote
		$.ajax({
			type: 'GET',
			data: 'action=vote&sub=like&c_id=' + $(this).attr('id'),
			url: '/actions/vote.php',
			success: function(response) {

				// If we have a true response
				if (response != 'error') {
					// Adjust vote count
					$('#results-' + c_id).html(response).fadeIn('slow');

					// Adjust the vote images
					$('#vote-' + c_id).empty().addClass('votebtn_back');
					$("#tooltip").remove();
					
					// If they liked, remove options
					$('.comment-like').hide();
					
					// Older format
					//$('#likehate_likebtn-' + c_id).empty().addClass('likehate_likebtn2');
					//$('#likehate_hatebtn-' + c_id).empty().append(' ');
				}
			}
		});
	});

	// Favorite
	$('a.link_save').click(function() {
		// Get the content ID
		var c_id	= $(this).attr('id');
		
		// Process this vote
		$.ajax({
			type: 'GET',
			data: 'action=favorite&c_id=' + $(this).attr('id'),
			url: '/actions/vote.php',
			success: function(response) {

				// If we have a true response
				if (response == 'success') {
					// Adjust the vote images
					$('#save-' + c_id).empty().addClass('savebtn_back');
				}
				$("#tooltip").remove();
			}
		});
	});

	/*---------------------------------------------------------------------------*/
	// Outgoing link tracking
	/*---------------------------------------------------------------------------*/
	$('a.outgoing').click(function() {
		// Get the content ID
		var c_id	= $(this).attr('id');

		// Record this outgoing hit
		$.ajax({
			type: 'GET',
			data: 'action=outgoing&c_id=' + $(this).attr('id'),
			url: '/actions/outgoing.php',
			success: function(response) {
				return true;
			}
		});

		return true;
	});

	/*---------------------------------------------------------------------------*/
	// Send Message
	/*---------------------------------------------------------------------------*/
	var composemsgoptions = {
		url:	'/actions/users.php?q=composemail', // post back url
		success:	function(response) {
			$('#responsemsg').empty();
			// display success or failures
			if (response=="success") {
				$('#composemsg').fadeOut('slow');
				$('#composemsg').resetForm();
				$('#responsemsg').empty();
				$('#smiliesbox').hide();
				$('#responsemsg').append('<div id="success">You message was sent successfully. <a href="#" onClick="$(\'#responsemsg\').hide(\'fast\',function() { $(\'#composemsg\').slideDown(); } );">Click here</a> to send another.</div>');
				$('#success').fadeIn('slow');
				$('#responsemsg').fadeIn('slow')
			} else {
				$('#responsemsg').empty();
				$('#responsemsg').append('<div id="error">'+response+'</div>');
				$('#error').fadeIn('slow');
			}
				
		},
		beforeSubmit:	function() { // double check all fields are supplied

    	$('#responsemsg').empty();
    	if ($('#sendtoinput').val()=="") {
        	$('#responsemsg').append('<div id="error">You did not specify a recipient.</div>');
        	error="1";
        }
        
        if ($('#subject').val()=="") {
        	$('#responsemsg').append('<div id="error">You did not enter a subject for the message.</div>');
      		error="1";
        }
        
        if ($('#body').val()=="") {
        	$('#responsemsg').append('<div id="error">You did not enter a message to send.</div>');
      		error="1";
        }
        
        if (error=="1") {
        	$('#error').fadeIn('slow');
        	return false;
        } else {
        	return true;
        }
    }
	};

	// submit the form
	$('#composemsg').ajaxForm(composemsgoptions);
	// ----Compose Message Functions
                    
	/*---------------------------------------------------------------------------*/
	// Delete Message
	/*---------------------------------------------------------------------------*/
				var deletemsgoptions = {
					url:	'/actions/users.php?q=deletemail', // post back url
					type:	'POST',
					success:	function(response) {
							$('div[name='+response+']').slideUp(function() { $('div[name='+response+']').remove(); });
							$('#responsemsg').empty();
					}
				};
		
            // submit the form
            $('form[name=deletemsgbtn]').ajaxForm(deletemsgoptions);
            // ----End Delete Message Functions
            
            // ----Change Password Form
            var changepasswordoptions = {
            	url:	'/actions/users.php?q=changepassword',
            	type: 'POST',
            	beforeSubmit: function() {
            		$('#responsemsg').empty();
            		if ($('input[name=oldpass]').val()=="") {
            			$('#responsemsg').append('<div id="error">Please enter your current password.</div>');
	              		error="1";
            		}
            		
            		if ($('input[name=newpass]').val()=="") {
            			$('#responsemsg').append('<div id="error">Please enter your new password.</div>');
	              		error="1";
            		}
            		
            		if ($('input[name=newpass2]').val()=="") {
            			$('#responsemsg').append('<div id="error">Please confirm your new password.</div>');
	              		error="1";
            		}
            		
            		pass1 = $('input[name=newpass]').val();
            		if (pass1.length<5) {
            			$('#responsemsg').append('<div id="error">Your password must be at least 5 characters.</div>');
	              		error="1";
            		}
            		
            		if ($('input[name=newpass]').val()!=$('input[name=newpass2]').val()) {
            			$('#responsemsg').append('<div id="error">Your new passwords do not match.</div>');
	              		error="1";
            		}
            		            		
            		// display error response
            		if (error=="1") {
	                	$('#error').fadeIn('slow');
	                	return false;
	                } else {
	                	return true;
	                }

            	},
            	success:	function (response) { 
            		$('#responsemsg').empty();
            		if (response=="success") {
            			$('form[name=changepasswordform]').fadeOut('slow');
							$('form[name=changepasswordform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your password has been updated.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=changepasswordform]').ajaxForm(changepasswordoptions);
            // ----End Change Password Form
            
            // ----Edit Box Form
            var editboxformoptions = {
            	url:	'/actions/users.php?q=editbox',
            	type: 'POST',
              	success:	function (response) { 
            		$('#responsemsg').empty();
            		if (response=="success") {
            			$('form[name=editboxform]').fadeOut('slow');
							$('form[name=editboxform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your profile box has been updated and visible from your profile page.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=editboxform]').ajaxForm(editboxformoptions);
            // ----End Edit Box Form
            
             // ----Edit Blog Form
            var editblogformoptions = {
            	url:	'/actions/users.php?q=editblog',
            	type: 'POST',
            	beforeSubmit: function() {
            		if ($('textarea[name=blogtext]').val()=="") {
            			$('#responsemsg').append('<div id="error">You did not enter text for your blog.</div>');
	              		error="1";
            		}
            		
            		if ($('textarea[name=blogtitle]').val()=="") {
            			$('#responsemsg').append('<div id="error">You did not enter a title for your blog.</div>');
	              		error="1";
            		}
            		
            		// display error response
            		if (error=="1") {
	                	$('#error').fadeIn('slow');
	                	return false;
	                } else {
	                	return true;
	                }    
            	},
              	success:	function (response) { 
            		$('#responsemsg').empty();
            		if (response=="success") {
            			$('form[name=editblogform]').fadeOut('slow');
							$('form[name=editblogform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your blog has been saved.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=editblogform]').ajaxForm(editblogformoptions);
            // ----End Edit Blog Form
            
             // ----Edit Account Form
            var changeaccountoptions = {
            	url:	'/actions/users.php?q=editaccount',
            	type: 'POST',
            	beforeSubmit: function() {
            		$('#responsemsg').empty();
            		if ($('input[name=email]').val()=="") {
            			$('#responsemsg').append('<div id="error">Please enter your current email.</div>');
	              		error="1";
            		}
            		
            		if ($('select[name=gender]').val()=="") {
            			$('#responsemsg').append('<div id="error">Please select your gender.</div>');
	              		error="1";
            		}
            		
            		sig1 = $('textarea[name=signature]').val();
            		if (sig1.length>250) {
            			$('#responsemsg').append('<div id="error">Your signature must be less than 250 characters.</div>');
	              		error="1";
            		}
                        		            		
            		// display error response
            		if (error=="1") {
	                	$('#error').fadeIn('slow');
	                	return false;
	                } else {
	                	return true;
	                }            	
            	},
              	success:	function (response) { 
            		$('#responsemsg').empty();           		
            		if (response=="success") {
            			$('form[name=changeaccountform]').fadeOut('slow');
							$('form[name=changeaccountform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your account changes have been updated.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=changeaccountform]').ajaxForm(changeaccountoptions);
            // ----End Edit Account Form
            
            // ----Edit Notifications Form
            var changenotificationformoptions = {
            	url:	'/actions/users.php?q=notification_settings',
            	type: 'POST',
              	success:	function (response) { 
            		$('#responsemsg').empty();           		
            		if (response=="success") {
            			$('form[name=changenotificationform]').fadeOut('slow');
							$('form[name=changenotificationform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your notification changes have been updated.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=changenotificationform]').ajaxForm(changenotificationformoptions);
            // ----End Edit Notifications Form
            
             // ----Edit Link Options Form
            var changelinkoptionsformoptions = {
            	url:	'/actions/users.php?q=link_settings',
            	type: 'POST',
              	success:	function (response) { 
            		$('#responsemsg').empty();           		
            		if (response=="success") {
            			$('form[name=changelinkoptionsform]').fadeOut('slow');
							$('form[name=changelinkoptionsform]').remove();
							$('#responsemsg').empty();
							$('#responsemsg').append('<div id="success">Your link display settings have been updated.</div>');
							$('#success').fadeIn('slow');
            		} else {
            			$('#responsemsg').append('<div id="error">'+response+'</div>');
            			$('#error').fadeIn('slow');
            		}
            	}
            };	
             // submit the form
            $('form[name=changelinkoptionsform]').ajaxForm(changelinkoptionsformoptions);
            // ----End Edit Link Options Form


            
	/*---------------------------------------------------------------------------*/
	// Submit
	/*---------------------------------------------------------------------------*/
	
	// Submit type
	$('a.submit_options').click(function() {
		var value	= $(this).attr('id');

		if (value == 1) {
			// Change class
			$('#tab_video').removeClass('submittab').addClass('submittabdis');
			$('#tab_photo').removeClass('submittab').addClass('submittabdis');
			$('#tab_blog').removeClass('submittab').addClass('submittabdis');
			$('#tab_story').removeClass('submittabdis').addClass('submittab');

			// Story
			$('#submit_video').fadeOut('fast', function() {
				$('#submit_photo').fadeOut('fast', function() {
					$('#submit_blog').fadeOut('fast', function() {
						$('#submit_story').fadeIn('slow');
					});
				});	
			});	
		} else if (value == 2) {
			// Change class
			$('#tab_story').removeClass('submittab').addClass('submittabdis');
			$('#tab_photo').removeClass('submittab').addClass('submittabdis');
			$('#tab_blog').removeClass('submittab').addClass('submittabdis');
			$('#tab_video').removeClass('submittabdis').addClass('submittab');

			// Video
			$('#submit_story').fadeOut('fast', function() {
				$('#submit_photo').fadeOut('fast', function() {
					$('#submit_blog').fadeOut('fast', function() {
						$('#submit_video').fadeIn('slow');
					});
				});	
			});		
		} else if (value == 3) {
			// Change class
			$('#tab_video').removeClass('submittab').addClass('submittabdis');
			$('#tab_story').removeClass('submittab').addClass('submittabdis');
			$('#tab_blog').removeClass('submittab').addClass('submittabdis');
			$('#tab_photo').removeClass('submittabdis').addClass('submittab');

			// Photo
			$('#submit_video').fadeOut('fast', function() {
				$('#submit_story').fadeOut('fast', function() {
					$('#submit_blog').fadeOut('fast', function() {
						$('#submit_photo').fadeIn('slow');
					});
				});	
			});	
		} else if (value == 4) {
			// Change class
			$('#tab_video').removeClass('submittab').addClass('submittabdis');
			$('#tab_photo').removeClass('submittab').addClass('submittabdis');
			$('#tab_story').removeClass('submittab').addClass('submittabdis');
			$('#tab_blog').removeClass('submittabdis').addClass('submittab');

			// Blog
			$('#submit_video').fadeOut('fast', function() {
				$('#submit_photo').fadeOut('fast', function() {
					$('#submit_story').fadeOut('fast', function() {
						$('#submit_blog').fadeIn('slow');
					});
				});	
			});	
		}
		return false;
	});
}); 

/*---------------------------------------------------------------------------*/
// Functions
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
// Follow me
/*---------------------------------------------------------------------------*/
function followuser(userid, username) {
	$('div[name=userid'+userid+']').fadeOut('slow',function() { $('div[name=userid'+userid+']').load('/actions/follow/'+userid+'/'+username,function() { $('div[name=userid'+userid+']').fadeIn('slow'); } )} );
}

/*---------------------------------------------------------------------------*/
// UnFollow Me
/*---------------------------------------------------------------------------*/
function unfollowuser(userid, username) {
	$('div[name=userid'+userid+']').fadeOut('slow',function() { $('div[name=userid'+userid+']').load('/actions/unfollow/'+userid+'/'+username,function() { $('div[name=userid'+userid+']').fadeIn('slow'); } )} );
}

/*---------------------------------------------------------------------------*/
// Report link
/*---------------------------------------------------------------------------*/
function report(c_id) {
	$('#report-' + c_id).load('/actions/report/' + c_id, function() {
		$('#report-' + c_id).fadeIn('normal');
	});
}

/*---------------------------------------------------------------------------*/
// Add Smilie
/*---------------------------------------------------------------------------*/
function add_smilie(myField, myValue) {
	// IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	// MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos,myField.value.length);

	} else {
		myField.value += myValue;
	}	
}

/*---------------------------------------------------------------------------*/
// Prompt user box
/*---------------------------------------------------------------------------*/
function textprompt(type, tag, myField) {
	
	if (type == 'text') {
		var n	= prompt("Enter the text you want to " + tag);
	} else if (type == 'image') {
		var n	= prompt("Enter the URL of the image to use", "http://");
	} else if (type == 'link') {
		var o	= prompt("Enter the URL of the link", "http://");
		var n	= prompt("Enter the title of the link", "Check this out");
	}
	
	if (n == '' || n == 'null' || n == 'http://') {
		return false;
	} else {
		if (tag == 'bold') {
			var prepend 	= '<b>';
			var append		= '</b>';
		} else if (tag == 'italic') {
			var prepend 	= '<i>';
			var append		= '</i>';
		} else if (tag == 'underline') {
			var prepend 	= '<u>';
			var append		= '</u>';
		} else if (tag == 'strikeout') {
			var prepend 	= '<s>';
			var append		= '</s>';
		} else if (tag == 'image') {
			var prepend 	= '<img src="';
			var append		= '">';
		} else if (tag == 'link') {
			var prepend 	= '<a href="' + o + '">';
			var append		= '</a>';
		}
		
		add_smilie(myField, prepend + n + append);
	}
}

/*---------------------------------------------------------------------------*/
function URLEncode (clearString) {
/*---------------------------------------------------------------------------*/
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

