var currentInterval = 0;

function addProfilePhoto( profile, photo, src, title, fullimg, width, height )
{
	fb.end();
	
	sajax_do_call( "ajaxJAddPhotoToProfile", [ profile, photo, src, title, fullimg, $('#profile_type').val(), width, height ], addProfilePhotoComplete );
	setTimeout( 'startProgress()', 500 );
}

function addProfilePhotoComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
	{
		var tokens = resp.responseText.split( '|' );
		addProfilePictureThumb( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5] );
	}
	else
		endProgress();
}

function addProfilePictureThumb( src, title, fullimg, width, height, rev )
{
	var a = $('#a_profile_picture_id');
	a.attr( 'href', fullimg );
	a.attr( 'rev', rev );
	
	var img = $('#img_profile_picture_id');
	img.attr( 'src', src );
	img.css( 'width', width + 'px' );
	img.css( 'height', height + 'px' );

	endProgress();
	fb.activateElements();
}

function attachLinkMenu( el, type )
{
	var menu = new PopupMenu( PopupMenu.MODE_BUTTON );

	menu.add( place_translations['frmsRemove' + type ], function( target ) { 
		if( confirm( place_translations['frmsRemove' + type + 'Confirm'] ) )
		{
			target.id = Math.floor(Math.random()*10000);
		
			sajax_do_call( "ajaxJRemoveLinkFromProfile", [ $('#profile_page').val(), target.id, target.rel, target.rev ], removeLinkComplete );
			startProgress();
		}
	} );

	menu.bind( el );
}

function removeLinkComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
		$("#" + resp.responseText.trim()).parent().parent().remove();
	
	endProgress();
}

function removeTagComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
		$("#" + resp.responseText.trim()).parent().remove();
	
	endProgress();
}

function addLinkForm( el )
{
	$('#tx_addlink_title').val( "" );
	$('#tx_addlink_url').val( "" );
	$('#add_link_panel').attr( 'class', '' );
	
	return false;
}

function addVideoForm( el )
{
	$('#tx_addvideo_url').val( "" );
	$('#add_video_panel').attr( 'class', '' );
	
	return false;
}

function addTagForm( el )
{
	$('#select_tag').val( "" );
	$('#add_tag_panel').attr( 'class', '' );
	
	return false;
}

function addLinkComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
	{
		try {
			var json = $.parseJSON(resp.responseText);

			var list;
			if( json.type == 'video' )
				list = $('#list_videos');
			else
				list = $('#list_links');
				
			var li = document.createElement( 'li' );
			$(li).html(json.item);
			
			var a = document.createElement( 'a' );
			a.href = "#";
			a.rel = json.rel;
			a.rev = json.rev;
			a.className = 'link_edit';
			
			var span = document.createElement( 'span' );
			span.appendChild( a );
			
			li.appendChild( span );
			attachLinkMenu( a, ( json.type == 'video' ) ? 'Video' : 'Link' );
			
			list.append( li );
			
			if( json.type != 'video' )
				faviconizeDivClass();
		}
		catch (e) {}		
	}
	
	endProgress();
}

function addTagComplete( resp )
{
	if( resp.status == 200 && resp.responseText != '-' )
	{
		try {
			var json = $.parseJSON(resp.responseText);

			var li = document.createElement( 'li' );
			
			var a = document.createElement( 'a' );
			a.href = json.link;
			a.appendChild( document.createTextNode( json.title ) );
			li.appendChild( a );
			
			a = document.createElement( 'a' );
			a.className = 'remove remove_tag';
			a.href = '#';
			a.rev = json.tag;
			a.id = 'rtag_' + (Math.floor ( Math.random ( ) * 10000 + 1 ));
			a.onclick = function() { return removeTag( this ); };
			li.appendChild( a );
			
			var list = $('#list_tags');
			list.append( li );
		}
		catch (e) {}		
	}
	
	endProgress();
}

function removeTag( el )
{
	if( confirm( place_translations['frmsRemoveTagConfirm'] ) ) 
	{
		sajax_do_call( "ajaxJRemoveTagFromProfile", [ $('#profile_page').val(), el.id, el.rev ], removeTagComplete );
		setTimeout( 'startProgress()', 100 );
	}		
	return false;
}

$("#add_tag_panel").ready( function() {
	var btnAddTagOk = $("#btn_addtag_ok").button();
	var btnAddTagCancel = $("#btn_addtag_cancel").button();
	
	btnAddTagOk.click( function() {
		if( $('#select_tag').val() )
		{
			$('#add_tag_panel').attr( 'class', 'hidden' );
			sajax_do_call( "ajaxJAddTagToProfile", [ $('#profile_page').val(), $('#select_tag').val() ], addTagComplete );
			startProgress();
		}
	});

	btnAddTagCancel.click( function() {
		$('#add_tag_panel').attr( 'class', 'hidden' );
	});
});
	
$("#add_link_panel").ready( function() {
	var btnAddLinkOk = $("#btn_addlink_ok").button();
	var btnAddLinkCancel = $("#btn_addlink_cancel").button();
	
	btnAddLinkOk.click( function() {
		if( $('#tx_addlink_url').val() )
		{
			$('#add_link_panel').attr( 'class', 'hidden' );
			sajax_do_call( "ajaxJAddLinkToProfile", [ $('#profile_page').val(), $('#tx_addlink_url').val(), $('#tx_addlink_title').val() ], addLinkComplete );
			startProgress();
		}
	});

	btnAddLinkCancel.click( function() {
		$('#add_link_panel').attr( 'class', 'hidden' );
	});
});
	
$("#add_video_panel").ready( function() {
	var btnAddVideoOk = $("#btn_addvideo_ok").button();
	var btnAddVideoCancel = $("#btn_addvideo_cancel").button();
	
	btnAddVideoOk.click( function() {
		if( $('#tx_addvideo_url').val() )
		{
			$('#add_video_panel').addClass('hidden');
			sajax_do_call( "ajaxJAddLinkToProfile", [ $('#profile_page').val(), $('#tx_addvideo_url').val(), "" ], addLinkComplete );
			startProgress();
		}
	});

	btnAddVideoCancel.click( function() {
		$('#add_video_panel').addClass( 'hidden' );
	});
	
});

$( function() {
	$('a.link_edit', $('#div_links')).each( function() { attachLinkMenu( this, 'Link' ); } );

	$('a.link_edit', $('#div_videos')).each( function() { attachLinkMenu( this, 'Video' ); } );

	$('a.remove_tag', $('#div_tags')).each( function() { 
		this.id = 'rtag_' + (Math.floor ( Math.random ( ) * 10000 + 1 ));
		this.onclick = function() { return removeTag( this ); };
	} );
});
