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;
}

jQuery(function() {

	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});	
	
	jQuery.validator.addMethod("alphanumeric", function(value, element) {
		return this.optional(element) || /^\w+$/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");

	$("#aspmnform").validate();
		
	//select all the a tag with name equal to modal
	$('a[rel="modal"]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
		$(id).addClass("window");
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(500);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var windowTop = $(window).scrollTop();
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  windowTop);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		/*$("#").replaceWith($("#"));
		$(id).appendTo('#viewport');
		$('#viewport').fadeIn(1000);*/
		$(id).fadeIn(1000);
	
	});
	
	//if close button is clicked
	$('img.close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();//.remove();
		$('#frame1').html('');
		$('#frame1').html('<iframe src="player-1.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
		$('#frame2').html('');
		$('#frame2').html('<iframe src="player-2.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
		$('#frame3').html('');
		$('#frame3').html('<iframe src="player-3.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();//.remove();
		$('#frame1').html('');
		$('#frame1').html('<iframe src="player-1.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
		$('#frame2').html('');
		$('#frame2').html('<iframe src="player-2.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
		$('#frame3').html('');
		$('#frame3').html('<iframe src="player-3.php" scrolling="no" frameborder="0" width="640" height="480"></iframe>');
	});			

});
