var symantec = symantec || {};

symantec.groups = {
    ajax_endpoint_subscription_set: 'group-sub'
};

Drupal.behaviors.group = function (context) {
  symantec.groups.setup_group_wall_filter();
  symantec.groups.setup_callout_form();  
  symantec.groups.setup_ptf_groups_home();
  symantec.lib.setup_tooltips();
  symantec.groups.setup_meta();
}


$(document).ready(function() {
  symantec.groups.subscription_selection();
});



/**
 * Setup for the groups callout form
 */
symantec.groups.setup_callout_form = function() {
  // Hide the form as soon as the page loads
  $('#callout-form').hide();
  $('a#callout-form-text').click(function() {
    $('#callout-form').toggle(400);
    return false;
  });
}

/**
 * Setup for the group metadata area
 */
symantec.groups.setup_meta = function() {
  // Accordion
  $("#accordion").accordion({ 
    header: "h3",
    autoHeight: false
 });
}

/**
 * Setup for tabs on the partner technical forums group page
 */
symantec.groups.setup_ptf_groups_home = function() {
	if ($('.tabset').length) {
		// setup group home tabs
		$('.tabset').tabs();
		$('.tabset-inner').tabs();
	}
	/*
  if ($('#container-1').length && $('#container-1a').length) {
    $('#container-1').tabs();
    $('#container-1a').tabs();
  }
*/
}

/**
 * Setup for filtering the group wall posts by content type
 * for filter dropdown on nod page TODO:
 */
symantec.groups.setup_group_wall_filter = function() {
  $('.dropdown-filter-products, .dropdown-language-filter, .dropdown-type-filter').change(function() {
    symantec.groups.refresh_group_wall_items();
  });
  
  symantec.groups.setup_paging();
    
}

/**
 * Sets up click handling on the pager returned by the group wall view
 */
symantec.groups.setup_paging = function() {
  $('.result-items-wrapper .pager-control a').unbind('click').click(function() {
     var href = $(this).attr('href');
     var page_part = href.match(/[\\?&]page=(\d+)/);
     if (page_part) {
       var page_number = page_part[1];
       symantec.groups.destination_page = page_number;
     } else {
       symantec.groups.destination_page = 0;
     }
     symantec.groups.refresh_group_wall_items();
     return false;
   });  
}

/**
 * Refreshes the content on the group wall, given the current filter state
 */ 
symantec.groups.refresh_group_wall_items = function() {
    var post_data = {
      product: $('.dropdown-filter-products').val(),
      language: $('.dropdown-language-filter').val(),
      content_type: $('.dropdown-type-filter').val(),
      group_id: Drupal.settings.group_nid
    };

    var target = $('.result-items-wrapper');
    target.html('<h2 class="ajax-loading">Loading</h2>');
    
    var ajax_endpoint = Drupal.settings.basePath + 'filters/groups/ajax';
    
    if (this.destination_page && this.destination_page != 0) {
      ajax_endpoint += '?page=' + this.destination_page;
    }    
    
    $.post(ajax_endpoint, post_data, function(response) {
      var response_data = eval('(' + response + ')');
      if (response_data['status'] && response_data['data']) {
        target.html(response_data['data']);
        symantec.groups.setup_paging();        
      }
      
      symantec.lib.setup_tooltips();      
    });
}



/**
 * Sets up click handling on the group subscription checkbox
 */
symantec.groups.subscription_selection = function() {
  $('#group_sub').unbind('click').click(function() {
    var group_label = $('#group_sub_label').html();  
    var group_id = $('#group_id').val();
    var group_sub = "";
    
    if ($('#group_sub').is(':checked')) {
      group_sub = 'sub';
      $('#group_sub_label').html('subcribing...');
      
    } else {
      group_sub = 'unsub';
      $('#group_sub_label').html('unsubscribing...');
    }

    var post_data = {gid: group_id, opt: group_sub};

    $.post(Drupal.settings.basePath + symantec.groups.ajax_endpoint_subscription_set, post_data, function(result, textResponse) {
      var data = eval('(' + result + ')');
      $('#group_sub_label').html(group_label);
    });
 });
}
