//----------------
// profile
//
// profile page in feed like goodness
// jeremiah robison
// 9.11.2008
//----------------

if( typeof( Profile ) == 'undefined' ){
  var Profile = {};
}


Profile.channelTemplate = new api.Channel.Template();

Profile.tab = '';
Profile.tab_time = null;
Profile.valid_tab = function(tab) {
	for (var i=0; i<Profile.tabs.length; i++) {
	    if (tab == Profile.tabs[i]) {
	        return true;
		}
	}
	return false;
};
Profile.read_fragment = function() {
	var fragment = window.location.hash.split('-');
	var new_tab = fragment[1];
	if (!Profile.tabs || !Profile.valid_tab(new_tab)) {
	    new_tab = 'new';
	}
	if (new_tab != Profile.tab) {
	    Profile.tab = new_tab;
		return true;
	}
	return false;
};
Profile.tab_set = function(tab) {
	window.location.hash = '-'+tab;
	Profile.reload_channels();
	return false;
};

Profile.tab_check = function() {
	if (Profile.tab_timer) {
	    clearTimeout(Profile.tab_timer);
	}
	if (Profile.read_fragment()) {
	    Profile.reload_channels();
	}
	Profile.tab_timer = setTimeout(Profile.tab_check, 1000);
};

Profile.reload_channels = function() {
	getObject('feed').innerHTML = '';
	Profile.load_channels(Profile.puid, 0, Profile.profile, Profile.tabs);
};
Profile.set_tabs = function(tabs) {
	if (tabs) {
	    Profile.tabs = tabs;
	}
};
Profile.render_tabs = function() {
	if (Profile.tabs) {
	    var tabs = Profile.tabs;
	    var tab_nodes = UL({});
	    for (var i=0; i<tabs.length; i++) {
	        tab_nodes.appendChild(LI({'id':'tab_'+tabs[i],
									  'class':(Profile.tab == tabs[i]) ? 'selected' : ''
									  },
									 A({'href':'#',
										'class':'profile_tab',
										     'onclick':'AppBase.ual("click_'+tabs[i]+'");return Profile.tab_set("'+tabs[i]+'");'
										},
									   translate('tab_'+tabs[i])
									  )));
		}
	tab_nodes.appendChild(DIV({'class':'breakline'}));
		$('#main_navigation').html(tab_nodes);
	}
};

Profile.empty_channels = function() {
    var messageDiv = DIV({'class':'no_channels'}, translate('no_channel_'+Profile.tab));
    getObject('feed').appendChild(messageDiv);
};

Profile.edit_mugshot = function(nxuid, fv_nxuid, fv_site, picker_swf, upload_url) {
	Profile.mugshot_nxuid = nxuid;

	var fo = new FlashObject(picker_swf, 'picker_swf', 310, 280, '8', '');
	fo.addParam("quality", "high");
	fo.addParam("wmode", "transparent");
	fo.addParam("allowScriptAccess", "always");
	fo.addVariable("nxuid", fv_nxuid);
	fo.addVariable("site", fv_site);
	fo.addVariable("uploadURL", upload_url);
	fo.write('mugshot_picker');

	$('#mug_edit').show();

	return false;
};

Profile.mugshot_callback = function(image_url, top, left, bottom, right) {
	$('#mug_edit').hide();

	AppBase.post(
	    '/profile',
		{'xaction':'set_mugshot',
		 'url':image_url,
		 'top':    (!top    || top    == 'undefined')?'0':top,
		 'left':   (!left   || left   == 'undefined')?'0':left,
		 'bottom': (!bottom || bottom == 'undefined')?'100':bottom,
		 'right':  (!right  || right  == 'undefined')?'100':right,
		 'nxuid':Profile.mugshot_nxuid
		 },
		function(response) {
	          getObject('mug').src = response['deco_url'];
		});
};
Profile.mugshot_close = function() {
    $('#mug_edit').hide();
};

Profile.mugshot_upload = function(response) {
	getObject('mug').src = response['deco_url'];
	$('#mug_edit').hide();
	AppBase.post(
	    '/profile',
		{'xaction':'set_mugshot',
		 'url':response['img_url'],
		 'nxuid':Profile.mugshot_nxuid}
		);
	return false;
};

Profile.fan_user = function(fan_permission, name) {
	AppBase.post(
	    '/profile',
		{'xaction':'fan_users',
		 'fan_permission':fan_permission,
		 'ref_override': 'profile__view'},
		function(response) {
		  if( response && response['error'] ){
		    alert( translate('blocked_fan_deny'));
		  }else{
		    getObject(fan_permission).innerHTML = '<span class="fan_added">'+translate("fan_added",name)+'</span>';
		  }

	    }
		);
	return false;
};

Profile.load_channels = function(puid, psid, profile, tabs) {
  var inviter_holder = $('#inviter_holder');
  if( inviter_holder ){ inviter_holder.html(elements.inviter('inviter')); }


  if( profile && profile.view ){
    Profile.channelTemplate.set_view( profile.view );
  }
  Profile.channelTemplate.set_puid( puid );
  Profile.puid = puid;
  Profile.psid = psid;
  if (profile) {
    Profile.profile = profile;
  }
  $('#loading').show();

  api.Channel.fetch(puid, psid, 'profile', Profile.on_load_channels );

  return false;
};

Profile.on_load_channels = function(channels, users, li_nxuid, psid ) {

  $('#loading').hide();
  Profile.display_more(Profile.puid, psid);
  for(var i=0; i<channels.length; i++) {
    if( channels[i]['images'].length == 0 ){ continue; }
    var story = Profile.channelTemplate.render_channel( channels[i] );
    getObject("feed").appendChild(story);
  }
  //Profile.tab_check();
};

Profile.display_more = function(puid, psid) {
  if (psid=='') {
    $('#more').hide();
  } else {
    $('#more').show();
    getObject('more').innerHTML='<input type="button" onclick="Profile.load_channels(\''+puid+'\',\''+psid+'\')" value="'+translate('More')+'"/>';
  }
};

	var load_my_channels = function() {
		AppBase.post(
			'/profile',
			{'xaction' : 'get_my_channels'},
			show_my_channels
		);
	};

	var show_my_channels = function(result) {

	  $('#my_channels').html(A({'href':'#',
				    'onclick':function() {$('#my_channels').hide();return false;}
				   },
				   translate('close_menu')
				  ));

	  for (var i=0; i<result.length; i++) {
	    var nxcid = result[i];
	    var node = DIV({},
			 A({'href':'#',
			    'onclick':'submit_group_channel("'+nxcid+'");return false;'
			   },
			   nxcid
			  ));
			$('#my_channels').append(node);
		}
	    $('#my_channels').show();
	};

	var show_picker = function(swf_src, permission) {
	    getObject('my_picker').innerHTML = '';
		var fo = new FlashObject(swf_src,'ss_picker', 550, 425, '8', '');

		$('select').css("visibility", "hidden");
		fo.addParam("quality", "high");
		fo.addParam("wmode", "transparent");
		fo.addParam("allowScriptAccess", "always");
		fo.addVariable("permission", permission);
		fo.write('my_picker');
		$('#my_picker').show();
		return false;
	};

	var hide_picker = function(){
	  $('#my_picker').hide();
	  $('select').css("visibility", "visible");
	  return false;
	};

	var picker_login = function() {
		AppBase.show_login();
		$('#my_picker').hide();
		return false;
	};

