﻿if(!circuit){ var circuit = {}; }

$(document).ready(function(){
	if($('img').length > 0) circuit.AlignImages();
	if($('.image-grid').length > 0) circuit.FadeImages();
	if($('.player').length > 0) circuit.YouTube();
	circuit.KonamiCode();
	
	$("#mc-embedded-subscribe").live('click', function(e) {
		e.preventDefault();
		circuit.MailChimp();
	});
	$("nav.menu select").change(function() {
		window.location = $(this).find("option:selected").val();
	});

	// ABOUT
	if($('.employees').length > 0) {
		$(document).mousemove(function(e){
			$('.employee-img').each(function(){
				// x
				var imgX = "-85px";
				if(e.pageX > ($(this).offset().left + 85)){
					imgX = "-170px";
				}else if(e.pageX < ($(this).offset().left)){
					imgX = "0";
				}else{
					imgX = "-85px";
				}
				
				// y
				var imgY = "-85px";
				if(e.pageY > ($(this).position().top + 85)){
					imgY = "-170px";
				}else if(e.pageY < ($(this).position().top + 0)){
					imgY = "0";
				}else{
					imgY = "-85px";
				}
				
				$(this).children('img').css({'left':imgX,'top':imgY});
			});
		});
	} else if ($('.contacts, .published').length > 0) {
		$(document).mousemove(function(e){
			$('.contacts li .contact-image, .published span').each(function(){
				// x
				var imgX = "-45px";		
				if(e.pageX > ($(this).offset().left + 45)){
					imgX = "-90px";
				}else if(e.pageX < ($(this).offset().left)){
					imgX = "0";
				}else{
					imgX = "-45px";
				}
				
				// y
				var imgY = "-45px";
				if(e.pageY > ($(this).position().top + 45)){
					imgY = "-90px";
				}else if(e.pageY < ($(this).position().top + 0)){
					imgY = "0";
				}else{
					imgY = "-45px";
				}
				
				$(this).children('img').css({'left':imgX,'top':imgY});
			});
		});
	}
});

circuit.AlignImages = function() {
	$('#page-content img.align-left, #page-content img.align-right').each(function() {
		var image = $(this);
    	image.wrap('<div class="figure '+ image.attr('class') +'"><table><tbody><tr><td></td></tr></tbody></table></div>');
    	image.closest('tbody').after('<caption align="bottom">'+ image.attr('alt') +'</caption>');
	});
}

circuit.FadeImages = function() {
	$(".image-grid ul li > a, .image-grid ul li > span").hover(function() {
		$('span', this).stop().animate({"opacity": "0"}, 200);
	}, function() {
		$('span', this).stop().animate({"opacity": "1"}, 1000);
	});
}

circuit.MuteYouTube = function(player) {

    if (typeof (player) != 'undefined') {
        player.tubeplayer("mute");
    }

}

circuit.YouTube = function(sound) {
    $('.player').each(function () {
        var player = $(this);
        var playsound = sound;
        if (typeof (circuit.MuteYouTube) != 'undefined') {

            var videoid = player.attr('rel');
            var tubePlayer = player.tubeplayer({
                autoPlay: 1,
                iframed: true,
                showControls: 0,
                width: 240, // the width of the player
                height: 240, // the height of the player
                showinfo: 0,
                loop: 1,
                onPlayerPlaying: function () {
                    if (playsound != 'play') {
                        circuit.MuteYouTube(player);
                    }
                },
                onPlayerEnded: function () {
                    player.tubeplayer("play");
                },
                allowFullScreen: "true", // true by default, allow user to go full screen
                initialVideo: videoid, // the video that is loaded into the player
                preferredQuality: "default", // preferred quality: default, small, medium, large, hd720
                onPlay: function (id) { }, // after the play method is called
                onPause: function () { }, // after the pause method is called
                onStop: function () { player.tubeplayer("play"); }, // after the player is stopped
                onSeek: function (time) { }, // after the video has been seeked to a defined point
                onMute: function () { }, // after the player is muted
                onUnMute: function () { } // after the player is unmuted
            });

            if (player.tubeplayer("isMuted") === false && playsound != 'play') {
                player.tubeplayer("mute");
            }

        }
    });
}

circuit.MailChimp = function() {

	$('#mce-success-response').hide();
	$('#mce-error-response').hide();

	var email = $('#mce-EMAIL').val();	
	var rege = /^([A-Za-z0-9_\+\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(email == '') {
		$('#mce-error-response').show().html('Fyll i din e-postadress i fältet ovan');
	} else if(rege.test(email) && email != '') {
		$.ajax({
			type: 'GET',
			url: 'http://circuit.us2.list-manage.com/subscribe/post-json?u=75a890ad290877a92434da7f7&id=8fd9842df0&c=?',
			dataType: 'json',
			data: 'EMAIL=' + email,
			success: function(resp){
				if (resp.result=="success"){
					$('#mce-'+resp.result+'-response').show();
					$('#mce-'+resp.result+'-response').html(resp.msg);
				} else {
					$('#mce-error-response').html(resp.msg);
				}
			}
		});
	} else {
		$('#mce-error-response').show().html('Felaktigt ifylld e-postadress');
	}

}

circuit.EasterEgg = function() {
	$('.circuit-board .board-image').html('').removeClass('c1 c2 c3 c4 c5 c6 c7');
	$('.circuit-board .board-wrapper').find('img, .player').remove();
	$('.circuit-board .board-wrapper').prepend('<img class="nyan" src="../Templates/Styles/gfx/nyancat.gif" />');
	$('.circuit-board').append('<div id="hiddenplayer" class="player" rel="QH2-TGUlwu4" />');
	circuit.YouTube('play');
}

circuit.KonamiCode = function() {
	var easterKonami = false;
	if (window.addEventListener) {
	    var kkeys = [],
	        konami = "38,38,40,40,37,39,37,39,66,65";
	    window.addEventListener("keydown", function (e) {
	        kkeys.push(e.keyCode);
	        if (kkeys.toString().indexOf(konami) >= 0) {
	            if (!easterKonami) {
	                circuit.EasterEgg();
	            }
	            easterKonami = true;
	        }
	    }, true);
	}
}

