

jQuery(document).ready(function() {
  setupImgRollover();

  jQuery('img#rebill_button').click(function() {
    jQuery('img#rebill_button').hide();
    jQuery('img#rebill_processing').show();
    jQuery("form#rebillForm").submit();
  });

  jQuery('img#update_cc_button').click(function() {
    jQuery('img#update_cc_button').hide();
    jQuery('img#update_cc_processing').show();
    jQuery("form#updateCC").submit();
  });

  jQuery('img.loginBtn').click(function() {
    jQuery('img.loginBtn').hide();
    jQuery('img.joinBtn').hide();
    jQuery('.loginForm').show();
  });

  // any image with a 'toggle' class will toggle when you mouse over it
  // (unless it has the 'on' class, in which case it should stay on)
  jQuery("img.toggle:not(.on)").hover(function() {
    var src = jQuery(this).attr('src');
    src = src.replace(/_(on|off)\./, '_on.');
    jQuery(this).attr('src', src);
  }, function() {
    var src = jQuery(this).attr('src');
    src = src.replace(/_(on|off)\./, '_off.');
    jQuery(this).attr('src', src);
  });

  // anything with an 'on' class should be toggled to 'on'
  jQuery("img.on").each(function() {
    var src = jQuery(this).attr('src');
    src = src.replace(/_(on|off)\./, '_on.');
    jQuery(this).attr('src', src);
  }); 

  // show the default search value when not in the search box

  function clearText(){
      if (this.defaultValue == this.value) this.value = '';
      else if (this.value == '') this.value = this.defaultValue;
  }
  


  jQuery("#query, #name, #title").focus(clearText).blur(clearText);
  jQuery("img.search_submit_button").hover(
    function() { jQuery(this).addClass('link_hover'); },
    function() { jQuery(this).removeClass('link_hover'); }
  );

  jQuery("#searchButton").remove();
  jQuery("img.search_submit_button").css({display:'inline'});

  jQuery("img.search_submit_button").click(function() {
    var queryElement = jQuery("#query")[0] || jQuery("#name")[0] || jQuery("#title")[0];
    var queryTerms = queryElement.value;
    var defaultTerms = queryElement.defaultValue;

    if (! jQuery("img.search_submit_button").hasClass('nobasic_search_button') ) {
      // redirect to [tab]/search/basic if user did not enter any search terms
      if (queryTerms == null || queryTerms == defaultTerms) {
        var action = jQuery("#frmSearch").attr("action") + "/basic";
        jQuery("#frmSearch").attr("action", action);
      }
    }
    jQuery("form#frmSearch").submit();
  
    return true;
  });

  jQuery("a.membershipOptionToggle").click(function() {
    jQuery(this).hide();
    jQuery(this).siblings('img.please_wait').show();

    var origObj = this;
    jQuery.ajax({
      type: "GET",
      url: "/ajax" + jQuery(origObj).attr('href'),
      dataType: 'json',
      success: function(msg,status) {
        if (msg.status == 'ENABLED') {
          window.location = 'http://' + window.location.hostname + '/?sn=' + msg.id;
        } else if (msg.status == 'DISABLED') {
          window.location.reload();
        } else {
          jQuery(origObj).siblings('img.please_wait').hide();
          jQuery(origObj).siblings('div.' + msg.errtype).show();
        }
        return true;
      },
     error: function(reqObj,msg,exceptionObj) {
       jQuery(origObj)
         .siblings('img.please_wait').hide()
         .siblings('div.system_error').show();
     }
    });

    return false;
  });


  jQuery("a.addActorFav").click(function() {
    jQuery(this).hide();
    jQuery(this).siblings('img.please_wait').show();

    var origObj = this;
    jQuery.ajax({
      type: "GET",
      url: "/ajax" + jQuery(origObj).attr('href'),
      dataType: 'json',
      success: function(msg,status) {
        if (msg.status == 'SUCCESS') {
          jQuery(origObj)
            .siblings('img.please_wait').hide()
            .siblings('img.addActorFavOff').show();
        } else if (msg.status == 'FAIL') {
          jQuery(origObj)
            .siblings('img.please_wait').hide()
            .siblings('div.add_to_fav_error').show();
        } else {
          jQuery(origObj)
            .siblings('img.please_wait').hide()
            .siblings('div.add_to_fav_error').show();
        }
        return true;
      },
     error: function(reqObj,msg,exceptionObj) {
       jQuery(origObj)
         .siblings('img.please_wait').hide()
         .siblings('div.add_to_fav_error').show();
     }
    });

    return false;
  });

  jQuery("a.removeActorFav").click(function() {
    jQuery(this).hide();
    jQuery(this).siblings('img.please_wait').show();

    var origObj = this;
    jQuery.ajax({
      type: "GET",
      url: "/ajax" + jQuery(origObj).attr('href'),
      dataType: 'json',
      success: function(msg,status) {
        if (msg.status == 'SUCCESS') {
          jQuery(origObj).parent().parent().hide();
        } else if (msg.status == 'FAIL') {
          jQuery(origObj)
            .siblings('img.please_wait').hide()
            .siblings('div.remove_from_fav_error').show();
        } else {
          jQuery(origObj)
            .siblings('img.please_wait').hide()
            .siblings('div.remove_from_fav_error').show();
        }
        return true;
      },
     error: function(reqObj,msg,exceptionObj) {
       jQuery(origObj)
         .siblings('img.please_wait').hide()
         .siblings('div.add_to_fav_error').show();
     }
    });

    return false;
  });

  jQuery("a.changeWidget").click(function() {
    var origObj = this;
    var state=1;
    if ( jQuery(origObj).attr('href').indexOf('state=0') > 0) {
      jQuery(origObj).parent().hide();
      state = 0;
    } else {
      jQuery(this).hide();
      jQuery(this).siblings('img.please_wait').show();
    }

    jQuery.ajax({
      type: "GET",
      url: "/ajax" + jQuery(origObj).attr('href'),
      dataType: 'json',
      success: function(msg,status) {
        if (msg.status == 'SUCCESS') {
          if (state) {
            window.location.href = '/'; 
          }
        } else { // if (msg.status == 'FAIL') {
          if (state) {
            jQuery(origObj).siblings('img.please_wait').hide();
            jQuery(origObj).siblings('div.error').show();
          }
        }
        return true;
      },
     error: function(reqObj,msg,exceptionObj) {
       if (state) {
        jQuery(origObj)
          .siblings('img.please_wait').hide()
          .siblings('div.system_error').show();
       }
     }
    });
   
    return false;
  });

  jQuery("img.loginSubmitBtn").click(function() { 
    jQuery("form.loginForm").submit(); 
  });  

  // any changes on sort buttons will update GDPRF2 cookie and submit form
  jQuery(".sort select").change(function() {
    var id = 'v' + this.id;
    if (this.id == 'sb') { 
      id = 'g' + this.id; 
    } else if (jQuery(this).hasClass('photo_sort')) {
      id='psb';
    } else if (this.id == 'sort_by') { 
      id='asb'; 
    }
  
    updateC(id, jQuery(this).val());
  });

  // Rollover highlighting for the signup page
  // Set up rollovers
  jQuery('#add_upsell').hover(function(){
      jQuery('#add_upsell').addClass('option_hover');
    }, function(){
      jQuery('#add_upsell').removeClass('option_hover');
    });

  jQuery('.plan_1').hover(function(){
      jQuery(this).addClass('option_hover_1');
    }, function(){
      jQuery(this).removeClass('option_hover_1');
    });
  jQuery('.plan_2, .plan_3').hover(function(){
      jQuery(this).addClass('option_hover');
    }, function(){
      jQuery(this).removeClass('option_hover');
    });
  // end highlighting for the signup page

  jQuery('.actor_filter_link').hover( function() {
    if (actor_filter_loading > 0) { return; }
    loadActorFilter();
  }, function() { } );

  jQuery('.actor_filter_link').click( function() {
    jQuery('.actor_filter_box').toggle();
  });


  jQuery('.image_upgrade_link').click(function() {
    jQuery(this).hide();
    jQuery(this).parent().siblings('img.please_wait').show();

    var origObj = this;
    jQuery.ajax({
      type: "GET",
      url: "/ajax/account/change_membership_options?option=IMAGE_UPSELL",
      dataType: 'json',
      success: function(msg,status) {
        if (msg.status == 'ENABLED') {
          window.location = 'http://' + window.location.hostname + '/?sn=57';
        } else {
          window.location = 'http://' + window.location.hostname + '/account/account';
        }
        return true;
      },
      error: function(reqObj,msg,exceptionObj) {
        window.location = 'http://' + window.location.hostname + '/account/account';
      }
    });

    return false;

  });


  setupDescriptionToggle();
});


