$(window).konami(function () {

    // Gayvius
	$('#header #logo').addClass('g');
	$('#header h1').html("Your customers are FABULOUS - oh you."); 
	
	$('body').append('<embed src="other/c.mp3" height="1" width="1" autoplay="true"></embed>');
	
	setTimeout(function(){
	alert("Yo yo");
	alert("We're Avius Insight");
	alert("You know we're alright");
	alert("Collecting data from your clients");
	alert("That you can access on site");
	alert("Our aim is to excel and make your revenue better");
	
	alert("You won't be complaining");
	alert("When you're bringing in the cheddar");
	alert("[Chorus] Aviussssssssssssssss");
	alert("Aviussssssssssssssss");
	alert("Aviussssssssssssssss");
	alert("GAYVIUSSSSSSSSSSSSS!!!!111one");
	},1000);
	
});

$(document).ready(function() {
		
	//jQuery.easing.def = "easeOutElastic";
	
	$('.technologies-holder li').hide();
	$('#th-default').show();
	$('.technologies-picker li img').css('opacity',0.5);
	
	// Mouse over
	$('.technologies-picker li').live('mouseenter',function(){
		
		// Animate picker
		$('.technologies-picker li img').stop(true,true).animate({
			opacity:0.1
		},150);
		
		$('img',this).stop(true,true).animate({
			opacity:1
		},150);
		
		// Animate out holders
		$('.technologies-holder li').stop(true,true).fadeOut(200);
		
		// Animate in hovered holder
		var tp = $(this).attr('id').split("-");
				
		$('#th-'+tp[1]).stop(true,true).fadeIn(200);
		
	
	});
	
	$('.technologies-picker').live('mouseleave',function(){
		$('.technologies-picker li img').stop(true,true).animate({
			opacity:0.5
		},150);
		
		// Animate out holders
		$('.technologies-holder li').fadeOut(200);
		
		$('#th-default').fadeIn(200);
	});
	
	// Menu Stuff
	$('#header .menu ul li').hover(function(){
		$(this).stop(true,true).animate({
			 paddingLeft: "20px",
			 width: '-=10px'
		},200);
	},function(){
		$(this).stop(true,true).animate({
			 paddingLeft: "10px",
			 width: '+=10px'
		},200);
	});
	
	$('#header .menu li').hover(function(){
		//$('ul',this).css('-webkit-transform','scale(1)');	
	
		$('ul',this).stop(true,true).fadeIn(150);
	
	},function(){
		//$('ul',this).css('-webkit-transform','scale(0.1)');	
		$('ul',this).stop(true,true).fadeOut(300);
		
	});
	
	// Menu Stuff
	$('.box-menu li').hover(function(){
		$(this).stop(true,true).animate({
			 paddingLeft: "25px",
			 width: '-=0px'
		},200);
	},function(){
		$(this).stop(true,true).animate({
			 paddingLeft: "15px",
			 width: '+=0px'
		},200);
	});
	
	$('.box-menu li').live('click',function(){
		
		window.location = $('a',this).attr('href');
	
	});
	
	/*
	
	// Number magic
	var timestamp = new Date().getTime();
	// Just for show
	setRealCounter((timestamp+'').substr(2,8));
	setClientCounter((timestamp+'').substr(2,8));
	
	setInterval(function(){
	
		//setRealCounter((Math.random(50000)+'').substr(2,8));
		var timestamp = new Date().getTime();
		setRealCounter((timestamp+'').substr(2,8));
		setClientCounter((timestamp+'').substr(2,8));
	},3000);
	*/
	
	
	
	// Image animations
	imageCounter3();
	imageCounter2();
	imageCounter1();
	imageCounter0();
	imageCounter(130);
	
	
	$('#contact-switch').live('click',function(){
		if(parseFloat($('#contact-switch-slider').css('margin-left')) < 70){
			showInfoUS();
		} else {
		
			showInfoUK();
		
		}
		
		return false;
	
	});
	
	$('.map-pin').hover(function(){
	
		$('.map-bubble').remove();
		$('.map-ripple').remove();
		
		
		$(this).append('<div class="map-ripple"></div>');
		$(this).append('<div class="map-bubble"><img src="images/clients/map-'+$(this).attr('rel')+'.png"></div>');
		
		if ($.browser.mozilla){ 
			$('.map-ripple').hide();
			$('.map-ripple').fadeIn(500);
		}
		
		// IE sucks at opacity, remove it
		if($.browser.msie){
			$('.map-bubble',this).hide();
			$('.map-bubble',this).show();
		} else {
			$('.map-bubble',this).css('opacity','0');
			$('.map-bubble',this).stop(true,true).animate({
				opacity:1,
				marginTop: "+=20px"
			},250);
		}
		
	},function(){
	
		// Again, IE is the worst, disable animations
		if($.browser.msie){
			$('.map-ripple',this).remove();
			$('.map-bubble',this).remove();
		} else {
			$('.map-ripple',this).remove();
			$('.map-bubble',this).stop(true,true).animate({
				opacity:0,
				marginTop: "-=20px"
			},250,function(){
				$(this).remove();
			});
		}
	});
	
	$('.map-bubble').live('mouseenter',function(){
		$(this).fadeOut(200);
	});
	
	
	$('#clients-all').hover(function(){
		$(this).stop(true,true).animate({
			height : "+=90px"
		},{duration: 1000 , easing: "easeOutElastic"});
	},function(){
		$(this).stop(true,true).animate({
			height : "-=90px"
		},{duration: 1000 , easing: "easeOutElastic"});
	
	});
	
	oldX = null;
	oldY = null;
	oldSRC = null;
	active = false;
	selected = null;
	
	$('.magic-album img').live('click',function(){
	
		selected = $(this);
		
		if(active != true){
			
			$(selected).css('z-index',5000);
			
			oldX = ($(selected).css('margin-left') == "auto") ? "0px" : $(selected).css('margin-left');
			oldY = ($(selected).css('margin-top') == "auto") ? "0px" : $(selected).css('margin-top');
			oldSRC = $(selected).attr('src');
			
			var newURL = $(selected).attr('src').split('.');
			newURL = newURL[0]+'-large.jpg';
			
			$('#overlay').fadeIn(300,function(){
				$(selected).stop(true,false).animate({
					width: "800px",
					height: "500px",
					marginTop : "-300px",
					marginLeft : "40px"
				},{duration: 1500 , easing: "easeOutElastic"});
				$(selected).attr('src',newURL);
			});
			
			active = true;
		} else {
						
			$(selected).attr('src',oldSRC);
			$(selected).stop(true,false).animate({
				width: "100px",
				height: "100px",
				marginTop : oldY,
				marginLeft : oldX
			},{duration: 1000 , easing: "easeOutElastic", complete:function(){
			
				$('#overlay').fadeOut(150);
				active = false;
				$(selected).css('z-index',500);
			}});
		
		}
		
	});
	
	$('#overlay').live('click',function(){
	
		$(selected).attr('src',oldSRC);
		$(selected).stop(true,false).animate({
			width: "100px",
			height: "100px",
			marginTop : oldY,
			marginLeft : oldX
		},{duration: 1000 , easing: "easeOutElastic", complete:function(){
		
			$('#overlay').fadeOut(150);
			
			$(selected).css('z-index',500);
		}});
		
		active = false;
	
	});
	

});

function imageCounter3(){
	$('#image-counter-numbers-3').animate({
		backgroundPosition: "0px 1300px"
	},800,'linear',function(){ $(this).css('background-position','0px 0px'); imageCounter3(); });
}

function imageCounter2(){
	$('#image-counter-numbers-2').animate({
		backgroundPosition: "0px 1300px"
	},1500,'linear',function(){ $(this).css('background-position','0px 0px'); imageCounter2(); });
}

function imageCounter1(){
	$('#image-counter-numbers-1').animate({
		backgroundPosition: "0px 1300px"
	},3000,'linear',function(){ $(this).css('background-position','0px 0px'); imageCounter1(); });
}

function imageCounter0(){
	$('#image-counter-numbers-0').animate({
		backgroundPosition: "0px 1300px"
	},4000,'linear',function(){ $(this).css('background-position','0px 0px'); imageCounter0(); });
}

function imageCounter(pos){
	$('#image-counter-numbers').animate({
		backgroundPosition: "0px "+pos+"px"
	},500,function(){ setTimeout(function(){imageCounter(pos+130);},500); });
}



function startSpinningCounter(n,h){

	$('#'+n+' ul li').stop(true,true).animate({
		backgroundPosition: "0px 2000px"
	},2000,'linear');

}


function setRealTimeCounter(targetNumber,div,numHeight){
	
	rcNum = $('#'+div+' ul li');
		
	i = 0;
	while(i < targetNumber.length){
		
		newNumber = targetNumber.charAt(i);
		newNumberPos = (newNumber * numHeight) + numHeight;
		
		$(rcNum[i]).animate({
			backgroundPosition: "0px "+ newNumberPos+"px"
		},{duration: 2000 , easing: "easeOutElastic"} );
	
		i++;					
	}
}
 


function showInfoUK(){
	$('#switch-us').fadeOut(300,function(){
		$('#switch-uk').fadeIn(300);
	}); 
	if(parseFloat($('#contact-switch-slider').css('margin-left')) > 50){
		$('#contact-switch-slider').animate({
			marginLeft: "-=70px"
		}, 150);
		
	}
	
	$('#pin-us').empty();
	$('#pin-uk').append('<div class="map-ripple"></div>');
}

function showInfoUS(){
	$('#switch-uk').fadeOut(300,function(){
		$('#switch-us').fadeIn(300);
	}); 
	if(parseFloat($('#contact-switch-slider').css('margin-left')) < 50){
		$('#contact-switch-slider').animate({
			marginLeft: "+=70px"
		}, 150);
		
	}
	
	$('#pin-uk').empty();
	$('#pin-us').append('<div class="map-ripple"></div>');
}



function shuffleCounterNumbers(div,numHeight){
	rcNum = $('#'+div+' ul li');
	
	i = 0;
	while(i < 8){
		ranPos = (Math.random(50000)+'').substr(2,3);
		$(rcNum[i]).css('background-position','0px '+ranPos+'px');
		i++;	
	}
}

function numRange(n,o){
	if (n > o){
		return n - o;
	} else {
		return o - n;
	}
}

function dropMapPins(){
	
	var mapPins = $('.map-pin');
	
	i = 0;
	var pinAnimation = setInterval(function(){
		
		if(i < mapPins.length){
		
			mT = parseFloat($(mapPins[i]).css('margin-top'));
			
			$(mapPins[i]).css('margin-top', '-400px');
			
			$(mapPins[i]).animate({
				marginTop: mT+'px',
				opacity : 1
			},300,function(){ $(this).css('opacity','none'); });
			
			
			i++;
		} else {
			clearInterval(pinAnimation);
		}
	},100);
	

}
