var visibleSubmenu = '';
var selectedSubmenu = '';
var submenuAnimationDelay = 250;

function toClassName(obj) {
	return obj.text().toLowerCase().replace('&', '').split(' ').join('');
}

function showSubmenu() {
	var className = toClassName($(this));
	if (visibleSubmenu == className) return;
	if (visibleSubmenu != '') {
		$('.submenu').hide();
		$('#menu li.' + visibleSubmenu).removeClass('selected');
	}
	if ($.browser.msie) $('.submenu.' + className).show();
	else $('.submenu.' + className).fadeIn(submenuAnimationDelay);
	$('#menu li.' + className).addClass('selected');
	visibleSubmenu = className;
}

function hideSubmenu() {
	if (visibleSubmenu == selectedSubmenu) return;
	if (visibleSubmenu != '') {
		if ($.browser.msie) $('.submenu').hide();
		else $('.submenu').fadeOut(submenuAnimationDelay);
		$('#menu li.' + visibleSubmenu).removeClass('selected');
	}
	if (selectedSubmenu != '') {
		if ($.browser.msie) $('.submenu.' + selectedSubmenu).show();
		else $('.submenu.' + selectedSubmenu).fadeIn(submenuAnimationDelay);
		$('#menu li.' + selectedSubmenu).addClass('selected');
	}
	visibleSubmenu = selectedSubmenu;
}

function initSubmenu() {
	if (selectedSubmenu != '') {
		$('.submenu.' + selectedSubmenu).show();
		$('#menu li.' + selectedSubmenu).addClass('selected');
	}
}

function initMap() {
	if ($('#about.contact .map').length == 0) return; 
    var myLatlng = new google.maps.LatLng(31.865531,-116.629225);
    var myOptions = {
    	zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.TERRAIN,
		mapTypeControl: false
    }
    var map = new google.maps.Map(document.getElementById("map_mx"), myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
    myLatlng = new google.maps.LatLng(37.368456, -121.916633);
    myOptions = {
    	zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.TERRAIN,
		mapTypeControl: false
    }
    map = new google.maps.Map(document.getElementById("map_us"), myOptions);
    marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
    $('#about.contact #extra').hide();
}

function initNews() {
	if ($('#news #posts').length == 0) return;
	if ($('.pagination').html().length == 0)
		$('#posts .post:last').addClass('last');
}

function initAbout() {

}

function initServices() {
	if ($('#services.engineering').length == 0) return;
	$('.bubbleInfo').each(function () {
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;
    
		var trigger = $('.trigger', this);
		var popup = $('.popup', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					top: -145,
					left: -66,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
}

function dspcStr(s) {
	return s.split(' ').join('');
}

function initProducts() {
	/*$('a.icon.cart').click(function() {
		if ($('#buy-now-ballon').css('display') != 'block')
			$('#buy-now-ballon').slideDown();
		else $('#buy-now-ballon').slideUp();
	});*/
	
	var pastValue = 0;
	$('#products.ubiqua #item_count')
		.focus(function() { pastValue = this.value; })
		.blur(function() {
			if (!isNaN(this.value * 1)) this.value = Math.round(this.value * 1);
			else this.value = pastValue;
			if (this.value > 19 || this.value < 1) {
				this.value = pastValue;
				$('#products.ubiqua .total strong').html('$' + (this.value * 999) + '.00 USD');
			}
		})
		.keyup(function() {
			var val = 0.0;
			if (!isNaN(this.value * 1)) val = Math.round(this.value * 1);
			else val = pastValue;
			if (val > 0 && val < 20)
				$('#products.ubiqua .total strong').html('$' + (val * 999) + '.00 USD');
		});
}

function initAccounts() {
	$('#sign-up-form').submit(function() {
		$('#sign-up-form input').removeClass('error');
		
		if (dspcStr($('#first_name').attr('value')) == '') {
			alert('You must specify your first name.');
			$('#first_name').addClass('error').focus().select();
			return false;
		}
		
		if (dspcStr($('#last_name').attr('value')) == '') {
			alert('You must specify your last name.');
			$('#last_name').addClass('error').focus().select();
			return false;
		}
		
		if (dspcStr($('#email').attr('value')) == '') {
			alert('You must specify your e-mail address.');
			$('#email').addClass('error').focus().select();
			return false;
		}
		
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($('#email').attr('value'))) {
			alert('Your e-mail address is invalid.');
			$('#email').addClass('error').focus().select();
			return false;
		}
		
		if ($('#email').attr('value') != $('#email_confirm').attr('value')) {
			alert('Your e-mail address doesn\'t match the confirmation.');
			$('#email_confirm').addClass('error');
			$('#email').addClass('error').focus().select();
			return false;
		}
		
		if (dspcStr($('#password').attr('value')) == '') {
			alert('You must specify a password.');
			$('#password').addClass('error').focus().select();
			return false;
		}
		
		if ($('#password').attr('value') != $('#password_confirm').attr('value')) {
			alert('Your password doesn\'t match the confirmation.');
			$('#password_confirm').addClass('error');
			$('#password').addClass('error').focus().select();
			return false;
		}
		
		return true;
	});
	$('#account-form').submit(function() {
		$('#account-form input').removeClass('error');
		
		if (dspcStr($('#first_name').attr('value')) == '') {
			alert('You must specify your first name.');
			$('#first_name').addClass('error').focus().select();
			return false;
		}
		
		if (dspcStr($('#last_name').attr('value')) == '') {
			alert('You must specify your last name.');
			$('#last_name').addClass('error').focus().select();
			return false;
		}
		
		if (dspcStr($('#email').attr('value')) != '') {
			var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test($('#email').attr('value'))) {
				alert('Your e-mail address is invalid.');
				$('#email').addClass('error').focus().select();
				return false;
			}
		
			if ($('#email').attr('value') != $('#email_confirm').attr('value')) {
				alert('Your e-mail address doesn\'t match the confirmation.');
				$('#email_confirm').addClass('error');
				$('#email').addClass('error').focus().select();
				return false;
			}
		}

		if (dspcStr($('#password').attr('value')) != '') {
			if (dspcStr($('#password').attr('value')) == '') {
				alert('You must specify a password.');
				$('#password').addClass('error').focus().select();
				return false;
			}
		
			if ($('#password').attr('value') != $('#password_confirm').attr('value')) {
				alert('Your password doesn\'t match the confirmation.');
				$('#password_confirm').addClass('error');
				$('#password').addClass('error').focus().select();
				return false;
			}
		}
		
		return true;
	});
	$('#account-forgot-form').submit(function() {
		$('#account-form input').removeClass('error');
		if (dspcStr($('#email').attr('value')) == '') {
			alert('You must specify your e-mail address.');
			$('#email').addClass('error').focus().select();
			return false;
		}
		
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($('#email').attr('value'))) {
			alert('Your e-mail address is invalid.');
			$('#email').addClass('error').focus().select();
			return false;
		}
		
		return true;
	});
	$('#account-password-form').submit(function() {
		$('#account-password-form input').removeClass('error');
		if (dspcStr($('#password').attr('value')) == '') {
			alert('You must specify a password.');
			$('#password').addClass('error').focus().select();
			return false;
		}
		
		if ($('#password').attr('value') != $('#password_confirm').attr('value')) {
			alert('Your password doesn\'t match the confirmation.');
			$('#password_confirm').addClass('error');
			$('#password').addClass('error').focus().select();
			return false;
		}
		
		return true;
	});
	$('#sign-up-form #first_name').focus();
	$('#sign-in-form #email').focus();
	$('#account-forgot-form #email').focus();
	$('#account-password-form #password').focus();
}

$(function() {
	$('#menu li').hover(showSubmenu);
	$('#header').hover(null, hideSubmenu);
	initSubmenu();
	initMap();
	initNews();
	initServices();
	initProducts();
	initAbout();
	initAccounts();
	Shadowbox.init({
		overlayOpacity: 0.75
	});
	
	$('body.signup form p:eq(7)').css('display', 'none');
});