joms = {
	apps: {
		windowTitle: '',
		toggle: function (id){
			jQuery(id).slideToggle('fast', 
				function(){
					if(jQuery(id).css('display') == 'none')
						jQuery(id).prev().children().addClass('app-box-r-collapse').removeClass('app-box-r-expand');
					else
						jQuery(id).prev().children().addClass('app-box-r-expand').removeClass('app-box-r-collapse');
				}
			);
		},
		showAboutWindow: function(appName){
			var ajaxCall = "jax.call('community', 'apps,ajaxShowAbout', '"+appName+"');";
			cWindowShow(ajaxCall, '' , 450, 200);
		},
		showPrivacyWindow: function(appName){
			var ajaxCall = "jax.call('community', 'apps,ajaxShowPrivacy', '"+appName+"');";
			cWindowShow(ajaxCall, '' , 450, 300);
		},
		showSettingsWindow: function(id, appName){
			var ajaxCall = "jax.call('community', 'apps,ajaxShowSettings', '"+id+"', '"+appName+"');";
			cWindowShow(ajaxCall, '', 450, 300);
		},
		savePrivacy: function(){
			var value   = jQuery('input[name=privacy]:checked').val();
			var appName = jQuery('input[name=appname]').val();
			jax.call('community', 'apps,ajaxSavePrivacy', appName, value);
		},
		saveSettings: function(){
			jax.call('community', 'apps,ajaxSaveSettings', jax.getFormValues('appSetting'));
		},
		remove: function(appName){
			var ajaxCall = "jax.call('community', 'apps,ajaxRemove', '"+appName+"');";
			cWindowShow(ajaxCall, this.windowTitle , 450, 100);	
		},
		add: function(appName){
			var ajaxCall = "jax.call('community', 'apps,ajaxAdd', '"+appName+"');";
			cWindowShow(ajaxCall, '', 450, 100);
		}
	},
	report: {
		emptyMessage: '',
		
		checkReport: function(){
			if( jQuery( '#report-message' ).val() == '' )
			{
				jQuery( '#report-message-error' ).html( this.emptyMessage );
				jQuery( '#report-message-error' ).css( 'color' , 'red' );
				return false;
			}
			return true;
		},
		showWindow: function ( reportFunc, arguments ){
			var ajaxCall	= 'jax.call("community" , "system,ajaxReport" , "' + reportFunc + '","' + location.href + '" ,' + arguments + ');';
			cWindowShow(ajaxCall, '', 450, 300);
		},
		submit: function ( reportFunc , pageLink , arguments ){
			if( joms.report.checkReport() )
			{
				var message	= escape( jQuery('#report-message').val() );
				var ajaxcall='jax.call("community", "system,ajaxSendReport","' + reportFunc + '","' + pageLink + '","' + message + '" , ' + arguments + ')';
				cWindowShow(ajaxcall, '', 450, 300);
			}
		}
	},
	flash: {
		enabled: function(){
			// ie 
			try
			{ 
				try
				{
					// avoid fp6 minor version lookup issues 
					// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
					var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
					try
					{ 
						axo.AllowScriptAccess = 'always'; 
					} 
					catch(e)
					{
						return '6,0,0';
					} 
				}
				catch(e)
				{
				}
				return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
			// other browsers 
			}
			catch(e)
			{ 
				try
				{ 
					if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
					{ 
						return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
					} 
				}
				catch(e)
				{
				} 
			}		
			return false; 
		}
	},
	notifications: {
		showWindow: function (){
			var ajaxCall = 'jax.call("community", "notification,ajaxGetNotification", "")';
			cWindowShow(ajaxCall, 'Notification', 450, 350);
		},
		updateNotifyCount: function (){
			var notifyCount	= jQuery('#toolbar-item-notify-count').text();
			
			if(jQuery.trim(notifyCount) != '' && notifyCount > 0)
			{
				//first we update the count. if the updated count == 0, then we hide the tab.
				notifyCount = notifyCount - 1;
				jQuery('#toolbar-item-notify-count').html(notifyCount);
				if (notifyCount == 0)
				{
					jQuery('#toolbar-item-notify').hide();
				}				
			}			
		}
	},
	filters:{
		bind: function(){
			var loading	= this.loading;
			
			jQuery(document).ready( function() 
			{
				//sorting option binding for members display
				jQuery('.newest-member').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
				        loading( jQuery(this).attr('class') );
						jax.call('community', 'frontpage,getNewestMember', frontpageUsers);
					}
				});
				jQuery('.active-member').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
			            loading( jQuery(this).attr('class') );
						jax.call('community', 'frontpage,getActiveMember', frontpageUsers);
					}
				});
				jQuery('.popular-member').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
				        loading( jQuery(this).attr('class') );
				    	jax.call('community', 'frontpage,getPopularMember', frontpageUsers);
					}
				});
					
				//sorting option binding for activity stream
				jQuery('.all-activity').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
			            loading( jQuery(this).attr('class') );
						jax.call('community', 'frontpage,getActivities', 'all');
					}
				});
				jQuery('.me-and-friends-activity').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
				        loading( jQuery(this).attr('class') );
				    	jax.call('community', 'frontpage,getActivities', 'me-and-friends');
					}
				});
				jQuery('.active-profile-and-friends-activity').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
				        loading( jQuery(this).attr('class') );
				    	jax.call('community', 'frontpage,getActivities', 'active-profile-and-friends');
					}
				});	
				jQuery('.active-profile-activity').bind('click', function() {
				    if ( !jQuery(this).hasClass('active-state') ) {
				        loading( jQuery(this).attr('class') );
				    	jax.call('community', 'frontpage,getActivities', 'active-profile');
					}
				});	
				
				// remove last link border
				jQuery('.popular-member').css('border-right', '0').css('padding-right', '0');
				jQuery('.me-and-friends-activity').css('border-right', '0').css('padding-right', '0');
				jQuery('.active-profile-activity').css('border-right', '0').css('padding-right', '0');
			});
		},
		loading: function(element){
			elParent = jQuery('.'+element).parent().parent().attr('id');
			if ( elParent === '' ) {
		        elParent = jQuery('.'+element).parent().attr('id');
			}
		    jQuery('#' + elParent + ' .loading').show();
		    jQuery('#' + elParent + ' a').removeClass('active-state');
		    jQuery('.'+element).addClass('active-state');
		},
		hideLoading: function(){
			jQuery( '.loading' ).hide();
		}
	},
	groups: {
		addInvite: function( element ){
			var parentId = jQuery('#' +element).parent().attr('id');
	
			if(parentId == "friends-list")
			{
				jQuery("#friends-invited").append(jQuery('#' +element)).html();
			}
			else
			{
				jQuery("#friends-list").append(jQuery('#' +element)).html();
			}
		},
		removeTopic: function( title , groupid , topicid ){
			var ajaxCall	= 'jax.call("community","groups,ajaxShowRemoveDiscussion", "' + groupid + '","' + topicid + '");';
			cWindowShow( ajaxCall , title , 350 , 150 );
		},
		removeBulletin: function( title , groupid , bulletinid ){
			var ajaxCall	= 'jax.call("community","groups,ajaxShowRemoveBulletin", "' + groupid + '","' + bulletinid + '");';
			cWindowShow( ajaxCall , title , 350 , 150 );
		},
		unpublish: function( groupId ){
			jax.call( 'community' , 'groups,ajaxUnpublishGroup' , groupId );
		},
		leave: function( groupid ){
			var ajaxCall	= 'jax.call("community" , "groups,ajaxShowLeaveGroup" , "' + groupid + '");';
			cWindowShow( ajaxCall , '' , 450 , 170 );
		},
		joinWindow: function( groupid ){
			var ajaxCall	= 'jax.call("community" , "groups,ajaxShowJoinGroup" , "' + groupid + '", location.href );';
			cWindowShow( ajaxCall , '' , 450 , 170 );
		},
		edit: function(){
			// Check if input is already displayed
			jQuery('#community-group-info .cdata').each(function(){
				// Test if the next div is cinput
	
				if(jQuery(this).next().html() && jQuery(this).css('display') != 'none' )
					jQuery(this).css('display' , 'none');
				else
					jQuery(this).css('display' , 'block');
			});
	
			jQuery('#community-group-info .cinput').each(function(){
				if(jQuery(this).css('display') == 'none')
					jQuery(this).css('display' , 'block');
				else
					jQuery(this).css('display' , 'none');
			});
	
			if(jQuery('div#community-group-info-actions').css('display') != 'none')
				jQuery('div#community-group-info-actions').css('display' , 'none');
			else
				jQuery('div#community-group-info-actions').css('display' , 'block');
		},
		save: function( groupid ){
			var name		= jQuery('#community-group-name').val();
			var description	= jQuery('#community-group-description').val();
			var website		= jQuery('#community-group-website').val();
			var category	= jQuery('#community-group-category').val();
			var approvals	= jQuery("input[@name='group-approvals']:checked").val();
			
			jax.call('community' , 'groups,ajaxSaveGroup' , groupid , name , description , website , category , approvals);
		},
		update: function( groupName , groupDescription , groupWebsite , groupCategory){
			// Re-update group data
			jQuery('#community-group-data-name').html( groupName );
			jQuery('#community-group-data-description').html( groupDescription );
			jQuery('#community-group-data-website').html( groupWebsite );
			jQuery('#community-group-data-category').html( groupCategory );
			this.edit();
		},
		removeMember: function( memberId , groupId ){
			jax.call('community' , 'groups,ajaxRemoveMember' , memberId , groupId );
		}
	},
	friends: {
		saveTag: function(){
			var formVars = jax.getFormValues('tagsForm');
			jax.call("community", "friends,ajaxFriendTagSave", formVars);
			return false;
		},
		saveGroup: function(userid) {
			if(document.getElementById('newtag').value == ''){
			    window.alert('TPL_DB_INVALIDTAG');
			}else{
				jax.call("community", "friends,ajaxAddGroup",userid,jQuery('#newtag').val());
			}
		},
		cancelRequest: function( friendsId ){
			var ajaxCall	= 'jax.call("community" , "friends,ajaxCancelRequest" , "' + friendsId + '");';
			cWindowShow(ajaxCall, 'Cancel Friend Request', 450, 100);	
		},
		connect: function( friendid ){
			var ajaxCall = 'jax.call("community", "friends,ajaxConnect", '+friendid+')';
			cWindowShow(ajaxCall, 'Add new friend', 450, 200);
		},
		addNow: function(){
		 	var formVars = jax.getFormValues('addfriend');
		 	jax.call("community", "friends,ajaxSaveFriend",formVars);
		 	return false;
		}
	},
	messaging: {
		loadComposeWindow: function(userid){
			var ajaxCall = 'jax.call("community", "inbox,ajaxCompose", '+userid+')';
			cWindowShow(ajaxCall, '', 450, 300);
		},
		send: function(){
			var formVars = jax.getFormValues('writeMessageForm');
			jax.call("community", "inbox,ajaxSend", formVars);
			return false;
		}
	},
	walls: {
		add: function ( uniqueId, addFunc ){

			jax.loadingFunction = function()
			{
				jQuery('#wall-message').attr('disabled', true);
				jQuery('#wall-submit').attr('disabled', true);
			}
		
			jax.doneLoadingFunction = function()
			{
				jQuery('#wall-message').attr('disabled', false);
				jQuery('#wall-submit').attr('disabled', false);
			};
			
			if(typeof getCacheId == 'function')
			{
				cache_id = getCacheId();
			}
			else
			{
				cache_id = "";
			}		
			
			jax.call('community', addFunc, jQuery('#wall-message').val(), uniqueId, cache_id);
		},	
		insert: function( html ){
			jQuery('#wall-message').val('');
			jQuery('#wallContent').prepend(html);	
		},
		remove: function( type , wallId , contentId ){
			if(confirm('Are you sure you want to delete this wall?'))
			{
				jax.call('community' , type + ',ajaxRemoveWall' , wallId , contentId );
				jQuery('#wall_' + wallId ).fadeOut('normal', function(){jQuery(this).remove()});
				
				// Process ajax calls
			}
		}
	},
	toolbar: {
		timeout: 500,
		closetimer: 0,
		ddmenuitem: 0,
		open: function( id ){
			// cancel close timer
			joms.toolbar.cancelclosetime();
		
			// close old layer
			if(joms.toolbar.ddmenuitem)
			{
				joms.toolbar.ddmenuitem.style.visibility = 'hidden';
			}
		
			// get new layer and show it
			joms.toolbar.ddmenuitem = document.getElementById(id);
			joms.toolbar.ddmenuitem.style.visibility = 'visible';			
		},
		close: function(){
			if(joms.toolbar.ddmenuitem)
			{
				joms.toolbar.ddmenuitem.style.visibility = 'hidden';
			}
		},
		closetime: function(){
			joms.toolbar.closetimer	= window.setTimeout( joms.toolbar.close , joms.toolbar.timeout );
		},
		cancelclosetime: function(){
			if( joms.toolbar.closetimer )
			{
				window.clearTimeout( joms.toolbar.closetimer );
				joms.toolbar.closetimer = null;
			}
		}
	},
	registrations:{
		windowTitle: '',
		showTermsWindow: function(){
			var ajaxCall = 'jax.call("community", "register,ajaxShowTnc", "")';
			cWindowShow(ajaxCall, this.windowTitle , 600, 350);
		}
	}
}

// close layer when click-out
jQuery(document).click( function() {
    joms.toolbar.close();
});