jQuery(function($) {
  
  $('#loading').after("<div id='loading-background'></div>");
  
  //PreLoad
  // Anzahl Bilder
  var amount = 7;
  // Anzahl geladener Bilder
  var loaded = 0;
  $('#img0, #img1, #img2, #img3, #img4, #img5, #img6, #img7, #img8, #img9, #img10, #img11, #img12, #img13, #img14, #img15, #img16, #img17, #img18').load(function(){
      loaded++;
      if(loaded >= amount){
          // fertig geladen
          $('#preload-container').remove();
          setTimeout(function(){
            $('#loading img#loading-gif').fadeToggle("normal");
          }, 1500);
          setTimeout(function(){
            $('#loading p img').fadeToggle("normal");
          }, 2000);
          setTimeout(function(){
            $('#loading').remove();
            $('#loading-background').fadeToggle("normal");
          }, 2500);
          setTimeout(function(){
            $('#loading-background').remove();
          }, 2800);
          
          setTimeout(function(){
            // Text Container wird eingeblendet
            $('.text').fadeToggle("fast", function(){
              $('.text').removeClass('hidden');
            });
          }, 2900);
          setTimeout(function(){
            // Text Container wird eingeblendet
            $('.text #home-container img').fadeToggle("fast");
          }, 3100);
          setTimeout(function(){
            // Text Container wird eingeblendet
            $('.text #home-container p').fadeToggle("fast");
          }, 3300);

          // Icons einblenden
          setTimeout(function(){
            $('#twitter').fadeToggle("fast"); 
          }, 3500);
          setTimeout(function(){
            $('#googleplus').fadeToggle("fast"); 
          }, 3600);
          setTimeout(function(){
            $('#androidweekly').fadeToggle("fast"); 
          }, 3700);
          setTimeout(function(){
            $('#mail').fadeToggle("fast"); 
          }, 3800);
          setTimeout(function(){
            $('#youtube').fadeToggle("fast"); 
          }, 3900);
          setTimeout(function(){
            $('#blogger').fadeToggle("fast"); 
          }, 4000);
          setTimeout(function(){
            $('#elements').fadeToggle("fast"); 
          }, 4100);
          setTimeout(function(){
            $('#maps').fadeToggle("fast"); 
          }, 4200);

          // erster Preil wird eingeblendet
          setTimeout(function(){
            $('img#help_one').fadeToggle("normal"); 
          }, 4300);
      }
  });

  
  // iPad Detection
  if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
    $('#handy').removeClass('p-effect');
    $('img#help_three').remove();
    $('#try-button').remove();
    $('#handy .background').css({
      "bottom" : "-120px",
      "left" : "-750px"
    });
  }

// Parallax positions
  var winheight = $(window).height();
  var winwidth = $(window).width();

  var background_left = 150;
  var background_bottom = -190;
  var handy_left = 300;
  var handy_bottom = -100;
  var text_left = -850;
  
  if (winheight < 720) {
    background_bottom = -220;
    $('#handy .background').css('bottom','-220px');
    handy_bottom = -260;
    $('#handy .device').css('bottom','-260px');
    $('#handy .text').css('bottom', '175px');
  }
  
  if (winwidth < 1310) {
    background_left = 450;
    $('#handy .background').css('left','-450px');
    handy_left = 680;
    $('#handy .device').css('left','-680px');
    text_left = -390;
    $('#handy .text').css('left','390px');
  }


  
  $('div#header-parallax.parallax').parallax({
    'elements': [
      // Handy
      {
        // initialposition Handy Background
        'selector': 'div#header-parallax  div#handy.p-effect .background',
        'properties': {
          'x': {
            'left': {
              'initial': background_left,
              'multiplier': 0.1,
              'invert': true
            }
          },
          'y': {
            'bottom': {
              'initial': background_bottom,
              'multiplier': 0.1,
              'invert': false
            }
          }
        }
      },
      {
        // initialposition Handy Device
        'selector': 'div#header-parallax  div#handy.p-effect .device',
        'properties': {
          'x': {
            'left': {
              'initial': handy_left,
              'multiplier': 0.15,
              'invert': true
            }
          },
          'y': {
            'bottom' : {
              'initial': handy_bottom,
              'multiplier': 0.15,
              'invert': false
            }
          }
        }
      },
        {
          // initialposition Handy Text
          'selector': 'div#header-parallax  div#handy.p-effect .text',
          'properties': {
            'x': {
              'left': {
                'initial': text_left,
                'multiplier': 0.2,
                'invert': true
              }
            }
          }
        }
    ]
  });

  
  
  // Parallax positions
    $('div.showcase-container.parallax').parallax({
      'elements': [
        // KAMERA
        {
          // initialposition Cam Background
          'selector': 'div.showcase-container.parallax #showcase-parallax.p-effect .background',
          'properties': {
            'x': {
              'left': {
                'initial': 90,
                'multiplier': 0.1,
                'invert': true
              }
            },
            'y': {
              'bottom': {
                'initial': -99,
                'multiplier': 0.1,
                'invert': false
              }
            }
          }
        },
        {
          // initialposition Cam Device
          'selector': 'div.showcase-container.parallax #showcase-parallax.p-effect .device',
          'properties': {
            'x': {
              'left': {
                'initial': 20,
                'multiplier': 0.2,
                'invert': true
              }
            },
            'y': {
              'bottom' : {
                'initial': -160,
                'multiplier': 0.2,
                'invert': false
              }
            }
          }
        }
      ]
    });
    
    // Kamera in Kamera
    $('#showcase-parallax.p-effect .device').parallax({
      'elements': [
        {
          'selector': 'div#cam-parallax',
          'properties': {
            'x': {
              'left': {
                'initial': -40,
                'multiplier': 0.05,
                'invert': false
              }
            },
            'y': {
              'top': {
                'initial': -30,
                'multiplier': 0.05,
                'invert': false
              }
            }
          }
        }
      ]
    });
  

  
  // Handy Icon Navigation
  $('.nav-button').click(function(ev) {
    // alle Pfeile werden geloscht
    $('img.help_arrow').fadeToggle("fast", function(){
      $('img.help_arrow').remove();
    });
    // erster Pfeil wird geloscht
    $('img#help_one').fadeToggle("fast", function(){
      $('img#help_one').remove();
    });
    // zweiter Pfeil wird erstellt
    setTimeout(function(){
      $('img#help_two').fadeToggle("normal"); 
    }, 400);
    // zweiter Pfeil kriegt Classe zum loschen
    $('img#help_two').addClass('help_arrow');
    // dritter Pfeil wird erstellt
    setTimeout(function(){
      $('img#help_three').fadeToggle("normal");
    }, 800);
    // dritter Pfeil kriegt Classe zum loschen
    $('img#help_three').addClass('help_arrow');
    // vierter Pfeil wird erstellt
    setTimeout(function(){
      $('img#help_four').fadeToggle("normal");
    }, 1200);
    // vierter Pfeil kriegt Classe zum loschen
    $('img#help_four').addClass('help_arrow');
    // Variable
    var container = $(this).attr('href');
    // alle Content Container werden ausgeblendet
    $('#header-parallax .content').addClass('hidden');
    $('.text').hide();
    // der richtige Content Container wird eingeblendet
    $(container).removeClass("hidden");
    $(".text").fadeIn("normal");
    // Allen Links wird die Classe active genommen
    $('.nav-button').removeClass('active');
    $('#nav li h3').removeClass('active');
    // Der gedruckte Link kriegt Classe active
    $(this).addClass('active');
    $(this).parent().children("h3").addClass('active');
    // Link wird deaktiviert
    return false;
  });
  
  // Open the Showcase Container
  $('a#showcase_one').click(function(){
    $('.showcase-container.parallax').addClass("go");
    $('#showcase-parallax').addClass("p-effect");
    $('#handy').removeClass("p-effect");
    $('.showcase-container.parallax.go').after("<div class='showcase-background'></div>");
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var centerx =  (windowWidth - 990) / 2;
    var centery =  (windowHeight - 500) / 2;
    $('.showcase-container.parallax.go').css({
      'top': centery,
      'left': centerx
    });
    return false
  });
    // click
  $('.showcase-container.parallax a.close').click(function(){
    $('.showcase-container').removeClass("go");
    $('#showcase-parallax').removeClass("p-effect");
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
      $('#handy').removeClass('p-effect');
    }
    else
    {
      $('#handy').addClass("p-effect");
    }
    $(".showcase-background").remove();
    return false
  });
  
  $('a#showcase_two').click(function(){
    $('.showcase-container.oldbrowser').addClass("go");
    $('#handy').removeClass("p-effect");
    $('.showcase-container.oldbrowser.go').after("<div class='showcase-background'></div>");
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var centerx =  (windowWidth - 589) / 2;
    var centery =  (windowHeight - 510) / 2;
    $('.showcase-container.oldbrowser.go').css({
      'top': centery,
      'left': centerx
    });
    return false
  });
  
  $('.showcase-container.oldbrowser a.close').click(function(){
    $('.showcase-container').removeClass("go");
    $(".showcase-background").remove();
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
      $('#handy').removeClass('p-effect');
    }
    else
    {
      $('#handy').addClass("p-effect");
    }
    return false
  });
  
  // Close the Showcase Container
    // esc
  $(document).keyup(function(e) {
    if (e.keyCode == 27) { 
      $('.showcase-container').removeClass("go");
      $('#showcase-parallax').removeClass("p-effect");
      $('#handy').addClass("p-effect");
      $(".showcase-background").remove(); 
    }
  });

  // Tweet
  $.getJSON("http://twitter.com/statuses/user_timeline/openchoi.json?callback=?", function(data) {
    var tweetText = data[0].text;
    // replace links and twitter handles
    tweetText = tweetText.replace(/http:\/\/(\S+)/, "<a href=\"http://$1\">http://$1</a>");
    tweetText = tweetText.replace(/@(\S+)/, "<a href=\"http://twitter.com/$1\">@$1</a>");
    $("#tweet").html("&quot;" + tweetText + "&quot;");
  });
  
  // Email Decoder
      var m = $.base64.decode('bWFpbHRvOm9wZW5jaG9pQGdtYWlsLmNvbQ==');
  // Email Parser
    $('a.email').each(function() {
       this.href = m;
    });
});
// Claim interval
var iam = [
  'I work at Interpark',
  'I graduated with a degree in public administration',
  'I consider myself a cha-do-nam. ha ha ^^;',
  'I&#39;m 33 years old',
  'I was born in <a href="http://nmap.do/xkqFBo">Chuncheon</a>',
  'I love minimalism',
  'I like macbook, iphone, ipod...',
  'I&#39;m a Learning & Development Specialist from Korea'];
var index = 0;

var anim = function() {
  if (index >= iam.length) {index = 0;}
  var claim = iam[index];
  index = index + 1;
  setTimeout(function() {
    $('#home-container p span').parent("p").fadeOut("fast", function(){
      $('#home-container p span').html(claim);
      $('#home-container p span').parent("p").fadeIn("normal", anim);
    });
  }, 4200);
}
setTimeout(anim, 3500);
