function preload_images() {
    

		$('<img />').attr('src', 'img/print_qual_large_image.png').load(function(){
		$('.qulityImage').append( $(this) );

    	});
		$('<img />').attr('src', 'img/interactive_custom_coding.png').load(function(){
		$('.costumCodingImage').append( $(this) );

    	});
}

// Usage:


$(document).ready( function(){
	preload_images();

	$('.imageLink a:not(.dontchange)').click(function(){
		var link = $(this);
		$('.portfoiloTab').fadeOut(200, function(){
			setTimeout(function(){$('.portfoiloTab#'+link.attr('rel')).fadeIn(200)}, 200);
		});
		
	});




  // xHTML strict target="_blank" replacement
  $('a[rel=external]').click(function(event){
    event.preventDefault();
    window.open(this.href);
    return false;
  });

  // If home page = show fadein  
  if($('#content:not(.black)').hasClass('bg0')) {
    handleBackgroundHome();
  }
  if($('#content:not(.black)').hasClass('bg0-2')) {
    handleBackgroundHome();
  }
  
  // Preloading CSS background images from all stylesheets
  $.preloadCssImages();

  // Initialize VideJS, so flash callback is possible
  VideoJS.setup();

  // IE fix

  if (jQuery.browser.msie) {
    $('#main-menu > li:first-child > ul').css('margin-top', '550px'); 

    $('#main-menu > li:last-child > a').css({
      'border-right' : '1px solid #2c2c2c'
    });

    $('#main-menu > li ul').css({
      'opacity' : 0.8
    }); 
  }


  // TMP
  var params = getUrlParameters();
  var ipad = false;
  if(params['ipad']) {
    ipad = true;
  }
  
  // Ipad fix
  if(isiPad() || ipad == true) {
    $('#header').css({'padding-top':'0px'});
    $('#container').css({'padding-bottom':'116px'});
    
    $('#main-menu > li:last-child > a').css({'padding-right':0});
    $('#main-menu > li:first-child > a').css({'left':0,'padding-left':0});
    
    $('#main-menu > li ul').css({'-moz-box-shadow':'none','-webkit-box-shadow':'none','box-shadow':'none'});
    $('#main-menu > li ul li').css({'padding':'4px 0px'});
  }
  
  
  
  // Twitter
/*
  var $twitter_user_name = "54blue";
  $('#twitter-feed-content').tweet({
    join_text: "auto",
    username: $twitter_user_name,
    count:5,
    auto_join_text_default: "", 
    auto_join_text_ed: "",
    auto_join_text_ing: "",
    auto_join_text_reply: "",
    auto_join_text_url: "",
    loading_text: "loading tweets..."
  });  
  // Profile data
  $.getJSON('http://twitter.com/users/'+$twitter_user_name+'.json?callback=?',
  function(data) {
    $('#twitter-feed-head').prepend('<img src="'+data.profile_image_url+'" height="26" width="26">');
    $('#twitter-feed-head b').html(data.name);
    $('#twitter-feed-head span').html(data.screen_name);
  });
  // Append linkt to twitter
  $('#twitter-feed-foot').attr('href','http://www.twitter.com/'+$twitter_user_name);
*/
  
  
  


  

  /* Background sliding effect when hovering */
  $('#main-menu > li > a.ready').mouseover(function(e){
    /* Hide content while moving background */
    $('.wrapper').hide();
    /* background sliding */
    handleBackgroundMove(e, $(this));
  });

  /* Background sliding effect when clicking */
  $('#main-menu > li > a:not(.ready)').click(function(e){
    /* Hide content while moving background */
    $('.wrapper').hide();
    /* Ability to use hover */
    $('#main-menu > li > a').live('mouseover', function(e) {
      handleBackgroundMove(e, $(this));
    });
    /* background sliding */
    handleBackgroundMove(e, $(this));
  });
  

  // Contacts section form
  $('a.contact_email').click(function(e){
    e.preventDefault();
    // Remove previous shadows
    $('.contact_email img').removeClass('box-shadow-indigo box-shadow-pink box-shadow-yellow');
    var color = $(this).attr('color');
    $(this).children().addClass('box-shadow-'+color);
    
    var mail = $(this).attr('rel');
    //console.log(mail);
    $('#contact-details').hide();
    $('#contact-form').fadeIn('300');
    $('#subject, #message, #phone, #email, #name').val(''); // reset
    reset_errors();
    $('#submit').val('Send').attr('disabled',''); 
    $('#contact-form-mail').val(mail);
    $('.formTitle').html($(this).attr('title'));
  });
  
  // Reset errors on contact form
  function reset_errors() {
    $('form .error').remove(); // remove error messages
    $('#subject, #message, #phone, #email, #name').removeClass('box-shadow-indigo box-shadow-pink box-shadow-yellow'); // remove input error beauties
  }
  
  $('#contact-form form').submit(function(){
    
    reset_errors();
    
    // Example Post
    var has_errors = false;
    
    if($('#subject').val().length < 1) {
      has_errors = true
      $('label[for="subject"]').append('<span class="error pink_color">Please enter subject.</span>');
      $('#subject').addClass('box-shadow-pink');
    }
    
    if($('#name').val().length < 1) {
      has_errors = true
      $('label[for="name"]').append('<span class="error pink_color">Please enter your name.</span>');
      $('#name').addClass('box-shadow-pink');
    }
    
    var email_value = $('#email').val();
    var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!email_reg.test(email_value) || email_value.length < 1) {
      has_errors = true
      $('label[for="email"]').append('<span class="error pink_color">Please enter valid email.</span>');
      $('#email').addClass('box-shadow-pink');
    }

    if($('#message').val().length < 1) {
      has_errors = true
      $('label[for="message"]').append('<span class="error pink_color">Please enter message.</span>');
      $('#message').addClass('box-shadow-pink');
    }
        
    
    if(has_errors) {  
      return false;
    }
    
    $.post($(this).attr('action'),
      $(this).serialize(),
      function(data) {
      
        reset_errors();   
        //console.log(data);
        if(data.empty_subject == 'empty_subject') {
          $('label[for="subject"]').append('<span class="error pink_color">Please enter subject.</span>');
          $('#subject').addClass('box-shadow-pink');
        }
        if(data.empty_name == 'empty_name') {
          $('label[for="name"]').append('<span class="error pink_color">Please enter your name.</span>');
          $('#name').addClass('box-shadow-pink');
        }

        if(data.invalid_email == 'true') {
          $('label[for="email"]').append('<span class="error pink_color">Please enter valid email.</span>');
          $('#email').addClass('box-shadow-pink');
        }
        if(data.empty_message == 'empty_message') {
          $('label[for="message"]').append('<span class="error pink_color">Please enter message.</span>');
          $('#message').addClass('box-shadow-pink');
        }
        
        if(data.email_sent == 'true') {
          $('#contact-form form').remove();
          $('#contact-form').append('<p style="font-size:14px;">Thank you<br /><br />Your message has been sent!</p>');
        }
      },
      'json'
    );
    
    return false;
  });

  

  // Close window with "X" button
  $('.close').click(function() {
    $('.wrapper').hide();
    $('#logo').fadeIn(500);
    //$('#label').hide().removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass('bg' + getCurrentActive(), 'slow').fadeIn(500);
    $('#label').hide().removeClass().addClass('bg' + getCurrentActive(), 'slow').fadeIn(500);



    // If video played - stop

    if($('.video-js').length > 0){
      //$('video')[0].player.pause();
      $('.video-js-box').remove();
    }
    
    // Add hovering capability to main menu
    //$('#main-menu > li > a').addClass('ready');
    $('#main-menu > li > a').live('mouseover', function(e) {
      handleBackgroundMove(e, $(this));
    });
  });
  setTimeout(function(){
	  $('#twitter-feed a').each(function(){
	  	$(this).attr('target', '_blank');
	  });

  }, 1000);
  
  // What is current opened section
  function getCurrentActive() { 
    return $.trim($('#content').attr('class').replace('_desaturated', '').replace('bg', ''));
  }  
  
  // Make desaturated home page and after some time - colorize it
  function handleBackgroundHome() {
    var item = 'bg0'; // Target class (without _desaturated is colorized)
    var content = $('#content');
    var fader = $('#content-fader');
    var fader_color = $('#content-fader-color');
    
    var bg_image = content.css('background-image');
    content.css({'background-image':'none'}); // Remove background from #content    
    content.attr('class', item+'_desaturated'); // Add class name bgX, where X is global ID number

    // Activate the colored and desaturted div (default:hidden)
    fader_color.show();
    fader_color.attr('class',''); // Remove all backgrounds and classes
    $('#logo').hide().show();
    // Mega uber-super-duper animation
    fader
      .clearQueue() // Remove all functions from queue
      .stop(true) // Stop all animations
      .queue(function(){
        $(this).animate({opacity:0}, {duration:0}).queue(function(){
          $(this).removeClass(); // Remove all backgrounds and clases
          $(this).dequeue();  // is eventually called so that the next function in line executes
          content.attr('class', item);
        }); // Smooth hide current desaturated fader 
        //$(this).removeClass() // Remove all backgrounds and clases
        $(this).dequeue(); // is eventually called so that the next function in line executes
      })
      //.animate({'opacity':0},200) // Hide desaturated fader
      //.removeClass() // Remove all backgrounds and clases
      .queue(function(){
        $(this).addClass(item+'_desaturated') // Add desaturated background
        $(this).dequeue(); // is eventually called so that the next function in line executes
      })
      //.addClass(item+'_desaturated') // Add desaturated background
      .show() // Show this sexy backgorund
      //.css({opacity:0.7}) // Lover opacity
      .animate({opacity:1}, {// Set target opacity
        duration:0, // How long time till target opacity
        complete:function(){ // When compleated animation
          //console.log('stopped');
          fader_color.attr('class', item); // Add new class to color fader div
          fader.animate({opacity:0}, { // Fade out the desaturated div, so color div is seen
            duration:1500,
            complete:function(){ // When opacity is 0, hide the fader
              fader.css({opacity:0}) // Hide desaturated fader
              content.css({'background-image':bg_image});
              fader_color.removeClass();
              fader.removeClass();
            }
          }); 
        }
      });

  }
  
  
  // Main background handling function
  function handleBackgroundMove(e, $self){
    // item is bg1 or bg2 or bg3 etc
    if(e.target.id) {
      var item = e.target.id;
    } else {
      // So if we manualy set ID
      var item = e.id_;
    }
    var current = item.replace('bg', '');

    // If cursor is on the active menu item, then do nothing
    //console.log('current: '+current+ ' == ' + getCurrentActive());
    if(current == getCurrentActive()) {
      return;
    }
    // Check direction of mouse movement
    var left =  getCurrentActive() - current;
    
    var content = $('#content');
    var fader = $('#content-fader');
    var fader_color = $('#content-fader-color');
    
    content.css({'background-image':'none'}); // Remove background from #content    
    content.attr('class', item+'_desaturated'); // Add class name bgX, where X is global ID number

    // Activate the colored and desaturted div (default:hidden)
    fader_color.show();
    fader_color.attr('class',''); // Remove all backgrounds and classes
    
    $('#logo, #label').stop(true, true).hide(); // Hide old logo and label, so new will be shown smooth
    
    // Mega uber-super-duper animation
    fader
      .clearQueue() // Remove all functions from queue
      .stop(true) // Stop all animations
      .queue(function(){
        $(this).animate({opacity:0}, {duration:0}).queue(function(){
          $(this).removeClass(); // Remove all backgrounds and clases
          $(this).dequeue();  // is eventually called so that the next function in line executes
        }); // Smooth hide current desaturated fader 

        $(this).dequeue(); // is eventually called so that the next function in line executes
      })
      
      .queue(function(){
        $(this).addClass(item+'_desaturated') // Add desaturated background
        $(this).dequeue(); // is eventually called so that the next function in line executes
      })

      .show() // Show this sexy backgorund
      .animate({opacity:1}, {// Set target opacity
        duration:0, // How long time till target opacity
        complete:function(){ // When compleated animation
          fader_color.attr('class', item); // Add new class to color fader div
          
          // How long will take fade in
          var fade_duration = 1500;
          
          // Fade in center label      
          $('#label').stop(true).hide().removeClass().addClass('bg' + current).fadeIn(fade_duration-300);
           // Fade in logo 
          $('#logo').stop(true).hide().fadeIn(fade_duration-600);
          
          fader.animate({opacity:0}, { // Fade out the desaturated div, so color div is seen
            duration:fade_duration,
            complete:function(){ // When opacity is 0, hide the fader
              //fader.css({opacity:0}) // Hide desaturated fader
            }
          }); 
        }
      });
    
    // Hide photograph link
    $('#photograph_link').hide();
  }


  // Is f* ipad or not
  function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
  }


  // Parse URL
  function getUrlParameters() {
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
      map[key] = value;
    });
   return map; 
  }
    
});
