var loginTimeout;
var relativeUrl;
var mobileDevice;

function nothing() {
	return false;	
}

function onLogin(e)
{
	$("#login").animate({top:'-80'}, 100);
	loginTimeout = setTimeout(function() {
			$("#login").animate({top:'-2'}, 250);
		}, 650);
}

function outLogin(e)
{
	clearTimeout(loginTimeout);
	$("#login").animate({top:'-82'}, 100, function() {
		
	});
}

function verify_name(name) {
	// Verify a name
	var regexp = /^[a-zA-Z\ ]*$/;	
	if(name.search(regexp) < 0)
		return false;
	else if(name.length < 2)
		return false;
	return true;
}

function verify_email(email) {
	// Verify a name
	var pos = email.lastIndexOf("@");
    return pos > 0 && (email.lastIndexOf(".") > pos) && (email.length - pos > 4);
}

function fadeOutMessageBox()
{
	$('#message_overlay').animate({'opacity': 0}, 'fast', function() {
		$(this).css({'display' : 'none'});	
	});
	
	return false;
}

function getMobile()
// Returns the name of the mobile device
{
	var userOS = navigator.userAgent.toLowerCase();
	var mobileList = new Array("iphone os 3",
								"iphone os 4",
								"iphone os 5",
								"ipad; cpu os 5",
								"iphone",
								"android 1",
								"android 2",
								"android",
								"ipad",
								"blackberry",
								"palmos");
	for(var device in mobileList) {
		if(userOS.indexOf(mobileList[device]) >= 0) {
			return mobileList[device];
			break;
		}
	}
	return false;
}

function fixMobileScroll()
// takes care of mobile device scrolling on fixed positioned elementes
{
	var m = getMobile();
	if(m == 'ipad' || m == 'iphone os 3' || m == 'iphone os 4'
		|| m == 'android 1' || m == 'android 2') {
		$(document).scroll(function() {
			// fix overlay scroll
			$('.modal_overlay').each(function() {
				$(this).css('top', window.pageYOffset + 'px');
				$(this).css('height', window.innerHeight + 'px');
			});
			// fix handhelf footer scroll
			$('#handheld_footer').css({'top': (window.pageYOffset + window.innerHeight) + 'px'});
		});
	}
}

/* trigger when page is ready */
$(document).ready(function (){	
	
	var homeOutImage = new Image();
	var homeOverImage = new Image();
	var homeActiveImage = new Image();
	var menuLinks;
	var url;
	
	// get relative URL of this page
	url = window.location.pathname;
	relativeUrl = url.substr(url.lastIndexOf('/') + 1, url.length);
	
	// define home button images
	homeOutImage.src = "images/home_btn.png";
	homeOverImage.src = "images/home_btn_h.png";
	homeActiveImage.src = "images/home_btn_a.png";
	
	// assign showLogin function
	$("#login").bind("mouseenter", onLogin);
	$("#login").bind("mouseleave", outLogin);
	
	// highlight menu link
	$('#top_menu a').each(function() {
		if($(this).attr('href') == relativeUrl && relativeUrl != 'index.html')
			$(this).attr('class', 'top_menu_btn_a');
		else if($(this).attr('href') == relativeUrl && relativeUrl == 'index.html') {
			$(this).attr('class', 'no_pointer');
			$("#home_btn").attr('id', 'home_page');
			$("#home_page").attr('src', homeActiveImage.src);
		}
	});
	
	// assign hover action for home button
	$("#home_btn").bind("mouseover", function() {
		$(this).attr('src', homeOverImage.src);
		$(this).bind("mouseout", function() {
			$(this).attr('src', homeOutImage.src);
		});
	});
	
	// adjust modal fixed position
	fixMobileScroll();
	
	// adjust modal message width for handhelds
	if($(document).width() < 440) {
		$('#message_box').css({'width' : ($(document).width() - 50) + 'px'});
	}
	
	// show modal message if message was received
	$('#message_overlay').css({'display': 'block'});
	$('#message_box').css({'margin-top': '-' + $('#message_box').height()/2 + 'px',
							'margin-left': '-' + $('#message_box').width()/2 + 'px'});
	$('#msg_ok_btn').css({'margin-top': ($('#message_box').height()/2 + 12) + 'px',
							'margin-left': ($('#message_box').width()/2 - 50) + 'px'});
	$('#msg_icon').css({'margin-top': '-' + ($('#message_box').height()/2 + 64) + 'px',
							'margin-left': '-' + ($('#message_box').width()/2 + 11) + 'px'});
});


/* optional triggers

$(window).load(function() {
	
});

$(window).resize(function() {
	
});

*/
