/**
 * @author daniel
 */
var org;
var speed = 500;
var activeRef = $('#ref-1');
var wait = false;
var currentId = 8;

var servicesOpen = false;
var contactOpen = false;
var refOpen = false;

$(document).ready(function(){
	// Fonts
	Cufon.replace('.cufon-replace', {hover:true});
	
	// Silly stuff
	$('#topmenu').draggable({
		zIndex:99,
		delay:250
	});
	var gridshown = false;
	var altisdown = false;
	$(window).keydown(function(e){
		if(e.keyCode == '18')
		{
			altisdown = true;
		}
		if(e.keyCode == '71' && altisdown)
		{
			$('#grid').css({
				top:$(window).scrollTop(),
				height:$(window).height()
			});
			gridshown = true;
			$('#grid').show();
		}
	});
	$(window).keyup(function(e){
		if(e.keyCode == '18')
		{
			altisdown = false;
		}
		if(gridshown)
		{
			gridshows = false;
			$('#grid').hide();
		}
	});
	
	// URL Deep linking..
	$.observeUrl();
	var currUrl = $(document).url();
	if(currUrl.attr('hash') != null)
	{
		$(this).dispatchUrlChange(currUrl.attr('hash'));
	}
	$(document).bind('hash:change', function(e, hash){
		$(this).dispatchUrlChange(hash);
	});
	
	// EVENTS
	
	// Newsletter
	$('#newsletter-submit').click(function(){
		$('#newsletter-form').submit();
	});
	
	// Services
		// Lefter menu
		$('.lefter-menu').click(function(e){
			$(this).navigateLefter();
			return false;
		});
	
	// References
	$(this).click(function(e){
		if (!$(e.target).parents().is('.item') && !$(e.target).hasClass('item')) {
			if (refOpen) {
				window.location.hash = '';
			}
		}
	});
		// References controls
		$('#referencer-box .ref-controls .ref-control').click(function(){
			$(this).refNavigate();
			return false;
		});
	
	// Searcher
	$('.searcher .search').focus(function(){
		$(this).searchOn();
	});
	$('.searcher .search').blur(function(){
		$(this).searchOff();
	});
	$('#search-submit').click(function(){
		$('#search').submit();
		return false;
	});
	
	// Top contact-dropdown
	org = $('#contact').height();
		// Form value
		$('#besked').focus(function(){
			$(this).addClass('active');
			if($(this).val() == 'Lorem ipsum dolor sit amet dolores...')
			{
				$(this).val('');
			}
		});
		$('#besked').blur(function(){
			if($(this).val() == '')
			{
				$(this).removeClass('active');
				$(this).val('Lorem ipsum dolor sit amet dolores...');
			}
		});
		// Submit click
		$('#contact-send').click(function(){
			$(this).sendMsg();
			return false;
		});
	
});

$.fn.dispatchUrlChange = function(hash)
{
	if(hash == '')
	{
		$(this).closeEverything();
	}
	if(hash == 'services')
	{
		$(this).showLefter();
	}
	if(hash == 'referencer')
	{
		$(this).refShow();
	}
	if (hash == 'kontakt') {
		$(this).toggleDrop();
	}
}

$.fn.closeEverything = function(){
	if(servicesOpen)
	{
		$(this).hideLefter();
	}
	if(refOpen)
	{
		$(this).refHide();
	}
	if(contactOpen)
	{
		$(this).toggleDrop();
	}
}

$.fn.sendMsg = function(){
	var emailregex = /[a-zA-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/gi;
	var validated = true;
	if(!emailregex.test($('#email').val()))
	{
		validated = false;
		$('#email').addClass('error');
	}
	else
	{
		$('#email').removeClass('error');
	}
	if($('#navn').val().length <= 3)
	{
		validated = false;
		$('#navn').addClass('error');
	}
	else
	{
		$('#navn').removeClass('error');
	}
	if($('#besked').val().length <= 3 || $('besked').val() == 'Lorem ipsum dolor sit amet dolores...')
	{
		validated = false;
		$('#besked').addClass('error');
	}
	else
	{
		$('#besked').removeClass('error');
	}
	if(validated)
	{
		if($('#password').val().length > 0)
		{
			//console.log('Spam error');
		}
		else
		{
			$.post(tmp_url + '/ajax.php',{
				name:$('#navn').val(),
				email:$('#email').val(),
				msg:$('#besked').val(),
				tlf:$('#tlf').val()
			}, function(){
				$('#navn').val('');
				$('#email').val('');
				$('#besked').val('');
				$('#tlf').val('');
				$('#top-contact-btn').toggleDrop();
			});
		}
	}
}

$.fn.searchOff = function(){
	if($(this).val() == '')
	{
		$(this).val('Søg...');
	}
	$('.searcher').removeClass('active');
	$(this).removeClass('input-active');
	$(this).next('.submit').removeClass('submit-active');
}

$.fn.searchOn = function(){
	if($(this).val() == 'Søg...')
	{
		$(this).val('');
	}
	$('.searcher').addClass('active');
	$(this).addClass('input-active');
	$(this).next('.submit').addClass('submit-active');
}

$.fn.navigateLefter = function(){
	var id = $(this).attr('id');
	if (!wait && id != currentId) {
		$('.lefter-menu').filter('.active').removeClass('active');
		$(this).addClass('active');
		wait = true;
		var currentElem;
		$('.lefter-item').each(function(){
			if (!$(this).hasClass('hidden')) {
				currentElem = $(this);
			}
		})
		
		var elem = $('#lefter-' + id);
		
		$(elem).css({
			top: $(window).height()
		});
		$(elem).removeClass('hidden');
		$(elem).animate({
			top: '50px'
		},'normal',function(){
			wait = false;
		});
		$(currentElem).animate({
			top: -$(currentElem).height()
		}, 'normal', function(){
			currentId = id;
			$(this).addClass('hidden');
		});
	}
}

$.fn.hideLefter = function(){
	$('#lefter').animate({
		left:'-400px'
	});
	servicesOpen = false;
	$('#services-menu').animate({
		right:0
	});
	$('#services-menu').find('img:first').animate({
		marginLeft:'-250px'
	});
	$('body').animate({
		backgroundPosition:"(-420px 0)"
	});
	$('#wrapper').animate({
		left:0,
		paddingLeft:0
	});
}

$.fn.showLefter = function(){
	if($('#contact').hasClass('open'))
	{
		$('#contact').toggleDrop();
	}
	servicesOpen = true;
	$('#lefter').animate({
		left:'0'
	});
	$('#services-menu').animate({
		right:'-260px'
	});
	$('#services-menu').find('img:first').animate({
		marginLeft:0
	});
	$('body').animate({
		backgroundPosition:"(-20px 0)"
	});
	$('#wrapper').animate({
		left:'400px',
		paddingLeft:'200px'
	});
}

$.fn.refNavigate = function(){
	if ($(this) != activeRef) {
		$('#referencer-box .ref-controls .active').removeClass('active');
		$(this).addClass('active');
		activeRef = $(this);
		var parts = $(this).attr('id').split('-');
		var id = parts[1];
		var top = 0;
		var left = 0;
		switch (parseInt(id)) {
			case 2:
				top = '-400px';
				break;
			case 3:
				top = '-400px';
				left = '-900px';
				break;
			case 4:
				left = '-900px';
				break;
		}
		$('#referencer-box .items').animate({
			'top': top,
			'left': left
		}, 'slow');
	}
}

$.fn.refShow = function(){
	
	$('#referencer-box').fadeIn('normal', function(){
		refOpen = true;
		$('#ref-1').removeClass('active');
		$('#ref-2').addClass('active');
		$('#referencer-box .items').animate({top:'-400px'}, speed, function(){
			$('#ref-2').removeClass('active');
			$('#ref-3').addClass('active');
		})
		.animate({left:'-900px'}, speed, function(){
			$('#ref-3').removeClass('active');
			$('#ref-4').addClass('active');
		})
		.animate({top:0}, speed, function(){
			$('#ref-4').removeClass('active');
			$('#ref-1').addClass('active');
		})
		.animate({left:0},speed);
	});
	
}

$.fn.refHide = function(){
	$('#referencer-box').fadeOut('normal', function(){
		refOpen = false;
	});
}

$.fn.toggleDrop = function(){
	
	if($('#top-contact-btn').attr('href') == '#kontakt')
	{
		$('#kontakt').attr('href', '#');
		$('#top-contact-btn').attr('href', '#')
	}
	else
	{
		$('#kontakt').attr('href', '#kontakt');
		$('#top-contact-btn').attr('href', '#kontakt')
	}
	
	if($('#contact').hasClass('open'))
	{
		$('#contact').animate({
			marginTop:-org
		},250);
		contactOpen = false;
		$('#contact').removeClass('open');
		$('#top-contact-btn').removeClass('active-contact');
	}
	else
	{
		$('#contact').animate({
			marginTop:0
		},750, "easeOutBounce");
		contactOpen = true;
		$('#contact').addClass('open');
		$('#top-contact-btn').addClass('active-contact');
	}
}

function startColourAnimation(currentColour)
{
	var colours = ['#00aeef', '#ec008c', '#fff200', '#231f20'];
	var rand = Math.floor(Math.random() * colours.length);
	while(rand == currentColour)
	{
		rand = Math.floor(Math.random() * colours.length);
	}
	var randColour = colours[rand];
	var changeSpeed = 10000;
	$('#referencer-box').animate({
		backgroundColor:randColour
	}, changeSpeed,function(){
		startColourAnimation(rand);
	});
}

var orgtopheight = '-300px';
var topdropheight = '300px';
var contactDistanceToBottom = '5px';
var openspeed = 750;
var closespeed = 250;
var easing = "easeOutBounce"
function doTopAnimation()
{
	if ($('#contact').hasClass('closed')) {
		$('#contact').animate({
			top: 0
		},
			openspeed,
			easing
		);
		$('#contact').removeClass('closed');
	}
	else
	{
		$('#contact').animate({
			top: orgtopheight
		},
			closespeed
		);
		$('#contact').addClass('closed');
	}
}
