var lang = {
  cs: {
    up: 'nahoru',
    down: 'dolů',
    previous: 'předchozí',
    next: 'další',
    close: 'zavřít',
    images: 'Zbývá nahrát {i} obrázků',
    exhibitions: 'výstavy',
    videos: 'videa',
    paintings: 'obrazy',
    projects: 'projekty'
  },
  en: {
    up: 'up',
    down: 'down',
    previous: 'previous',
    next: 'next',
    close: 'close',
    images: '{i} images to load',
    exhibitions: 'exhibitions',
    videos: 'videos',
    paintings: 'paintings',
    projects: 'projects'
  }
};

window.onload = function() {
  $('#wrapper').fadeIn('normal');
  $('#loading').fadeOut('fast');
};

$(document).ready(function() {
  /* Opera limits width to 32768 px, we need to cut content there or things will
   * turn out messy
   **/
  
  if($.browser.opera && $('#barackova').width() >= 32767){
    $('#barackova').width(32767);
    $('#gallery').css({
      width: 32767 - 868 - 68,
      overflow: 'hidden' 
    });
    $('#gallery ul').css({
      width: 32767 - 868 - 136,
      height: 540,
      position: 'relative',
      overflow: 'hidden' 
    });
  }

  // menu
  $('#menu li:not(.active) ul').hide();
  
  // open/close/clickthru
  $('#menu > li.active > a').toggleClass('open');
  $('#menu > li > a').click(function(e) {
    var ul = $(this).nextAll('ul');
    if (typeof ul[0] != 'undefined') {
      ul = $(ul[0]);
      var h = ul.height();
      if ($(this).hasClass('open')) {
        ul.animate({
          height: 0
          }, 400, 'easeOutQuad', function() {
          $(this).hide().height(h);
        });
        if ($(this).parent().hasClass('active') === false) {
          e.preventDefault();
        }
      }
      else {
        ul.height(0).show();
        ul.animate({
          height: h
          }, 400, 'easeOutQuad');
        e.preventDefault();
      }
    }
    $(this).toggleClass('open');
  });
  // ^^

  // external links
  $('a[rel=external]').attr('target', '_blank');

  // gallery colorbox
  $('#gallery li a[rel!=external]').colorbox({
    transition: 'fade',
    opacity: '0.85',
		current: '{current}/{total}',
		previous: lang[lge].previous,
		next: lang[lge].next,
		close: lang[lge].close,
		loop: false
  });

  $('#gallery li a.cbox_video').each(function() {
    var dim = $(this).attr('dim').split('|');
    
    $(this).colorbox({
      transition: 'fade',
      opacity: '0.85',
  		current: '{current}/{total}',
  		previous: lang[lge].previous,
  		next: lang[lge].next,
  		close: lang[lge].close,
  		loop: false,
  		iframe: true,
      width: dim[0],
      height: dim[1]
    });
  });

  // portfolio colorbox
  $('#item a').colorbox({
    transition: 'fade',
    opacity: '0.85',
		current: '{current}/{total}',
		previous: lang[lge].previous,
		next: lang[lge].next,
		close: lang[lge].close,
		loop: false
  });

  $('#item a.cbox_video').each(function() {
    var dim = $(this).attr('dim').split('|');
    
    $(this).colorbox({
      transition: 'fade',
      opacity: '0.85',
  		current: '{current}/{total}',
  		previous: lang[lge].previous,
  		next: lang[lge].next,
  		close: lang[lge].close,
  		loop: false,
  		iframe: true,
      width: dim[0],
      height: dim[1]
    });
  });

  // content slider
  outer = $('#content').height();
  inner = $('#text').height();
  if (inner > outer) {slider(lang, $('#content'), $('#text'), 'NULL');}

  // listing slider
  outer = $('#listing').height();
  inner = $('#listing ul').height();
  if (inner > outer) {slider(lang, $('#listing'), $('#listing ul'), 'listing');}
});

// slideOnLoad
var slideOnLoad = 0;

// content slider
function slider(lang, o, i, type) {
  var outer = o.height();
  var inner = i.height();

  // adjust markup
  var w = parseInt(o.width()) + parseInt(o.css('paddingTop'));
  var dim = {
    height: outer,
    top: o.position().top,
    left: o.position().left + w + 10
  };
	o.css({overflow: 'hidden', width: w, padding: 0}).after('<h4 id="slider"></h4>');
  $('#slider').css(dim).html('<span id="up">'+ lang[lge].up +'</span><span id="down">'+ lang[lge].down +'</span>');
  $('#up').hide();

	// defaults
	outer = Math.ceil(outer * 0.9);
  var slide = 0;
  var count = Math.ceil(inner/outer) - 1;

  // count slides and animate
  function animate(dir, n, speed) {
    if (n === undefined) {n = 1;}
    slide = dir == 'down' ? parseInt(slide) + parseInt(n) : parseInt(slide) - parseInt(n);
    var top = slide * outer * -1 * n;
    if (speed === undefined) {speed = outer * 3;}
    i.animate({marginTop: top}, speed);
    
    if (type == 'listing') {
      $('#listing a').each(function() {
        var href = $(this).attr('href').split('?');
        href = href[0];
        if (slide !== 0) {href += '?slide=' + slide;}
        $(this).attr('href', href);
      });
    }
  }

  // if there is any slide onLoad, fadeIn #up and check if #down is still available
  if (type == 'listing' && slideOnLoad > 0) {
    // don't animate onLoad, just set margin-top
    animate('down', slideOnLoad, 0);
    $('#up').fadeIn('fast');
    if (slide >= count) {$('#down').fadeOut('fast');}
  }
	
	// bind "up" slide
	$('#up').click(function() {
    animate('up');
    if (slide <= 0) {$(this).fadeOut('fast');}
    $('#down').fadeIn('fast');
  });
  
  // bind "down" slide
	$('#down').click(function() {
    animate('down');
    if (slide >= count) {$(this).fadeOut('fast');}
    $('#up').fadeIn('fast');
  });
}

// Cufón
Cufon.now();
