// B-SENSE V2 JAVASCRIPT //

// Custom functions
jQuery.fn.reset = function(fn) {
return fn ? this.bind("reset", fn) : this.trigger("reset");
};

$(document).ready(function(){
	// MAIN //
	var defaultEmailValue = 'my@email.com';

	// CREW //
	$(".crew_container .toggleInfo").hide();
	$(".crew_container").click(function(){
		
		$(this).find(".toggleInfo").slideToggle("fast");
		
		if ($(this).attr('id') != "") {
			getTwitterStatus($(this).attr('id'), this);
		}
		
	});
	
	// SEARCH
	$('input[name="zoekterm"]').keyup(submitSearchForm).attr('autocomplete', 'off');
	
	//TAGGING
	$('input[name="tagFriend"]').keyup(submitTagFriends).attr('autocomplete', 'off');
	$('#tagFriendsSearchResults input[type="checkbox"]').click(submitTagFriend);
	$('#taggedPeople a').click(submitRemovePhotoTag);
	
	//FRIENDS
	$('a.addUserAsFriend').click(submitAddUserAsFriend);
	
	// PHOTO OPTIONS
	$('#photoOptions').show();
	
	$('.opt_tag').click(function(e){
		$('#tagFriendsContainer').fadeIn('fast');
		submitTagFriendsReset();
		$('#tagFriendsContainer form').reset();
		
		return false;
	});
	
	$('.closeTagContainer').click(function(e){
		$('#tagFriendsContainer').fadeOut('fast');
		return false;
	});
	
	//comments
	$('.postcomment').autoResize({
    	onResize : function() {
        	$(this).css({opacity:0.8});
    	},
    	animateCallback : function() {
       		$(this).css({opacity:1});
    	},
    	animateDuration : 300,
 		extraSpace : 20
	});
	
	// LOGIN //
	$('#login-email').attr("value",defaultEmailValue);
	$('#login-password').attr("value","password");
	
	$('#login-email').click(function() {
		if (this.value == defaultEmailValue) {
			this.value = '';
		}
	});
	$('#login-email').blur(function() {
		if (this.value == '') {
			this.value = defaultEmailValue;
		}
	});

	$('#login-password').click(function() {
		if (this.value == 'password') {
			this.value = '';
		}
	});
	$('#login-password').blur(function() {
		if (this.value == '') {
			this.value = 'password';
		}
	});
	
	$('.sharepic-facebook').popupWindow({centerBrowser: 1, height: 400, width: 550});
	$('.register-facebook').popupWindow({centerBrowser: 1, height: 400, width: 800});

	$('#selectava').hide();

	$('input[name=selectavatar]').click(function() {
		$('#selectava').dialog({
			title: 'Upload een avatar',
			modal: true,
			width: 330,
			height: 200,
			buttons: {
				'Uploaden': function () {
					alert('bezig met uploaden');
				},
				'Sluiten': function() {
					$(this).dialog('close');
				}
			}
		});
	});


}); // .einde ready

// FUNCTIONS //
function submitSearchForm(e) {
	
	$.ajax({
		type: "POST",
		data: $('#search form').serialize(),
		url: "/search/",
		timeout: '5000',
		success: function(data){
					$('#contentbox #content').html(data);
				 }
		});
			
 }
 
 function submitTagFriends(e) {
	
	$.ajax({
		type: "POST",
		data: $('#tagFriendsContainer form').serialize(),
		url: "?callback=?",
		timeout: '5000',
		success: function(data){
					
					$('#tagFriendsSearchResults fieldset').html(data);
					$('#tagFriendsSearchResults input[type="checkbox"]').click(submitTagFriend);
					
				 }
		});
 }
 
 function submitTagFriendsReset() {
	
	$.ajax({
		type: "POST",
		data: {tagFriend: ""},
		url: "?callback=?",
		timeout: '5000',
		success: function(data){
					
					$('#tagFriendsSearchResults fieldset').html(data);
					$('#tagFriendsSearchResults input[type="checkbox"]').click(submitTagFriend);
					
				 }
		});
 }
 
 function submitTagFriend(e) {
 	
	//$(this).attr('selected', "");
	$('#tagFriendsContainer').hide();
	
	$.ajax({
		type: "POST",
		data: $('#tagFriendsContainer form').serialize(),
		url: "?callback=?",
		timeout: '5000',
		success: function(data){
					
			$('#taggedPeople').html(data).hide().fadeIn();
			$('#taggedPeople a').click(submitRemovePhotoTag);
					
		}
	});	
}

function submitRemovePhotoTag(e) {
	
	$.ajax({
		type: "POST",
		data: {untagID: $(this).attr('id')},
		url: "?callback=?",
		timeout: '5000',
		success: function(data){
					
			$('#taggedPeople').html(data).hide().fadeIn();
			$('#taggedPeople a').click(submitRemovePhotoTag);
					
		}
	});	
	
	return false;
	
}

function submitAddUserAsFriend(e) {
	
	$.ajax({
		type: "POST",
		data: {frid: $(this).attr('id')},
		url: "?action=addfriend&callback=?",
		timeout: '5000',
		success: function(data){
					
			$('span.addAsFriend').html(data);
					
		}
	});	
	
	return false;
	
}
 
 function getTwitterStatus(gebruikersnaam, parseTo){
 
 		$.ajax({
 			type: "GET",
 			dataType: "json",
 			url: "http://api.twitter.com/1/users/show.json?callback=?",
			data: {
				screen_name: gebruikersnaam,
			},
 			timeout: 5000,
			success: function(data) {
				$(parseTo).find('.facebook .status').html(data.status.text);
			}
		});
 }


 
