// $Id: ajaxsubmit.js,v 1.6 2009/02/19 22:53:21 katbailey Exp $

Drupal.ajaxSubmit = Drupal.ajaxSubmit || {};

/**
 * Attaches the ajaxsubmit behaviour to forms.
 */
Drupal.behaviors.ajaxSubmit = function () {
  $('form.ajaxsubmit:not(.ajaxsubmit-processed)').each(function () {
    var target = document.createElement('div');
    var form = this;
    $(target).addClass('ajaxsubmit-message');
    $(this).before(target);
    var options = {
      dataType: 'json',
      beforeSubmit: function () {
        Drupal.ajaxSubmit.beforeSubmit(target, form);
      },
      success: function (data) {
        Drupal.ajaxSubmit.success(target, data, form);
      },
     complete: function(data, status) {
       Drupal.ajaxSubmit.error(target, form);
       if (status == 'error' || status == 'parsererror') {
         //alert(Drupal.t('An error occurred.'));
         window.location = document.location.href;
       }
     }
    };
    $(this)
      .addClass('ajaxsubmit-processed')
      .ajaxForm(options)
      .find('input[name=ajaxsubmit]')
      .attr('value', '1')
      .end()
      .find('input.form-submit')
      .click(function() {
		var click_id =$(this).attr('id');
		//alert(click_id);
		if(click_id =='edit-submit-1' || click_id =='edit-submit'){
			$(this).hide();
        }		
		if(click_id =='edit-send'){	
			var name =  $('#edit-your-name').val();
			var email_id = $('#edit-friend-email').val();
			var email_val = allthelooks_validateMultiEmail(email_id);	
			//alert(email_val);
			if(name !='' && email_id!='' && email_val !=false){
				$(this).hide();
				$("#popups #popups-body #allthelooks-sendto-friend-form .popup_close_all").css('position','relative');	
				$("#popups #popups-body #allthelooks-sendto-friend-form .popup_close_all").css('left','150px');
            }			
        }
		$(this).addClass('throbbing');
      });
  });
};

/**
 * Tidy up on form submission.
 */
Drupal.ajaxSubmit.beforeSubmit = function (target, form) {
  // Remove any error messages.
  $(':input', form).removeClass('error');
  $(target).html('');
};


/**
 * Handler for successful response.
 */
Drupal.ajaxSubmit.success = function (target, data, form) {
  $('form input.throbbing').removeClass('throbbing');
  $(target).html(data.message);
 
  if (data.errors) {
	  $('.comment_buttons #edit-submit-1').show();
	  $('.comment_buttons #edit-submit').show();
	  $('.comment_buttons #edit-preview').show(); 
	
	 for (var name in data.errors) {
      $('[@name=' + name + ']', form).addClass('error');
    }
  }
  
  // Set preview.
  if (data.preview) {
    $(target).append(data.preview);
  }
  
  // Invoke any callback functions.
  if (data.__callbacks) {
    $.each(data.__callbacks, function(i, callback) {
      eval(callback)(target, data);
    });
  }
  	
  var offset = $(target).offset();
// Commented to avoid scrolling of page on ajax submit
//  window.scrollTo(0, offset.top - 10);

  if($(target).html() == ''){
    window.location = document.location.href;

    // Code Added for url with Hash (#) to refresh
    urlT = document.location.href;
    var hashPos = urlT.indexOf("#nid=");
    if(hashPos != -1){
      var urlC = allthelooks_makeRefreshUrl(urlT);
      window.location = urlC;     
    }
  }

  // Redirect.
  if (data.destination) {
    window.location = Drupal.url(data.destination);
  }
  
  
   
};

/**
* Handler for error.
*/
Drupal.ajaxSubmit.error = function (target, data, form) {
	
 $('.comment_buttons #edit-preview').show();
 //$('.comment_buttons #edit-submit').show();
 $('input.form-submit', form).removeClass('throbbing');
};


function allthelooks_makeRefreshUrl(url)
{

  // The function parse url is defined in "modules\custom\allthelooks_search\js\allthelooks_search_photo.js"
  var temp = parseURL(url);
  var replace_string ='';

  if(temp.params.rl != undefined && temp.params.rl !=''){
    replace_string = 'rl='+temp.params.rl;
  }
  var hashPos = url.indexOf("&rl=");
  if(hashPos == -1){
    hashPos = url.indexOf("#nid=");
  }
  if(hashPos != -1){
    var hashSubStr = url.substring(hashPos);
    var random = Math.ceil(100*Math.random());

    if(replace_string == ''){
      if(url.indexOf("?") != -1){
        var addStr = '&rl='+random+hashSubStr;
      }
      else {
        var addStr = '?rl='+random+hashSubStr;
      }
      var urlResult =  url.replace(hashSubStr, addStr);
    }else{
      var addStr = hashSubStr.replace(replace_string,'rl='+random);
      var urlResult =  url.replace(hashSubStr, addStr);
    }

    return urlResult;
  }
}

