/**
 * @author dylan
 */
$(function() {
	$.preloadImages("/images/menu-up-arrow.png","/images/tooltip-web.png","/images/tooltip-branding.png","/images/tooltip-print.png","/images/menu-item-bg-left.png", "/images/menu-item-bg-right.png");
	
	setupMenu();
	
	if ($('#contactSubmit').length) {contactFormValidation()}
});

function contactFormValidation() {
	// contact form validation
	$('#contactSubmit').click(function() {
		$('#contactSubmit').disabled=true;
		$('.info-box').fadeOut(200);
		if($('#name').val()=="") {
			showContactInfoBox("We'd like to know your name.","name");
			return true;
		}
		if($('#email').val()=="" && $('#phone').val()==""){
			showContactInfoBox("Please tell us how to best contact you.","email");
			return true;
		}
		if($('#message').val()=="") {
			showContactInfoBox("What's on your mind?","message")
			return true;
		}
		document.form.submit();
	});
	
	// call function to submit login form when enter button is pressed
	$("form input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('#contactSubmit').click();
			return false;
		} else {
			return true;
		}
	});
}


function showContactInfoBox(text,elId) {
	$('#'+elId).focus();
	$('.info-box').text(text).fadeIn(300)
		.fadeOut(300)
		.fadeIn(300);
	$('contactSubmit').disabled=false;
}

function setupMenu(){
	var menuId = $('#currentPage').attr('name');
	
	// header menu hover styles
	if (!menuId) {
		menuId = "noMenu";
	}
	$('#headerMenu>li>a:not(' + menuId + ')').hover(function(){
		//$(this).css('cursor','hand'); // IE7
		$('#' + this.id + ">span:first").addClass('menu-item-bg-left')
		$('#' + this.id + ">span:eq(1)").addClass('menu-item-bg-center')
		$('#' + this.id + ">span:last").addClass('menu-item-bg-right')
		$('#' + this.id + ">span:first").removeClass('menu-item-no-bg-left')
		$('#' + this.id + ">span:eq(1)").removeClass('menu-item-no-bg-center')
		$('#' + this.id + ">span:last").removeClass('menu-item-no-bg-right')
	}, function(){
		//$(this).css('cursor','auto'); // IE7
		$('#' + this.id + ">span:first").addClass('menu-item-no-bg-left')
		$('#' + this.id + ">span:eq(1)").addClass('menu-item-no-bg-center')
		$('#' + this.id + ">span:last").addClass('menu-item-no-bg-right')
		$('#' + this.id + ">span:first").removeClass('menu-item-bg-left')
		$('#' + this.id + ">span:eq(1)").removeClass('menu-item-bg-center')
		$('#' + this.id + ">span:last").removeClass('menu-item-bg-right')
	});
	
	// tooltips
	$('#menuHome').hover(function(){showTooltip(this,'View our recent work.')},function(){hideTooltip(this)});
	$('#menuweb-design').hover(function(){showTooltip(this,'View our web portfolio.')},function() {hideTooltip(this)});
	//$('#menubranding-and-identity').hover(function(){showTooltip(this,'View our branding portfolio.')},function(){hideTooltip(this)});
	//$('#menuprint-work').hover(function(){showTooltip(this,'View our print portfolio.')},function() {hideTooltip(this)});
	$('#menuAbout').hover(function(){showTooltip(this,'Learn how we work.')},function() {hideTooltip(this)});
	$('#menuContact').hover(function(){showTooltip(this,'Hello :)')},function() {hideTooltip(this)});
	
	// style header menu item for current page
	var menuContents = $("#" + menuId).text();
	var menuItemLi = $("#" + menuId).parent(0);
	
	var newMenuItem = $("<div>" + menuContents + "</div>").attr("class", "currentMenuItem");
	$("#" + menuId).remove();
	$(newMenuItem).appendTo(menuItemLi);
}

function showTooltip(el,text) {
	if ($(el).is('.working')) {
		return;
	} else {
		$(el).addClass('working');
		$('#tooltip-content').text(text);
		$('#tooltip').css('left',$(el).offset().left - 16);
		$('#tooltip').css('top',$(el).offset().top - 68); // was 48
		$('#tooltip').css('opacity','0');
		$('#tooltip').css('display',"block");
		$('#tooltip').animate({top:'+=20',opacity:'1',queue:false},400,'easeOutCubic',function(){
			$(el).removeClass('working');
		});
	}
}

function hideTooltip(el) {
	$('#tooltip').stop();
	$(el).removeClass('working');
	$('#tooltip').animate({
		top: '-=0',
		opacity: '0'
	}, 0, 'easeOutCubic');
}

function pageName() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/')+1);
	return sPage;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}