// JavaScript Documentvar friendBox;

function addToFriend(id) {
  if (friendBox == undefined) {
    friendBox = new MessageBox({title: '���������� � ������', width: 400});
 	}
  friendBox.removeButtons();
  friendBox.addButton({label: '������', style: 'button_no', onClick: function(){friendBox.hide();}});
  friendBox.addButton({label: '�������� � ������', onClick: function(){
    var form = ge('addFriendForm');
    if (!form) return;
    var params = serializeForm(form);
    Ajax.postWithCaptcha('friend.php', params, {onSuccess: function(ajax, responseText){
      friendBox.content(responseText).removeButtons();
      friendBox.addButton({label: '�������', onClick: function(){friendBox.hide();}});
      friendBox.show();
      setTimeout("friendBox.hide(600)", 1200);
    }});
  }});
  friendBox.content('<div class="box_loader"></div>').show();
  Ajax.Post({
    url: 'friend.php',
    query: {act: 'a_add_form', fid: id},
    onDone: function(ajaxObj, responseText){
      var text = responseText;
      try {
        var response = eval('(' + responseText + ')');
        if (response.error) {
          friendBox.removeButtons().addButton({label: '�������', style: 'button_no', onClick: function(){friendBox.hide();}});
        }
        friendBox.content(response.text).show(); 
        if (response.script) {
          var script = response.script.replace(/^[\s\\n]+/g, '');
          eval(script);
        }     
      } catch (e) {}
    }
  });
}


// EX ACTIVITY.JS


activity_editor = {
  
  active:             false,  // Is Editor Visible
  menu_active:        false,  // Is Suggestion Menu Visible
  activity_text:        '',
  
  activity_blur:        false,
  activity_reblur:      false,

  //
  // Functions to transition between modes
  //
  
  setup: function(initial_activity) {
    this.activity_text = initial_activity;
  },
  
  reset: function() {
    ge('edit_activity_text').value='';
    return false;
  },                 
  
  show: function() {
    if (!this.active) {
	  historyAjaxHide();
      hide('profile_activity');
      hide('profile_empty_activity');
      this.hide_menu();
      show('activity_editor');
      if (ge('activity_text').firstChild) {
        this.activity_text = ge('activity_text').firstChild.nodeValue;
      }
      ge('edit_activity_text').value = this.activity_text;
      ge('edit_activity_text').focus();
      ge('edit_activity_text').select();
      this.activity_blur = true;
	  setTimeout(function() { activity_editor.active = true; }, 200);
      // this.active = true;
    } else {
      ge('edit_activity_text').focus();
    }
    return false;
  },
  
  hide: function() {
    if (!this.active) {
      return;
    }
	setTimeout(function() { activity_editor.active = false; }, 200);
    // this.active = false;
    this.activity_blur = false;
    hide('activity_editor');
    show((this.activity_text != '') ? 'profile_activity' : 'profile_empty_activity');
	//if (this.activity_text != '') {
	//	show('profile_activity');
	//}
    return false;
  },
  
  toggle_menu: function(event) {
   if (browser.safari || browser.msie || browser.opera) {
     this.activity_reblur = true;
    }
    if (this.menu_active) {
      return this.hide_menu();
    } else {
      return this.show_menu();
    }
  },

  show_menu: function(event) {
    this.menu_active = true;
    ge('edit_activity_toggle').style.backgroundImage='url('+base_domain+'images/activity_on.gif)';
    ge('edit_activity_toggle').style.backgroundColor='#fff';   
    ge('edit_activity_select').style.display='block';
    return false;
  },
  
  hide_menu: function() {
    this.menu_active = false;
    ge('edit_activity_toggle').style.backgroundImage='url('+base_domain+'images/activity_off.gif)';
    ge('edit_activity_toggle').style.backgroundColor='#D8DFEA';
    hide('edit_activity_select');
    return false;
  },
  
  activity_select: function(elem) {
    if (browser.safari || browser.msie || browser.opera) {
      this.activity_reblur = true;
    }
    this.hide_menu();
    ge('edit_activity_text').value = elem.innerHTML;
    ge('edit_activity_text').focus();
    ge('edit_activity_text').select();
	return false;
  },
  
  menu_over: function(elem) {
		elem.style.backgroundColor = '#45688E';
		elem.style.color = '#fff';  	
  },
  
  menu_out: function(elem) {
		elem.style.backgroundColor = 'white';
		elem.style.color = '#000';  	
  },

  blur: function(real_blur) {
  	if (browser.opera && !real_blur) {
		setTimeout(function() { activity_editor.blur(1); }, 100);
		return false;	
	}
    if (this.activity_reblur) {
      ge('edit_activity_text').focus();
      this.activity_reblur = false;
      return false;
    }
    if (this.activity_blur) {
      if (ge('edit_activity_text').value != this.activity_text) {
        this.submit_activity_set(ge('edit_activity_text').value);
      } else {
        this.hide();
      }
    }
    return true;
  },

  //
  // Ajax
  //

  activity_set: function(activity) {
    this.activity_text = activity || '';
    this.reset();
    this.hide();
    if (activity != null && activity != '') {		
      ge('activity_text').innerHTML = this.activity_text;
      ge('activity_time_link').innerHTML = activity_update_just_now;
    } else {
      ge('activity_text').innerHTML = activity_cant_update;
    }
  },
    
  submit_activity_set: function(activity) {
    if (activity == null || activity == '') return;
    var selectInput = function() { ge('edit_activity_text').focus(); };
    var unsetBg = function() { ge('edit_activity_text').style.backgroundImage=''; };
    var onDone = function(ajaxObj, response) { unsetBg(); this.activity_set(response); }.bind(this);
    var onFail = function(ajaxObj) { unsetBg(); this.activity_set(null); }.bind(this);
    var options = {onSuccess: onDone, onFail: onFail, onCaptchaShow: unsetBg, onCaptchaHide: selectInput};
    ge('edit_activity_text').style.backgroundImage='url('+base_domain+'images/progress_candy_hor.gif)';
    Ajax.postWithCaptcha('/profile.php', {'setactivity':activity, 'activityhash': ge('activityhash').value}, options);
    return false;
  },
  
  activity_cleared: function() {
    this.activity_text = '';
    ge('activity_text').innerHTML = '';
    this.hide();
    ge('activity_blank_nag').innerHTML=activity_deleted;
    setTimeout(function(){
		// hide('activity_blank_nag');
      	ge('activity_blank_nag').innerHTML = activity_change_status;
    }.bind(this), 2500);
  },

  submit_activity_clear: function() {
    this.activity_blur = false;
    ajax = new Ajax();
    ajax.onDone = function(ajaxObj, response) { ge('edit_activity_text').style.backgroundImage=''; this.activity_cleared(); }.bind(this);
    ajax.onFail = function(ajaxObj) { ge('edit_activity_text').style.backgroundImage=''; this.activity_cleared(); }.bind(this); // fingers crossed
	ge('edit_activity_text').style.backgroundImage='url('+base_domain+'images/progress_candy_hor.gif)'; 
    ajax.post('/profile.php', {'clearactivity':'1', 'activityhash': ge('activityhash').value});
    return false;
  },
  
  //
  // User Interaction
  //
  
  handle_key_press: function(event) {
    event = event || window.event;
    key_code = event.keyCode || event.which;
    
    if (key_code == this.KEYS.RETURN) {
      if (ge('edit_activity_text').value != '')
        this.submit_activity_set(ge('edit_activity_text').value);
      return false;
    }
    if (key_code == this.KEYS.ESC) {
      this.hide();
      return false;
    }
    return true;
  },
  
  KEYS: {BACKSPACE:8,TAB:9,RETURN:13,ESC:27,LEFT:37,UP:38,RIGHT:39,DOWN:40,DELETE:46}
  
}

historyShown = false;

function getActivityHistory(user_id) {
    ajax = new Ajax();
    ajax.onDone = onGotHistory;
    ajax.onFail = historyAjaxHide;
	historyAjaxProgress();
    ajax.post('/profile.php', {'activityhistory':'1', 'id': user_id});	
}

function onGotHistory(ajaxObj, response) { 
	historyAjaxShow();
	ge('historyContainer').innerHTML = response; 
}

function deleteHistoryItem(item_id) {
    ajax = new Ajax();
    ajax.onDone = onGotHistory;
    ajax.onFail = historyAjaxHide;
	historyAjaxProgress(1);
    ajax.post('/profile.php', {'deletehistoryitem':'1', 'iid': item_id, 'activityhash': ge('activityhash').value});	
}

function historyAjaxProgress(onlyshowprogress) {
	if (!onlyshowprogress) {
		hide('historyContainer');
		hide('historyHeader');
	}
	show('historyProgress');
	show('history');
//	ge('historyHeader').style.backgroundImage='url('+base_domain+'images/progress_candy_hor.gif)'; 
}

function historyAjaxShow() {
//	ge('historyHeader').style.backgroundImage='';
	historyShown = true;
	show('history');
	hide('historyProgress');
	show('historyHeader');
	show('historyContainer');
}

function historyAjaxHide() {
//	ge('historyHeader').style.backgroundImage=''; 
	historyShown = false;
	hide('history');
	hide('historyProgress');
	hide('historyHeader');
	hide('historyContainer');
}


// EX EFFECTS.JS

// Profile tabs 
var tab_masks = {
'friendsCommon': 1,
'friends':       2,
'friendsOnline': 4,
'albums':        8,
'videos':        16,
'questions':     32,
'matches':       64,
'notes':         128,
'groups':        256,
'apps':          512,
'personal':      1024,
'education':     2048,
'career':        4096,
'places':        8192,
'military':      16384,
'opinions':      32768,
'audios':        65536,
'wall':          131072,
'gifts':         262144,
'optional':      524288
}


// Group tabs
var group_tab_masks = {
'information': 1,
'members':     2,
'groupType':   4,
'admins':      8,
'groupEvents': 16,
'officers':    32,
'links':       64,
'applied':     128,
'photos':      256,
'topics':      512,
'wall':        1024,
'albums':      2048,
'videos':      4096,
'audios':      8192,
'recentNews':  16384,
'voting':      32768,
'apps':        65536,
'description': 131072
}
//...

function collapseBox(id, container, dopen, dclose, group){
  var box = ge(id);
  if (!box) return;
  
  var masks = (group) ? group_tab_masks : tab_masks;
  var cookie_key = (group) ? 'group_closed_tabs' : 'closed_tabs';
      
  var c = geByClass("c", box)[0];
  if (!c) return;
  var newClass = isVisible(c) ? "bShut" : "bOpen";
  if (slideToggle(c, 300, function() {
    if (!masks[id]) return;
    var closed_tabs = parseInt(getCookie('remix' + cookie_key));
    if (isVisible(c)) {
      closed_tabs = isNaN(closed_tabs) ? 0 : closed_tabs & ~masks[id];
    } else {
      closed_tabs = isNaN(closed_tabs) ? masks[id] : closed_tabs | masks[id];
    }
    setCookie('remix' + cookie_key, closed_tabs, 360);
  })) {
    container.parentNode.className = newClass;
  }
  
  return false;
}


function quickReply(id, dopen, dclose, foca){
  var box = ge(id);
  if(!box) return;
  var c = geByClass("r", box)[0];
  if (!c) return;
  if (!isVisible(c)) 
    slideDown(c, 200, function(){ ge("reply_field").focus(); });
  else 
    slideUp(c, 200);
}

// EX FAVE.JS

function addFave() {
 ajax = new Ajax();
 ge('faveProgress').innerHTML = "<IMG SRC='images/upload.gif'>";
 ajax.onDone = function(ajaxObj, response) {
  ge('addToFaves').innerHTML = response;
 }
 ajax.onFail = function(ajaxObj) {
 }
 ajax.post('/fave.php', {'act':'addPerson', 'mid': ge('mid').value});
}

function deleteFave() {
 ajax = new Ajax();
 ge('faveProgress').innerHTML = "<IMG SRC='images/upload.gif'>";
 ajax.onDone = function(ajaxObj, response) {
  ge('addToFaves').innerHTML = response;
 }
 ajax.onFail = function(ajaxObj) {
 }
 ajax.post('/fave.php', {'act':'deletePerson', 'mid': ge('mid').value});
}

function addFaveGroup() {
 ajax = new Ajax();
 ge('faveProgress').innerHTML = "<IMG SRC='images/upload.gif'>";
 ajax.onDone = function(ajaxObj, response) {
  ge('addToFaves').innerHTML = response;
 }
 ajax.onFail = function(ajaxObj) {
 }
 ajax.post('/fave.php', {'act':'addGroup', 'gid': ge('mid').value});
}

function deleteFaveGroup() {
 ajax = new Ajax();
 ge('faveProgress').innerHTML = "<IMG SRC='images/upload.gif'>";
 ajax.onDone = function(ajaxObj, response) {
  ge('addToFaves').innerHTML = response;
 }
 ajax.onFail = function(ajaxObj) {
 }
 ajax.post('/fave.php', {'act':'deleteGroup', 'gid': ge('mid').value});
}