var defaultRating = false;

$(document).ready(function() {
	// User navi
	$('#user').hover(function() {
		if($.browser.msie) $('#sidebar').css('marginTop','140px');
		$('#user ul').show();
	},function() {
		if($.browser.msie) $('#sidebar').css('marginTop','0px');
		$('#user ul').hide();
	});
	
	// Photo
	$('#photo img').hover(function() {
		$('.fullsize').addClass('fullsize-hover');
	},function() {
		$('.fullsize').removeClass('fullsize-hover');
	});
	
	// Search
	$('#search-reveal').click(function() {
		$(this).hide();
		$('#search-form').fadeIn(300);
		$('#search-form .field').focus();
	});
	
	// Comment form
	$('#commentform textarea').click(function() {
		$(this).addClass('expanded');
	});
	
	$('#newcomment').click(function() {
		$('#commentform textarea').addClass('expanded');
		$('#commentform textarea').focus();
		return false;
	});
	
	// Sign in
	$('#signin').click(function() {
		$('#username').focus();
		return false;
	});
	
	// Comment: reply
	$('#comments .reply').click(function() {
		id = $(this).parent().parent().parent().attr('id');
		id = id.replace(/comment-/,'');
		
		$.get('?getcommentquote='+id,function(result) {
			if(result) {
				$('#commentform input[name=replyto]').val(id);
				$('#commentform textarea').val(result);
				$('#commentform textarea').addClass('expanded');
				document.location = '#commentform';
				$('#commentform textarea').focus();
				//setCaretToEnd(document.getElementById('commentform').elements.comment);
			}
		});
		
		return false;
	});

	// Comment: edit
	$('#comments .edit').click(function() {
		id = $(this).parent().parent().parent().attr('id');
		id = id.replace(/comment-/,'');
		
		$.get('?getcomment='+id,function(result) {
			if(result) {
				$('#commentform input[name=editcomment]').val(id);
				$('#commentform textarea').val(result);
				$('#commentform textarea').addClass('expanded');
				document.location = '#commentform';
				$('#commentform textarea').focus();
			}
		});
		
		return false;
	});
	
	// Photo: delete
	$('#photos .delete,#photo .delete').live('click',function() {
		if(confirm('Are you sure you want to delete this photo?')) {
			return true;
		} else {
			return false;
		}
	});

	// Pages: delete
	$('#pages .delete').live('click',function() {
		if(confirm('Are you sure you want to delete this page?')) {
			return true;
		} else {
			return false;
		}
	});
	
	// Comment: thumbs
	$('#comments .thumb').click(function() {
		dir = $(this).hasClass('thumbup') ? 'up' : 'down';

		id = $(this).parent().parent().parent().attr('id');
		if(!id) id = $(this).parent().parent().parent().parent().attr('id');
		id = id.replace(/comment-/,'');

		if(id) {
			$('#comment-'+id+' .thumbs').fadeOut(100);
	
			$.get('?thumb='+dir+'&comment='+id,function(result) {
				if(result == 'ERROR') {
					alert('Sorry, we couldn\'t process your thumb. Try again later.');
				} else {
					result = result.split(',');
					$('#comment-'+id+' .thumbup').text(result[0]);
					$('#comment-'+id+' .thumbdown').text(result[1]);
					$('#comment-'+id+' .thumbs').fadeIn(100);
				}
			});
		}
		
		return false;
	});
	
	// Comment: show hidden comment
	$('.showcomment').click(function() {
		$(this).parent().parent().find('.comment-content').slideDown();
		$(this).parent().remove();
	});
	
	// Comment: hide
	$('.hidecomment').click(function() {
		url = $(this).attr('href');
		$.get(url,function(result) {
			if(result == 'ERROR') {
				alert('Fail');
			} else {
				if($('#comment-'+result).find('.hidecomment').html() == 'Hide') {
					$('#comment-'+result).find('.hidecomment').html('Unhide');
					$('#comment-'+result).animate({opacity:0.3},200);
				} else {
					$('#comment-'+result).find('.hidecomment').html('Hide');
					$('#comment-'+result).animate({opacity:1},200);
				}
			}
		});
		
		return false;
	});

	// Post: hide
	$('.hidepost').click(function() {
		url = $(this).attr('href');
		$.get(url,function(result) {
			if(result == 'ERROR') {
				alert('Fail');
			} else {
				if($('#post-'+result).find('.hidepost').html() == 'Hide') {
					$('#post-'+result).find('.hidepost').html('Unhide');
					$('#post-'+result).animate({opacity:0.3},200);
				} else {
					$('#post-'+result).find('.hidepost').html('Hide');
					$('#post-'+result).animate({opacity:1},200);
				}
			}
		});
		
		return false;
	});

	// Forum: thumbs
	$('#forum-posts .thumb').click(function() {
		dir = $(this).hasClass('thumbup') ? 'up' : 'down';

		id = $(this).parent().parent().parent().attr('id');
		if(!id) id = $(this).parent().parent().parent().parent().attr('id');
		id = id.replace(/post-/,'');

		if(id) {
			$('#post-'+id+' .thumbs').fadeOut(100);
	
			$.get('?thumb='+dir+'&post='+id,function(result) {
				if(result == 'ERROR') {
					alert('Sorry, we couldn\'t process your thumb. Try again later.');
				} else {
					result = result.split(',');
					$('#post-'+id+' .thumbup').text(result[0]);
					$('#post-'+id+' .thumbdown').text(result[1]);
					$('#post-'+id+' .thumbs').fadeIn(100);
				}
			});
		}
		
		return false;
	});
	
	$('.thumb').hover(function() {
		dir = $(this).hasClass('thumbup') ? 'Up' : 'Down';
		$(this).parent().find('.thumbs'+dir).show();
	},function() {
		dir = $(this).hasClass('thumbup') ? 'Up' : 'Down';
		$(this).parent().find('.thumbs'+dir).hide();
	});
	
	// Forum: show hidden message
	$('.showpost').click(function() {
		$(this).parent().parent().find('.post-content').slideDown();
		$(this).parent().remove();
	});
	
	// Forum: form
	$('#postform textarea').click(function() {
		$(this).addClass('expanded');
	});

	$('#newpost').click(function() {
		$('#postform textarea').addClass('expanded');
		$('#postform textarea').focus();
		return false;
	});
	
	// Forum: reply
	$('#forum-posts .reply').click(function() {
		id = $(this).parent().parent().parent().attr('id');
		id = id.replace(/post-/,'');
		
		$.get('?getpostquote='+id,function(result) {
			if(result) {
				$('#postform input[name=replyto]').val(id);
				$('#postform textarea').val(result);
				$('#postform textarea').addClass('expanded');
				document.location = '#postform';
				$('#postform textarea').focus();
				setCaretToEnd(document.getElementById('postform').elements.message);
			}
		});
		
		return false;
	});


	// Forum: edit
	$('#forum-posts .edit').click(function() {
		id = $(this).parent().parent().parent().attr('id');
		id = id.replace(/post-/,'');
		
		$.get('?getpost='+id,function(result) {
			if(result) {
				$('#postform input[name=editpost]').val(id);
				$('#postform textarea').val(result);
				$('#postform textarea').addClass('expanded');
				document.location = '#postform';
				$('#postform textarea').focus();
			}
		});
		
		return false;
	});
	
	$('#forum-posts .delete').click(function() {
		if(confirm('Are you sure you want to delete this post?')) {
			url = $(this).attr('href');
			$.get(url,function(result) {
				if(result) {
					$('#post-'+result).fadeOut(100);
				}
			});
		}
		
		return false;
	});
	
	// Rating
	$('.rating a').hover(function() {
		id = $(this).parent().attr('id');
		if(!defaultRating) defaultRating = $('#'+id+' .highlight').length;
		rating = $(this).attr('id');
		rating = rating.replace(/rate-/,'') * 1;
		
		for(i = 1; i <= 5; i++) {
			if(i <= rating) {
				$('#rate-'+i).addClass('highlight');
			} else {
				$('#rate-'+i).removeClass('highlight');
			}
		}
	});
	
	$('.rating a').click(function() {
		id = $(this).parent().attr('id');
		id = id.replace(/rating-/,'');
		rating = $(this).attr('id');
		rating = rating.replace(/rate-/,'');
		
		$('#rating-'+id).fadeOut(100);

		$.get('?rate='+rating+'&photo='+id,function(result) {
			if(result == 'SIGNIN') {
				$('#rating-'+id).html('<span class="ratings">Please login to rate photos</span>');
			} else if(result == 'ERROR') {
				alert('Sorry, we couldn\'t handle your rating, please try again later');
			} else if(result == 'OWN') {
				alert('We are guessing 5 stars, since it\'s your own photo. :)');
			} else {
				result = result.split(':');
				defaultRating = result[0];
				totalRatings = result[1];
	
				for(i = 1; i <= 5; i++) {
					if(i <= result) {
						$('#rate-'+i).addClass('highlight');
					} else {
						$('#rate-'+i).removeClass('highlight');
					}
				}
				
				thisText = totalRatings == 1 ? ' (1 rating)' : ' ('+totalRatings+' ratings)';
				$('#rating-'+id+' .ratings').text(thisText);
			}

			$('#rating-'+id).fadeIn(100);
		});
		
		return false;
	});
	
	$('.rating').mouseout(function() {
		for(i = 1; i <= 5; i++) {
			if(i <= defaultRating) {
				$('#rate-'+i).addClass('highlight');
			} else {
				$('#rate-'+i).removeClass('highlight');
			}
		}
	});

	$('#photoform select[name=game]').change(function() {
		$('#photoform input[name=ready]').val(0);
		$('#photoform').submit();
	});
	
	// Photos: load more
	$('#photos .more .loadmore').live('click',function() {
		url = $(this).attr('href');
		$(this).remove();
		
		$.get(url,function(result) {
			$('.more').replaceWith(result);
		});
		
		return false;
	});
	
	// Messages
	$('#message-topics .delete').click(function() {
		id = $(this).parent().attr('id');
		id = id.replace(/topic-/,'');

		if(confirm('Are you sure you want to delete this message?')) {
			$.get('/messages/',{'delete':id,'ajax':1},function(result) {
				if(result == 'OK') {
					$('#topic-'+id).slideUp(200);
				} else {
					alert('An error occured when deleting message. Please try again.');
				}
			});
		}

		return false;
	});

	if($('#to').length > 0) {
		if($('#to').val() != '') {
			$('#title').focus();
		} else {
			$('#to').focus();
		}
	}	

	// Friends
	$('#friends .delete').click(function() {
		return confirm('Are you sure you want to remove this user from your friends?');
	});
	
	// Moderator
	$('#moderate-ban').click(function() {
		url = $(this).attr('href');
		var reason = prompt('Why are you banning this user?','');
		if(reason != null && reason != '') {
			document.location = url+'&reason='+reason;
		}
		return false;
	});

	$('#moderate-delete').click(function() {
		if(confirm('Are you sure?')) {
			return true;
		}
		
		return false;
	});
	
	// Photos
	$('#photo-url').focus(function() {
		val = $(this).val();
		if(val == 'http://') {
			$(this).val('');
		}
	});

	$('#photo-url').blur(function() {
		val = $(this).val();
		if(val == '') {
			$(this).val('http://');
		}
	});
	
	if($('#messageform #to').length > 0) {
		$('#messageform #to').autocomplete('?searchusers=1',{delay: 200});
	}
});

function setCaretToEnd (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    var length = control.value.length;
    control.setSelectionRange(length, length);
  }
}
