$(document).ready(function() {
  // Skrytí prvků, které se zobrazí až po kliknutí na provázaný odkaz
  $('#add-profile-file, #registredStudents, #more_info_section, #add-work-position, #add-event-file, .noact #event-form').hide();
  
  // Zobrazení formuláře pro přidání souboru k profilu
  $('.show-add-profile-file').click(function() {
    $('#add-profile-file').show('slow');
    $(this).hide('slow');
    return false;
  });
  $('.close-add-profile-file').click(function() {
     $('#add-profile-file').hide('slow');
     $('.show-add-profile-file').show('slow');
     $('#add-profile-file input[type=text]').val('');
     return false;
  });
  
  // Zobrazení registrovaných studentů pro adminy
  $('.stud a').click(function() {
    $('#registredStudents').show('slow');
    $(this).hide('slow');
    return false;
  });
  
  // Skrývání pole Atelier při registraci studenta
  var registerStundetFrontend = function() {
    var multimediaAndDesign = 'Multimedia a design';
    var $programSelect = $('#registerStudentForm #program');
    var $atelierSelect = $('#registerStudentForm #atelier');
    
    var logic = function() {
      if($programSelect.val() == multimediaAndDesign)
        $atelierSelect.parent().parent().hide();
      else
        $atelierSelect.parent().parent().show();
    }
    logic();
      
    $programSelect.change(logic);
  }
  registerStundetFrontend();
  
  
  // Zobrazení podrobnějších informací u eventů a profilů firem
  // todo:
  $('.more_info_text').click(function() {
    $('#more_info_section').show('slow');
    $('.section .perex').hide('slow');
    $(this).hide('slow');
    return false;
  });
  
  // Zobrazit formulář pro přidání pracovní pozice
  $('.show-add-work-position').click(function() {
    $('#add-work-position').show('slow');
    $(this).hide('slow');
    return false;
  });
  $('.close-add-work-position').click(function() {
    $('#add-work-position').hide('slow');
    $('.show-add-work-position').show('slow');
    $('#add-work-position input[type=text]').val('');
    return false;
  });
  
  // Zobrazení formuláře pro přidání souboru k eventu
  $('.show-add-event-file').click(function() {
    $('#add-event-file').show('slow');
    $(this).hide('slow');
    return false;
  });
  $('.close-add-event-file').click(function() {
     $('#add-event-file').hide('slow');
     $('.show-add-event-file').show('slow');
     $('#add-event-file input[type=text]').val('');
     return false;
  });
  
  // Zobrazení formuláře pro přidání události studentem
  $('.noact .show-event-form').click(function() {
    $('.noact #event-form').show('slow');
    $(this).hide('slow');
    return false;
  });
  $('.noact .close-event-form').click(function() {
     $('.noact #event-form').hide('slow');
     $('.noact .show-event-form').show('slow');
     $('.noact #event-form input[type=text]').val('');
     return false;
  });
  
  // Zobrazení pouze prvních šesti fotografií, další se zobazí po rozkliknutí
  var count = 0;
  var str = '';
  $('<div class="blek"></div>').appendTo('.photos').hide();
  
  $('.photos span.photo').each(function() {
    if(++count > 5)
    {
      $(this).appendTo('.blek');
    }
  });
  
  if(count > 5)
  {
    $('<p class="allphot"><a href="#">Zobrazit kompletní fotogalerii</a></p>').insertAfter('.photos');
    
    $('.allphot a').click(function() {
      $(this).hide('slow');
      $('.blek').show('slow');
      return false;
    });
  }  
  
  // ...a u těchto fotografií také uděláma moc pěkný efekt
	$('.photos a').fancyzoom({imgDir:'http://inovacemk.cz/wp-content/themes/inovacemk/images/ressources/'}); 
	
	/*
    Zobrazení popisku
  */
  var dateTimeDesc = function(form, element, desc) {
    
    var defaultColor = element.css('color');
    
    var showDesc = function() {
      element.css('color', 'gray');
      element.css('text-align', 'center');
      element.val(desc);
    }
    
    var hideDesc = function() {
      element.val('');
      element.css('color', defaultColor);
      element.css('text-align', 'left');
    }
  
    if(element.val() == '' || element.val() == desc)
    {
      showDesc();
    }
    
    $(element).focus(function() {
      if(element.val() == desc)
      {
        hideDesc();
      }
    });

    $(element).blur(function() {
      if(element.val() == '')
      {
        showDesc();
      }
    });
    
    $(form).submit(function() {
      if(element.val() == desc)
      {
        element.val(''); 
      }
    });
  }
  
  var form = $('#event-form');
  dateTimeDesc(form, $('#start_time', form), 'čas');
  dateTimeDesc(form, $('#end_time', form), 'čas');
  dateTimeDesc(form, $('#start_date', form), 'datum*');
  dateTimeDesc(form, $('#end_date', form), 'datum');
  dateTimeDesc(form, $('#registration_start_date', form), 'datum');
  dateTimeDesc(form, $('#registration_end_date', form), 'datum');
  dateTimeDesc(form, $('#unregistration_end_date', form), 'datum');
  
  dateTimeDesc(form, $('#location', form), 'umístění (např. název budovy)');
  dateTimeDesc(form, $('#address', form), 'adresa');
  dateTimeDesc(form, $('#city', form), 'město');
  
  $('#searchtext').focus(function() {
    if($(this).val() == 'Vyhledávání...')
      $(this).val('');
  });
  
  $('#searchtext').blur(function() {
    if($(this).val() == '')
      $(this).val('Vyhledávání...');
  });
  
  if($('textarea#description'))
  {
    count = function() {
      var length = 0;
      if($('textarea#description').val())
      {
        $('textarea#description').val($('textarea#description').val().substring(0, 160));
        length = $('#description').val().length; 
      }
      $('.zbyva').html(160 - length);
    }
    
    $('textarea#description')
  	 .bind("keydown", function () { count(); })
  	 .bind("keypress", function () { count(); })
  	 .bind("keyup", function () { count(); })
  	 .bind("focus", function () { count(); })
  	 .bind("mouseover", function () { count(); })
  	 .bind("mouseout", function () { count(); })
  	 .bind("paste", function () { 
  	   var me = this;
  		setTimeout(function () { count(); }, 10);
  	 });
  	if (this.addEventListener) {
  	 this.addEventListener('input', function () { count(); }, false);
  	};
  	count();
	}
	
	$('.show-add-more-lecturer').click(function() {
	  data = $('<div class="lecturer">' + $('.lecturer:last').html() + '</div>');
	  $('input, select', data).each(function() {
	   $(this).val('');
	   pos = $(this).attr('name').lastIndexOf('_');
	   id = parseInt($(this).attr('name').substr(pos + 1), 10) + 1;
	   $(this).attr('name', $(this).attr('name').substr(0, pos) + '_' + id);
	   $(this).attr('id', $(this).attr('id').substr(0, pos) + '_' + id);
	   
	   //$(this).attr('name', ) 
    });
    $('h3', data).html('Přednášející č. ' + (id + 1) )
    $('.show-add-more-lecturer').before(data);
	  return false;
  });
});

Cufon.replace('h2', { fontFamily: 'Aller' });
Cufon.replace('#perex', { fontFamily: 'AllerLight' });  
