// base class... lets stick new stuff in here.
function bantr(bantr_uid,url_login_params,script_location,APP_ID_BANTR,APP_ID_FACEBOOK,local_url,app_url,api_key) {
  this.bantr_uid = bantr_uid;
  this.url_login_params = url_login_params;
  this.kid = new kid();
  this.script_location = script_location;
  this.APP_ID_BANTR = APP_ID_BANTR;
  this.APP_ID_FACEBOOK = APP_ID_FACEBOOK;
  this.local_url = local_url;
  this.app_url = app_url;
  this.api_key = api_key;
}

bantr.prototype.simple_fade_in = function simple_fade_in(id) {
  Animation(f$(id)).to('height', 'auto').from('0px').to('opacity', 1).from(0).blind().show().go();
}

bantr.prototype.simple_fade_out = function simple_fade_out(id) {
  Animation(f$(id)).to('height', '0px').to('opacity',0).hide().go();
}

bantr.prototype.show_error = function show_error(heading,message) {
  if (this.script_location == this.APP_ID_FABCEOOK) {
    new Dialog().showMessage(heading,message);
  } else {
    alert ('<h2>'+heading+'</h2>'+message);
  }
}


// for kid related js - in kid.js
function kid() {
  this.feed = new kid_feed();
}

// kid feed related js
function kid_feed() {
}

kid.prototype.show_follow_interstitial = function show_follow_interstitial(kid_id) {
  toggle('interstitial_outer');
  custom_ajax('index.php?action_type=show_fb_follow_request_form',{'rewrite_id':'interstitial_content','parameters':{'kid':kid_id},'on_done_call_function':fb_parse_dom}); 
}

kid.prototype.stop_following = function stop_following(kid_id) {
  var par = f$('un_follow_parent');
  par.setTextValue('Saving...');
  var callback = function() {
    bantr.simple_fade_out('follower_pic_'+bantr.bantr_uid);
    par.setTextValue('Saved.');
    var t = setTimeout(function() {bantr.simple_fade_out('un_follow_parent')},2000);
  }
  custom_ajax('index.php?action_type=user_unfollow_kid',{'parameters':{'kid_id':kid_id},'on_done_call_function':callback});
}


function embrace(template_data) {
  if (typeof template_data == 'object') {
    this.template_data = template_data;
  }
}
embrace.prototype.start_flow = function start_flow(obj) {
  var callback = function() {
    custom_ajax('index.php?action_type=get_embrace_donate_popup',{'rewrite_id':'interstitial_content'});
    f$('interstitial_outer').setStyle('display','block');
    f$('dummy_top_of_page_input').focus();
  }
  f$('entire_content').addClassName('fade_out');
  if (this.template_data) {
    show_feed_dialog(this.template_data,callback);
  } else {
    callback();
  }
}

embrace.prototype.go_pay = function go_pay() {
  var error_count = 0;
  clear_validation_errors('embrace_donate_form');
  error_count += validate_field('embrace_amount','Please enter an amount to donate.','exists','','embrace_errors');
  error_count += validate_field('embrace_amount','Please enter a number.','numeric','','embrace_errors');
  error_count += validate_field('embrace_amount','Please enter $5 or more.','min_int:5','','embrace_errors');
  if (error_count == 0) {
    f$('embrace_donate_form').submit();
  }

}

embrace.prototype.back_from_checkout = function back_from_checkout(template_data,donate_amount){
  var callback = function() {
    custom_ajax('index.php?action_type=get_embrace_donated_popup&donate_amount='+donate_amount,{'rewrite_id':'interstitial_content','on_done_call_function':fb_parse_dom});
    f$('interstitial_outer').setStyle('display','block');
  }
  f$('entire_content').addClassName('fade_out');
  if (template_data) {
    show_feed_dialog(template_data,callback);
  } else {
    callback();
  }
}

embrace.prototype.donate_finished = function donate_finished(){
  var error_count = 0;
  clear_validation_errors('embrace_donate_form');
  error_count += validate_field('friend_checkboxes','Please select at least one friend.','radios');
  if (error_count == 0) {
    var callback = function() {
      toggle('embrace_donated_1');
      toggle('embrace_donated_2');
    }
    custom_ajax('index.php?action_type=embrace_donated_send_message',{'rewrite_form':'embrace_donate_form','rewrite_id':'message_sent_result','on_done_call_function':callback});
    show_loading(f$('embrace_donated_1'));
  }
}

embrace.prototype.skip_to_end = function skip_to_end() {
  toggle('embrace_donated_1');
  toggle('embrace_donated_2');
}

function delete_notepad(type, id, entity_id, entity_type) {
  f$('feeditem_container_'+ type + '_' + id).setStyle('display','none');
  custom_ajax('index.php?action_type=delete_wall_post',{'rewrite_id':'feeditem_container_'+ type + '_' + id,'parameters':{'entity_id':entity_id,'entity_type':entity_type,'wall_post_id':id}});
}




function validate_and_submit_kid_comments(type, id) {
  var error_count = 0;
  var text = f$('feed_comment_input_' + type + '_' + id).getValue();
  f$('feed_comment_button_' + type + '_' + id).setStyle('display','none');
  if (text == '') {
    f$('feed_comment_button_' + type + '_' + id).setStyle('display','block');
    error_count++;
  }
  var callback = function() {
    toggle('add_new_comment_loading_'+type+'_'+id);
    toggle('feed_comment_input_'+type+'_'+id);
    f$('feed_comment_button_' + type + '_' + id).setStyle('display','block');
    f$('feed_comment_input_'+type+'_'+id).setValue('');
    fb_parse_dom();
  }
  var par = f$('feed_comment_'+type+'_'+id);
  par.getParentNode().setStyle('display','');
  var div = bantr.create_element('div');
  par.insertBefore(div,f$('dummy_content_'+type+'_'+id));
  var new_div_id = 'n_' + Math.floor(Math.random()*100000000);
  div.setId(new_div_id);
  if (error_count == 0) {
    custom_ajax('index.php?from=kid_feed',{'rewrite_id':new_div_id,'rewrite_form':'add_new_comment_'+type+'_'+id,'on_done_call_function':callback});
    toggle('add_new_comment_loading_'+type+'_'+id);
    toggle('feed_comment_input_'+type+'_'+id);
  }
}



function text_area_autogrow(el_id, o) {
  if (typeof el_id == 'string') {
    e = f$(el_id);  
  } else {
    e = el_id;
  }
  this.options		  	= o || {};
  this.dummy			  	= null;
  this.timeout	 	  	= null;
  this.line_height	  = this.options.lineHeight || e.getStyle('lineHeight');
  this.min_height		  = this.options.minHeight || e.getStyle('minHeight');
  this.max_height		  = this.options.maxHeight || e.getStyle('maxHeight');
  this.textarea		  	= e;
  this.line_height = this.line_height.replace(/px/,'');
  this.init();  
}

text_area_autogrow.prototype.init = function init() {			
  var self = this;
  this.textarea.addEventListener('focus',function(){self.checkExpand()}.bind(this));
  this.textarea.addEventListener('blur',function(){self.stopExpand()}.bind(this));
  this.checkExpand();	
};
	
text_area_autogrow.prototype.stopExpand = function stopExpand() {
  clearTimeout(this.timeout);	
};
		
text_area_autogrow.prototype.checkExpand = function checkExpand() {
  var self = this;
  
	if (this.dummy == null) {
    var dummy = bantr.create_element('div');
    this.dummy = dummy;
    var par = this.textarea.getParentNode();
    par.appendChild(this.dummy);
  }
    this.dummy.setStyle({
                    fontSize   : this.textarea.getStyle('fontSize'),
                    fontFamily : this.textarea.getStyle('fontFamily'),
                    width      : (parseInt(this.textarea.getClientWidth()))+'px', // 2px margin for error seems ff and ie are slightly different in how they space textareas vs divs
                    padding    : this.textarea.getStyle('padding'),
    //                seems to break IE
                    overflowX  : 'hidden',
                    position   : 'absolute',
                    top        : '0',
                    border : '1px solid #f00',
                    left		   : '-9999px'
                    });
    if (this.line_height) {
      this.dummy.setStyle('lineHeight',this.line_height + 'px');
    }
  
/*  if (!this.basic_line_height) {
    this.dummy.setInnerXHTML('<span>X</span>');
    this.basic_line_height = this.dummy.getOffsetHeight();
  }*/
  this.basic_line_height = 0;
  var html = this.textarea.getValue().replace(/(<|>)/g, '');
  html = html.replace(/\n/g, '<br />');
  
  this.dummy.setInnerXHTML('<span>'+html+'</span>');
  if (this.max_height > 0 && (this.dummy.getOffsetHeight() + this.line_height > this.max_height)) {
    this.textarea.setStyle('overflowY', 'auto');	
  } else {
    this.textarea.setStyle('overflowY', 'hidden');
    if (this.textarea.getOffsetHeight() < this.dummy.getOffsetHeight() + this.line_height || (this.dummy.getOffsetHeight() < this.textarea.getOffsetHeight())) {	
      this.textarea.setStyle({height: (parseInt(this.dummy.getOffsetHeight()) + this.basic_line_height) + 'px'});	
    }
  }
  
  this.timeout = setTimeout(function() {self.checkExpand()}, 400);
};

function compliments() {
  this.ajaxed = false;
  this.changed = false;
}

compliments.prototype.get= function get(caller,to_uid,thread_post_id,page_key,default_compliment, default_compliment_text) {
  var caller = bantr.rereg(caller);
  this.page_key = page_key;
  this.calling_element = caller;
  this.li = caller.getParentNode();
  this.to_uid = to_uid;
  this.thread_post_id = thread_post_id;
  if (typeof default_compliment == 'undefined') {
    this.default_compliment = null;
  } else {
    this.default_compliment = default_compliment;
  }

  if (typeof default_compliment_text == 'undefined') {
    this.default_compliment_text = null;
  } else {
    this.default_compliment_text = default_compliment_text;
  }

  try {
    this.hide();
  } catch(e) {}
  if (!this.ajaxed) {
    this.create();
  } else {
    this.set_position(); 
    this.set_default();
  }
}

compliments.prototype.set_default = function set_default() {
  if (!this.default_compliment && !this.default_compliment_text) return 0;
  this.click(f$('compliment_' + this.default_compliment), this.default_compliment_text);
}

compliments.prototype.create = function create() {
  //loading message
  var span = bantr.create_element('span');
  span.setTextValue('Loading...');
  this.calling_element.setStyle('display','none');
  this.li.appendChild(span);
  this.loading_div = span;
  // create the comp. form and fill it.
  var div = bantr.create_element('div');
  div.setId('compliments_holder');
  var par = f$('entire_content');
  par = par.getParentNode();
  var insert_before = f$('entire_content');
  par.insertBefore(div,insert_before);
  this.dialogue = div;
  this.ajaxed = true;
  var temp_this = this;
  var callback = function() {
    temp_this.set_position();
    temp_this.loading_div.setStyle('display','none');
    temp_this.calling_element.setStyle('display','block');
    var text_area = f$('compliment_text');
    text_area.addEventListener('keydown', function(event) {
      temp_this.changed = true;
    }.bind(this));
    temp_this.set_default();
  }
  custom_ajax('index.php?action_type=get_compliments',{'rewrite_id':'compliments_holder','on_done_call_function':callback});
  return true;
}

compliments.prototype.click = function click(clicker,long_text) {
  var clicker = bantr.rereg(clicker);
  var children = clicker.getElementsByTagName('input');
  children[0].setChecked(true);
  if (!this.changed) {
    f$('compliment_text').setValue(long_text);
  }
}

compliments.prototype.hide = function hide() {
  this.dialogue.setStyle({'display':'none','left':'-5000px'});
  f$('compliment_result').setTextValue('');
  f$('compliment_result').setStyle('display','none');
  f$('compliment_up').setStyle('display','block');
}

compliments.prototype.send = function send() {
  if (!this.sent) {
    var error_count = 0;
    error_count += validate_field('compliment_radios','Please select a compliment','radios');
    error_count += validate_field('compliment_text','Please write something','exists');
    if (error_count == 0) {
      f$('compliment_to_uid').setValue(this.to_uid);
      f$('compliment_thread_post_id').setValue(this.thread_post_id);
      this.sent = true;
      var cont = f$('compliment_form');
      divs = cont.getElementsByTagName('div');
      for (var i = 0;i<divs.length;i++) {
        if (divs[i].hasClassName('submit_button') || divs[i].hasClassName('skip_button')) {
          divs[i].addClassName('clicked');
        }
      }
      var temp_this = this;
      var callback = function() {
        temp_this.changed = false;
        temp_this.sent = false;
        f$('compliment_result').setStyle('display','block');
        f$('compliment_saving').setStyle('display','none');
        temp_this.set_position();
        var cont = f$('compliment_form');
        divs = cont.getElementsByTagName('div');
        for (var i = 0;i<divs.length;i++) {
          if (divs[i].hasClassName('submit_button') || divs[i].hasClassName('skip_button')) {
            divs[i].removeClassName('clicked');
          }
        }
      }
      f$('compliment_saving').setStyle('display','block');
      f$('compliment_up').setStyle('display','none');
      this.set_position();
      custom_ajax('index.php?action_type=send_compliment',{'rewrite_form':'compliment_form','rewrite_id':'compliment_result','on_done_call_function':callback});
    }
  }
}

compliments.prototype.set_position = function set_position() {
  var caller = this.li;
  var dialogue = this.dialogue;
  dialogue.setStyle({'display':'block'});
  var all_content = f$('entire_content');
  var popWidth = dialogue.getOffsetWidth();
  var contentAbsoluteOffsetLeft = all_content.getAbsoluteLeft();
  var contentWidth = all_content.getOffsetWidth();
  var callerAbsoluteOffsetLeft = caller.getAbsoluteLeft();
  var callerWidth = caller.getOffsetWidth();
  var objMoveLeft = (callerAbsoluteOffsetLeft+callerWidth) - contentAbsoluteOffsetLeft - popWidth + 4;
  if (objMoveLeft < 0) {
    objMoveLeft = 0;
  }
  dialogue.setStyle('left',objMoveLeft+'px');
  position_a_above_b(dialogue,caller);
}

function position_a_above_b(above,caller) {
  var caller = bantr.rereg(caller);
  var all_content = f$('entire_content');
  var contentAbsoluteOffsetTop = all_content.getAbsoluteTop();
  var contentHeight = all_content.getOffsetHeight();
  var callerAbsoluteOffsetTop = caller.getAbsoluteTop();
  var bottom_pos = contentHeight - callerAbsoluteOffsetTop + 4 + contentAbsoluteOffsetTop;
  above.setStyle('top','auto');
  above.setStyle('bottom',bottom_pos+'px');
}

// drop menus
var current_up_menu_obj = null;
function sub_menu_hover(caller_id,menu_id) {
  try {
    this.caller = f$(caller_id);
    this.menu = f$(menu_id);
    this.caller.addEventListener('mouseover', this.caller_mouseover.bind(this)).addEventListener('mouseout', this.caller_mouseout.bind(this));
    this.menu.addEventListener('mouseover', this.menu_mouseover.bind(this)).addEventListener('mouseout', this.menu_mouseout.bind(this));
    this.t = '';
  } catch (e) {} // sometimes this gets called with no menus.
}

sub_menu_hover.prototype.caller_mouseover = function(event) {
  this.show_sub_menu();
};

sub_menu_hover.prototype.show_sub_menu = function() {
  if (current_up_menu_obj != null) {
    current_up_menu_obj.setStyle('display','none');
  }
  caller_coords = getPos(this.caller);
  this.menu.setStyle('display','block');
  this.menu.setStyle('top',(caller_coords.height+caller_coords.top)+'px');
  this.menu.setStyle('left',caller_coords.left+'px');
  this.clear_active_on_out = false;
  current_up_menu_obj = this.menu;
}

sub_menu_hover.prototype.caller_mouseout = function(event) {
  this.hide();
};

sub_menu_hover.prototype.menu_mouseover = function(event) {
  this.show_sub_menu();
  this.caller.addClassName('browsing_sub');
  if (!this.caller.hasClassName('menu_item_active')) {
    this.clear_active_on_out = true;
    this.caller.addClassName('menu_item_active');
  }
  
};

sub_menu_hover.prototype.menu_mouseout = function(event) {
  this.hide();
};

sub_menu_hover.prototype.hide = function() {
  this.menu.setStyle('display','none');
  if (this.clear_active_on_out) {
    this.caller.removeClassName('menu_item_active');
  }
  this.caller.removeClassName('browsing_sub');
  
};
// end drop menus


function generic_upload(image_max) {
  this.image_max = image_max;
}

generic_upload.prototype.add_temp_image_to_form = function add_temp_image_to_form(temp_s3_id,temp_s3_path,temp_s3_id_thumb,temp_s3_thumb_path) {
  var pic_holder = f$('uploaded_thumbs');
  pic_holder.setStyle('display','block');
  var this_pic_holder = bantr.create_element('div');
  this_pic_holder.setClassName('temp_uploaded_pic');
  pic_holder.appendChild(this_pic_holder);
  var this_input_id = bantr.create_element('input');
  this_input_id.setName('uploaded_img_temp_s3_id[]');
  this_input_id.setValue(temp_s3_id);
  this_input_id.setType('hidden');
  this_pic_holder.appendChild(this_input_id);
  var this_input_thumb_id = bantr.create_element('input');
  this_input_thumb_id.setName('uploaded_img_temp_s3_thumb_id[]');
  this_input_thumb_id.setValue(temp_s3_id_thumb);
  this_input_thumb_id.setType('hidden');
  this_pic_holder.appendChild(this_input_thumb_id);
  var this_input_thumb_path = bantr.create_element('input');
  this_input_thumb_path.setName('uploaded_img_temp_s3_thumb_path[]');
  this_input_thumb_path.setValue(temp_s3_thumb_path);
  this_input_thumb_path.setType('hidden');
  this_pic_holder.appendChild(this_input_thumb_path);
  var this_input_path = bantr.create_element('input');
  this_input_path.setName('uploaded_img_temp_s3_path[]');
  this_input_path.setValue(temp_s3_path);
  this_input_path.setType('hidden');
  this_pic_holder.appendChild(this_input_path);

  var this_thumbnail = bantr.create_element('img');
  this_thumbnail.setSrc(temp_s3_thumb_path);
  this_thumbnail.setClassName('thumbnail');
  this_pic_holder.appendChild(this_thumbnail);
  var x = bantr.create_element('div');
  x.setClassName('delete_thumb_image');
  x.setTitle('click to remove');
  x.addEventListener('mousedown', function(event) {
    f$('uploaded_thumbs').removeChild(this_pic_holder);
    this.done_upload();
    if (this.image_max == 1) {
      f$('upload_iframe').setStyle('display','');
    }
    var pic_holder = f$('uploaded_thumbs');
  }.bind(this));
  this_pic_holder.appendChild(x);
  this.done_upload();
};

generic_upload.prototype.done_upload = function done_upload() {
  var pic_holder = f$('uploaded_thumbs');
  var children = pic_holder.getChildNodes();
  if (children.length >= 1) {
    f$('terms_and_conditions_container').setStyle('display','');
  } else {
    f$('terms_and_conditions_container').setStyle('display','none');
  }
  if (children.length == this.image_max) {
    f$('photo_form').setStyle('display','none');
  } else {
    f$('photo_form').setStyle('display','');
  }
};
