// Pridava tridu na uvode v aktualitach
function addClassNews() {
  var cont = 'box_news';
  if($(cont) == null) return false;
  var item_in_menu = $$('#news_menu span.item').length;

  for(i=1; i<=item_in_menu; i++)  {
    var el = $('box_news').getElements('div.news_home:nth-child('+(i)+')').getChildren();
  
    if(el[0].length == 3) {
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(last)').addClass('last');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(1)').addClass('first');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(2)').addClass('second');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(3)').addClass('third');
    }
    
    if(el[0].length == 4) {
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(last)').addClass('last');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(2)').addClass('first');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(3)').addClass('second');
      $$('#box_news div.news_home:nth-child('+(i)+') div.item:nth-child(4)').addClass('third');   
    }
  }
}

function archiveNews() {
  var cont = 'news_menu';
  var text = ['Archiv studentských aktualit','Všechny akce na MU','Archiv aktualit akademického senátu','Archiv aktualit fakulty'];
  var href = ['http://www.ped.muni.cz/aktuality/studentske-aktuality','http://www.muni.cz/ped/events/calendar','aktuality/akademicky-senat/','aktuality/aktuality-fakulty/'];
  
  if(!$(cont)) return false; 
  var items = $(cont).getElements('span.item');
  var numItems = items.length;
  
  items.each(function(item, index){
    item.addEvent('click', function(){
      if(numItems==3) $$('div.archive').set('html', '<p><a href="'+href[index]+'" target="_blank">'+text[index]+'</a></p>');
      else $$('div.archive').set('html', '<p><a href="'+href[index]+'" target="_blank">'+text[index]+'</a></p>');
    });
  });
}


// Doporucit stranku - ajax odesilani
function recommendPage() {
  var clickEl = 'recp';
  var contentEl =  'recommend_page_content';
  var close = 'close_recommend';
  var textName = 'Vaše jméno';
  var textEmail = 'e-mail příjemce';

  if(!$(clickEl)) return false;
  
  var effects = new Fx.Morph(contentEl, {duration: 350, transition: Fx.Transitions.Sine.easeOut});
  var diplay = function(){ 
    $(contentEl).setStyle('display','none') 
    $('recommend_page').getElements('input[name=name]').set('value',textName);
    $('recommend_page').getElements('input[name=email]').set('value',textEmail);
    $('send_text').setStyle('display','none');
    $('recommend_page').setStyle('display','block');
  };
  
  $(clickEl).addEvent('click', function(){
    $(contentEl).setStyle('display','block');
    effects.start({'top': '0'});
  });
  
  $(close).addEvent('click', function(){
    effects.start({'top': '-60px'});
    diplay.delay(420);
  });
  
  
  // Odeslani
  $('recommend_page').addEvent('submit', function(e) {
  	new Event(e).stop();
  	//var log = $('log_res').empty().addClass('ajax_loading');
  	this.set('send', {onComplete: function(response) { 
			//log.removeClass('ajax_loading');
			//log.set('html', response);
		}});
		
		if(e.target.name.value=='' || e.target.name.value==textName) {
      alert('Zadejte prosím jméno');
      return false;
    }
    
    if(e.target.email.value=='' || e.target.email.value==textEmail) {
      alert('Zadejte prosím email');
      return false;
    }
		
		if(!emailValidate(e.target.email.value)) {
      alert('Zadejte prosím validní emailovou adresu');
      return false;
    }

    this.send();
    $('recommend_page').setStyle('display','none');
    var text = '<b>Doporučení bylo odesláno na adresu '+e.target.email.value+'</b> | <a href="#" id="send_repeat">Poslat dalšímu známému</a>';
    var sendText = new Element('div', {'html': text, 'id': 'send_text'});
    $('recommend_page_content').grab(sendText,'bottom');
    e.target.email.set('value',textEmail);
    e.target.name.set('value',textName);
    
    if($('send_text'))  {
      $('send_repeat').addEvent('click', function(ev){
        new Event(ev).stop();
        $('recommend_page').setStyle('display','block');
        $('send_text').destroy();
      });
    }
    
  });
  
  // Kliknuti do inputu jmena
  $('recommend_page').getElement('input[name=name]').addEvents({
    focus: function() {
			if (this.value.contains(textName)) this.value = '';
		},
		blur: function() {
			if (this.value == '') this.value = textName;
		}
  });
  
  // Kliknuti do inputu emailu
  $('recommend_page').getElement('input[name=email]').addEvents({
    focus: function() {
			if (this.value.contains(textEmail)) this.value = '';
		},
		blur: function() {
			if (this.value == '') this.value = textEmail;
		}
  });
}

function emailValidate(adresa)  {
  re = /^[A-Za-z0-9\._-]+[@][A-Za-z0-9\._-]+[\.].[A-Za-z0-9]+$/;
  return adresa.search(re) == 0;
}


// Nastaveni tridy pro rozcestnik v rubrice o fakulte
function addClassGuidepostFaculty() {
  if($('guidepost_about_faculty') == null) return false;
  $$('#guidepost_about_faculty div:even').set('class', 'even');
}


// Pridava tridu pro tabulku pracovni poozice
function addClassTableWorkingPosition() {
  $$('table.pracovni_pozice tr td:nth-child(first)').set('class', 'prvni');
}


// Pridava tridy v rubrice pronajem prostor tabulkam pronajem_prostor_cena a pronajem_prostor_info
function addClassTableLease() {
  $$('table.pronajem_prostor_cena tr:even').set('class', 'even');
  $$('table.pronajem_prostor_cena td:nth-child(2)').set('class', 'right');
  
  $$('table.pronajem_prostor_info td:nth-child(first)').set('class', 'bold');
}


// Pridava tridu pro tabulku kontakt
function addClassTableContact() {
  $$('table.kontakt tr td:nth-child(1)').set('class', 'left');
  $$('table.kontakt tr td:nth-child(2)').set('class', 'center');
  $$('table.kontakt tr td:nth-child(3)').set('class', 'right');
  $$('table.oddeleni tr td:nth-child(1)').set('class', 'first');
  
  var contactLeft = $('main_content').getElements('table.kontakt td.left');
  var contactLeftValues = contactLeft.get('html');
  
  var contactCenter = $('main_content').getElements('table.kontakt td.center');
  var contactCenterValues = contactCenter.get('html');
  
  contactLeftValues.each(function(item, index){
    var div  = new Element('div');
    contactLeft[index].set('html','');
    contactLeft[index].grab(div);
    contactLeft[index].getElement('div').set('html',item);
    
    var div  = new Element('div');
    contactCenter[index].set('html','');
    contactCenter[index].grab(div);
    contactCenter[index].getElement('div').set('html',contactCenterValues[index]);
  });
}


// Pokud uzivatel zada spatne prihlasovaci udaje, tak se zobrazi popup okno
function feLogin() {
  if($('error_login') == null) return false;
  var html = '<p>Špatné přihlašovací údaje</p><p class="btn"><button type="button" id="re_login">Přihlásit se znovu</button></p>';

  var frame = new Element(
    'div', {'id':'frame', 
      'styles':{'opacity': 0,'z-index': 50, 'position':'absolute','left':0,'top':0,
      'background-color':'#000','width':window.getScrollWidth(),'height':window.getScrollHeight()-1}});
  var errorLoginText = new Element('div', {'id':'error_login_text', 'styles':{'opacity': 0,'background-color':'#fff'}});

  
  $$('body').adopt(frame);
  $$('body').adopt(errorLoginText);
  
  $('error_login_text').set('html',html);
  $('error_login_text').setStyles({
      position: 'absolute',
      left: (window.getScrollLeft()+(window.getWidth()/2)-100),
      top: 40
  });
  
  var effectFrame = new Fx.Tween(frame,{duration: '300',transition: Fx.Transitions.Sine.easeOut});
  var effectErrorLoginText = new Fx.Tween(errorLoginText,{duration: '300',transition: Fx.Transitions.Sine.easeOut});
  
  effectFrame.start('opacity', '0.6');
  effectErrorLoginText.start('opacity', '1');

  $('re_login').addEvent('click', function(){
    $('error_login_text').destroy();
    $('frame').destroy();
    document.getElementById("user").focus();
  });

}


window.addEvent('domready', function() {
  addClassNews();  
  archiveNews();
  recommendPage();
  addClassGuidepostFaculty();
  addClassTableWorkingPosition();
  addClassTableLease();
  addClassTableContact();
  
  //feLogin();
});
