// document ready
$(document).ready(function() {
  // cufon
  $('.header .right li a, .header .left p, .thumb-list h2').addClass('cufon');
  Cufon.replace('.cufon');
  
  // big photo scroll handler
  fixMainHeight(); initialScroll();
  
  // big photo text protect
//   fixPhotoTextWidth();
  if ($('#big-photo-wrap').length > 0) {
    if ($('#big-photo-text .inner').text().trim().length < 1) {
      $('#big-photo-text .inner').html('&nbsp;');
    }
    $('#big-photo-text p').last().css('margin-bottom', 0);
  }

  
  // thumb list align
  $('.thumb-list.all ul').first().remove();
  alignThumbs();
  
  // window resize
  $(window).resize(function(){alignThumbs(); fixMainHeight();});

//   // thumb list cookies
//   var items = $('.thumb-list').find('li a');
//   items.each(function(){
//     var name = $(this).attr('href');
//     if ($.cookie(name) == 'visited') {
//       $(this).addClass('blackwhite');
//     }
//   });
//   items.click(function(){
//     var name = $(this).attr('href');
//     $.cookie(name, "visited", { path: '/', expires: new Date(new Date().getTime()+3*3600000) });
//   });

  

	// ochrana emila
	$('a.em').each(function(){
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		e = $(this).text().replace('/','@');
		$(this).text(e);
	});
	
	// FIXME: automaticke nacteni stejneho obsahu pri prepnuti jazyka
	$('.language-picker a').click(function(){
		var href = window.location.href;
		window.location.href = href.replace(/(\/+)[a-zA-Z]{2}(\/+)/, $(this).attr('href'));
		return false;
	})

  $('#big-photo-text p, .project .text p').cstypo();

});

// functions
function alignThumbs() {
  var maxWidth = 0;
  $('.thumb-list ul').each(function(){
    var elUl = $(this); var elDiv = elUl.parents('.thumb-list'); var elLi = elUl.find('li'); var header = elUl.prev('h2');
    var liWidth = elLi.outerWidth(true);
    var amount = Math.floor(elDiv.width()/liWidth);
    if (elLi.length > amount) {
      amount = amount*liWidth;
      elUl.width(amount);
      elUl.prev('h2').width(amount);
    } else {
      amount = 0;
      elLi.each(function(){
        amount += $(this).outerWidth(true);
      });
      elUl.width(amount);
    }
    if (amount > maxWidth) {maxWidth = amount;}
    header.width(elUl.width()-20);
  });
  if ($('.thumb-list').is('.all')) {
    $('.thumb-list').find('h2, ul').width(maxWidth);
  }

}

function fixMainHeight() {
  if ($('#big-photo-img').length > 0) {
    var targetTop = $('#big-photo-img').height() - 225; // img height - 225
    $('body').height('auto');
    if ($('body').height() < ($(window).height() + targetTop)) {
      $('body').height($(window).height()+targetTop);
    }
  }
}
function initialScroll() {
  if ($('#big-photo-img').length > 0) {
    var targetTop = $('#big-photo-img').height() - 225; // img height - 225
     if ($(window).scrollTop() < targetTop) {
       $(window).scrollTop(targetTop);
     }
  }
}

// function fixPhotoTextWidth() {
//   if ($('#big-photo-text').length > 0) {
//     var wrap = $('#big-photo-wrap'); var img = $('#big-photo-img'); var text = $('#big-photo-text');
//     var targetWidth = wrap.width()-img.outerWidth(true);
//     if (targetWidth > 500) {targetWidth = 500;}
//     if (targetWidth < 280) {targetWidth = 280;}
//     text.width(targetWidth-10);
//   }
// }
