$(function(){
	$('#category').selectmenu({
		style:'dropdown', 
		width:230,
		menuWidth: 230,
		maxHeight:150
	});
	$('#sort').selectmenu({
		width:180,
		menuWidth: 180,
		maxHeight:150
	});
	$('#numsort').selectmenu({
		width:50,
		menuWidth: 50,
		maxHeight:150
	});
	$('#sort_submit').selectmenu({
		width:150,
		menuWidth: 150,
		maxHeight:150
	});
	$('#category').change(function(){
				$('#form_categories').submit();
			});
	$('#sort').change(function(){
				$('#form_sort_options').submit();
			});
	$('#numsort').change(function(){
				$('#form_sort_filters').submit();
			});
	$('#sort_submit').change(function(){
				$('#form_sort').submit();
			});
	
});

$(document).ready(function(){
	which = null;
	
	$('#categories_list .topicslink').click(function() {
		which = this.id;
		if($(this).hasClass('shown')){
			$(this).addClass('hidden').removeClass('shown').html('<strong class="hide">Hide topics</strong>');
			$("#" + $(this).attr('id') + "_topics").show();
		} else {
			$(this).addClass('shown').removeClass('hidden').html('<strong class="hide">View topics</strong>');
			$("#" + this.id + "_topics").hide();
		}

		if($("#"+this.id+"_topics .fetch").length==1){ //If the 'fetch' LI is still there, do the fetch
			$.ajax({
				url: '/topics/?id=' + this.id.substr(3,this.id.length),
				dataType:'json',
				context:this,
				success: function(data) {
					if(data[0].noresult){
							$('<li><a href="/f/'+ data[0]._id +'">  ' + data[0].title + '</a></li>').appendTo($("#"+which + "_topics ul"));
					} else {
						$.each(data, function(i,item){
							$('<li><a href="/r/?id='+ item._id +'&url=' + item.link + '" target="_blank"> ' + item.title + '</a></li>').appendTo($("#"+which + "_topics ul"));
						});
					}
					$("#" + $(this).attr('id') + "_topics .fetch").remove();
				}
			});
		} else {
			//console.log('may already have results - do nothing');
		}
		return false;
		
	});
	
	//Edit links on myBabtu page
	$('#edit_forums').click(function(){
		if($(this).hasClass('edit')){//Go into edit mode
			$(this).removeClass('edit');
			$('#flex49').removeClass('edit');
			$('#edit_forums').html('Edit');
			$('#flex49 .unfollow').animate({width:'toggle'},300);
		} else {
			$(this).addClass('edit');
			$('#flex49').addClass('edit');
			$('#flex49 .unfollow').animate({width:'toggle'},300);
			$('#edit_forums').html('Finished');
		}
		return false;
	});
	$('#flex49 .unfollow').click(function(){
	   var removeUrl = $(this).attr('href');
	   var thisElem = $(this);
	   $(this).addClass('loading');
		$.ajax({
			   url: removeUrl, 
			   context: thisElem,
			   success: function(){
					if($(this).parent().siblings().length==0){
						$('<li class="wide_li"> <b>You are not following any forums!</b> </li>').appendTo($(this).parent().parent());
					}
					$(this).parent('li').remove();
				}
		});
		return false;
	});
	
	
	// Edit the 'Contacts' on the MyPage
	
	//Edit Profile Info on myBabtu page
	$('#editProfile').click(function(){
		if($(this).hasClass('edit')){//Go into edit mode
			$(this).removeClass('edit');
			$('#myProfile').removeClass('edit');
			$('#changePhoto').animate({width:'toggle'},300);
			$('#editProfile').html('Edit');
			
			
		} else {
			$(this).addClass('edit');
			$('#myProfile').addClass('edit');
			$('#changePhoto').animate({width:'toggle'},300);
			$('#editProfile').html('Finished');
			
			/* Make the threadDepth stuff editable */
			$('#currentThreadDepth').addClass('hide');
			$('#selectThreadDepth').removeClass('hide');
		}
		return false;
	});
	//Make the threadDepth select dropStyled
	$('#selectThreadDepth select').selectmenu({
		width:60,
		menuWidth: 60
	});
	//Allow the new photo stuff to affect the parent window's form
	$('#useNewPhoto').click(function(){
									 alert($(this).val());
									 alert(window.opener.selectThreadDepth.profilePhoto.value);
									 window.opener.selectThreadDepth.profilePhoto.val($(this.val()));
	});

	

	//Edit Contacts on myBabtu page
	$('#editContacts').click(function(){
		if($(this).hasClass('edit')){//Go into edit mode
			$(this).removeClass('edit');
			$('#myContacts').removeClass('edit');
			$('#editContacts').html('Edit');
			$('#myContacts .unfollow').animate({width:'toggle'},300);
		} else {
			$(this).addClass('edit');
			$('#myContacts').addClass('edit');
			$('#myContacts .unfollow').animate({width:'toggle'},300);
			$('#editContacts').html('Finished');
		}
		return false;
	});
	
	
	$('#myContacts .unfollow').click(function(){
	   var removeUrl = $(this).attr('href');
	   var thisElem = $(this);
	   $(this).addClass('loading');
		$.ajax({
			   url: removeUrl, 
			   context: thisElem,
			   success: function(){
					if($(this).parent().siblings().length==0){
						$('<li class="wide_li"> <b>You are not following anyone!</b> </li>').appendTo($(this).parent().parent());
					}
					$(this).parent('li').remove();
				}
		});
		return false;
	});
	
	$('#categories_list .toggle, .forum_meta .toggle').click(function(e){
		$('#toggleConfirm').animate({'height':0}, "linear");
		var toggleUrl = "/topics/toggle.php?id=" + $(this).attr('rel');
		var clicked = e.target;
		$.ajax({
			dataType: "json",
			url: toggleUrl,
			context:clicked,
			success: function(data){
				$('#toggleConfirm').remove();
				var code = data[0]['code'];
				var message = data[0]['message'];
				var action = data[0]['action'];
				$('#main').prepend('<div id="toggleConfirm" class="ra '+ code +'">' + message + '</div>');
				if(action == 'added'){
					$(this).addClass('following').removeClass('follow');
					$('#toggle').text('remove from myBabtu');
				}
				else if(action == 'removed'){
					$(this).addClass('follow').removeClass('following');
					$('#toggle').text('add to myBabtu');
				}
			}
		});
		return false;
	});
	
	// Header Add Forum submit form
	$('#submitForum').click(function(){
			//on, let's build the cat dropdowns
			$.getJSON('/topics/get_cat.php',function(data){
													$.each(data, function(i,item){
														$('<option value="'+ item.id +'">'+ item.name +'</option>').appendTo($("#selectCat"));
													});
												});
			$('#selectCat').change(function(id){
						var catId = $(this).val();
						$.getJSON('topics/get_cat.php?id='+catId,function(data){
													$("#selectSubCat").children().remove();
													$.each(data, function(i,item){
														$('<option value="'+ item.id +'">'+ item.name +'</option>').appendTo($("#selectSubCat"));
														$('#subcat').show();
													});
												});
			});
	
		return false;
	});
	
	$("#changePhoto").fancybox({
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'showCloseButton'	: true,
		'onClosed'			: function(){parent.location.reload();}
	});
	$("#submitForum").fancybox({
		   'autoDimensions'	: false,
			'width'         	: 600,
			'height'        	: 300,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type':'iframe'
	});	
	$("#betaFeedback").fancybox({
			'autoDimensions'	: false,
			'width'         	: 500,
			'height'        	: 200,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
	});
	$("#signUpBtn").fancybox({
			'type'				: 'iframe',
		   'autoDimensions'	: false,
			'width'         	: 650,
			'height'        	: 400,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
	});
	$(".big_box2").tooltip({
			'position':'center right'
								  });
	$("ul.tabs").tabs("div.panes > div");

});
