﻿/*	Author:	Michael Reid 
	Notes:	
	Updates:
			1-5-2011  :  Created proof of concept advantage calculator in pure js.
			1-6-2011  :	 Rebuilt adv calc function (proof of concept) using jQuery.
			1-7-2011  :	 Added error testing and css classes to output.
			1-12-2011 :  Added single level drop down menu function.
         1-24-2011 :  Added functionality to adv calc function.
         1-27-2011 :  Rocked the casbah... and finished adv calc.
*/

$(document).ready(function() {

   $(document).ready(function(){
      // marquee awesome
      $('marquee').marquee('superAwesomeMarquee').mouseover(function(){
         $(this).trigger('stop');
      }).mouseout(function(){
         $(this).trigger('start');
      });
   });


	// drop down menu
	$(function(){
		$("header nav ul li").hover(function(){
			$(this).addClass("hover");
			$('ul',this).css('visibility', 'visible');
		}, function(){
			$(this).removeClass("hover");
			$('ul',this).css('visibility', 'hidden');
		});
	});
	
	// allow for drop-down menu to display over youtube iframes.
	// !IMPORTANT GOTCHA: script can break non-youtube iframes!
	$('iframe').each(function(){
		var url = $(this).attr("src");
		$(this).attr("src",url+"?wmode=transparent");
	});
	
	
	if ( document.getElementById('newcalc') ) {
		newCalc();
	}
	
	// validate contact submit page
	if ( document.getElementById('contact') ) {
		$('form').validate({
			rules: {
				name: "required",
				title: "required",
				email: {
					required: true,
					email: true
				},
				referral: "required"
			},
			messages: {
				name: "Please enter your full name",
				title: "Please enter your job title",
				email: "Please enter a valid email address",
				referral: "Please choose how you heard about PowerGrit"
			}
		});
	}
});

function newCalc() {
	// Dialog Initialization			
	$('div.dialog').dialog({
		autoOpen: false,
		width: 600,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}
		}
	});

	// Dialog Click Binding
	$('a[rel="dial"]').click( function(){ // binds click event to anchors with id: 'thisId'
		$('#' + $(this).attr('id') + '_dialog').dialog('open'); // opens dialog with id: 'thisId_dialog'
	});
	
	// Calculator
	
	$(':submit').click(function(){
		var input = $('input:text'); // bind all DOM lookups to input array
		var ohnoes = false;
		// iterate over input array, converting valids string to a floating point number 
		input.each(function(i) {
			if ( isNaN(this.value) || this.value === '' ) {
				ohnoes = true; // triggers warning
			}
			else {
				input[i] = parseFloat(this.value);
			}
		});
		if ( ohnoes ) {
			$('#error').show();
			$('#save').slideUp();
		}
		else {
			// do the calculations
			input[0] = input[0]/60; // convert from minutes to hours
			input[2] = input[2]/60; // convert from minutes to hours
			input[4] = input[4]/60; // convert from minutes to hours
			var jobs =  input[6];
			var totalHours =  input[0] + input[2] + input[4];
			var totalRates = (input[0] * input[1]) + (input[2] * input[3]) + (input[4] * input[5]);
			var savingHours  = ( jobs * totalHours ).toFixed(2);
			var savingAnnual = ( jobs * totalRates ).toFixed(2);
			$('#savingHours').html(savingHours);
			$('#savingAnnual').html(savingAnnual);
			$('#error').hide(); // hide error in case previous click threw one.
			$('#save').slideDown(); // #save is display:none until needed on screen.
		}
		return false; // prevents form from submitting and refreshing page.
	});
}

