swfobject.registerObject("soundManager", "10.0.0");

var indexX = 0;
var indexY = 0;
//
var startX;
var startY;
//
var stopX;
var stopY;
//
var totalGalleries = -1;
//
var window_height = $(window).height();
var window_width = $(window).width();
var imagewidth = 1440;
var imageheight = 900;

var scaleMode;
var directionVar;
var directionX;
var directionY;
//
var newTop;
var newLeft;
//
var movementVar = 50;
var animationTime = 800;
var animationType = "easeInOutQuad";
var delayTime = 0;
//
var flashVideo = swfobject.getObjectById("myFlashContent");
var flashLoaded = true;
//
var currentId;
var currentUrl;
var firstProject = true;
//
var client;
var classes;
var insideClient = false;
var texten;
var insideProject = false;
var project;
var backgroundImage;
var backgroundImageFixed;
var infoToShow;
var infoToShowFixed;
//
var rebuildArray;
var videoPlayer;
var hasSound = false;
var yearToShow;
//
var home = true;
//
var flashMovie;
var flashReadyVar;

function address_handler(){
  
  $.address.init(function(event) {
		// Hello, I'm setting up the adress handler
    var initAdressArray = event.value.split('/');
		
		if(initAdressArray[1] != "" && initAdressArray[1] != "home" ) {
			// Add Query for loading projects by URL field
			
			var projectItem = $('.projectwrapper').find('#'+initAdressArray[1]+'');
			projectItem = projectItem.parent();
			
			var projectItemId = projectItem.attr('class');
			projectItemId = projectItemId.substring(projectItemId.search("infoId"));
			projectItemId = projectItemId.replace("infoId", "");
			
			currentUrl = initAdressArray[1];
			indexX = initAdressArray[2];
			loadProject(projectItemId, currentUrl, indexX);
		}

  }).change(function(event) {
    
    var adressArray = event.value.split('/');
    
		if(adressArray[1] == "") {
			addressHome();
			
			return false;
		}
		else if(adressArray[1] == "home") {
			// Go home
			indexY = -1;
			
			animateProject();
			if(hasSound)
			{
				lowPitch();	
			}

		}
		else if(adressArray[1] != "home" && adressArray[1] != "contact" && totalGalleries > 0){
			// Slide to Project Index
			indexY = 0;
			
			indexX = adressArray[2];
			animateProject();
			if(hasSound)
			{
				highPitch();
			}
		}
		position_info();
  });
  
}

function animateProject() {
	// Get vars
	if(indexY == -1) {
		// If we are Home
		newTop =  (-window_height * (indexY));
	} else {
		newTop =  (-window_height * (indexY) -10);
	}
	newLeft =  -window_width * (indexX) - (10*(indexX));
	$("#innerWrapper").stop().animate({
	  left  : newLeft,
	  top   : newTop
	}, animationTime, animationType );
	//
	$(".home").animate({
	  left  : -newLeft
	}, animationTime );
	//
	//updatePagezor();
	//
	$("#pagezor ul li.active").removeClass("active");
	$("#pagezor .boll:eq("+indexX+")").parent().addClass("active");
	//
	
}

function updatePagezor() {
  
  // Update Pagezor
  $('#pagezor ul').html('');
  totalGalleries = $('.gallery').size();
  for (var i = 0 ;i < totalGalleries;i++)
  {
    var navVar = "$.address.value('/"+currentUrl+"/"+i+"/');";
    var appendVar = '<li><a onClick="'+navVar+'" class="boll"></a></li>';
    $('#pagezor ul').append(appendVar);
  }
}

function position_info()
{
  
  var infopopup = $('#infopopup');

  if( indexY == -1 )
  {
    infopopup.css({
      'position'  : 'absolute'
    });
  }
  else
  {
    infopopup.css({
      'position'  : 'fixed'
    });
  }
}

function addressHome(){
  $.address.value('/home/');
}

function hypersize() {

  window_width = $(window).width();
  window_height = $(window).height();

  var imageratio = imageheight/imagewidth;
  var browserratio = window_height/window_width;

  if(indexY == -1)
  {
    newTop =  (-window_height * (indexY));
  }
  else
  {
    newTop =  (-window_height * (indexY) -10);
  }
  
  newLeft =  -window_width * (indexX) - (10*(indexX));
  movementVar = 0.10 * window_width;
  
  
  
  // Place home and contact
  $('#innerWrapper').css({
    'left': newLeft,
    'top': newTop
  });
  
  $('.home').css({
    'top'     : -window_height + 'px',
    'left'    : -$("#innerWrapper").offset().left,
    'width'   : window_width +'px',
    'height'  : window_height +'px'
  });
  
  $('.gallery').each(function(){
    $(this).css({
     'width'   : window_width+'px',
     'height'  : window_height+'px'
    });
  });
  
  if (browserratio < imageratio)
  { 
    $('.gallery img, .home_bg img, .error_bg img').css({
      'width'   : window_width + 'px',
      'height'  : window_width * imageratio + 'px'
    });
  }
  else
  {
    $('.gallery img, .home_bg img, .error_bg img').css({
      'width'   : window_height / imageratio + 'px',
      'height'  : window_height + 'px'
    });
  }
  
  $('.gallery img').css({
    'margin-left' : (window_width - $('.gallery img').width())/2,
    'margin-top'  : (window_height - $('.gallery img').height())/2
  });
  
  $('.home_bg img, .error_bg img').css({
    'margin-left' : (window_width - $('.home_bg img, .error_bg img').width())/2,
    'margin-top'  : (window_height - $('.home_bg img, .error_bg img').height())/2
  });
  
}

function triggerMouseUp()
{
	$('#innerWrapper').trigger('mouseup');
}

function flashReady() {
	
	// Init SoundManager
	flashMovie = swfobject.getObjectById("soundManager");
	flashReadyVar = true;
}

function loadSounds() {
	flashMovie.loadSound01("http://dev.kiwiab.se/assets/sounds/knapp7.mp3");
}

function draggable_navigation(){

  var innerWrapper = $('#innerWrapper');
  
  innerWrapper.draggable({
		start: function() {
			startX = innerWrapper.offset().left;
			startY = innerWrapper.offset().top;
		},
		stop: function() {
			stopX = innerWrapper.offset().left;
			stopY = innerWrapper.offset().top;
			//			
			$(this).delay(delayTime,function(){
				getMovement();
			});
		}
	});
}

function getMovement()
{	
	// What direction are we heading
	directionX = Math.abs(startX - stopX);
	directionY = Math.abs(startY - stopY);
	directionVar = Math.max(directionX,directionY);
	
	// Go Vertical
	if(directionVar == directionY)
	{
		// Vertical direction
		if(startY - stopY < 0) {
			directionY = "up";
			//
			if(startY - stopY < -movementVar && indexY > -1)
			{
				indexY--;
			}
			else
			{
				animateProject();
			}
		}
		else {
			directionY = "down";
			//
			if(startY - stopY > movementVar && indexY < 0 && totalGalleries > 0)
			{
				indexY++;
			}
			animateProject();
		}
		updateYPage();
	}
	// Go Horizontal
	else if(directionVar == directionX)
	{
		if(startX - stopX < 0)
		{
			directionX = "left";
			if(startX - stopX < -movementVar && indexX > 0)
			{
				indexX--;
			}
			else
			{
				animateProject();
			}
		}
		else
		{
			directionX = "right";
			//
			if(startX - stopX > movementVar && indexX < (totalGalleries-1))
			{
				indexX++;
			}
			else
			{
				animateProject();
			}
		}
		$.address.value('/'+currentUrl+'/'+indexX+'/');
		return false;
	}
}

function updateYPage() {
	// Set level Y
	if(indexY >= 0) {
		$.address.value('/'+currentUrl+'/'+indexX+'/');
		return false;
	}
	else
	{
		addressHome();
		return false;
	}
	
}

function loadProject(currentId, currentUrl, indexX)
{
	insideProject = true;
	$.getJSON("assets/querys/get.php?id="+currentId+"&get=images-jSON", function(data){
		// Clear wrapper
		$(".gallery_wrapper").html('');
		rebuildArray = [];
			
		// Loop out images
  		$.each(data, function(i,item){
		    $(".gallery_wrapper").append('<div class="content_holder gallery"><img src="assets/img/'+item+'" alt="" /></div>');
		    rebuildArray.push('<div class="content_holder gallery"><img src="assets/img/'+item+'" alt="" /></div>');
		});
		
		// Load Video
		$.ajax({
			type: 'GET',
			url: 'assets/querys/get.php?get=video',
			data: ({
				//Project ID
				id: currentId
			}),
			cache: false,
			success: function(html){
				// Add video
				if(html != "") {
					videoPlayer = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="myFlashContent"><param name="movie" value="assets/swf/VideoPlayer.swf" /><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="flashvars" value="urlPath=http://s76078.gridserver.com/hyundai/HyundaiBanner_Stream_v1.flv&amp;movieDescription=Hejsvejs" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="assets/swf/VideoPlayer.swf" width="100%" height="100%"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="flashvars" value="urlPath=http://s76078.gridserver.com/hyundai/HyundaiBanner_Stream_v1.flv&amp;movieDescription=Hejsvejs" /><!--<![endif]--><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object>';
					//$(".gallery_wrapper td:last").append('<div class="content_holder gallery">'+videoPlayer+'</div>');
					//var videoPlayer = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/0UE3CNu_rtY&hl=sv_SE&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/0UE3CNu_rtY&hl=sv_SE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
					$(".gallery_wrapper tr").append('<td><div class="content_holder gallery videoslide" style="width: 1043px; height: 673px; ">'+videoPlayer+'</div></td>');
					$('object').css('visibility', 'visible');
					//updatePagezor();
				}
			}
		});

		// Load Speech
		$.ajax({
			type: 'GET',
			url: 'assets/querys/get.php?get=speechSound',
			data: ({
				//Project ID
				id: currentId
			}),
			cache: false,
			success: function(html){
				// Play sound
				if(html != "") {
					var speechURL = "assets/img/"+html;
					playSpeech(speechURL);
					hasSound = true;
					
				}
			}
		});

		//Remove background
		$(".gallery_wrapper").wrapInner("<table><tr>");
		$(".gallery").wrap("<td>");
			
		// Update pagezor
		$('#pagezor ul').html('');
		totalGalleries = $('.gallery').size();
		for (var i = 0 ;i < totalGalleries;i++)
		{
			var navVar = "$.address.value('/"+currentUrl+"/"+i+"/');";
			var appendVar = '<li><a onClick="'+navVar+'" class="boll"></a></li>';
			$('#pagezor ul').append(appendVar);
		}
		
    hypersize();
		
		// Animate
		window_height = $(window).height();
		window_width = $(window).width();
		newLeft =  -window_width * (indexX) - (10*(indexX));
		$("#innerWrapper").stop();
		$("#innerWrapper").animate({
		  left  : newLeft
		}, 800, "easeInOutQuad" );
		//
		$(".home").animate({
		  left  : -newLeft
		}, 800 );
		//
		$("#pagezor ul li.active").removeClass("active");
		$("#pagezor .boll:eq("+indexX+")").parent().addClass("active");
			
	});
}

function key_navigation(){
  
  function check_key(e)
  {
    // Navigate right
    if(e.keyCode == 39 && indexX < (totalGalleries-1)) {
			indexX++;
			$.address.value('/'+currentUrl+'/'+indexX+'/');
		}
		// Navigate left
		else if(e.keyCode == 37 && indexX > 0) {
			indexX--;
			$.address.value('/'+currentUrl+'/'+indexX+'/');
		}
		// Navigate up
		else if(e.keyCode == 38 && indexY > -1) {
			indexY--;
			addressHome();
			
		}
		// Navigate down
		else if(e.keyCode == 40 && indexY == -1 && totalGalleries !== 0) {
			indexY++;
			$.address.value('/'+currentUrl+'/'+indexX+'/');
			
		}
  }
  
	if ($.browser.mozilla)
	{
	  $(document).keypress(check_key);
	}
  else
  {
    $(document).keydown(check_key);
  }
  
}

function displayHomeBg(){
  $('.home_bg').html('<img src="assets/img/home.jpg" class="home_picture" alt="" />');
  hypersize();
}

function hideProjects(){
	$('.projectname li').not('.projectyear').each(function(){
		$(this).hide();
	});
	$('.projectyear').hide();
}

function hideInfo(){
	$('.clientdescription').hide();
	$('.projectdescription').hide();
}

function show_kiwi_desc()
{
  $('.clientdescription.clientId0').show();
}

function hide_kiwi_desc()
{
  $('.clientdescription.clientId0').hide();
}

function set_css(){
  //Set .gallery width and height equal to browser width
  $(".gallery").css('width', parseInt(window_width, 10) + 'px');
  $(".gallery").css('height', parseInt(window_height, 10) + 'px');
  
  //Set .home width and height equal to browser width
  $(".home").css('width', parseInt(window_width, 10) + 'px');
  $(".home").css('height', parseInt(window_height, 10) + 'px');
}

function logotype(){
  $('#logotype').click(function() {
		hideProjects();
		hideInfo();
		displayHomeBg();
		insideClient = false;
		insideProject = false;
		indexX = 0;
		totalGalleries = -1;
		//addressHome();
		$('.gallery_wrapper').html('');
		$('#pagezor ul').html('');
		show_kiwi_desc();
		home = true;
    $('.clientname a, .projectname a').each(function(){
      $(this).removeClass('current');
    });
	});
	
	$('#logotype').hover(function(){
	  hideProjects();
		hideInfo();
		displayHomeBg();
		show_kiwi_desc();
	},function(){
	  if ( home === false )
	  {
	    hide_kiwi_desc();
	  }
	  
    if( insideClient === true && insideProject === false ){
      hideInfo();
      hideProjects();
      $('.projectname li.project'+insideClientID).each(function(){
        $(this).show();
        yearToShow = $(this).attr('class');
        yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
        yearToShow = yearToShow.replace("year", "");
        $('.projectyear').each(function() {
          texten =	$(this).text();
          if(jQuery.trim($(this).text()) == yearToShow){
            $(this).show();
          }
        });
      });
      $('.home_bg').html('<img src="'+backgroundImageFixed+'" alt="" />');
      $('.clientdescription.clientId'+insideClientID).show();
    }else if(insideProject === true){
      $('.home_bg').html('<img src="'+backgroundImageFixedProject+'" alt="" />');
      hideInfo();
      $('.projectId'+infoToShowFixed).show();
      
      $('.projectname li.project'+insideClientID).each(function(){
  			$(this).show();
  			yearToShow = $(this).attr('class');
  			yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
  			yearToShow = yearToShow.replace("year", "");
  			$('.projectyear').each(function() {
  				texten =	$(this).text();
  				if(jQuery.trim($(this).text()) == yearToShow){
  					$(this).show();
  				}
  			});
      });
      
    }
    hypersize();
	});
}

function client_menu(){
  //Hover
	$('.clientname ul li').hover(function() {
		hideProjects();
		// Play Sound
		sendMouseOverSound();
		
		classes = $('a',this).attr('className').split(' ');
    for (i = 0; i < classes.length; i++){
      if ( classes[i].substr(0,6) == 'client' ){
        client = classes[i].substr(6);
        break;
      }
    }
    
    backgroundImage = $('a', this).siblings('.bgimage'+client).children('img:eq(0)').attr('src');
    $('.home_bg').html('<img src="'+backgroundImage+'" alt="" />');
    
		$('.projectname li.project'+client).each(function(){
			$(this).show();
			yearToShow = $(this).attr('class');
			yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
			yearToShow = yearToShow.replace("year", "");
			$('.projectyear').each(function() {
				texten =	$(this).text();
				if(jQuery.trim($(this).text()) == yearToShow)
				{
					$(this).show();
				}
			});
		});
		
		hideInfo();
		if(client != 33 && client != 46 && client != 47){
			$('.clientdescription.clientId'+client).show();
		}
		
		hypersize();
	},function(){
	  
	  
    if( insideClient ){
      hideInfo();
      hideProjects();
      $('.projectname li.project'+insideClientID).each(function(){
        $(this).show();
        yearToShow = $(this).attr('class');
        yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
        yearToShow = yearToShow.replace("year", "");
        $('.projectyear').each(function() {
          texten =	$(this).text();
          if(jQuery.trim($(this).text()) == yearToShow){
            $(this).show();
          }
        });
      });
      $('.home_bg').html('<img src="'+backgroundImageFixed+'" alt="" />');
      $('.clientdescription.clientId'+insideClientID).show();
    }else if(insideProject === true){
      $('.home_bg').html('<img src="'+backgroundImageFixedProject+'" alt="" />');
      hideInfo();
      $('.projectId'+infoToShowFixed).show();
    }else{
      $('.home_bg').html('<img src="assets/img/home.jpg" alt="" />');
      hideInfo();
      hideProjects();
      show_kiwi_desc();
    }
    hypersize();
  });
  
  
  
  $('.clientname ul li').click(function(){
    
    hideProjects();
    insideClient = true;
    insideProject = false;
    home = false;
    indexX = 0;
    totalGalleries = -1;
    //addressHome();
    $('.gallery_wrapper').html('');
    $('#pagezor ul').html('');
    
    classes = $('a',this).attr('className').split(' ');
    for (i = 0; i < classes.length; i++){
      if ( classes[i].substr(0,6) == 'client' ){
        client = insideClientID = classes[i].substr(6);
        break;
      }
    }
    $('.projectname li.project'+insideClientID).each(function(){
			$(this).show();
			yearToShow = $(this).attr('class');
			yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
			yearToShow = yearToShow.replace("year", "");
			$('.projectyear').each(function() {
				texten =	$(this).text();
				if(jQuery.trim($(this).text()) == yearToShow){
					$(this).show();
				}
			});
    });
    
    backgroundImageFixed = $('a', this).siblings('.bgimage'+client).children('img:eq(0)').attr('src');
    $('.home_bg').html('<img src="'+backgroundImageFixed+'" alt="" />');
      
    hypersize();
    
  });
  
  $('.clientname a').click(function(){
    $('.clientname a, .projectname a').each(function(){
      $(this).removeClass('current');
    });
    $(this).addClass('current');
  });
  
}

function project_menu(){
  //Hover
  $('.projectname li:not(.projectyear)').hover(function() {
    
    sendMouseOverSound();
    infoToShow = $(this).attr('class');
    infoToShow = infoToShow.substring(infoToShow.search("infoId"));
    infoToShow = infoToShow.replace("infoId", "");
    
    hideInfo();
    $('.projectId'+infoToShow).show();
    
    classes = $(this).attr('className').split(' ');
    for (i = 0; i < classes.length; i++){
      if ( classes[i].substr(0,7) == 'project' ){
        project = classes[i].substr(7);
        break;
      }
    }
    backgroundImage = $(this).children('.bgimage'+project).children('img:eq(0)').attr('src');
    $('.home_bg').html('<img src="'+backgroundImage+'" alt="" />');
    hypersize();
    
  },function(){
    hideInfo();
    hideProjects();
    $('.projectname li.project'+insideClientID).each(function(){
      $(this).show();
      yearToShow = $(this).attr('class');
      yearToShow = yearToShow.substring(yearToShow.search("year"), yearToShow.search("year")+6);
      yearToShow = yearToShow.replace("year", "");
      $('.projectyear').each(function() {
        texten =	$(this).text();
        if(jQuery.trim($(this).text()) == yearToShow){
          $(this).show();
        }
      });
    });
    
    if( insideProject )
    {
      $('.home_bg').html('<img src="'+backgroundImageFixedProject+'" alt="" />');
      $('.projectId'+infoToShowFixed).show();
      
    }
    else
    {
      $('.home_bg').html('<img src="'+backgroundImageFixed+'" alt="" />');
      $('.clientdescription.clientId'+insideClientID).show();
    }
    hypersize();
	});
	
	// Project click
	$('.projectname li:not(.projectyear)').click(function() {
  	
		insideProject = true;
		home = false;
		
		// Store url
		currentUrl = $(this).find('a').attr("id");
		currentId = $(this).attr('class');
		currentId = currentId.substring(currentId.search("infoId"));
		currentId = currentId.replace("infoId", "");
		
		$.getJSON("assets/querys/get.php?id="+currentId+"&get=images-jSON", function(data){
			// Clear wrapper
			$(".gallery_wrapper").html('');
			rebuildArray = [];
				
			// Loop out images
      $.each(data, function(i,item){
        $(".gallery_wrapper").append('<div class="content_holder gallery"><img src="assets/img/'+item+'" alt="" /></div>');
        rebuildArray.push('<div class="content_holder gallery"><img src="assets/img/'+item+'" alt="" /></div>');
      });
        
			// Load Video
			$.ajax({
				type: 'GET',
				url: 'assets/querys/get.php?get=video',
				data: ({
					//Project ID
					id: currentId
				}),
				cache: false,
				success: function(html){
					// Add video
					if(html != "") {
						videoPlayer = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="myFlashContent"><param name="movie" value="assets/swf/VideoPlayer.swf" /><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="flashvars" value="urlPath=http://s76078.gridserver.com/hyundai/HyundaiBanner_Stream_v1.flv&amp;movieDescription=Hejsvejs" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="assets/swf/VideoPlayer.swf" width="100%" height="100%"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="flashvars" value="urlPath=http://s76078.gridserver.com/hyundai/HyundaiBanner_Stream_v1.flv&amp;movieDescription=Hejsvejs" /><!--<![endif]--><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object>';
						//$(".gallery_wrapper td:last").append('<div class="content_holder gallery">'+videoPlayer+'</div>');
						//var videoPlayer = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/0UE3CNu_rtY&hl=sv_SE&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/0UE3CNu_rtY&hl=sv_SE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
						$(".gallery_wrapper tr").append('<td><div class="content_holder gallery videoslide" style="width: 1043px; height: 673px; ">'+videoPlayer+'</div></td>');
						$('object').css('visibility', 'visible');
						updatePagezor();
					}
				}
			});

			// Load Speech
			$.ajax({
				type: 'GET',
				url: 'assets/querys/get.php?get=speechSound',
				data: ({
					//Project ID
					id: currentId
				}),
				cache: false,
				success: function(html){
					// Play sound
					if(html != "") {
						var speechURL = "assets/img/"+html;
						playSpeech(speechURL);
						hasSound = true;
					}
					else
					{
						hasSound = false;
					}
				}
			});

			// Follow the mouse
			if(firstProject) {
				$("#follower").fadeIn('slow');
				$("#follower").animate({opacity: 1.0}, 10000).fadeOut('slow', function() {
          $(this).remove();
					firstProject = false;
				});
				$(document).mousemove(function(e){
				  $("#follower").css({
				    top: (e.pageY - 5) + "px",
				    left: (e.pageX + 15) + "px"
				  });
				});
			}
				
			//Remove background
			$(".gallery_wrapper").wrapInner("<table><tr>");
			$(".gallery").wrap("<td>");
				
			// Update pagezor
			updatePagezor();
			// Navigate to new gallery
			indexX = 0;
			$.address.value('/'+currentUrl+'/'+indexX+'/');
			
			// Update sizes
			hypersize();
			
			
    });
    
    classes = $(this).attr('className').split(' ');
    for (i = 0; i < classes.length; i++){
      if ( classes[i].substr(0,7) == 'project' ){
        project = classes[i].substr(7);
        break;
      }
    }
    
    backgroundImageFixedProject = $(this).children('.bgimage'+project).children('img:eq(0)').attr('src');
    $('.home_bg').html('<img src="'+backgroundImageFixedProject+'" alt="" />');
    
    infoToShowFixed = $(this).attr('class');
    infoToShowFixed = infoToShow.substring(infoToShow.search("infoId"));
    infoToShowFixed = infoToShow.replace("infoId", "");
    
    hideInfo();
    $('.projectId'+infoToShowFixed).show();
  });
  
  $('.projectname a').click(function(){
    $('.projectname a').each(function(){
      $(this).removeClass('current');
    });
    $(this).addClass('current');
  });
  
}

function nav_icons(){
  // Navigate home
	$('#homeIcon').click(function(){
		addressHome();
		return false;
	});
	
	$('#infoIcon').toggle(function(){
	  
	  
	  if ( home )                                     { hide_kiwi_desc(); }
    if ( insideClient && insideProject === false )  { $('.clientdescription.clientId'+insideClientID).hide(); }
    if ( insideProject )                            { $('.projectdescription.projectId'+infoToShowFixed).hide(); }
	  
	},function(){
	  
	  
	  if ( home )                                     { show_kiwi_desc(); }
	  if ( insideClient && insideProject === false )  { $('.clientdescription.clientId'+insideClientID).show(); }
	  if ( insideProject )                            { $('.projectdescription.projectId'+infoToShowFixed).show(); }
	  
	});
  
	// Sound on/off
	$('#soundIcon').click(function(){
		flashMovie.toggleSounds();
	});
}

function new_window(){
  $('#infopopup a').click(function(){
    window.open(this.href);
    return false;
  });
}

function sendMouseOverSound() {
	if(flashReadyVar) {
		flashMovie.playHover();
	}
}

function highPitch() {
	if(flashReadyVar) {
		flashMovie.highPitch();
	}
}

function lowPitch() {
	if(flashReadyVar) {
		flashMovie.lowPitch();
	}
}

function playSpeech(soundToPlay) {
	if(flashReadyVar) {
		flashMovie.loadSound02(soundToPlay);
	}
}

function sound_icon(){
  
  var soundActive = true;
  $('#interactionIcons #soundIcon').toggle(function(){
    soundActive = false;
    $(this).css({
      'background-position' : '0 -102px'
    });
  },function(){
    soundActive = true;
    $(this).css({
      'background-position' : '0 -34px'
    });
  });
  
  // Sound icon hover
  $('#interactionIcons #soundIcon').hover(function(){
    if ( soundActive == true ){
      $(this).css({
        'background-position' : '0 -34px'
      });
    }else{
      $(this).css({
         'background-position' : '0 -102px'
       });
    }
  }, function(){
    if ( soundActive == true ){
      $(this).css({
        'background-position' : '0 0'
      });
    }else{
      $(this).css({
        'background-position' : '0 -68px'
      });
    }
  });
  
}

function home_icon(){
  // Home icon hover
  $('#interactionIcons #homeIcon').hover(function(){
    $(this).css({
      'background-position' : '0 -38px'
    });
  }, function(){
    $(this).css({
      'background-position' : '0 0'
    });
  });
}

function info_icon(){
  // Info icon hover
  $('#interactionIcons #infoIcon').hover(function(){
    $(this).css({
      'background-position' : '0 -35px'
    });
  }, function(){
    $(this).css({
      'background-position' : '0 0'
    });
  });
}



jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	return this;
};


$(window).ready(function(){
  
  try { hypersize(); }            catch(e)  { /*console.log(e);*/ }
  
  try { address_handler(); }      catch(e)  { /*console.log(e);*/ }
  try { draggable_navigation(); } catch(e)  { /*console.log(e);*/ }
  try { key_navigation(); }       catch(e)  { /*console.log(e);*/ }
  
  try { displayHomeBg(); }        catch(e)  { /*console.log(e);*/ }
  try { hideProjects(); }         catch(e)  { /*console.log(e);*/ }
  try { hideInfo(); }             catch(e)  { /*console.log(e);*/ }
  
  try { show_kiwi_desc(); }       catch(e)  { /*console.log(e);*/ }
  try { set_css(); }              catch(e)  { /*console.log(e);*/ }
  
  try { logotype(); }             catch(e)  { /*console.log(e);*/ }
  try { client_menu(); }          catch(e)  { /*console.log(e);*/ }
  try { project_menu(); }         catch(e)  { /*console.log(e);*/ }
  
  try { nav_icons(); }            catch(e)  { /*console.log(e);*/ }
  
  try { sound_icon(); }           catch(e)  { /*console.log(e);*/ }
  try { home_icon(); }            catch(e)  { /*console.log(e);*/ }
  try { info_icon(); }            catch(e)  { /*console.log(e);*/ }
  
  try { new_window(); }           catch(e)  { /*console.log(e);*/ }

});

$(window).resize(function() {
  hypersize();
});

