// post-submit callback 
   function showResponse(responseText, statusText)  { 
} 




function gallery_shuffle(no_of_images) {
	// decrements the number for the function to be clearer
	no_of_images = no_of_images - 1;
	all_images = $('.gallery li');
	all_images.hide().removeClass('even');
	sel_images = new Array();
	// picks random numbers for the images
	for (i=0; i<=no_of_images; i++) {
		do {
			new_rand = Math.round(Math.random() * (all_images.size() - 1));
		}	while (jQuery.inArray(new_rand, sel_images) >= 0);
		sel_images[i] = new_rand;
	}
	sel_images.sort();
	// shows the lucky images
	for (i=0; i<=no_of_images; i++) {
		all_images.eq(sel_images[i]).show().addClass('visible');
	}
	$('.gallery .visible:odd').addClass('even');
}



	
function init_gallery() {
   var images = new Array();
   $('.gallery img').each(function(i) {
      var imagesrc = $(this).attr('src');
      var imagename = imagesrc.replace("sample","large_sample");
      images[i] = new Image();
      images[i].src = imagename;
   });

   $('.gallery li').each(function(i) {
      $(this).hover(function() {									
	 var captiontext = $(this).children('img').attr('alt');
 	 $('#preview p').text(captiontext);
	 $('#preview').show();
	 $().mousemove(function(mouse) {
	    var x = getViewPort();
	    var y = getScroll();
	    if (mouse.pageY+images[i].height-150 < x + y - 80) {
	       $('#preview').css({top:mouse.pageY-150, left:mouse.pageX+20});
	    } else {
	       $('#preview').css({top:y+x-images[i].height-80, left:mouse.pageX+20});	
	    }
   }); 
   
   $('#preview').css({width:images[i].width});
   
   $('#preview img').attr('src', images[i].src); }, function() {
      $('#preview').hide();	
   });
});
}

function getViewPort() {
   var viewportheight = 0;
   if (typeof window.innerWidth != 'undefined') {
      viewportheight = window.innerHeight
   } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
      viewportheight = document.documentElement.clientHeight
   } else {
      viewportheight = document.getElementsByTagName('body')[0].clientHeight
   }
   return viewportheight;
}

function getScroll() {
   var scrollheight = 0;
   if( typeof( window.pageYOffset ) == 'number' ) {
      scrollheight = window.pageYOffset;
   } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      scrollheight = document.body.scrollTop;
   } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      scrollheight = document.documentElement.scrollTop;
   }
   return scrollheight;
}

	

function init_quote_rotate() {
   var howmayQuotes = $(".quote").size();
   $(".quote").hide();
   var ranNum= Math.floor(Math.random() * howmayQuotes);
   $(".quote:nth("+ ranNum +")").show();
}

/*************************************************************************
 * validateContactForm()
 * Purpose: 	Validates form elements on about_us-contact.html.
 * 		Required fields include 'firstname', 'lastname', and a form of
 *		contact (phone or email).  Makes use of the jQuery Form Plugin.
 * Input: 	formData, jqForm, options
 * Returns:	true if validation successful, false otherwise
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function validateContactForm(formData, jqForm, options) { 
    	resetContactValidation();
	var error = 0;
    	// jqForm is a jQuery object which wraps the form DOM element 
	var form = jqForm[0];
	var firstNameValue = $('#firstname').val();
	var lastNameValue = $('#lastname').val(); 
     	var phoneChecked = form.phoneCheckbox.checked;
	var emailChecked = form.emailCheckbox.checked;
	
    	if (!firstNameValue) {
	   $('#firstname').addClass('error');
	   error = 1;
	}
	if (!lastNameValue) { 
         $('#lastname').addClass('error'); 
    	   error = 1;
	}
	if (phoneChecked) {
	   if (!($('#phone').val())) {
		$('#phone').addClass('error');
	   	error = 1;   
	   }
	} 
	if (emailChecked) {
	   contactSubmitted = 1;
 	   if (!($('#email').val())) {
		alert('No email value entered!');
		$('#email').addClass('error');
		error = 1;
	   }
	} 
	if (!phoneChecked && !emailChecked) {
	   $('#phone').addClass('error');
	   $('#email').addClass('error');
	   error = 1;
	}
	if (!error) {
	   $('#form').remove();
	   $('p.form-mess').text('If you have any questions please contact our headquarters at info@pyrotecnico.com or at 800. 854. 4705.').removeClass('error');;
	   $('.titl-contactuswe').removeClass('titl-sorrybutthere').addClass('titl-thankyou');
         return true;
      } else {
 	   $('p.form-mess').text('Please provide all the required fields').addClass('error');
	   $('.titl-contactuswe').addClass('titl-sorrybutthere');
	   error = 1;
	   return false;
      }
}

/*************************************************************************
 * resetContactValidation()
 * Purpose: 	Removes the error class from the form elements.
 * Input: 	none
 * Returns:	none
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function resetContactValidation() {
   $('#firstname').removeClass('error');
   $('#lastname').removeClass('error');
   $('#zipcode').removeClass('error');
   $('#phone').removeClass('error');
   $('#email').removeClass('error');
}


/*************************************************************************
 * validateTrainingForm()
 * Purpose: 	Validates form elements on training-certified.html.
 * 		Required fields include 'firstname', 'lastname', 'zipcode' and 
 		a form of contact (phone or email).  Makes use of the jQuery Form Plugin.
 * Input: 	formData, jqForm, options
 * Returns:	true if validation successful, false otherwise
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function validateTrainingForm(formData, jqForm, options) {
	resetTrainingValidation();
	var error = 0;
	var form = jqForm[0];
	var firstNameValue = $('#firstname').val();
	var lastNameValue = $('#lastname').val(); 
 	var zipCodeValue = $('#zipcode').val();
 	var stateValue = $('#state').val();
    var trainingDate = $('#training_date').val();
 	var phoneChecked = form.phoneCheckbox.checked;
	var emailChecked = form.emailCheckbox.checked;
	
    if (!firstNameValue) {
	   $('#firstname').addClass('error');
	   error = 1;
	}
	if (trainingDate == 0) {
	   $('#training_date').addClass('error');
	   error = 1;
	}
	if (!lastNameValue) { 
         $('#lastname').addClass('error'); 
    	   error = 1;
	}
	if (!stateValue) { 
         $('#state').addClass('error'); 
           error = 1;
	}
	if (!zipCodeValue) {
	   $('#zipcode').addClass('error');
	   error = 1;
	} else {
	   // check to make sure it is numeric
	   var numberCheck = isNumeric(zipCodeValue);
	   if (!numberCheck) {
		$('#zipcode').addClass('error');
		error = 1;
	   }
	}
	if (phoneChecked) {
	   if (!($('#phone').val())) {
		$('#phone').addClass('error');
	   	error = 1;   
	   }
	} 
	if (emailChecked) {
	   contactSubmitted = 1;
 	   if (!($('#email').val())) {
		$('#email').addClass('error');
		error = 1;
	   }
	} 
	if (!phoneChecked && !emailChecked) {
	   $('#phone').addClass('error');
	   $('#email').addClass('error');
	   error = 1;
	}
	var attendeeCheck = checkAdditionalAttendees();
	if (attendeeCheck == 1) {
	   error = 1;
	}
	if (!error) {
	   $('#form').remove();
	   $('p.form-mess').text('If you have any questions please contact our headquarters at info@pyrotecnico.com or at 800. 854. 4705.').removeClass('error');;
	   $('.titl-sorrybutthere').removeClass('titl-sorrybutthere').addClass('titl-thankyou');
	   $('.titl-becomea2').removeClass('titl-becomea2').addClass('titl-thankyou');
         return true;
      } else {
 	   $('p.form-mess').text('Please provide all the required fields').addClass('error');
	   $('.titl-becomea2').removeClass('titl-becomea2').addClass('titl-sorrybutthere');
	   error = 0;
	   return false;
      }
}

/*************************************************************************
 * isNumeric()
 * Purpose: 	Determines if the provided string is numeric.
 * Input: 	sText - the string to check
 * Returns:	true if numeric, false otherwise
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function isNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

/*************************************************************************
 * checkAdditionalAttendess()
 * Purpose: 	Validates the additional attendee first and last name form
 *		elements.  If a first name is entered without a last name 
 *		or vice versa that pair is tagged as an error.
 * Input: 	none
 * Returns:	true if valid, false otherwise
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function checkAdditionalAttendees() {
   var error = 0;
   var firstName = '#firstname_';
   var lastName = '#lastname_';
   
   for (i=0; i<7; i++) {
      firstName = firstName + i;
      lastName = lastName + i;
      if ($(firstName).val()) {
         if (!($(lastName).val())) {
	    $(lastName).addClass('error');
	    error = 1;
	 }   
      }
      firstName = '#firstname_';
      lastName = '#lastname_';
   }
   return error;
}

/*************************************************************************
 * resetTrainingValidation()
 * Purpose: 	Removes the error class from the form elements.
 * Input: 	none
 * Returns:	none
 * Date:	07/29/2007 by Kyle Cunningham <kyle@lottershelly.com>
 *************************************************************************/
function resetTrainingValidation() {
   $('#firstname').removeClass('error');
   $('#lastname').removeClass('error');
   $('#state').removeClass('error');
   $('#zipcode').removeClass('error');
   $('#phone').removeClass('error');
   $('#email').removeClass('error');
   
   var firstName = '#firstname_';
   var lastName = '#lastname_';
   
   for (i=0; i<7; i++) {
      firstName = firstName + i;
      lastName = lastName + i;
      $(firstName).removeClass('error');
      $(lastName).removeClass('error');
      firstName = '#firstname_';
      lastName = '#lastname_';
   }
}

/*******************************************************************
 * browserCheck()
 *------------------------------------------------------------------
 * Purpose:  This method is used to interrogate the browser and
 *	     determine which plugins are installed.  Uses javascript
 *	     contained in watch.shtml.
 * Dependency:  javascript code in watch.shtml.
 * Input(s): None.
 * Created:  March 28, 2007
 * Author:  Kyle Cunningham <kyle@lottershelly.com>
 ******************************************************************/
function browserCheck() {
	var browserName = navigator.appName ;
	var browserVersion = navigator.userAgent;
	var versionNumber;
	var regex = new RegExp('/');
	if (navigator.appName == 'Microsoft Internet Explorer') {
  		var versionID = browserVersion.indexOf('MSIE');
		versionNumber = browserVersion.substr(versionID,8);
	} else {
		var versionID = browserVersion.search(regex);
		versionNumber = browserVersion.substring(versionID+1);
	}
	if ( !(navigator.javaEnabled()) ) {
  		java="No" ;
	} else {
  		java="Yes" ;
	}
	var infoString = 'Browser Name: ' + browserName;
	infoString += '\nPlatform Name: ' + navigator.platform;
	infoString += '\nBrowser Version: ' + browserVersion;
	infoString += '\nVersion Number: ' + versionNumber;
	infoString += '\nJava enabled: ' + java;
	var quickTimeIsInstalled = detectQuickTime();
	var videoDisplay = document.getElementById('video_display');
	var mov_html = '<embed src="http://www.lucasware.com/media/SouthernWineQT.mov"' + 
				  'autostart="true" WIDTH="350" HEIGHT="350"/ align="left" style="margin: 0px 0px 0px -13px"/>' 
	var wmv_html = '<embed src="http://www.lucasware.com/media/southernwine.wmv"' + 
				  'autostart="true" WIDTH="350" HEIGHT="350"/ align="left"/>'
	var activeXIsInstalled = detectWindowsMedia();
	if (activeXIsInstalled) {
		videoDisplay.innerHTML = wmv_html;
		return;
	}
	if(quickTimeIsInstalled) {
		videoDisplay.innerHTML = mov_html;
		return;
	}
	else {
		var noPluginHTML = '<div id="watch_jennifer_text">' + 
			   'Your computer must have Windows Media Player or QuickTime installed to properly view' + 
			   ' the video. You can download either of these free players using the links below.' + 
			   '</div>' +
			   '<br>' +
			   '<table border="0" cellspacing="0" cellpadding="0">' +
			      '<tr>' + 
				    '<td><a href="http://www.microsoft.com/windows/windowsmedia/player/10/default.aspx" target="_blank">' +
					'Download Windows Media Player</a></td>' +
				  '</tr>' +						
				  '<tr>' +	
					'<td><a href="http://www.apple.com/quicktime/download/win.html" target="_blank">Download Quicktime Player</a></td>' +
				  '</tr>' +
			   '</table>';
		videoDisplay.innerHTML = noPluginHTML;
		return;
	}
} // end browserCheck() 


jQuery(function($) {
   init_gallery();
   init_quote_rotate();
});
