x = 0;

$(document).ready(function() {
  $('.persons-nav ul li ul').not('.expanded').hide();
 
  $('.persons-title-header').removeAttr('href').css({ 'cursor': 'pointer' });
 
  $('.persons-title-header').click(function(){
      $(this).siblings('ul').slideToggle();
  });
 
  $('#specialists-dropdown-menu').change(function(){
      var form = $('#specialists-picker');
      var value = $(this).val();
 
      if(value.indexOf('#') == 0) {
      location.href = value;
    }
    else {
      form.submit();
    }
  });
 
 
  $('#services-split-list').easyListSplitter({ colNumber: 2 });
 
  $("#persondd").change(function(){
    var form = $('#personform');
    var value = $(this).val();
 
    if(value.indexOf('#') == 0) {
      location.href = value;
    }
    else {
      form.submit();
    }
  });
 
  $('A[href="mailto:etunimi.sukunimi@merilampi.com"]').each(function() {
    var source = $(this).attr('title') ? $(this).attr('title') : $(this).text();
    var user = source.split(' ').join('.').toLowerCase()
      .replace(/ä/g, 'a').replace(/ö/g, 'o').replace(/å/g, 'a').replace(/é/g, 'e');
    $(this).attr('href', $(this).attr('href').replace(/^mailto:[^@]+/, 'mailto:' + user.replace(/\.\./g, '.')));
    if (! $(this).hasClass('keep-text'))
      $(this).text($(this).attr('href').replace('mailto:', ''));
  });
 
  $('A[href="mailto:etunimi.sukunimi@merilampi.com"]').click(function(e) {
    e.preventDefault();
    document.location.href = $(this).attr('href');
  });
 
 
/* Front page service navigation */
 
    var services_nav_timeout = false;
 
 
    $("#services-nav a").click( function (e) {
				e.preventDefault();
        
        if( $("body").hasClass('services-open')) {
 			$("body").removeClass('services-open');
 		}
        
        var subnavi = $(this).next("code,div.code").html();
        /*
        if (!subnavi)
        {
            $("#services-box").slideUp("fast");
            clearTimeout(services_nav_timeout);
            return;
        }
        */
 
        $("#services-box .box_2 h2, #services-box .box_2 .content, #services-box .box_3 .content").hide();
 
        $("#services-box .box_1 .content").html( subnavi );
        $("#services-box .box_1 .content a:eq(0)").trigger("mouseenter");
        $("#services-box").slideDown("fast", function(){
        	$('body').addClass('services-open');
        });
        clearTimeout(services_nav_timeout);
        
    });
    
    $("body").click(function(){
 		if( $("body").hasClass('services-open')) {
 			$('#services-box').slideUp("fast");
 			$("body").removeClass('services-open');
 			clearTimeout(services_nav_timeout);
 		}
 	});

		
		$("#services-nav a, #services-box").mouseover( function () {
        clearTimeout(services_nav_timeout);
    });
 	
    $("#services-nav a, #services-box").mouseout( function () {
        services_nav_timeout = setTimeout( function () {
            $("#services-box").slideUp("fast");
            clearTimeout(services_nav_timeout);
        }, 2000);
    });
 
    $("#services-box").click( function () {
        $(this).slideUp("fast");
        clearTimeout(services_nav_timeout);
    });

 	
 
    
    $("#services-box .box_1 a").live("hover",
        function () {
            $("#services-box .box_1 a").removeClass("selected");
            $(this).addClass("selected");
 
            $("#services-box .box_2 h2").text( $(this).attr("title") ).show()
            $("#services-box .box_2 .content").html( $("code[rel='"+ $(this).attr("rel") +"'],.code[rel='"+ $(this).attr("rel") +"']").html() ).show();
            
            $("#services-box .box_3 .content").hide();
            $("#services-box .box_3 .content_"+ $(this).attr("rel") ).show();
        },
        function () {}
    );
 
    /* Front page boxes "scrolling" */
 
    $(".front-box").each( function () {
        var _this = $(this);
        _this.find(".news-item").not(":eq(0)").hide();
 
        _this.find(".scroller a").click( function () {
 
            if ($(this).hasClass("disabled")) return;
            _this.find(".scroller a").removeClass("disabled");
 
            var items = -1;
            var selected = 0;
            $.each(_this.find(".news-item"), function (i, item) {
                if ($(item).is(":visible")) selected = i;
                items++;
            });
 
            if ($(this).hasClass("scroll-up")) selected--;
            if ($(this).hasClass("scroll-down")) selected++;
 
            if (selected == 0)
            {
                selected = 0;
                $(this).addClass("disabled");
            }
            if (selected == items)
            {
                $(this).addClass("disabled");
            }
 
            _this.find(".news-item").hide();
            _this.find(".news-item:eq("+ selected +")").fadeIn("fast");
        });
    });
});
 
 
$(document).ready(function() {
    var divs = $('#header-roller div');
    var i = 0;
    var timer = false;
    
    divs.each(function() {
        var div = $('<div class="bg"></div>').css({
            background: 'url("'+$(this).attr('data-bg')+'") no-repeat top'
        });
        
        $('#content').before(div);
    });
    
    
    $('#wrap').css({background: 'none'});
    
    var bgs = $('#wrap .bg');
    
    if(divs.length === 1) {
        var div = divs.fadeTo(600, 1);
        bgs.fadeTo(600, 1);
    }
    else {
    function change() {
        clearTimeout(timer);
        
        var div = divs.fadeOut(300).eq(i % divs.length).fadeTo(600, 1);
        ul.find('li').eq(i % divs.length).addClass('selected').siblings().removeClass('selected')
        bgs.fadeOut(300).eq(i++ % divs.length).fadeTo(600, 1);
        
        timer = setTimeout(change, div.attr('data-timer'));
    }
    
    
    var ul = $('<ul></ul>').appendTo('#header-roller');
    
    divs.each(function(e) {
        $('<li></li>').click(function() {
            i = e;
            change();
        }).appendTo(ul);
    });
    
    change();
    }
    
var randomnumber = Math.round(Math.random()*6);
$('.sub-image-random').addClass('sub-random-' + randomnumber);

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"").replace(/^\n+|\n+$/g,"").replace(/^\r+|\r+$/g,"").replace(/^\t+|\t+$/g,"");
}


    $('#search-field').keyup(function(e) {
        var v = trim($(this).attr('value'));
        if(v.length>0) {
            $.ajax({
                url : '/autofill',
                data : 'autofill=' + v,
                cache: false,
                success: function(html){
                    if($(html).find('li').length>1) {
                    	$('#suggestions').addClass('active');
                    	$('#suggestions').show();
                        $('#suggestions').html(html);
                    }
                }
            });
        }
    });
    
    $('body').click(function(){
    	if ($('#suggestions').hasClass('active')) {
			console.log('aktiivinen');
			$('#suggestions').removeClass('active');
			$('#suggestions').hide();
		}
	});

//H1 title liian pitkä
var title = $('#right h1').text();
if (title.length > 80) {
	$('#right').css('padding', '3px 10px 0 10px');
}

//otayhteytta liitetiedostot
$('.liitetiedosto2').hide();
$('.liitetiedosto3').hide();

$('.right-col-form #q3').change(function(){
	$('.liitetiedosto2').show();	
});
$('.right-col-form #q4').change(function(){
	$('.liitetiedosto3').show();	
});

    
});
