//=====================================================//
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');
                    if ($('#recaptcha_code').length > 0){
                        Recaptcha.reload();
                    }
                }}
                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');
   });

    $('input#shippingSameAsBilling').click(function(){
        var cbVal = $(this).is(':checked');

        if (cbVal) {
            $('select[name=shipping_salutation]').val($('select[name=salutation]').val());
            $('input[name=shipping_first_name]').val($('input[name=first_name]').val());
            $('input[name=shipping_last_name]').val($('input[name=last_name]').val());
            $('input[name=shipping_company_name]').val($('input[name=company_name]').val());
            $('input[name=shipping_address_street]').val($('input[name=address_street]').val());
            $('input[name=shipping_address_town]').val($('input[name=address_town]').val());
            $('input[name=shipping_address_state]').val($('input[name=address_state]').val());
            $('input[name=shipping_phone]').val($('input[name=phone]').val());
            $('input[name=shipping_mobile]').val($('input[name=mobile]').val());
            $('input[name=shipping_email]').val($('input[name=email]').val());
        }
    });



    //$('#facebook #competitionForm a.enter-button').click(Facebook.validateCompetitionForm);
    if ($('body').hasClass('facebook') || $('body').hasClass('facebookWCT') || $('body').hasClass('facebook2')) {
        $('#fb-win-concert-ticket #competitionForm a.enter-button').click(Facebook.validateWinTicketToConcert);
        $('#fb-playboy-club-party #competitionForm a.enter-button').click(Facebook.validatePlayboyClubParty);
        $('#fb-moon-cake #competitionForm a.enter-button').click(Facebook.validateMoonCake);
        $('#fb-win-headphones #competitionForm a.enter-button').click(Facebook.validateWinHeadphones);
        $('#fb-festival-wine #competitionForm a.enter-button').click(Facebook.validateFestivalWine);
        $('#fb-moments-time #competitionForm a.enter-button').click(Facebook.validateMomentsTime);
        $('#fb-clockenflap-vip #competitionForm a.enter-button').click(Facebook.validateClockenflapVip);
        $('#fb-star-cruises #competitionForm a.enter-button').click(Facebook.validateStarCruises);
        //Facebook.resizeFrame();

    }
});

