(function($){$(function() {
	
	var calculatePrice = function() {
		var storage = $('#additional_storage').attr('value');
		var package_price = parseFloat($('#package_price').attr('value'));
		var price_per_gb = 6;
		var new_price;
		
		if ( storage == '' ) {
			storage = 0;
		} else {
			storage = parseInt(storage);
		}
		/*
		if ( $('#yearly_payments:checked').length == 1 ) {
			// yearly payment 
			new_price = (package_price * 10) + (storage * price_per_gb * 10);
			new_price = "<span>CAD " + new_price + "</span>/year";
		} else {
			// monthly payments 
			new_price = package_price + (storage * price_per_gb);
			new_price = "<span>CAD " + new_price + "</span>/month";
		}
		*/
		new_price = (package_price * 10) + (storage * price_per_gb * 10);
		new_price = "<span>CAD " + new_price + "</span>/year";
		
		return new_price;
	}
	
	var roundNumber = function(number,decimals) {
		var rounded_number = Math.round(number*Math.pow(10,decimals))/Math.pow(10,decimals);
		return rounded_number;
	}
	
	var isValidEmailAddress = function(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);
	}
	
	var validateEmail = function() {
		var email = $('#email').attr('value');
		
		if(email != 0) {
			$("#email_validation").removeClass('valid');
			$("#email_validation").removeClass('notvalid');
			
			if(isValidEmailAddress(email)) {
				$("#email_validation").addClass('valid');
			} else {
				$("#email_validation").addClass('notvalid');
			}
		} else {
			$("#email_validation").removeClass('valid');
			$("#email_validation").removeClass('notvalid');
		}
	}
	
	var equalHeight = function(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	
	
	/* CSS Tweaks */
	$("#pPricing ul#pricing li:first").addClass('first');
	$("#pPricing ul#pricing li:last").addClass('last');
	$("#pPricing div.features div:first").addClass('first');
	$("#pPricing div.features div:last").addClass('last');
	
	
	if ($.fn.slides) {
		$('#pFeatures #slider').slides({
			container: 'slides',
			effect: 'fade',
			fadeSpeed: 400,
			preload: true,
			preloadImage: 'http://www.securusvault.com/wp-content/themes/securusvault/img/loading.gif',
			play: 5000,
			pause: 2500,
			hoverPause: true,
			generatePagination: false
		});
	}
	
	if ($.fn.numeric) {
		$('#additional_storage').numeric();
	}
	
	if ($.fn.fancybox) {
		// network solution certification
		$('#footer a.ns').fancybox({
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.8,
			'width'				: 387,
			'height'			: 502
		});
		
		$('a.login').fancybox({
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.85,
			'padding'			: 0
		});
	}
	
	var new_price = calculatePrice();
	$('#price h4').html(new_price);
	
	$('#additional_storage').bind('blur',function(){
		var new_price = calculatePrice();
		$('#price h4').html(new_price);
	});
	
	$('#yearly_payments').change(function(){
		var new_price = calculatePrice();
		$('#price h4').html(new_price);
	});
	
	$('#email').keyup(function(){
		validateEmail();
	});
	
	$('#email').bind('blur',function(){
		validateEmail();
	});
	
	$('#registration').submit(function(){
		// required fields must be filled, and additional storage field can't be empty
		if ( isValidEmailAddress($('#email').attr('value')) && ($('#additional_storage').attr('value') != '') && ($('#username').attr('value') != '')) {
			return true;
		} else {
			if ( $('#username').attr('value') == '' ) {
				$('#username').focus();
				return false;
			}
			
			if ( !isValidEmailAddress($('#email').attr('value')) ) {
				$('#email').focus();
				return false;
			}
			
			if ( $('#additional_storage').attr('value') == '' ) {
				$('#additional_storage').focus();
				return false;
			}
		}
	});
	
	
	equalHeight($("#pPricing #pricing li div"));
	
	/* change video size in support > videos section proportionally */
	var CurrentObject = $('#pSupport #content div.content object');
	var CurrentEmbedObject = $('#pSupport #content div.content embed');
	var iCurrentObjectW = CurrentObject.attr('width');
	var iCurrentObjectH = CurrentObject.attr('height');
	var iObjectW = $('#pSupport #content div.content').css('width');
	
	if (iCurrentObjectW > iObjectW) {
		var iObjectH = Math.floor(( iCurrentObjectH * iObjectW ) / iCurrentObjectW );
		CurrentObject.attr('width',iObjectW).attr('height',iObjectH);
		CurrentEmbedObject.attr('width',iObjectW).attr('height',iObjectH);
	}
	
	// unique rules for removing form field value on focus, and return it on blur
	$('input[type=text]').live('focus',function() {
		if ($(this).val() == $(this).attr('alt')) {
			$(this).val('');
		}
		$(this).addClass('selected');
	});
	
	$('input[type=text]').live('blur',function() {
		if ($(this).val() == '') {
			$(this).val($(this).get(0).defaultValue);
		}
		$(this).removeClass('selected');
	});
	
	$('input[type=password]').live('focus',function() {
		if ($(this).val() == $(this).attr('alt')) {
			$(this).val('');
		}
		$(this).addClass('selected');
	});
	
	$('input[type=password]').live('blur',function() {
		if ($(this).val() == '') {
			$(this).val($(this).get(0).defaultValue);
		}
		$(this).removeClass('selected');
	});
	
	
	// download brochure form
	$('#download-form').live('submit',function(){
		var name 	= $('#cm-name').attr('value');
		var email 	= $('#cm-email').attr('value');
		var company = $('#cm-company').attr('value'); 
		
		$.ajax({
			type: 		'POST',
			url: 		'/wp-content/themes/securusvault/download.php',
			data: 		'name=' + escape(name) + '&email=' + escape(email) + '&company=' + escape(company),
			dataType: 	'text',
			success: 	function(data, textStatus) {
							if (data == 'OK') {
								// display success message
								$('#download-form ul').hide();
								var success_message = 	'<h4>Thank you for downloading brochure</h4>' +
														'<p>SecurusVault Data Centre brochure is sent you your email address!</p>';
								$('#download-form .message').html(success_message).show();
							} else {
								// display error
								var error_message;
								if (data == "BAD-EMAIL") {
									error_message = '<h4>Error</h4>' +
													'<p>Email address you entered is invalid!</p>';
								} // neispravan email
								else if (data == "PHPMailerException") {
									error_message = '<h4>Error</h4>' +
													'<p>Something went wrong while sending email... Please try again!</p>';
								} // Greska pri slanju email sa attachmentom
								else if (data == "PHPException") {
									error_message = '<h4>Error</h4>' +
													'<p>Something went wrong while sending email... Please try again!</p>';
								} // Greska pri slanju email sa attachmentom
								$('#download-form .message').html(data).show();
							};
						}
		});
		
		// do not submit form. we already sent data with ajax
		return false;
	});

	
});})(jQuery);
