var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

var $j = jQuery.noConflict();
$j(function () {

	jQuery.validator.addMethod("alphanumeric", function(value, element) {
		return this.optional(element) || /^\w+$j/i.test(value);
	}, "Letters, numbers,or underscores only please");
	
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number");

	$j("#aspmnform").validate();
	
	$j(function() {
		$j("#datepicker").datepicker();
	});
	
if ( $j("#show_requestor2").val() == "0" ) {
	$j("#requestor2").hide();
}

$j("#addRequestor").click(function() {
    $j('#requestor2').slideToggle();
}); 
$j("#remove").click(function() {
    $j('#requestor2').slideToggle();
    $j("#requestor2 input[type=text]").val("");
    $j("#requestor2 input[type=radio]").removeAttr("checked");
    $j("#requestor2 span.error").hide();
});

$j("div.background2").css("opacity","0");
$j("div.background3").css("opacity","0");
$j("div.background4").css("opacity","0");
$j("div.background5").css("opacity","0");

	  var temp1 = $j("#informationTitle").html();
	  var temp2 = $j("#informationTitle2").html();
	  var temp3 = $j("#informationTitle3").html();
	  var temp4 = $j("#informationTitle4").html();

	window.setTimeout(function() {
		rotateImages1();
	}, 1);
	
	function rotateImages1() {
		setTimeout(function() {
			  $j("#informationTitle").html(temp2);
			  $j("div.background").animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background3").animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background4").animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background2").animate({ 
				opacity: 1
			  }, 700 );
			  rotateImages2();
		}, 6000);
	};
	
	function rotateImages2() {
		setTimeout(function() {
			  $j("#informationTitle").html(temp3);
			  $j("div.background").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background3").stop().animate({ 
				opacity: 1
			  }, 700 );
			  $j("div.background4").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background2").stop().animate({ 
				opacity: 0
			  }, 700 );
			  rotateImages3();
		}, 6000);
	};
	
	function rotateImages3() {
		setTimeout(function() {
			  $j("#informationTitle").html(temp4);
			  $j("div.background").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background3").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background4").stop().animate({ 
				opacity: 1
			  }, 700 );
			  $j("div.background2").stop().animate({ 
				opacity: 0
			  }, 700 );
			  rotateImages4();
		}, 6000);
	};
	
	function rotateImages4() {
			setTimeout(function() {
			  $j("#informationTitle").html(temp1);
			  $j("div.background").stop().animate({ 
				opacity: 1
			  }, 700 );
			  $j("div.background3").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background4").stop().animate({ 
				opacity: 0
			  }, 700 );
			  $j("div.background2").stop().animate({ 
				opacity: 0
			  }, 700 );
			  rotateImages1();
		}, 6000);
	};
	
	//hide the element with id "feast"
	$j("div.feast").hide();
	
	//slides the element with class "feast" when img with class "expandable" is clicked
	$j("img.expandable").click(function()
	{
		if($j('img.expandable').is('.collapsed')) {
			$j('img.expandable').attr('src','/images/collapse.png');
			$j('img.expandable').removeClass('collapsed');
			$j('img.expandable').addClass('expanded');
		}
		else {
			$j('img.expandable').attr('src','/images/expand.png');
			$j('img.expandable').removeClass('expanded');
			$j('img.expandable').addClass('collapsed');
		}
		$j('div.feast').slideToggle(900);
	});
	
	$j("ul.sf-menu").superfish(); 
	
	$j('a[rel="external"]').click( function() {
		window.open( $j(this).attr('href') );
		return false;
	});	
	
	$j('#searchArea span.printLink').click(function() {
		$j("div.feast").show();
		window.print();
		return false;
	});	
	
	//grab all the anchor tag with rel set to shareit
	$j('a[rel=shareit], #shareit-box').mouseenter(function() {		
		
		//get the height, top and calculate the left value for the sharebox
		var height = $j(this).height();
		var top = $j(this).offset().top;
		
		//get the left and find the center value
		var left = $j(this).offset().left + ($j(this).width() /2) - ($j('#shareit-box').width() / 2);		
		
		//grab the href value and explode the bar symbol to grab the url and title
		//the content should be in this format url|title
		var value = $j(this).attr('href').split('|');
		
		//assign the value to variables and encode it to url friendly
		var field = value[0];
		var url = encodeURIComponent(value[0]);
		var title = encodeURIComponent(value[1]);
		
		//assign the height for the header, so that the link is cover
		$j('#shareit-header').height(height);
		
		//display the box
		$j('#shareit-box').show();
		
		//set the position, the box should appear under the link and centered
		$j('#shareit-box').css({'top':top, 'left':left});
		
		//assign the url to the textfield
		$j('#shareit-field').val(field);
		
		//make the bookmark media open in new tab/window
		$j('a.shareit-sm').attr('target','_blank');
		
		//Setup the bookmark media url and title http://www.facebook.com/sharer.php?u=http://davidwalsh.name/dw-content/digg-share.php?My%20Story%201
		$j('a[rel=shareit-mail]').attr('href', 'mailto:?subject=' + title);
		$j('a[rel=shareit-delicious]').attr('href', 'http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=' + url + '&title=' + title);
		$j('a[rel=shareit-designfloat]').attr('href', 'http://www.designfloat.com/submit.php?url='  + url + '&amp;title=' + title);
		$j('a[rel=shareit-digg]').attr('href', 'http://digg.com/submit?phase=2&amp;url=' + url + '&amp;title=' + title);
		$j('a[rel=shareit-stumbleupon]').attr('href', 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title);
		$j('a[rel=shareit-twitter]').attr('href', 'http://twitter.com/home?status=See this page on the Ameritox Site: ' + url);
		$j('a[rel=shareit-facebook]').attr('href', 'http://www.facebook.com/sharer.php?u=' + url);
		
	});

	//onmouse out hide the shareit box
	$j('#shareit-box').mouseleave(function () {
		$j('#shareit-field').val('');
		$j(this).hide();
	});
	
	//hightlight the textfield on click event
	$j('#shareit-field').click(function () {
		$j(this).select();
	});
	
	$j("#ui-datepicker-div").hide();
		
});
