Cufon.replace('header', { fontFamily: 'Montrachet' });
Cufon.replace('section#about, a.ref-url, div#contact-bubble, span.ref-url', { fontFamily: 'HelsinkiLight' });
Cufon.replace('section#people h2, div.phone, div.email, section#references h1, section#people h1, section#contact h1, p#social, p#copy span', { fontFamily: 'HelsinkiRegular' });
Cufon.replace('section#people h3', { fontFamily: 'HelsinkiBook' });

var next_page_index = 2;
var img_w = 900;
var speed = 500;

$(document).ready(function() {
	
	// flash
	//var flashvars = {},params = {wmode:"transparent"},attributes = {};
	//swfobject.embedSWF("dynyflash.swf", "theflash", '900', '459', "9.0.0", false, flashvars, params, attributes);
	
	/// aaa vaihto
	$('a#aaa').click(function() {
		$('div.topbanner-content').animate({"left": "-900px"}, speed);
	});
	$('a#sm-etusivu').click(function() {
		$('div.topbanner-content').animate({"left": "0"}, speed);
	});
	
	// set easing
	jQuery.easing.def = "easeOutCubic";
	
	var max_page_index = $('div#wrap').children().size() - 3;
	
	// reposition left menu
	var new_l = parseInt(($(window).width() - 990) / 2);
	$('ul#sidemenu').css('left', new_l + 'px');
	
	// reposition scroll btns
	$('a#scroll-up, a#scroll-do').css('right', new_l + 'px');
	
	// scroll page
	$('a#scroll-do').click(function() {
		// check closest element
		var topsid = new Array();
		var i = 0;
		$('div#wrap').children().each(function () {
			offset_top = parseInt($(this).offset().top - $(window).scrollTop());
			id = $(this).attr('id');
			if (id)
			{
				if (offset_top > 85)
				{
					topsid[i] = id;
					i++;
				}
			}
		});
		topsid = topsid.slice(0, topsid.length - 1);
		var next_top_id = topsid[0];
		var elm = $('div#wrap #'+next_top_id);
		var new_t = $(elm).offset().top - 85;
		$('html, body').stop().animate({
         		scrollTop: new_t
        }, 1000);
	});
	
	$('a#scroll-up').click(function() {
		var topsid = new Array();
		var i = 0;
		$('div#wrap').children().each(function () {
			offset_top = parseInt($(this).offset().top - $(window).scrollTop());
			id = $(this).attr('id');
			if (id)
			{
				if (offset_top < 85)
				{
					topsid[i] = id;
					i++;
				}
			}
		});
		topsid.reverse();
		var next_top_id = topsid[0];
		var elm = $('div#wrap #'+next_top_id);
		var new_t = $(elm).offset().top - 85;
		$('html, body').stop().animate({
         		scrollTop: new_t
        }, 1000);
	});

	$('ul#mainmenu a, ul#sidemenu a').click(function(event) {
		event.preventDefault();
		 var elm = $(this).attr('href');
		 var new_t = $(elm).offset().top - 85;
		 $('html, body').stop().animate({
         	scrollTop: new_t
         }, 1000);
	});
	
	// scroll from thumb references
	$('div.thumb a').click(function(event) {
		event.preventDefault();
		var elm = $(this).attr('href');
		var new_t = $(elm).offset().top - 85;
		$('html, body').stop().animate({
         scrollTop: new_t
        }, 1000);
	});
	
	// thumb mo 
	$('div.thumb a').mouseover(function() {
		var src = $(this).find('img').attr('src');
		var newsrc = src.replace('_mv.jpg', '.jpg')
		$(this).find('img').attr('src', newsrc);
	});
	
	$('div.thumb a').mouseout(function() {
		var src = $(this).find('img').attr('src');
		var newsrc = src.replace('.jpg', '_mv.jpg')
		$(this).find('img').attr('src', newsrc);
	});

    
    // change reference pic
    $('a.prev').click(function() {
    	// get ref id
    	var id = parseInt($(this).parent().attr('id').substring(4));
    	var index = parseInt(id - 1);
    	
    	var elm_move = $(this).parent().find('div.ref-content');
    	image_count[index] =  elm_move.children().size();
		eval('var moving'+index+' = false');
		if (eval('moving'+index) == false && (image_current[index] - 1) > 0)
		{
			eval('var moving'+index+' = true');
			image_current[index]--;
			var newx = -(image_current[index] - 1) * img_w;
			eval('var moving'+index+' = false');
			if (image_current[index] == 1)
				$(this).parent().find('a.prev').hide();
			if (!$(this).parent().find('a.next').is(':visible'))
				$(this).parent().find('a.next').fadeIn();
			$(elm_move).animate({"left": newx + "px"}, speed);
		}
    });
    
    $('a.next').click(function() {
    	// get ref id
    	var id = parseInt($(this).parent().attr('id').substring(4));
    	var index = parseInt(id - 1);

    	// load next image
    	var next_src = eval('refe_arr'+id+'[image_current['+index+']]');;
    	var next_src_holder = $(this).parent().find('div.ref-content span:nth-child('+(1 + image_current[index])+')');
    	next_src_holder.html('<img src="'+next_src+'" width="900" height="459">');
    	
    	// move
    	var elm_move = $(this).parent().find('div.ref-content');
    	image_count[index] =  elm_move.children().size();
		eval('var moving'+index+' = false');
		if (eval('moving'+index) == false && image_current[index] < image_count[index])
		{
			eval('var moving'+index+' = true');
			image_current[index]++;
			var newx = -(image_current[index] - 1) * img_w;
			if (image_current[index] == (image_count[index] - 1))
			{
				$(this).parent().find('a.next').hide();
				eval('var moving'+index+' = false');
			}
			else
				eval('var moving'+index+' = false');
			if (!$(this).parent().find('a.prev').is(':visible'))
				$(this).parent().find('a.prev').fadeIn();
			$(elm_move).animate({"left": newx + "px"}, speed);
		}
    }); 
    
    // social logos mouseover/out
    $('a#sociallogos').mouseover(function() { $('p#social').fadeIn(); });
    $('a#sociallogos').mouseout(function() { $('p#social').fadeOut(); });
    
});

$(window).resize(function() {
	// reposition left menu
	var new_l = parseInt(($(window).width() - 990) / 2);
	$('ul#sidemenu').css('left', new_l + 'px');
	
	// reposition scroll btns
	$('a#scroll-up, a#scroll-do').css('right', new_l + 'px');
});
