$(document).ready(function() {
	$('input.autofocus').addClass("idleField");
	$('input.autofocus').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){this.select();}
	});
	$('input.autofocus').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
});

Array.prototype.inArray = function (value) {
	for (var i=0; i < this.length; i++) if (this[i] === value) return true;
	return false;
};

function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}

var curHash;
var checkInt;
var videoList;
var vimeoUrl = 'http://player.vimeo.com/video/[id]?color=ff0179';

$(window).ready(function(){
	
	// Load the first video
	if(is_array(videoList)) curHash = '#' + videoList[0];
	
	if(window.location.hash != '') curHash = window.location.hash;
	reloadHash();
	
	// Start listening to hash change
	checkInt = setInterval(checkForHash, 250);
	
	// Frontpage hover fader
	$('#homenav > ul').hover(function(e){
		$("ul", this).show();
		$("ul .hidden", this).hide();
		$("ul .hidden", this).each(function(){
			$(this).fadeIn(100);
		});
	}, function(e){
		$("ul", this).show();
		$("ul .hidden", this).fadeOut(200);
		
	});
});

function checkForHash(){
	if(window.location.hash && window.location.hash != curHash) {
		curHash = window.location.hash;
		reloadHash();
	}
}

function reloadHash(){
	if(!videoList || !videoList.inArray(parseFloat(curHash.substr(1, curHash.length)))) return false;

	// Piilotta <div class="video_item active">
	$('.video_item.active').removeClass('active');
	// Näyttää <div id="video_item_12345678">
	$('#video_item_' + parseFloat(curHash.substr(1, curHash.length))).addClass('active');

	// piilottaa kaikki <div class="video_details">
	$('.video_details').hide();
	// Näyttää <div id="video_details_12345678">
	$('#video_details_' + parseFloat(curHash.substr(1, curHash.length))).show();

	// Vaihdetaan vimeo iframen source
	var videoUrl = vimeoUrl.replace('[id]', parseFloat(curHash.substr(1, curHash.length)));
	$('#vimeo_player iframe').attr('src', videoUrl);
}
