/* Author: 

*/


jQuery(document).ready(function ($) {

	// Etusivun slider
    $('#slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:500, // Slide transition speed
        pauseTime:5000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:true, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:true, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.png', // ...this in thumb Image src
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:false, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Edellinen', // Prev directionNav text
        nextText: 'Seuraava', // Next directionNav text
        beforeChange: function(){move_indicator()}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
    

	// Mainostekstit sivujen yläkuvien päällä
   	InOut($('.text-overlay li:first'));
   	
 	
 	// Fallback INPUT-elementtien täyttämiseen, eli jos '' ei toimi
	if (Modernizr.input.placeholder) {
	  // your placeholder text should already be visible!
	} else {
		
		var placeholder = $('#s').attr('placeholder');

	   	$('#s').val(placeholder);
	   	$('#s').focus(function() {
	   		$(this).val(null);	
	   	});
	   	$('#s').blur(function() {
	   		if ($(this).val() == '') {
	   			$(this).val(placeholder);	
	   		}
	   	});
	   	
	   	var inputs = ['#user', '#password'];
	   		
		for (var i = inputs.length-1; i >= 0; i--) {
		   	var value = $(inputs[i]).attr('placeholder');
		   	$(inputs[i]).val(value);
		   	$(inputs[i]).focus(function() {
		   		$(this).val(null);	
		   	});
		   	$(inputs[i]).blur(function() {
		   		if ($(this).val() == '') {
		   			$(this).val(value);	
		   		}
		   	});
		}
	}


   	// Footer aina sivun alalaidassa
   	$('#colophon').pinFooter('relative');
   	
   	$(window).resize(function() {
   		$('#colophon').pinFooter('relative');
	});

});


function InOut( elem ) {
	if ($('html').hasClass('opacity')) {
		elem.delay(500)
		.fadeIn(2500)
		.delay(2500)
		.fadeOut(1500, function(){ 
			if(elem.next().length > 0) // if there is a next element
				{InOut( elem.next() );} // use it
			else
				{InOut( elem.siblings(':first'));} // if not, then use go back to the first sibling
		});
	}
	// FadeIn/Out ei toimi kunnolla IE7/8:ssa
	else {
		elem.delay(500)
		.slideToggle(2500)
		.delay(2500)
		.slideToggle(1500, function(){ 
			if(elem.next().length > 0) // if there is a next element
				{InOut( elem.next() );} // use it
			else
				{InOut( elem.siblings(':first'));} // if not, then use go back to the first sibling
		});
	}
}

// etusivun sliderin indikaattorinuoli
function move_indicator() {
	var start = $('#indicator').position();
	var newPos = start.left + 245;
	if (newPos > 900) {
		newPos = 114;
	}
    $('#indicator').animate({
	   	left: newPos+'px'
    })
}


/******************************************************
	* jQuery plug-in
	* Easy Pinned Footer
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone
******************************************************/

// tää oli vähän paska, mutta korjattu toimivaks, vois vielä putsaa koodin ja laittaa assetiks

(function($) {
	// plugin definition
	$.fn.pinFooter = function(options) {		
		// Get the height of the footer and window + window width
		var wH = $(window).height();
		var wW = getWindowWidth();
		var fH = $(this).outerHeight(true);
		var bH = $("body").outerHeight(true);
		//console.log(bH);
		var pH = $("#page").outerHeight(true);		
		var mB = parseInt($("body").css("margin-bottom"));
		
		if (options == 'relative') {
			if ((bH) < wH) {
				$(this).css("position","fixed");
				$(this).css("width",wW + "px");
				$(this).css("top",wH - fH + "px");
				//console.log(bH);
				//console.log(fH);
				//console.log(pH);
				$("body").css('min-height',pH+fH+'px');
			}
		} else { // Pinned option
			// Set CSS attributes for positioning footer
			$(this).css("position","fixed");
			$(this).css("width",wW + "px");
			$(this).css("top",wH - fH + "px");
			$("body").css("height",(bH + mB) + "px");
		}
	};
	
	// private function for debugging
	function debug($obj) {
		if (window.console && window.console.log) {
			window.console.log('Window Width: ' + $(window).width());
			window.console.log('Window Height: ' + $(window).height());
		}
	};
	
	// Dependable function to get Window Height
	function getWindowHeight() {
		var windowHeight = 0;
		if (typeof(window.innerHeight) == 'number') {
			windowHeight = window.innerHeight;
		}
		else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
			else {
				if (document.body && document.body.clientHeight) {
					windowHeight = document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	};
	
	// Dependable function to get Window Width
	function getWindowWidth() {
		var windowWidth = 0;
		if (typeof(window.innerWidth) == 'number') {
			windowWidth = window.innerWidth;
		}
		else {
			if (document.documentElement && document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			}
			else {
				if (document.body && document.body.clientWidth) {
					windowWidth = document.body.clientWidth;
				}
			}
		}
		return windowWidth;
	};
})(jQuery);
