$(document).ready(function()
{
	$(".nav.menu").hover( function(){ $('.dropdown', this).show(); }, 
						  function(){ $('.dropdown', this).hide(); });
						  
	//$('[type=text]').clearOnFirstFocus();
	
	//$('#loginForm [type=password]').each(passToTextToPass);
	//$('#signUpForm [type=password]').each(passToTextToPass);
	
	$('#signUpForm').submit( signUpValidation);
	$('#changePassForm').submit(changePassValidate);
	
	$('.login button, .sign-up button, #trip-builder button').each(orangeButton);
	$('#contactForm button,	#tripAlertsForm :submit,  .changeEmail :submit, #changePassForm :submit, #bigSearchForm :submit').each(orangeButton);
	$('.lightbox').parent(':not(.displayLightbox)').children('.lightbox').each(initModalLinks);

	initAudioSamples();
	
	$(".storycontent img").lazyload({ threshold : 200, placeholder : "siteImages/loading.gif" });
});

// turns a button element into a  orangeBox div that submits the form its in
orangeButton = function(i, elem)
{
	newBtn = $('<span type="submit" class="orangeBox orangeButton"><tt class="topLeft"></tt><tt class="topRight"></tt><h3 style="margin-top:0px;">' + $(elem).text() + '</h3><tt class="bottomLeft"></tt><tt class="bottomRight"></tt></span>');

	realElem = $(elem).get(0);
	computedWidth = parseInt(realElem.style.width) + 20;
	if(isNaN(computedWidth))
	{
		newWidth = 100;
	}
	else
	{ 
		newWidth = computedWidth;
	}
	$(newBtn).width(newWidth);
	
	$(newBtn).attr('tabIndex', $(elem).attr('tabIndex')).children('h2').css('textAlign','center').attr('id', $(elem).attr('id'));
	$(newBtn).click(function(){$(this).parents('form').submit();});
	$(newBtn).keypress(function (e){ if(e.which == 13){$(this).parents('form').submit();} });
	$(elem).replaceWith(newBtn);
}

toPassField = function()
{
	var newElem = $('<input type="password" class="' + $(this).attr('class') + '"/>').attr('tabIndex',$(this).attr('tabIndex')).attr('name', $(this).attr('name')).width($(this).css('width')).removeClass('wasPass');
	$(this).replaceWith(newElem);
	$(newElem).focus();
}
passToTextToPass = function(i, elem)
{
	var newElem = $('<input type="text" class="' + $(this).attr('class') + ' wasPass" />').attr('tabIndex',$(this).attr('tabIndex')).attr('name', $(this).attr('name')).width($(this).css('width')).val($(this).val()).focus(toPassField);
	$(this).replaceWith(newElem);
}

$.fn.clearOnFirstFocus = function()
{
	this.one('focus', function(){ $(this).val(''); })
}

function initModalLinks(i, elem)
{
	name = $(elem).attr('id');
	$('[href=/' + name + ']').click(function(e)
	{
		e.preventDefault();
		if($.browser.msie && $.browser.version < 7)
		{
			$(".lightboxContent", elem).css("display", "block");
			$(".orangeButton", elem).css('visibility', 'visible');

			$(elem).modal({});
		}
		else
		{
			$(elem).modal({close:true, onOpen: modalOpen, onClose: modalClose });
		}
	});
}

function modalOpen(dialog)
{
	dialog.overlay.css('display', 'block');
	dialog.container.css('display', 'block');
	dialog.data.css('display', 'block');
	$('.lightboxContent', dialog.data).slideDown(1000, function(){
		$('.orangeButton', dialog.data).css('visibility', 'visible');
	});
}

function modalClose(dialog)
{
	$('.orangeButton', dialog.data).css('visibility', 'hidden');
	$('.lightboxContent', dialog.data).slideUp(1000, function(){
		
		dialog.data.css('display', 'none');
		dialog.overlay.css('display', 'none');
		dialog.container.css('display', 'none');
		$.modal.impl.dialog = {}
		$.modal.impl.unbindEvents();
	});
}

function initAudioSamples()
{
	$("[href$='.mp3']").each(function(i, elem)
	{
		url = $(elem).attr('href');
		$(elem).replaceWith($('<object class="audioSamplePlayer" type="application/x-shockwave-flash" data="musicplayer.swf?song_url=' + url + '&b_bgcolor=D9D5BC&b_fgcolor=393939" width="17" height="17">' + 
												'<param name="movie" value="musicplayer.swf?song_url=' + url + '&b_bgcolor=C3BCA4" />' +
												'<param name="wmode" value="transparent" />' +
											'</object>'));
	
	});
}

function signUpValidation(e)
{
	cereal = $("#signUpForm").serializeArray();
	$("#signUpForm :text, #signUpForm :password").removeClass('error');
	if(cereal[0].value == '' || cereal[0].value == 'Name:')
	{
		userMessage('error', '#signUpForm', 'Please enter your name.')
		$("#signUpForm input[name='name']").addClass('error');
	}
	else if(validateEmail(cereal[1].value) == false)
	{
		userMessage('error', '#signUpForm', 'Please input a valid email.')
		$("#signUpForm input[name='email']").addClass('error');
	}
	else if(cereal[2].value.length < 6 || cereal[2].value == 'Password:')
	{
		userMessage('error', '#signUpForm', 'Password must be at least 6 characters long.')
		$("#signUpForm input[name*='pass']").addClass('error');
	}
	else if(cereal[2].value != cereal[3].value)
	{
		userMessage('error', '#signUpForm', 'The two password fields do not match.')
		$("#signUpForm input[name*='pass']").addClass('error');
	}
	else
	{
		return true;
	}
	return false;
}

function userMessage(type, parent, message)
{
	$("." + type + "Message", parent).remove();
	msgElem = $('<div class="userMessage ' + type + 'Message">' + message + '</div>');
	$(parent).prepend(msgElem);
	setTimeout( function(){$(msgElem).fadeOut(1500);},  4000);
}
function validateEmail(email)
{
	result = $.ajax({url : 'validator.php?type=email&value=' + email, 'async':false});
	return result.responseText == 'true';
}

function changePassValidate()
{
	cereal = $("#changePassForm").serializeArray();
	$("#changePassForm :text, #changePassForm :password").removeClass('error');
	if(cereal[1].value.length < 6)
	{
		userMessage('error', '#changePassForm', 'Password must be at least 6 characters long.')
		$("#changePassForm input[name*='newPass']").addClass('error');

	}
	else if(cereal[1].value != cereal[2].value)
	{
		userMessage('error', '#changePassForm', 'The two password fields do not match.')
		$("#changePassForm input[name*='confirmPass'], #changePassForm input[name*='newPass']").addClass('error');
	}
	else
	{
		return true;
	}
	return false
}