var tab2req = new Array();
tab2req['profile'] = 'Profile';
tab2req['ilike'] = 'iLike';
tab2req['ishare'] = 'iShare';
tab2req['photos'] = 'Photos';
tab2req['friends'] = 'Friends';
tab2req['rivals'] = 'Rivals';
tab2req['wall'] = 'Comment Wall';
tab2req['notifications'] = 'Notifications';
tab2req['dreamfooty'] = 'DreamFooty';

function removeActivityItem(element_id) {
	$(element_id).setStyles({
		'overflow': 'hidden',
		'minHeight': 0
	});

	var eff = new Fx.Morph(element_id, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
	eff.start({'opacity': [1, 0], 'height': [$(element_id).getSize().y, 0]}).chain(function() {
		$(element_id).destroy();
	});
	
	var req = new Request({
		url: '/ajRemoveActivityItem.php?activityId=' + element_id,
		method: 'get',
		async: true
	}).send();
}

var profile_animStatus = false;
var profile_switchStatus = false;

function profile_slideOutIn(new_element_id) {
	if(profile_animStatus) {
		profile_slideOutIn.delay(250, false, new_element_id);
	} else {
		profile_animStatus = true;
		
		var pslider = new Fx.Slide($(profile_defaultTab), {mode: 'horizontal', duration: 250});
	
		$(profile_defaultTab + '_floater').setStyle('float', 'right');
		pslider.slideOut().chain(function() {
			var profile_data = new_element_id == profile_defaultTab ?
								$(profile_defaultTab).retrieve('default_save') :
								$(new_element_id).innerHTML;
								
			$(profile_defaultTab).set('html', profile_data);
	
			$(profile_defaultTab + '_floater').setStyle('float', 'left');
			pslider.slideIn().chain(function() {
				profile_currentTab = new_element_id;
				profile_animStatus = false;
				profile_switchStatus = false;
			});
		});
	}
}

function profile_switchTab(new_tab_id) {
	if(new_tab_id == profile_currentTab) {
		return;
	}

	if(profile_switchStatus) {
		profile_switchTab.delay(100, false, new_tab_id);
	} else {
		profile_switchStatus = true;

		if(profile_currentTab == profile_defaultTab) {
			if(!$(profile_currentTab).retrieve('default_save')) {
				$(profile_currentTab).store('default_save', $(profile_currentTab).innerHTML);
			}
		}
	
		if($(profile_currentTab + '_btn')) {
			$(profile_currentTab + '_btn').toggleClass('profiles_tabl_on');
			$(profile_currentTab + '_btn').toggleClass('profiles_tabl_off');
		}

		$(new_tab_id + '_btn').toggleClass('profiles_tabl_on');
		$(new_tab_id + '_btn').toggleClass('profiles_tabl_off');
		
		/*
		 * Save current tab innerHTML and reset it after modifying the url hash,
		 * otherwise the content will be set to the original page content and any
		 * DOM changes will be ignored.
		 */
		
		profile_currentHTML = $(profile_currentTab).innerHTML;
		
		tab_id = new_tab_id.replace(/profile_tab_/, '');
		
		if(tab_id == 'notifications') {
			profile_up_notiftime();
		}
		
		if(tab_id != 'profile') {
			newsearch = '?e=' + tab2req[tab_id];
		
			if(document.location.search != newsearch) {
				document.location.hash = document.location.pathname + newsearch;
			} else {
				document.location.hash = '#';
			}
		} else {
			document.location.hash = document.location.pathname;
		}
		
		//document.location.hash = new_tab_id.replace(/profile_tab_/, '');
		
		$(profile_currentTab).set('html', profile_currentHTML);
	
		profile_slideOutIn(new_tab_id);
	}
}

var profile_currentPhotoOffset = 0
function profile_showNextPhoto() {
	if(profile_currentPhotoOffset + 1 < profile_albumPhotos.length) {
		profile_currentPhotoOffset += 1;
	} else {
		profile_currentPhotoOffset = 0;
	}
		
	profile_showPhoto(profile_currentPhotoOffset);
}

function profile_showPrevPhoto() {
	if(profile_currentPhotoOffset > 0) {
		profile_currentPhotoOffset -= 1;
	} else {
		profile_currentPhotoOffset = profile_albumPhotos.length - 1;
	}
		
	profile_showPhoto(profile_currentPhotoOffset);
}

function profile_reloadThumbs() {
	albumLength = profile_albumPhotos.length;

	if(albumLength <= 5) {
		showThumbs = profile_albumPhotos;
	} else {
		// show current photo in the middle
		if(profile_currentPhotoOffset >= 2) {
			sliceStart = profile_currentPhotoOffset - 2;
		} else {
			sliceStart = 0;
		}
			
		while(sliceStart != 0 && sliceStart + 5 >= albumLength) {
			sliceStart -= 1;
		}
		
		sliceStop = (sliceStart + 5 >= albumLength) ? albumLength - 1 : sliceStart + 5;
		
		showThumbs = profile_albumPhotos.slice(sliceStart, sliceStop);
	}
		
	$('current_album_thumbs').empty();
	showThumbs.each(function(thumbFn) {
		newContainer = new Element('div', {'class': 'current_album_thumb'});
		
		var thumbIndex = profile_albumPhotos.indexOf(thumbFn);
		newThumb = new Element('img', {
			//'src': '/static/user-serve/album-images/sml/' + thumbFn + '.jpg',
			'src': profile_imageLocation(thumbFn, 'sml'),
			'border': 0,
			'events': {
				'click': function() { profile_showPhoto(thumbIndex); }
			}
		});
		
		if(thumbFn == profile_albumPhotos[profile_currentPhotoOffset]) {
			newContainer.setStyle('border', '1px solid #3399ff');
		}
		
		newContainer.grab(newThumb);
		$('current_album_thumbs').grab(newContainer);
	});
}

function profile_showPhoto(photoOffset) {
	profile_currentPhotoOffset = photoOffset;

	//var photoPath = '/static/user-serve/album-images/lrg/' + profile_albumPhotos[photoOffset] + '.jpg';
	var photoPath = profile_imageLocation(profile_albumPhotos[photoOffset], 'lrg');
	var photo = new Image;
	photo.onload = function() {
		$('current_photo').setProperty('src', photoPath);
		$('current_photo_desc').set('html', profile_photoDesc[photoOffset]);
	};
	photo.src = photoPath;
	
	profile_reloadThumbs();
}

function loadFavourites(user_id, page_num) {
	var favUrl = '/ajGetFavourites.php?user_id=' + user_id + '&page=' + page_num;
	
	var htmlUp = new Request({url: favUrl, method: 'get', onSuccess: function(resp) {
		$('profile_favourites_container').set('html', resp);
	}}).send();
}

function profile_imageLocation(imgstr, size) {
	if(imgstr.substr(0, 4) == 'http') { // assume facebook
		if(size == 'sml') {
			imgstr = imgstr.replace(/\/n/, '/t') + '.jpg';
		} else {
			imgstr = imgstr + '.jpg';
		}
	} else { // assume footytube
		imgstr = '/static/user-serve/album-images/' + size + '/' + imgstr + '.jpg';
	}
	
	return imgstr;
}

function profile_friendreq_handler(options) {
	if(options.status) {
		$(options.params.element_id).destroy();
		
		statusEl = $(options.params.element_id + '_s');
		
		sname = statusEl.getProperty('rel');
		sname_anchor = '<a href="/profile/' + sname + '" class="standard_link">' + sname + '</a>';
		
		if(options.params.choice == 'accept') {
			statusStr = 'You are now friends with ' + sname_anchor;
		} else if(options.params.choice == 'deny') {
			statusStr = 'You have denied ' + sname_anchor + '\'s friend request.';
		} else if(options.params.choice == 'ignore') {
			statusStr = 'You have ignored ' + sname_anchor + '\'s friend request.';
		}
		
		statusEl.set('html', statusStr);
	}
}

var profile_notiftime_updated = false;
function profile_up_notiftime() {
	if(!profile_notiftime_updated) {
		var up = new Request({url: '/ajNotifView.php', method: 'get'}).send();
		profile_notiftime_updated = true;
	}
}

var profile_redirectHash = true;
if(profile_redirectHash	&& self.document.location.hash) {
		document.location.href = 
			document.location.protocol + '//' +
			document.location.hostname +
			document.location.hash.substring(1);
}