/**
 * Select payment gateway
 */
jQuery.fn.selectPaymentGateway = function(s) {
  var settings = jQuery.extend({
    'types' : null
  }, s);
  
  return this.each(function() {
    var wrapper = $(this);
    var wrapper_id = wrapper.attr('id');
    
    var type_details_wrapper = $('<div class="payments_gateway_types_details"></div>').appendTo(wrapper);
    
    // Define all types
    if(settings['edit_mode'] != true) {
    	var types_wrapper = $('<div class="payments_gateway_types"></div>').prependTo(wrapper);
      
    	if ($.isEmptyObject(settings['types']) === false) {
        for(var type_class in settings['types']) {
          var type_wrapper = $('<div class="payment_gateway_type"></div>').appendTo(types_wrapper);
          var type_id = wrapper_id + '_' + type_class;

          var radio = $('<input type="radio" name="payment_gateway[type]" value="' + type_class +'" id="' + type_id + '">');
          radio.click(function() {
            var current_type_class = $(this).attr('value');



            if(type_details_wrapper.attr('currently_selected') == current_type_class) {
              return;
            } // if

            type_details_wrapper.empty().append('<h3>' + settings['types'][current_type_class]['name'].clean() + '</h3>').attr('currently_selected', current_type_class);

            if(settings['types'][current_type_class]['description']) {
              type_details_wrapper.append('<div class="payment_gateway_type_description">' + settings['types'][current_type_class]['description'] + '</div>');
            } // if

            if(settings['types'][current_type_class]['options']) {
              type_details_wrapper.append('<div class="payment_gateway_options">' + settings['types'][current_type_class]['options'] + '</div>');
            } // if

          }).appendTo(type_wrapper);

          if(settings['types'][type_class]['selected'] == true) {
            var selected_radio = radio;
          };

          type_wrapper.append('<label for="' + type_id + '">' + settings['types'][type_class]['name'].clean() + '</label>');
        } // for

        // And select first one
        if(selected_radio) {
          selected_radio.click();
        } else {
          types_wrapper.find('input[type=radio]:first').click();
        } // if
      } else {
        wrapper.append('<p class="empty_page">' + App.lang('There are no defined payment options') + '</p>');
        $('div.button_holder').hide();
      } // if
	    
    //edit_mode
    } else {
    	type_details_wrapper.empty().append('<h3>' + settings['types'][0]['name'].clean() + '</h3>');
    	 
    	 if(settings['types'][0]['description']) {
    		 type_details_wrapper.append('<div class="payment_gateway_type_description">' + settings['types'][0]['description'] + '</div>');
    	 } // if
        
         if(settings['types'][0]['options']) {
        	 type_details_wrapper.append('<div class="payment_gateway_options">' + settings['types'][0]['options'] + '</div>');
         } // if
    }
    
  });
  
};