$(document).ready(function(){
  /*setzorder();*/
  rotatePicsLeft(1);
  setTimeout(function() {rotatePicsCenter(1);}, 4000);
  setTimeout(function() {rotatePicsRight(1);}, 8000);
});

function rotatePicsLeft(currentPhoto) {
  var numberOfPhotos = $('#slidesleft img').length;
  currentPhoto = currentPhoto % numberOfPhotos;
	
  $('#slidesleft img').eq(currentPhoto).fadeOut(4000, function() {
		// re-order the z-index
    $('#slidesleft img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePicsLeft(++currentPhoto);}, 8000);
  });
}

function rotatePicsCenter(currentPhoto) {
  var numberOfPhotos = $('#slidescenter img').length;
  currentPhoto = currentPhoto % numberOfPhotos;
	
  $('#slidescenter img').eq(currentPhoto).fadeOut(4000, function() {
	// re-order the z-index
    $('#slidescenter img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePicsCenter(++currentPhoto);}, 8000);
  });
}

function rotatePicsRight(currentPhoto) {
  var numberOfPhotos = $('#slidesright img').length;
  currentPhoto = currentPhoto % numberOfPhotos;
	
  $('#slidesright img').eq(currentPhoto).fadeOut(4000, function() {
	// re-order the z-index
    $('#slidesright img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePicsRight(++currentPhoto);}, 8000);
  });
}


function setzorder() {
  var numberOfPhotos = $('#slideshow img').length;
  $('#slideshow div').each(function(i) {
	$(this).css(
	  'zIndex', (numberOfPhotos - i) 
	);
  });
}

function MM_preloadImages() { //v3.0
  var d=document; 
  if(d.images) { 
	if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
	for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ 
	  d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
	}
  }
}

// New Customer Account Form Processing (login.php)

$(function(){
    $("#usebilling").change(function() {
        if ($("#usebilling:checked").length > 0) {
            bindAddr();
        } else {
            unbindAddr();
        }
    });
});

var bindAddr = function() {
    // First copy values
    $("input[name='firstnameSA']").val($("input[name='firstnameBA']").val());
    $("input[name='lastnameSA']").val($("input[name='lastnameBA']").val());
    $("input[name='addressSA']").val($("input[name='addressBA']").val());
    $("input[name='address2SA']").val($("input[name='address2BA']").val());
    $("input[name='citySA']").val($("input[name='cityBA']").val());
    $("input[name='zipSA']").val($("input[name='zipBA']").val());
    $("input[name='emailSA']").val($("input[name='emailBA']").val());
    $("input[name='phoneSA']").val($("input[name='phoneBA']").val());
    $("input[name='phextSA']").val($("input[name='phextBA']").val());
    $("input[name='faxSA']").val($("input[name='faxBA']").val());

    $("select[name='companySA']").val($("select[name='companyBA']").val());
    $("select[name='stateSA']").val($("select[name='stateBA']").val());
    $("select[name='countrySA']").val($("select[name='countryBA']").val());
	
    // Then bind fields
    $("input[name='firstnameBA']").keyup(function() {
        $("input[name='firstnameSA']").val($(this).val());
    });
    $("input[name='lastnameBA']").keyup(function() {
        $("input[name='lastnameSA']").val($(this).val());
    });
    $("input[name='addressBA']").keyup(function() {
        $("input[name='addressSA']").val($(this).val());
    });
    $("input[name='address2BA']").keyup(function() {
        $("input[name='address2SA']").val($(this).val());
    });
    $("input[name='cityBA']").keyup(function() {
        $("input[name='citySA']").val($(this).val());
    });
    $("input[name='zipBA']").keyup(function() {
        $("input[name='zipSA']").val($(this).val());
    });
    $("input[name='emailBA']").keyup(function() {
        $("input[name='emailSA']").val($(this).val());
    });
    $("input[name='phoneBA']").keyup(function() {
        $("input[name='phoneSA']").val($(this).val());
    });
    $("input[name='phextBA']").keyup(function() {
        $("input[name='phextSA']").val($(this).val());
    });
    $("input[name='faxBA']").keyup(function() {
        $("input[name='faxSA']").val($(this).val());
    });
	
    $("select[name='salutationBA']").change(function() {
        $("select[name='salutationSA']").val($(this).val());
    });
    $("select[name='stateBA']").change(function() {
        $("select[name='stateSA']").val($(this).val());
    });
    $("select[name='countryBA']").change(function() {
        $("select[name='countrySA']").val($(this).val());
    });
};

var unbindAddr = function() {
    $("input[name='firstnameBA']").unbind("keyup");
    $("input[name='lastnameBA']").unbind("keyup");
    $("input[name='addressBA']").unbind("keyup");
    $("input[name='address2BA']").unbind("keyup");
    $("input[name='cityBA']").unbind("keyup");
    $("input[name='zipBA']").unbind("keyup");
    $("input[name='emailBA']").unbind("keyup");
    $("input[name='phoneBA']").unbind("keyup");
    $("input[name='phextBA']").unbind("keyup");
    $("input[name='faxBA']").unbind("keyup");

    $("select[name='salutationBA']").unbind("change");
    $("select[name='stateBA']").unbind("change");
    $("select[name='countryBA']").unbind("change");
};

