var wait = 6000;
var fxDelay = 1500;

var i = 0;
var curImage = 0;
var clickDisabled = false;

var playBackEnabled = false;
var disableMove = false;

var imageShow, imageHide;

var showCaseArr = new Array();

$(document).ready(function() {

	/* fancybox settings */
	$("a.group").fancybox({
		'speedIn'		:	300, 
		'speedOut'		:	300, 
		'overlayShow'	:	true,
		'titlePosition'	:	'outside',
		'autoScale'		:	'false'
	});

	$("a.iframe").fancybox({
		'type'			: 	'iframe',
		'height'		:	'80%',
		'width'			:	'80%'
	});

	updateFooter();
	positionSubMenu();

	if(showCaseArr != "undefined") {
		numOfImages = showCaseArr.length;
	}
	
	if(playBackEnabled) {
		startPlayBack();	
	}
	
	var bgColor = $('#sc-1').css('backgroundColor');
	$('.displayWindowHolder').css( { backgroundColor: bgColor } );
	
	$('[rel|=btn_showCase]').bind('click', function(event) {
		
		if(clickDisabled) {
			return;
		}
		
		goTo(parseInt($(event.currentTarget).attr('id').match(/[\d\.]+/g)[0]));
		clearInterval(playInterval);
		playInterval = setInterval('play()',wait);
		clickDisabled = true;
		
	});

	$('a.workListImage').each(function() {
		$('#' + this.id).css( {backgroundPosition: '0px -645px'} );	
	});

	$('a.workListImageSmall').each(function() {
		$('#' + this.id).css( {backgroundPosition: '-240px -645px'} );	
	});
	
	var showInfoTimeout = new Object;
	$('a.workListImage').mouseenter(function() {

		var numId = this.id.match(/[\d\.]+/g)[0];
		showInfoTimeout.numId = setTimeout("showWorkInfo('"+ numId +"')", 300);

	});

	$('a.workListImage').mouseleave(function() {

		// clear timeout
		clearTimeout(showInfoTimeout.numId);
		
		var numId = this.id.match(/[\d\.]+/g)[0];

		$(this).animate({backgroundPosition: '(0px -645px)'}, 500 );
		$("#workInfo-" + numId).animate({marginTop: '237px'}, 500 );
		
	});
	
	$('.input_joinNewsLetter').click(function() {
		$(this).css({color: 'black'});
		$(this).val(($(this).val() != "Sähköpostiosoitteesi" ? $(this).value() : ""));
	});

	setMenuNavigation();
	
});

$(window).bind('resize', function() { 
	updateFooter();
	positionSubMenu();
});

function showWorkInfo(numId) {
	
	$("#workLink-" + numId).animate({backgroundPosition: '(0px -405px)'}, 500 );
	$("#workInfo-" + numId).animate({marginTop: ($("#workLink-" + numId).height() - $("#workInfo-" + numId).outerHeight())}, 500 );
	
}

function updateFooter() {
	
	if(typeof(disableFooterUpdate) != "undefined" && disableFooterUpdate == true) {
		return;
	}
	
	var windowHeight = $(window).height();
	var siteHeight = parseInt($('#header').height() + $('#contentHolder').height());
	
	var diff = (windowHeight - siteHeight < 240 ? 240 : windowHeight - siteHeight);
	
	$('#footer').css( { height: diff, maxHeight: 500 } );
}

function updateBgColors(id) {
	
	var color = $('#sc-' + parseInt(id)).css('backgroundColor');
	$('.displayWindowHolder').animate( {
			backgroundColor: color
		},
		1000
	);
	
	setTimeout(showImage, 550);
	
}

function showImage() {
	$('#nav-sc-' + imageShow).addClass('on');
	
	$("#" + showCaseArr[imageShow]).fadeIn(fxDelay / 2, function() {
		clickDisabled = false;
	});
}

function positionSubMenu() {
	if($(window).width() < 960) {
		$('#subMenuHolder').css( { marginLeft: '0px', left: '0' } );
	}
	else {
		$('#subMenuHolder').css( { marginLeft: '-480px', left: '50%' } );
	}
}

function startPlayBack() {
	playInterval = setInterval('play()',wait);
}

function stopPlayBack() {
	clearInterval(playInterval);
}

function play() {
	
	imageShow = i+1;
	imageHide = curImage;
	
	if (imageShow == numOfImages) {
		goTo(0);	
		i = 0;					
	} else {
		goTo(curImage + 1);			
		i++;
	}
	
}

function goTo(i) {
	
	if(i > numOfImages - 1) {
		i = 0;
	}
	
	imageShow = i;
	imageHide = curImage;

	curImage = i;
	
	setTimeout("updateBgColors('"+ parseInt(curImage + 1) +"')", 300)
	$('[rel|=btn_showCase]').removeClass('on');
	$("#" + showCaseArr[imageHide]).fadeOut(300);
	
}

function showReelPlayStart() {
	stopPlayBack();
}

function formMethods() {
	
	var self = this;
	var requiredFields = new Object();
	requiredFields["tapaaminen"] = new Array(
			'ajankohta',
			'nimi',
			'puhelin',
			'email',
			'viesti',
			'tapaaminen_spam'
	);
	
	requiredFields["tarjouspyynto"] = new Array(
			'tarjous_nimi',
			'tarjous_puhelin',
			'tarjous_email',
			'tarjous_viesti',
			'tarjous_spam'
	);
	
	requiredFields["tahdonlisatietoja"] = new Array(
			'tahdonlisatietoja_viesti',
			'tahdonlisatietoja_nimi',
			'tahdonlisatietoja_puhelin',
			'tahdonlisatietoja_email',
			'tahdonlisatietoja_spam'
	);
	
	requiredFields["brief"] = new Array(
			'nimi',
			'yritys',
			'brief_spam'
	);

	this.check = function(formname) {

		var errors = 0;
		for(i=0; i<requiredFields[formname].length; i++) {
			if(
				$("#" + requiredFields[formname][i]).val() == ""
				|| (requiredFields[formname][i] == formname + "_spam" && $("#" + requiredFields[formname][i]).val() != "kiivi")
			){
				$("#" + requiredFields[formname][i]).css( { background: '#FFEFEF'} );
				errors++;
			}
			else {
				$("#" + requiredFields[formname][i]).css( { background: 'transparent'} );
			}
		}

		if(errors > 0) {
			$("#" + formname + "_fillRequired").show();
		}
		else {
			$("#contactForm").submit();
		}

	}
	
	this.changeForm = function(value) {
		$('[rel|=changeableForm]').hide();
		$("#contactForm_" + value).show();
	}
	
}

var formmethods = new formMethods();

function setMenuNavigation() {
	
	if(typeof(autoScrollDisabled) != "undefined" && autoScrollDisabled == true) {
		return false;
	}
	
	$('a[href*=#]').click(function() {
		$(this).addClass('on');
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
				&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffsetTop = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffsetTop}, 1000, 'easeOutCirc');
				return false;
			}
		}
	 });	
}

