


function customer_review_init() {
  jQuery("#customer-review .links .write-review").removeClass('active');

  jQuery("#customer-review .links .view-all-reviews")
    .attr('href', '#customer-review')
    .removeClass('active')
    .click(function() {
      customer_review_load_all(this);
    });
}

function customer_review_cancel() {
  var inp = jQuery("#cust-review-comment");
  var frm = jQuery(inp.get(0).form);
  data = cust_review_init_input(inp);
  inp.val('').blur();
  
  jQuery('.hideable', frm).hide();
  inp.css('height', (data.height)+'px').removeClass('comment-focused');
  
  return false;
}

function customer_review_long_comment_init() {
  var link = jQuery(".review-long:not(.processed) .review-more a").each(function() {
    var wrap = jQuery(this).parent().parent();
    wrap.addClass('processed')
    jQuery(this).click(function() {
      var wrap = jQuery(this).parent().parent();
      var html = jQuery(".review-therest", wrap).html();
      jQuery(".review-more", wrap).hide();
      jQuery(".review-teaser", wrap).append(html);
      jQuery(".review-therest", wrap).html('');
      return false;
    });
  });


}

function customer_review_view_more(cid) {

}


function customer_review_load_all(A) {
  jQuery(A).addClass('throbbing');

  var nid    = jQuery(A).attr('rel');
  var href   = '/customer_review/action/get/'+nid+'/0/0';
  var params = {};
  jQuery.getJSON(href, params, function(R) { customer_review_load_all_callback(R, A); });


}
function customer_review_load_all_callback(R, A) {
  if (R.status) {
    jQuery("#comments .customer-review").remove();
    jQuery("#comments").empty().append(R.data.html);
    customer_review_long_comment_init();
  }
  jQuery(A).remove('throbbing').fadeOut();
}

function cust_review_init_input(inp) {
  var data = jQuery(inp).data('data');
  if (typeof(data) == 'undefined') {
    data = {};
    data.height = jQuery(inp).height();
    jQuery(inp).data('data', data);
  }
  return data;
}


function cust_review_focus(_inp) {
  var inp = jQuery(_inp);
  var val = inp.val();
  var ph  = inp.attr('placeholder');
  var self = _inp;
  var F = jQuery(self.form);

  var data = cust_review_init_input(inp);
  if (!inp.hasClass('comment-focused')) {
    jQuery('.hideable', F).show();
    inp.css('height', (data.height+200)+'px').addClass('comment-focused').focus();
  }
}

function cust_review_blur(_inp) {
  var inp  = jQuery(_inp);
  var self = _inp;
  var F    = jQuery(self.form);
  data     = cust_review_init_input(inp);

  if (inp.hasClass('comment-focused') && inp.val() == '') {
    //jQuery('.hideable', F).hide();
    //inp.css('height', (data.height)+'px').removeClass('comment-focused');
  }
  return true;
}



if (Drupal.jsEnabled) {
  jQuery(document).ready(function () {
    customer_review_init();
    customer_review_long_comment_init();

    if (function_exists('get_hash')) {
      var hash = get_hash();
      if (hash == '#comment_form' || hash == '#comment') {
        jQuery("#customer-review-form-wrapper").slideDown();
      }
    }

  });
}



