$.fn.prepStart = function (options){
  var defaults = {
    visibility: 'visible',
    opacity: 0.0,
    zIndex:1000,
    'filter':'alpha(opacity=0)'
  };
  return this.stop(true).css($.extend(defaults,options));
}

$.fn.prepEnd = function (options){
  var defaults = {
    opacity: 1.0,
    zIndex:100,
    'filter':'alpha(opacity=100)'
  };
  return this.stop(true).css($.extend(defaults,options));
}


Fuel.PageController.add_page_transition(1, function(callback){
	$layer1.prepStart().animate({opacity: 1.0, top: 0}, 500, "easeInOutQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
	
}, function(callback){
	$layer1.prepEnd().animate({opacity: 0.0, top: -200 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#b60606");

Fuel.PageController.add_page_transition(2, function(callback){
	$companyLogos.prepStart().animate({opacity: 1.0, top: '10%'}, 500, "easeInOutQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$companyLogos.prepEnd().animate({opacity: 0.0, top:'1%' }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#8a0651");




Fuel.PageController.add_page_transition(3, function(callback){
	$mapps.prepStart().animate({opacity: 1.0}, 500, "easeInOutQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$mapps.prepEnd().animate({opacity: 0.0 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#ff7700");

Fuel.PageController.add_page_transition(4, function(callback){
	$core.prepStart().animate({opacity: 1.0}, 500, "easeInOutQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$core.prepEnd().animate({opacity: 0.0 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#dc9d10");

Fuel.PageController.add_page_transition(9, function(callback){
	$press.prepStart().animate({opacity: 1.0}, 500,"easeInQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$press.prepEnd().animate({opacity: 0.0 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#1f8311");

Fuel.PageController.add_page_transition(5, function(callback){
	$news.prepStart().animate({opacity: 1.0}, 500,"easeInQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$news.prepEnd().animate({opacity: 0.0}, 500,"easeOutQuint", function(){
	  $(this).css({'visibility':'hidden'});
	  callback();
	});
}, "#8f3333");

Fuel.PageController.add_page_transition(6, function(callback){
	$engage.prepStart().animate({opacity: 1.0}, 500,"easeInQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$engage.prepEnd().animate({opacity: 0.0 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#1c6e7a");

Fuel.PageController.add_page_transition(7, function(callback){
	$engage.prepStart().animate({opacity: 1.0}, 500,"easeInQuint", function(){
	  $(this).css({'filter':'none'});
	  callback();
	});
}, function(callback){
	$engage.prepEnd().animate({opacity: 0.0 }, 500, "easeInOutQuint", function(){
	  $(this).css({visibility:'hidden'});
	  callback()
	});
}, "#00bd50");


// DOM Ready
jQuery(function($){			

	$.getJSON(
		'',
		{ no: 15 },
		function(data){
			var $nf = $("#news-feed");
			$.each(data, function(){
				var $li = $([
				'<li>',
					'<div class="news-container">',
						'<h3>', this.title, '</h3>',
					'</div>',
					'<p class="news-date">', this.time, '</p>',
					'<a class="news-link" href="', this.permalink, '">View Article</a>',
				'</li>'
				].join('')).appendTo($nf);
			})
			
			$nf.fuelNews({nav: $("#news-nav > span")}).find('h3').center();;	
		})
	

	
  // $layer1 = $("#layer1").css({
  //  opacity: 0.0, display:"block", top: -200
  // });
	
	$layer1 = $("#layer1").css({display: 'block', visibility: 'hidden', top: -200});
	$mapps = $("#mapps").css({display: 'block', visibility: 'hidden'});
	$core = $("#core").css({display: 'block', visibility: 'hidden'});
	$news = $("#news").css({display: 'block', visibility: 'hidden'});
	$press = $("#press").css({display: 'block', visibility: 'hidden'});
	$engage = $("#engage").css({display: 'block', visibility: 'hidden'});
	$webdevlopment = $("#webdevelopment").css({display: 'block', visibility: 'hidden',top: '1%'});
	$companyLogos = $("#company-logos").css({display: 'block', visibility: 'hidden',top: '1%'});
	/*
	$mapps = $("#mapps").css({
		opacity: 0.0, display: "block"
	})
	
	$core = $("#core").css({
		opacity: 0.0, display: "block"
	})
	
	
	
	$news = $("#news").css({
		opacity: 0.0, display: "block"
	});
	
	$press = $("#press").css({
		opacity: 0.0, display: "block"
	});
	
	$engage = $("#engage").css({
		opacity: 0.0, display: "block"
	});*/

	/*function fix_tweets(){
		var $height = $("#tweets").innerHeight(),
		counter = 0;
		$("#tweets p").show().removeClass('last').each(function(){
			counter += $(this).outerHeight(true);
			if(counter > $height + 15) {
				$(this).hide().prev().addClass('last');
			}
		})
	}
	var timer = null;
	$(window).resize(function(){
		if(timer) window.clearTimeout(timer);
		timer = window.setTimeout(function(){
			timer = null;
			fix_tweets();
		}, 500);
	})
	$("#tweets").twitterFeed('fuelbrandinc', function(){
		fix_tweets();
	})*/
	

	
	$companyLogos.find('a').hover(function(){
	  if(!$.browser.msie)
	   $(this).stop().animate({opacity: 1.0}, 500).siblings().stop().animate({opacity: 0.3}, 500); 
	}, function(){
	  if(!$.browser.msie)
		$(this).siblings().andSelf().stop().animate({opacity: 1.0}, 500);
	})
	
	$("#engage form label").inFieldLabels();
	
	$("#fq1 .box").expandingBox({
		mask: $("#fq1 .open"),
		trigger: $("#fq1"),
		speed: 200
	});
	
	$("#fq2 .box").expandingBox({
		mask: $("#fq2 .open"),
		trigger: $("#fq2")
	});
	
	$("#fq3 .box").expandingBox({
		mask: $("#fq3 .open"),
		trigger: $("#fq3")
	});
	
	$("#fq4 .box").expandingBox({
		mask: $("#fq4 .open"),
		trigger: $("#fq4")
	});
	
	$("#fq5 .box").expandingBox({
		mask: $("#fq5 .open"),
		trigger: $("#fq5")
	});
	
	$("#m-fq1 .box").expandingBox({
		mask: $("#m-fq1 .open"),
		trigger: $("#m-fq1")
	});
	
	$("#m-fq2 .box").expandingBox({
		mask: $("#m-fq2 .open"),
		trigger: $("#m-fq2")
	});
	
	$("#m-fq3 .box").expandingBox({
		mask: $("#m-fq3 .open"),
		trigger: $("#m-fq3")
	});
	
	$("#m-fq4 .box").expandingBox({
		mask: $("#m-fq4 .open"),
		trigger: $("#m-fq4")
	});
	
	$("#m-fq5 .box").expandingBox({
		mask: $("#m-fq5 .open"),
		trigger: $("#m-fq5")
	});
	
	$("#goal div.inline-link").each(function(){
		var $this = $(this),
				$popup = $this.find('.popup');
				
				$popup.css({right: -425}).hide();
				
				$this.bind('mouseover', function(e){
					if(e.target !== this) return;
					$popup.show().stop(true).animate({ opacity: 1.0, right: -414}, 200)
				}).bind('mouseout',function(e){
					if(e.target !== this) return;
					$popup.animate({ opacity: 0.0, right: -425}, 200, function(){
						$(this).hide();
					})							
				});
	});
	
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	};
	
	var submitonce = false;
	var $email   = $("#email").blur(function(){
		var email   = $email.val().trim();
		$email.addError(!email || !/^.*@.*\..*$/.test(email));
	}),
	    $name    = $("#name").blur(function(){
				var name    = $name.val().trim();
		$name.addError(!name);
	}),
	    $message = $("#message").blur(function(){
				var message = $message.val().trim();
		$message.addError(!message);
	});
	
	$.fn.addError = function(test){
		if(submitonce && test){
			this.closest('p').addClass('error');
		} else if(submitonce) {
			this.closest('p').removeClass('error');
		}
		return test;
	}
	 
	$('form').submit(function(e){ 
		submitonce = true;
		var email   = $email.val().trim(),
		    name    = $name.val().trim(),
		    message = $message.val().trim(),
				error   = false;
		
		
		if( $email.addError(!email || !/^.*@.*\..*$/.test(email))) error = true;
		if( $name.addError(!name) ) error = true;
		if( $message.addError(!message) ) error = true;
		

		if(!error){
			$.post('email.php',{
				email: email,
				message: message,
				name: name
			}, function(data){
				if(data == true){
					$("#engage-success").fadeIn(200, function(){
						submitonce = false;
						$message.val('').blur();
						
						window.setTimeout(function(){
							$("#engage-success").fadeOut(200);
						}, 3000)
					});
				}
			}, 'json');
		}
		if(!error){ 
			$("#engage-success").fadeIn(200, function(){
					submitonce = false;
					$name.val('').blur();
					$email.val('').blur();
					$message.val('').blur();

					window.setTimeout(function(){
						$("#engage-success").fadeOut(200);
					}, 3000)
				});
		}
		
		e.preventDefault();
	})
	$('#submit-a').click(function(e){
		$('form').submit();
		e.preventDefault();
	})
	
	$.History.bind(function(fragment){
		
		var page = fragment.split('/');
		if(page.length > 1){
			var p = 0;
			switch(page[1]){
				case 'about':
				p = 1;
				break;
				case 'companies':
				p = 2;
				break;
				case 'mapps':
				p = 3;
				break;
				case 'core':
				p = 4;
				break;
				case 'press':
				p = 9;
				break;
				case 'news':
				p = 5;
				break;
				case 'engage':
				p = 6;
				break;
				case 'webdevelopment':
				p = 7;
				break;
			}
			if(p > 0){
				Fuel.PageController.changePage( p );
				$("#show-nav").trigger('mouseover');
			}
		}
	})
})


