//############################################################

function setuplist(settings){
  //-------------------------------------
	var m = $.extend({
	   id            : 'setuplist',
	   container     : this,
	   categories_id : 0,
	   heading       : 'The Ultimate set up includes:',

	   url  : 'includes/ajax/setuplist.php',
	   guid : parseInt(Math.random()*1000000)
	}, settings || {});
  //-------------------------------------
  function init(){
      m.root_id = m.id+"_"+m.guid;

      m.url = window.location.protocol + '//' + window.location.host;
      if ("https:" == window.location.protocol) {
          m.url = m.url + '/~potncom/includes/ajax/setuplist.php';
      } else {
          m.url = m.url + '/includes/ajax/setuplist.php';
      } // if

      m.container = $('#'+m.id);
          m.container.css('overflow-x', 'hidden')
          m.container.css('overflow-y', 'hidden')

      //if($.browser.msie){
      //    setTimeout(refresh, 100);
      //} else {
          refresh();
      //}
      
  }; // function init()
  //-------------------------------------
  function refresh(){
      //m.container.append("<img id='loading_"+m.root_id+"' src='images/saving.gif' style='z-index:2;' >");

      m.container.html('Loading...');
      $('.cbx_setuplist').unbind('click');

      $.ajax({
        type : 'POST',
        url : m.url,
        datatype:'html',
        //timeout:20000, // if too low ie 20ms no error reported like ie 6
        data:{
          action        : 'init',
          categories_id : m.categories_id,
          products_id   : m.products_id,
          heading       : m.heading
        },
        success:function(retval, textStatus){
            //$("#loading_"+m.root_id).remove();

            if(retval.substr(0, 6) == 'Error:'){
                alert(retval);
            }
            //alert(textStatus);
            m.container.html(retval);
            $('.cbx_setuplist').bind('click', setuplist_calc);
            //alert(textStatus);
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            m.container.html("<b>Error</b>"+textStatus+"<br>\n"+errorThrown);
        }
      });

/***
      //if ( $.browser.msie ){
      //    if(parseInt( $.browser.version ) == 1){
      //    }
      //}

      $.post
      (
          m.url,

        {
          action        : 'init',
          categories_id : m.categories_id,
          products_id   : m.products_id,
          heading       : m.heading
        },

        function(retval){
            $("#loading_"+m.root_id).remove();

            if(retval.substr(0, 6) == 'Error:'){
                alert(retval);
            }
            
            $('#'+m.id).html(retval);
            $('.cbx_setuplist').bind('click', setuplist_calc);

        }, // function(retval)
        
        'html'

      ) // $.post
***/
  } // function
  //-------------------------------------
  function setuplist_calc(){
      var total    = 0.00;
      var shipping = 0.00;
      var price    = 0.00;
      var delivery = 0.00;
      
      $('.cbx_setuplist').each(
          function(i){
            if( ! this.checked) return;
            
            price    = Number( this.attributes['price'].value );
            quantity = Number( this.attributes['quantity'].value );
            delivery = Number( this.attributes['delivery'].value );
            
            if(delivery > shipping) shipping = delivery;
            total += (price);
          }
      );
      
      $('#setuplist_sub_total'      ).html(total.toFixed(2));
      $('#setuplist_shipping_amount').html(shipping.toFixed(2));
      total += shipping;
      $('#setuplist_total'          ).html(total.toFixed(2));
  
  }
  //-------------------------------------
  init();

  //return m.container;
  //-------------------------------------
} // function

//====================================================

$(document).ready(function() {
    jQuery('div.setuplist').each( 
        function(i) {
            var id = this.id;
            
            var categories_id = '';
            var products_id   = '';
            var heading       = '';
            
            try{
                categories_id = this.attributes['categories_id'].value;
            }
            catch(e){
                categories_id = '';
            }

            try{
                products_id = this.attributes['products_id'].value;
            }
            catch(e){
                products_id = '';
            }

            try{
                heading = this.attributes['heading'].value;
            }
            catch(e){
                heading = 'The Ultimate set up includes:';
            }

            jQuery('#'+id).html( setuplist( {id:id, categories_id:categories_id, products_id:products_id, heading:heading} ) );
        }
    );
});

//====================================================

