$(document).load(function() {
	
	// Slideshow
	var slideShow; 
	var slides = [1,2,3,4];
	var maxThumbs = 5;
	
	// Check if there's slideshow object
	for (var i = 0; i < slides.length; i++) {
		var slideName = 'P4Slides_' + slides[i];
		if (typeof window[slideName] != undefined && window[slideName] != undefined) {
			slideShow = window[slideName];
		}
	}
	
	if(slideShow != undefined) {
		
		var slideImages = slideShow.getImages().length;
		var slideParent = slideShow.$el.parent().attr('class');
	
		// Show controls only if more than 1 image in carousel and carousel is placed in header
		if(slideImages > 1 && slideParent != 'carousel-right') {
			$(".header").append('<a href="#" title="Edellinen" class="carousel_button_prev">Edellinen</a>');
			$(".header").append('<a href="#" title="Seuraava" class="carousel_button_next">Seuraava</a>');
		} 

		if(slideImages == 1) {
			slideShow.pause();
		}
		
		// Set active class to thumbnail
		slideShow.bind('p4SlidesChange', function() {
			
			$.fn.reverse = [].reverse;
			
			// Thumbnail highlight
			var currentThumbId = slideShow.getCurrent();
			$('.p4SlidesThumb').removeClass('activeThumb');
			if(currentThumbId >= 0) {
				$('.p4SlidesThumb:eq('+currentThumbId+')').addClass('activeThumb');
			}
			
			// Setup icons and text transition for slides with text area
			if($('.p4SlidesTextArea').length > 0 && currentThumbId >= 0) {
				Cufon.replace('.p4SlidesTextAreaName_Otsikko');
				
				var areas = $('.p4SlidesTextArea');
				
				if($('.textblock').length == 0) {
					$('.imageSwapContainer').prepend('<div class="textblock block-2" />');
					$('.imageSwapContainer').prepend('<div class="textblock block-1" />');
					
					$(areas).each(function(index) {
						$(this).removeClass('p4SlidesTextArea');	
						$(this).addClass('slide-text-area');	
					});
					
					$('.block-1').append('<div class="text-area-icon icon-'+slideShow.getCurrent()+'" />');
					$('.block-1').append(areas);
					$('.block-1').fadeToggle('8000');
					
				} else {
				
					$('.textblock').each(function() {
						if($(this).css('display') == 'block') {
							$(this).fadeToggle('8000', function() {
								$(this).empty();
							});
						}
						if($(this).css('display') == 'none') {
							$(areas).each(function(index) {
								$(this).removeClass('p4SlidesTextArea');	
								$(this).addClass('slide-text-area');	
							});
							$(this).append('<div class="text-area-icon icon-'+slideShow.getCurrent()+'" />');
							$(this).append(areas);
							$(this).fadeToggle('8000');
						}
					});
				}
			}
		});
		
		// Attach next and previous image methods to slider buttons
		$('.carousel_button_prev').click(function(event) {
			event.preventDefault();
			slideShow.previous();
		});
		
		$('.carousel_button_next').click(function(event) {
			event.preventDefault();
			slideShow.next();
		});
	
	}
	
});

Cufon.replace('.headernavi .horbar a', { hover:true });
Cufon.replace('.container .content h1, .container .content h2, .container .content h3');
Cufon.replace('.bottomblock .blocktitle .tm_text');
Cufon.replace('.bottomblock p.phone');
Cufon.replace('.thumb-titles span');
Cufon.replace('.multi_tabs a, .xm-anchors a, .map-navi a', { hover:true });
Cufon.replace('.xm-slot-name, .category-colors p');
Cufon.replace('.content .newstitle');

