Event.observe(document, 'dom:loaded', initPage);
var identify_counter = 1;
function initPage() {
    if($('Map')) {
        $('Map').select('area').each(function(area){
            title = area.title;
            Event.observe(area, 'mouseover', Map.show.bindAsEventListener(area, title));
            Event.observe(area, 'mouseout', Map.show.bindAsEventListener(area));
        })
    }
    $$('.foto_image','.user_avatar_photo').each(function(photo){
        var anchor = photo.up('div').down('.photo_link');
        photo.onclick = function() {window.location = anchor;};
    })

    $$('.hint').each(function(item){
        bind_hint(item)
    })

}

function bind_hint(item) {
        item.observe('focus', function(e){
            el = e.element();
            hint = $$('.'+el.id).first();
            new Effect.Appear(hint, {duration : 0.2});

        })
        item.observe('blur', function(e){
            el = e.element();
            hint = $$('.'+el.id).first();
            new Effect.Fade(hint, {duration : 0.2});
        })
}


Map = {
    show: function(e, title) {
        $('region_name').update(title);
    },
    
    hide: function(e) {
        $('region_name').update();
    }
}

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var LoginForm = {
  setToPassword: function() {
    $('openid_fields').hide();
    $('password_fields').show();
  },

  setToOpenID: function() {
    $('password_fields').hide();
    $('openid_fields').show();
  }
}

var EditForm = {
  // show the form
  init: function(postId) {
    $('edit-post-' + postId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current post id we're editing
  setReplyId: function(postId) {
    $('edit').setAttribute('post_id', postId.toString());
    $('post_' + postId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },

  // clears the current post id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('post_' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },

  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },

  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}

//Event.addBehavior({
// '#search,#monitor_submit': function() { this.hide(); }
//})

function public_profile(){
  $('current_user_schools_classes_privacy_1').checked = true;
  $('current_user_age_privacy_1').checked = true;
  $('current_user_forum_privacy_1').checked = true;
  $('current_user_photo_comments_privacy_1').checked = true;
  $('current_user_actual_job_privacy_1').checked = true;
  $('current_user_description_privacy_1').checked = true;
  $('current_user_phone_number_privacy_1').checked = true;
  $('current_user_skype_privacy_1').checked = true;
  $('current_user_yahoo_messanger_privacy_1').checked = true;
  $('current_user_city_privacy_1').checked = true;
  $('current_user_friends_privacy_1').checked = true;
  $('current_user_users_photos_privacy_1').checked = true;
  $('current_user_hi5_privacy_1').checked = true;
  $('current_user_facebook_privacy_1').checked = true;
  $('current_user_email_privacy_1').checked = true;
}

function private_profile(){
  $('current_user_schools_classes_privacy_2').checked = true;
  $('current_user_age_privacy_2').checked = true;
  $('current_user_forum_privacy_2').checked = true;
  $('current_user_photo_comments_privacy_2').checked = true;
  $('current_user_actual_job_privacy_2').checked = true;
  $('current_user_description_privacy_2').checked = true;
  $('current_user_phone_number_privacy_2').checked = true;
  $('current_user_skype_privacy_2').checked = true;
  $('current_user_yahoo_messanger_privacy_2').checked = true;
  $('current_user_users_photos_privacy_2').checked = true;
  $('current_user_city_privacy_2').checked = true;
  $('current_user_friends_privacy_2').checked = true;
  $('current_user_users_photos_privacy_2').checked = true;
  $('current_user_hi5_privacy_2').checked = true;
  $('current_user_facebook_privacy_2').checked = true;
  $('current_user_email_privacy_2').checked = true;
}

function closed_profile(){
  $('current_user_schools_classes_privacy_3').checked = true;
  $('current_user_age_privacy_3').checked = true;
  $('current_user_photo_comments_privacy_3').checked = true;
  $('current_user_actual_job_privacy_3').checked = true;
  $('current_user_description_privacy_3').checked = true;
  $('current_user_phone_number_privacy_3').checked = true;
  $('current_user_skype_privacy_3').checked = true;
  $('current_user_yahoo_messanger_privacy_3').checked = true;
  $('current_user_users_photos_privacy_3').checked = true;
  $('current_user_city_privacy_3').checked = true;
  $('current_user_friends_privacy_3').checked = true;
  $('current_user_users_photos_privacy_3').checked = true;
  $('current_user_forum_privacy_2').checked = true;
  $('current_user_hi5_privacy_3').checked = true;
  $('current_user_facebook_privacy_3').checked = true;
  $('current_user_email_privacy_3').checked = true;
}


function identify_own(el) {

    if(el.id) {return el.id}
    else {
        new_id = "anymous_element_" + identify_counter;
        el.id = new_id;
        identify_counter += 1;
        return new_id
    }
}

function still_active(href) {
    new Ajax.Request(href, {method : "get"});
}

function family_surname(effect) {
    var sex = $('user_gender_id').value;
    var year = $('user_birth_date_1i').value;
    var current_year = String(new Date().getFullYear()-15);
    var f_surname = $('user_family_surname');
    var li = f_surname.up('li');
    if(sex == '2' && current_year>year) {
        f_surname.enable();
        effect ? new Effect.Appear(li, {duration:0.2}) : li.show();
    }
    else {
        effect ? new Effect.Fade(li, {duration:0.2, afterFinish:function(){f_surname.disable();}}) : li.hide();
    }

}

function check_all(checkbox, checkboxes) {
    value = checkbox.checked
    checkboxes.each(function(item){
        item.checked = value;
    })
}

function fb_connect_invite_friends(options) {

  if ( typeof options != 'object' ) { options = {} }
  if ( options.title == undefined ) { options.title = "Invite your friends" }
  if ( options.type == undefined ) { options.type = window.location.hostname }
  if ( options.all_friends_invited == undefined ) { options.all_friends_invited = "<div style='padding: 10px; font-size: 1.2em;'>You've already invited all of your friends and they've accepted.</div>" }
  if ( options.invitation_copy == undefined ) { options.invitation_copy = "" }
  if ( options.invitation_choice_url == undefined ) { options.invitation_choice_url = window.location.protocol + '//' + window.location.host + '/' }
  if ( options.invitation_choice_label == undefined ) { options.invitation_choice_label = 'Accept' }
  if ( options.request_action_url == undefined ) { options.request_action_url = window.location.href }
  if ( options.request_action_text == undefined ) { options.request_action_text = options.title }
  if ( options.friend_selector_rows == undefined ) { options.friend_selector_rows = 3 }
  if ( options.friend_selector_email_invite == undefined ) { options.friend_selector_email_invite = 'true' }
  if ( options.friend_selector_bypass == undefined ) { options.friend_selector_bypass = 'cancel' }
  if ( isNaN(options.width) ) { options.width = 600 }
  if ( isNaN(options.height) ) { options.height = 510 }

  var api = FB.Facebook.apiClient
  var sequencer = new FB.BatchSequencer()
  var friends = api.friends_get(null, sequencer)
  var friends_app_users = api.friends_getAppUsers(sequencer)

  sequencer.execute(function() {

    var friend_ids = ''
    try {
      friend_ids = friends.result.sort().join(',')
    }catch(e) {;}

    var exclude_ids = ''
    try {
      exclude_ids = friends_app_users.result.sort().join(',')
    }catch(e) {;}

    var dialog = new FB.UI.FBMLPopupDialog(options.title, '')

    if ( friend_ids.length > 0 && exclude_ids.length > 0 && friend_ids == exclude_ids ) {
      var fbml = ''
      fbml += '<fb:fbml>'
      fbml += options.all_friends_invited
      fbml += '</fb:fbml>'

      dialog.setFBMLContent(fbml)
      dialog.setContentWidth(300)
      dialog.setContentHeight(70)
    }else {
      var content = ''
      content += options.invitation_copy
      content += "<fb:req-choice url='" + options.invitation_choice_url + "' label='" + options.invitation_choice_label + "' />"

      var fbml = ''
      fbml += '<fb:fbml>'
      fbml += '<fb:request-form type="' + options.type + '" content="' + content + '" invite="true" action="' + options.request_action_url + '" method="post">'
      fbml += '<fb:multi-friend-selector'
      fbml += ' actiontext="' + options.request_action_text + '" '
      fbml += ' showborder="true" '
      fbml += ' rows="' + options.friend_selector_rows + '" '
      fbml += ' exclude_ids="' + exclude_ids + '" '
      fbml += ' bypass="' + options.friend_selector_bypass + '" '
      fbml += ' email_invite="' + options.friend_selector_email_invite + '" '
      fbml += '/>'
      fbml += '</fb:request-form>'
      fbml += '</fb:fbml>'

      dialog.setFBMLContent(fbml)
      dialog.setContentWidth(options.width)
      dialog.setContentHeight(options.height)
    }
    dialog.show()
  })


}


