//=====================================================//
var LoadReadyLocal = {
    commentForm: function(){
        var options = {
            success: function(json, statusText, jqFormObj) {
                extraPar= {callback: function(){
                    document.location = '#commentsTop';
                    var returnText = json.returnText;
                    $("#commentsList").html(returnText);
                    $('#frmComment').resetForm();
                    $('#commentForm').slideDown('slow');
                    $('a#reloadCaptcha').click();
                }}
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };

        $('#frmComment').ajaxForm(options);
    }
};

LoadReady = $.extend(LoadReady, LoadReadyLocal);


$(function() {
    $('h4.has-children').click(function(){
        $(this).next().slideToggle();
        $(this).toggleClass('closed');
    });

    $('ul.facets li.has-children').click(function(e){
        clickedItem = $(this);
        status = $('ul', $(this)).css('display');
        
        if (status == 'none'){
            $(clickedItem).removeClass('closed').addClass('open');
            $('ul', clickedItem).slideDown('slow');
        } else {
            $(clickedItem).removeClass('open').addClass('closed');
            $('ul', clickedItem).slideUp('slow');
        }

    });

    $('ul.facets li.has-children a').click(function(e){
        e.stopPropagation();
    });

    $('#search').submit(function(e){
        e.preventDefault();
        Util.showProgressInd();
        Util.clearPrepopulatedTextbox($(this));
        $(this).unbind('submit');
        $(this).trigger('submit');
    });

});



//*********************************************************//
$(function(){

   $('#productItems input').click(function(){
      $('#productItems div').removeClass('selectedProduct');
      $(this).parent().addClass('selectedProduct');

      if ( !$(this).attr('hasCoupon')){
          $('#couponMessage').html('');
      }

   });

   $('input[hasCoupon=1]').live('click', function(){
      couponProductID = $(this).val();
      $.nyroModalManual({
        url: '/index.php?_room=member&_spAction=enterCouponNo&showHTML=0',
        modal: true,
        minWidth: 200,
        minHeight: 90,
        width:200,
        height:90,
        resizeable: true
      })

   });

   $('a#couponConfirm').live('click', function(e){
       e.preventDefault();
       var coupon_no  = $('#coupon_no').val();

       var url = '/index.php?_room=member&_spAction=enterCouponNoSubmit&showHTML=0';
       $.get(url, {coupon_no:coupon_no, product_id:couponProductID}, function (data) {
         hasValue = data.hasValue;

         if (hasValue == 0){
            $('#couponError').html('invalid coupon no.');
         } else {
            $('#coupon_no_h').val(coupon_no);
            $.nyroModalRemove();
            $('#couponMessage').html('Coupon Used: <b>' + coupon_no + '</b>');
            $('#couponMessage').css({'color':'green', 'font-weight':'bold'});
         }

       }, 'json');
   });

   $('a#couponCancel').live('click', function(e){
      e.preventDefault();
      $.nyroModalRemove();
      $('input:radio[name=product_id]:first').attr('checked', 'checked');
      $('#productItems div').removeClass('selectedProduct');
      $('input:radio[name=product_id]:first').parent().addClass('selectedProduct');
   });

});