function toggleContact (e) {
  e.stopPropagation();
  
  var $contact = $("#contact");
  $contact.appendTo("#top");
  
  //scroll to top
  if($('body').scrollTop() > 0) {
    $('body').animate({scrollTop:0}, function() {
      $contact.stop().delay(400).slideToggle(300, 'easeOutCubic');
    });    
  } else {
    $contact.stop().delay(400).slideToggle(300, 'easeOutCubic');
  }
  
  return false;

}

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

function emailHide(part1, part2){
	var email = part1 +"@"+part2;
	var str = "<a href='mailto:"+email+"'>"+email+"</a>";
	document.write(str);
}

function isIE() {
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if (browser=="Microsoft Internet Explorer")
	return true;
else
	return false;
}

function iResize(iFrames) {
  
  for (var i = 0, j = iFrames.length; i < j; i++) {
    iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
  }
  
  iFrames.load(function() {
     this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
  });

}

function creditCardTypeFromNumber(num) {
 num = num.replace(/[^\d]/g,'');  // first, sanitize the number by removing all non-digit characters.
 
 if (num.match(/^5[1-5]\d{14}$/)) { return 'mastercard';
 } else if (num.match(/^4\d{15}/) || num.match(/^4\d{12}/)) { return 'visa';
 } else if (num.match(/^3[47]\d{13}/)) { return 'amex';
 } else if (num.match(/^6011\d{12}/)) { return 'discover'; }
 return 'UNKNOWN';
}

$(function() {
	$('#close').click(function () { $("#close").parent().parent().slideUp("fast"); }); 
	$('.flash, .flash_error').slideDown("fast");
	
	$(".fancy").fancybox({overlayColor: '#000'});
  
  $("#top").hover(
    function(){
      $(".main:not(.on) a").stop().animate({color: "#808080"}, 200);
      if(!$.browser.msie){        
        $("#menuLogoColor").animate({opacity : 0.5}, 200);
      }
      $(".icon span").animate({opacity : 1}, 200);
    }, function (){
      $(".main:not(.on) a").stop().animate({color: "#bbb"}, 200);
      if(!$.browser.msie){        
        $("#menuLogoColor").animate({opacity : 0}, 200);
      }
    }
  );
  
  
  var t = setTimeout(function(){$('#menuSub').animate({"top":"3", "opacity":"1"}, 300);},500);
  
  $("#mce-EMAIL").one('focus', function(){
    //slide up newsletter
    $('#footer .grid_3.omega p').slideUp("easeOutExpo");
    // slide down fields
    $('#mce-hidden').delay(500).slideDown("easeOutExpo");
  });
  
  $(".contactLink").click(function(e){toggleContact(e)});
  
  //clears default values
  $(":input[placeholder]").placeholder();
  
  // wrap number li's for bolded number
  $(".section ol li").each(function (index) {
    $(this).wrapInner("<span />");
  });
  
  $(".capitalize").namely();
      
  $(".submitter").click(function(){ $(this).closest('form').submit();});
  
  $("#new_order .submitter").click(function(){ $(this).html("Processing Order</strong><br/>[Do Not Click Again]"); $(this).unbind("click"); });

  $("select.full").selectmenu({width: '100%'});
  $("select.large").selectmenu({width: 352});
  $("select.medium").selectmenu({width: 252});
  $("select.small").selectmenu({width: 102});
  $("select.xsmall").selectmenu({width: 60});
  $("select").selectmenu();
  
  $("#menuSearch").click(function(){
    //if open, submit
    if($("#menuSearch.open").length) {
      $("#search form").submit();
    } else {
      $(this).parent().addClass("open");
      $("#search").show(100);
      $("#search-q").focus();
    }
  });
  
	$('li.sublink').hover(
		function() { $('ul', this).fadeIn("fast"); $('.main-link', this).addClass("main-link-on"); },
		function() { $('ul', this).fadeOut("fast"); $('.main-link', this).removeClass("main-link-on"); }
	);
		
	$("#programs-popup").click(function(){$("#programs").fadeIn(); });
	$("#programs").mouseleave(function(){$(this).fadeOut();});
	
	$("#content-form").submit(function(){
		$.post($(this).attr('action')+".js", $(this).seralize(), null, "script");
		return false;
	});
		
	$("#contactForm").submit(function(){
	  var name = $("#contact_name").val();
	  var email = isValidEmailAddress($("#contact_address").val());
	  var error = false;
	  if(name === null || name === "") {
	    $("#contact_name").addClass("errorField");
      error = true;
	  } else {
	    $("#contact_name").removeClass("errorField");	    
	  }
	
	  if (!email) {	    
	    $("#contact_address").addClass("errorField");
	    return false;
	  } else {
	    $("#contact_address").removeClass("errorField");	    
	  }
	  
	  if(error) {
	    return false;
	  }
	  return true;
	});
	
	
	// Common Order Functions
	
	$(".order_contact_name").blur(function(){
	  var name = $(this).val();
	  name = name.split(" ", 2);
	  fname = name[0];
	  lname = name[1];
	  $(".billing_first_name, .shipping_first_name").val(fname);
	  $(".billing_last_name, .shipping_last_name").val(lname);
	});
	
	$(".cc").click(function(){
	  var id = $(this).attr("id");
	  $(".cc").each(function(){
	    if($(this).attr("id") != id){
	      $(this).fadeTo("fast", 0.3);
	    } else {
	      $(this).fadeTo("fast", 1);	      
	    }
	  });
	});
	
	$("#cc-num").keyup(function(){
	  type = creditCardTypeFromNumber($(this).val());
	  if(type != "UNKNOWN"){
	    typeID = "cc-"+type;
	    $(".cc").each(function(){
  	    if($(this).attr("id") != typeID){
  	      $(this).fadeTo("fast", 0.3);
  	    } else {
  	      $(this).fadeTo("fast", 1);	      
  	    }
  	  });
	  }
	});
	
});
