var currentProductImage = null;
var timeOutArr = Array(); // used to hold setTimeout's or setInterval's .. just for easy storage and retrival..

function subFormOnEnter( ev, frm ) {
  var keycode;
  if ( window.event ) {
    keycode = window.event.keyCode;
  } else if ( ev ) {
    keycode = ev.which;
  } else {
    return true;
  }
  if ( keycode == 13 ) {
    frm.submit();
  }
}

function getHttpObject() {
  if ( typeof XMLHttpRequest != 'undefined' ) {
    return new XMLHttpRequest();
  }

  try {
    return new ActiveXObject( "Msxml2.XMLHTTP" );
  } catch ( e ) {
    try {
      return new ActiveXObject( "Microsoft.XMLHTTP" );
    } catch ( e ) {
    }
  }
  return false;
}

function getMouseX( ev ) {
  var tempX = 0;
  var IE = document.all ? true : false;
  if ( IE ) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollLeft;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = ev.pageX;
  }
  if ( tempX < 0 ) {
    tempX = 0
  }
  return tempX;
}

function getMouseY( ev ) {
  var tempY = 0;
  var IE = document.all ? true : false;
  if ( IE ) { // grab the x-y pos.s if browser is IE
    tempY = event.clientY + document.documentElement.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempY = ev.pageY;
  }
  if ( tempY < 0 ) {
    tempY = 0
  }
  return tempY;
}

// JavaScript Document
function isNumeric( val ) {
  var re = /^[0-9_]+$/;
  return re.test( val );
}

function isAlpha( val ) {
  var re = /^[A-Za-z]+$/;
  return re.test( val );
}

function isEmail( val ) {
  if ( val.search( /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/ ) != -1 ) {
    return true;
  } else {
    return false;
  }
}

function submitContactForm() {
  var ff = document.form_contact;
  var first = ff.first.value;
  var last = ff.last.value;
  //var company = ff.company.value;
  //var position = ff.position.value;
  var phone = ff.phone.value;
  var email = ff.email.value;
  var message = ff.message.value;
  first = encodeURIComponent( first.trim() );
  last = encodeURIComponent( last.trim() );
  //company = encodeURIComponent(company.trim());
  phone = encodeURIComponent( phone.trim() );
  //position = encodeURIComponent(position.trim());
  email = encodeURIComponent( email.trim() );
  message = encodeURIComponent( message.trim() );
  if ( first == "" || last == "" || message == "" || email == "" ) {
    alert( "please check required fields" );
    return;
  }
  var params = "first=" + first + "&last=" + last + "&phone=" + phone + "&email=" + email + "&message=" + message;
  var http = getHttpObject();
  http.open( "POST", "/ajax/contact.php", true );
  http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
  http.onreadystatechange = function() {
    if ( http.readyState == 4 ) {
      alert( http.responseText );
    }
  }
  http.send( params );
}

function showNews( val ) {
  document.cookie = "treeview=" + val;
  window.location = "/?page=17";
}

function parse_css_bg_url( str ) {
  var output = str.replace( /^url:?\(("|')?/i, "" ).replace( /("|')?\)$/i, "" ).replace( /^http:\/\//i, "" );
  var pathStart = output.indexOf( "/" );
  if ( pathStart !== -1 && pathStart !== false ) {
    output = output.substr( pathStart );
  }
  return output;
}

function init_home_bg_crossfade() {
  // simulate php spewing imgs to fade around
  // notice they're in reverse order
  var img_srcs = Array( "yellow_porsche3.jpg", "white_porsche.jpg", "silver_porsche_2.jpg", "red_porsche.jpg", "racing.jpg", "yellow_porsche2.jpg", "black_porsche3.jpg", "black_porsche4.jpg", "black_porsche.jpg" );
  $( img_srcs ).each( function( this_idx, this_val ) {
    $( "#bg_hold_and_load" ).prepend( '<img class="bg_to_crossfade" src="/images/' + this_val + '">' );
  } );
  $( "#bg_hold_and_load" ).children( "img:first" ).addClass( "bg_spalsh_top" );
  // end of simulation.

  var ani_time_wait = 8000; //  2 sec -- controls how fast things take UNTIL they begin to fade in.
  var ani_time_in = 2000; // 2 sec -- controls how fast the ani speed is.
  // keep in mind that the wait time counts against the ani time. if both are 2 sec, then it's constinatly animating.
  // possible fix to deal with the wrapper img bg img.

//var splash_src = parse_css_bg_url( $( "#bg_hold_and_load" ).children( "div" ).css( "background-image" ) ); // old photo from the wrapper.
  var splash_src = $( "#bg_hold_and_load" ).children( ":first" ).attr( "src" ); // first photo from the back-end
  //$( "#bg_hold_and_load" ).prepend( '<img src="' + splash_src + '" class="bg_spalsh_top bg_to_crossfade">' );
  $( "#bg_hold_and_load" ).children( "div" ).css( { // the wrapper bg img appears ontop of the otehr bg's to fade around.
    "background-image" : "none"
  } );
  $( '<img id="bg_splash_bottom" src="' + splash_src + '" class="bg_to_crossfade">' ).insertAfter( ".bg_to_crossfade:last" );
  var img_stack = $( ".bg_to_crossfade:not(:first)" );
  var img_idx = 0;
  if ( img_stack.length > 2 ) { // did we get only 1 img (and it's clone)? don't fade it around then...
    timeOutArr["bg_cf_ani"] = setInterval( function() {
      if ( img_idx == img_stack.length ) {
        $( ".bg_to_crossfade:not(:first)" ).css( {
          "display" : "none"
        } );
        img_idx = 0;
      }
      $( img_stack[ img_idx++ ] ).fadeIn( ani_time_in );
    }, ani_time_wait );
  }
}
