$(document).ready(function() {
   
   $('.kerrokaverilleLinkki').each(function() {
      var linkki = $(this).attr('href');
      var here = document.location.href;
      var title = linkki+'?'+here;
      $(this).attr('href', title);
   });
   
   var here = document.location.href;
   var array = here.split('?');
   if(array[1]) {
      $('#kerrokaverille textarea').append('\n\n' + array[1]);
   }
   
   
   $('.facebookLink').each(function() {
      var here = document.location;
      var title = $('title').text();
      var fb = 'http://www.facebook.com/sharer.php?u=<'+ here + '&t=<' + title + '>';
      $(this).attr('href', fb);   
   });
   
    
    $('#nostot .nosto select').change(function(){
		  var chosenoption = this.options[this.selectedIndex]
		  if (chosenoption.value != "nothing"){
				window.open(chosenoption.value, "_self", "" );
		  }
    });
    
    $('.submenuContent').each(function(){
		  var sisalto = $(this).text().length;
                  if(sisalto == 0) {
                    $(this).parent().hide();
                  }
    });
    
    $('#centerLeftTabit ul li.current').each(function(){
		  $(this).next('li').addClass('next');
                  $(this).prev('li').addClass('edel');
    });
    
    var otsikkoH1 = $('#nosto4 h1').text().split('p');
    if(otsikkoH1[1]) {
      var otsikkoAlku = otsikkoH1[0];
      var otsikkoLoppu = otsikkoH1[1];
      $('#nosto4 h1').empty().append(otsikkoAlku+'-<br />p'+otsikkoLoppu);
      }
    
    $('#eventmenu li a').each(function(){
		  var link = String(document.location);
                  if(link.indexOf('uutiset.html') != -1) {
                    $('#eventmenu li#sivu1').addClass('current');
                  }
                  if(link.indexOf('tapahtumat.html') != -1) {
                    $('#eventmenu li#sivu2').addClass('current');
                  }
                  if(link.indexOf('koulutukset.html') != -1) {
                    $('#eventmenu li#sivu3').addClass('current');
                  }
    });
    
    if(document.getElementById('blogijuttu')) {
        var juttu = $('#blogijuttu').text().length;
        if(juttu > 250) {
            var teksti = $('#blogijuttu').text().substr(0,250);
            $('#blogijuttu').html(teksti+ '...');
        }
    }
   
	 $('#menu ul li a').each(function(i) {
		  var showDelayTimer = null,
		  hideDelayTimer = null,
		  $trigger = $(this),
		  $content = $('#submenu'+(i+1));
		  $([$trigger.get(0), $content.get(0)]).mouseover(function() {
				if (showDelayTimer) { clearTimeout(showDelayTimer); }
				if (hideDelayTimer) { clearTimeout(hideDelayTimer); }						
				showDelayTimer = setTimeout(function() {
					 showDelayTimer = null;
					 if ($content.is(':animated, :visible')) { return; }
					 else {
						  $content.fadeIn(150);
						  $trigger.parent().addClass('hover').end().find('.kuvaus').css('color', '#330000');
					 }
				}, 150);
		  }).mouseout(function () {
				if (hideDelayTimer) { clearTimeout(hideDelayTimer); }
				if (showDelayTimer) { clearTimeout(showDelayTimer); }
				hideDelayTimer = setTimeout(function () {
					 hideDelayTimer = null;
					 $content.fadeOut(150, function() {
                                             if($trigger.parent().hasClass('current')) {
						$trigger.parent().removeClass('hover');
                                             }else {
                                                $trigger.parent().removeClass('hover').end().find('.kuvaus').css('color', '#999999');
                                             }
					 });
				}, 0);
		  });	
	 });
});
