
// create Onioning namespace
if(typeof Onioning == "undefined") Onioning = {};



function facebook_post(a){
  window.open('/questions/facebook_post/'+a,'_blank','toolbar=0,status=0,width=626,height=436');
  return false;
}

function twitter_post(link, status){
  location.href = 'http://www.twitter.com/home?status='+escape(status);
}



Onioning.Paging = function(baseUri, linkId, totalPages, pageParamName){

  this.page = 1; // init page
  this.totalPages = totalPages;
  this.baseUri = baseUri;
  if("undefined" == typeof pageParamName)
    pageParamName = "page"
  var self = this;
  $(
    function(){

      $(linkId).live('click',
        function(){
          var elmore = $(this);
          var loading_img = elmore.append('<p class="loading"><img src="/images/ajax-loader.gif"></p>');
          elmore.find('a').hide();
          var more_button = elmore;
          self.page++;

          var params = {};
          params[pageParamName] = self.page;
          $.get(self.baseUri, params,
                function(data, status){
                  elmore.find('a').show();
                  elmore.find('p.loading').remove();

                  if(self.page >= self.totalPages){
                    more_button.hide();
                  }
                }, 'script');

          return false;

        });
    });
};

/*
 * Visits
 */
$( function(){
     $('#my_profile ul.visits li')
       .find('div.delete').click(
         function(){
           var self = $(this);
           var vid = self.attr('visit_id');
           self.addClass('loading');
           $.getScript('/home/delete_visit_log/'+vid);
           return false;
         });

   });




$( function(){
     // 질문 폼
     new Onioning.CountableForm("form#new_question textarea",{ txtCount : ".text_count", maxLength : 300});

     // daily question 답변 폼
     new Onioning.CountableForm("#public_question_form .public_answer_textarea", { txtCount:"h3.text_count", maxLength : 1000});

     // answer form
     new Onioning.CountableForm("form.edit_answer textarea",{ txtCount : "h3.characters", btnSubmit : "input.save" , maxLength :  1000});

     // comment form
     new Onioning.CountableForm(".QandA .comment_textarea", { txtCount : "h4" , maxLength : 200} );

     // talk form
     //new Onioning.CountableForm("#talk_form_wrap textarea", ".text_count");

     Onioning.IS_PROFILE = true;

   });


$( function(){
     $('.comments_trigger').live('click', function(){
                                     var qaJo = $(this).parents(".answer_set:eq(0)");
                                     qaJo.find('.comment_form .comment_set_blank').hide();
                                     qaJo.find('.comment_set').show().find("textarea").focus();
                                     return false;
                                   });
} );



$( function(){
     $('.comment').mouseover( function(){
                                $(this).find('.comment_toolbar').show();
                              });
     $('.comment').mouseout( function(){
                               $(this).find('.comment_toolbar').hide();
                             });
   });



// Admiring List
$( function(){
     $('li.admirer > div').mouseover( function(){
                                        $(this).addClass('hover');
                                        $(this).find('.block').show();
                                      }).mouseout( function(){
                                                     $(this).removeClass('hover');
                                                     $(this).find('.block').hide();
                                                   });

   } );



// QABOX : Answer Form
$(function(){

    $('div.my_answer_set a.answer_form_trigger')
      .live('click', function(){
        $(this)
          .parents('div.my_answer_set:eq(0)').hide()
          .parents('.set:eq(0)').find('.my_answer').show();
            // trigger global event
        $(window).trigger('answer_form_triggered', [$(this).parents('form:first')]);

        return false;
      });
    $('div.my_answer .answer_form_cancel')
      .live('click', function(){
         $(this)
          .parents('.my_answer:eq(0)').hide()
          .parents('.set:eq(0)').find('div.my_answer_set').show()

         return false;
       });

    $('.QandA .question_set')
      .live('mouseover', function(){
              $(this).parents('.set').addClass('mover');
            })
      .live('mouseout', function(){
              $(this).parents('.set').removeClass('mover');
            });



    $('.QandA .deletable_answer .a_delete_trigger')
      .live('mouseover', function(){
              $(this).parents(".deletable_answer:eq(0)").addClass('delete');
              $(this).find('.a_pre_group .delete_button').show();
            })
      .live('mouseout', function(){
              $(this).parents(".deletable_answer:eq(0)").removeClass('delete');
              $(this).find('.a_pre_group .delete_button').hide();
            });

    $('.QandA .comment_textarea_trigger')
      .live('focus', function(){
              var aJo = $(this).parents(".answer_set:eq(0)");
              $(this).parents(".comment_set:eq(0)").hide();
              aJo.find('.comment_form')
                .parents(".comment_set:eq(0)")
                  .show()
                .find("textarea")
                  .css("background", "#fff")
                  .focus();
            });
});



/* Friends Controller : admiers/admirings */
$(function(){

    $('#my_admirers .set')
      .live('mouseover', function(){
              $(this).addClass('block_user');
              $(this).find('div.block').show();
            })
      .live('mouseout', function(){
              $(this).removeClass('block_user');
              $(this).find('div.block').hide();
            });
});

$(function(){
  if (jQuery.fn.fancybox) {
    $("a.add_friends").livequery(function(){
      $(this).fancybox({
        "frameHeight": 510,
        "hideOnContentClick": false
      });
    });
  }
});


