var moving = false;
var img_w = 804;
var speed = 500;
var image_count = 0;
var image_current = 1;

$(document).ready(function() {
	img_w = 804;
	speed = 500;
	
	image_count =  $('div#home-carusel-container').children().size(); // how many images on carusel
	image_current = 1;
	
	var caruInterval = setInterval( 'nextPhoto()', 7000);
	
	$('a#home-caru-next').click(function() {	
		clearInterval(caruInterval);
		nextPhoto();
	});
	
	$('a#home-caru-prev').click(function() {
		clearInterval(caruInterval);
		if (moving == false)
		{		
			if (image_current > 1)
			{
				$('a#home-caru-next').css({backgroundPosition: '0 0'});
				image_current--;
				moving = true;
				$('a.caru-navi').css({backgroundPosition: '0 0'});
				$('a#caru-navi-' + image_current).css({backgroundPosition: '-22px 0'});
				$("div#home-carusel-container").animate({"left": "+="+img_w+"px"}, speed, 'easeOutQuart', function() {																	
					moving = false;
				});
				if (image_current == 1)
					$(this).css({backgroundPosition: '0 -127px'});
			}
		}
	});
	
	$('a.caru-navi').click(function() {	
		clearInterval(caruInterval);
		image_current = parseInt($(this).attr('id').substring(10));
		if (moving == false)
		{
			$('a.caru-navi').css({backgroundPosition: '0 0'});
			$('a#caru-navi-' + image_current).css({backgroundPosition: '-22px 0'});
			var newx = -(image_current - 1) * img_w;
			$("div#home-carusel-container").animate({"left": newx + "px"}, speed, 'easeOutQuart', function() {																	
				moving = false;
			});
			if (image_current == 1)
			{
				$('a#home-caru-prev').css({backgroundPosition: '0 -127px'});
				$('a#home-caru-next').css({backgroundPosition: '0 0'});
			}
			else if (image_current == image_count)
			{
				$('a#home-caru-prev').css({backgroundPosition: '0 0'});
				$('a#home-caru-next').css({backgroundPosition: '0 -127px'});
			}
			else
			{
				$('a#home-caru-prev').css({backgroundPosition: '0 0'});
				$('a#home-caru-next').css({backgroundPosition: '0 0'});
			}
			
			// load next photo
			var nextslot = image_current + 1;
			$('span#photo-slot-' + nextslot).html('<img src="img/content/kuva' + nextslot + '.jpg" width="804" height="410" alt="Design-Talo" />');
		}
	});
	
	$('a#katsovideo').click(function() {
		var win_w = $(window).width();
		var win_h = $(window).height();
		
		var magn_w = 638;
		var magn_h = 486;
		
		var magn_x = parseInt((win_w - magn_w) / 2);
		var magn_y = $(window).scrollTop() + parseInt((win_h - magn_h) / 2);
		
		fadeInBg();
		
		$('div#popup-video').css({left: magn_x + 'px'});
		$('div#popup-video').css({top: magn_y + 'px'});
		$('div#popup-video').css({display: 'block'});
		
		var flashvars = {};
		var params = {};
		var attributes = {};
		swfobject.embedSWF("dt_messutalo_v1.swf", "theflash", "640", "480", "9.0.0", false, flashvars, params, attributes);
	});
	
	$('a#sulje').click(function() {						
		$('div#popup-bg').remove();
		$('div#popup-video').css({display: 'none'});
		$('div#theflash').html('Videon katsomiseen tarvitset Flash-playerin.');
	});
	
	$('a#kaveri').click(function() {
		var win_w = $(window).width();
		var win_h = $(window).height();
		
		var magn_w = 581;
		var magn_h = 504;
		
		var magn_x = parseInt((win_w - magn_w) / 2);
		var magn_y = $(window).scrollTop() + parseInt((win_h - magn_h) / 2);
		
		fadeInBg();
		
		$('div#popup-kaveri').css({left: magn_x + 'px'});
		$('div#popup-kaveri').css({top: magn_y + 'px'});
		$('div#popup-kaveri').css({display: 'block'});
	});
	
	$('a#sulje-kaveri, a.sulje-kaveri').click(function() {
		$('div#popup-bg').remove();
		$('div#popup-kaveri').css({display: 'none'});
	});
	
	$('a.sulje-kaveri').live('click', function() {
		$('div#popup-bg').remove();
		$('div#popup-kaveri').css({display: 'none'});
	});
	
	$('input#nimi-saaja').keyup(function() {
		$('textarea#viesti').val('Hei '+$(this).val()+'!\n\nKaverisi '+$('input#lahettaja').val()+' tutustui Design-Talon Kuopion asuntomessujen kohteeseen ja halusi vinkata siit\344 sinullekin. P\344\344set katsomaan kohdetta ja sen yksityiskohtia t\344st\344: www.messutalo2010.fi');
	});
	
	$('input#lahettaja').keyup(function() {
		$('textarea#viesti').val('Hei '+$('input#nimi-saaja').val()+'!\n\nKaverisi '+$(this).val()+' tutustui Design-Talon Kuopion asuntomessujen kohteeseen ja halusi vinkata siit\344 sinullekin. P\344\344set katsomaan kohdetta ja sen yksityiskohtia t\344st\344: www.messutalo2010.fi');
	});
	
	$('input#kaveri-laheta').click(function(event) {	
		event.preventDefault();
		if ($(this).parent().parent().find('input#nimi-saaja').val() == '')
		{
			alert('Vastaanottajan nimi puuttuu');
			return false;
		}
		else if ($(this).parent().parent().find('input#lahettaja').val() == '')
		{
			alert('L\344hett\344j\344n nimi puuttuu');
			return false;
		}
		else if ($(this).parent().parent().find('input#sahkoposti').val() == '')
		{
			alert('Vastaanottajan s\344hk\366postiosoite puuttuu');
			return false;
		}
		else if ($(this).parent().parent().find('textarea#viesti').val() == '')
		{
			alert('Viesti puuttuu');
			return false;
		}
		var recipient_name_j = $(this).parent().parent().find('input#nimi-saaja').val();
		var recipient_j = $(this).parent().parent().find('input#sahkoposti').val();
		var sender_j = $(this).parent().parent().find('input#lahettaja').val();
		var msg_j = $(this).parent().parent().find('textarea#viesti').val();
		var elm = $(this);
		$.post('http://www.messutalo2010.fi/sender.php', { recipient_name: recipient_name_j, recipient: recipient_j, sender: sender_j, msg: msg_j }, function() {
			elm.parent().html('<p>Viesti l&auml;hetettiin.</p><p><a href="javascript:;" class="vinkki-sulje">Sulje</a></p>');
		});
		elm.parent().parent().html('<p>Viesti l&auml;hetettiin.</p><p><a href="javascript:;" class="sulje-kaveri">Sulje</a></p>');
	});
	
});

function fadeInBg() {
	$('body').append('<div id="popup-bg"></div>');	
	$('div#popup-bg').css({
		opacity: '0.73',
		display: 'none',
		width: $(document).width(),
		height:	$(document).height()
	}).fadeIn();
}

jQuery.easing.easeOutQuart = function (x, t, b, c, d) { 
    return -c * ((t=t/d-1)*t*t*t - 1) + b; 
}; 

function nextPhoto() {
	if (moving == false)
	{		
		if (image_current < image_count)
		{
			$('a#home-caru-prev').css({backgroundPosition: '0 0'});
			image_current++;
			moving = true;
			$('a.caru-navi').css({backgroundPosition: '0 0'});
			$('a#caru-navi-' + image_current).css({backgroundPosition: '-22px 0'});
			$("div#home-carusel-container").animate({"left": "-="+img_w+"px"}, speed, 'easeOutQuart', function() {																	
				moving = false;
			});
			if (image_current == image_count)
				$('a#home-caru-next').css({backgroundPosition: '0 -127px'});
				
			// load next photo
			var nextslot = image_current + 1;
			$('span#photo-slot-' + nextslot).html('<img src="img/content/kuva' + nextslot + '.jpg" width="804" height="410" alt="Design-Talo" />');
		}
		else
		{
			$('a#home-caru-next').css({backgroundPosition: '0 0'});
			$('a#home-caru-prev').css({backgroundPosition: '0 -127px'});
			image_current = 1;
			moving = true;
			$('a.caru-navi').css({backgroundPosition: '0 0'});
			
			$("div#home-carusel-container").animate({"left": "-=100px"}, 200, 'easeOutQuart', function() {																	
				$("div#home-carusel-container").animate({"left": 0}, 1000, 'easeOutQuart', function() {																	
					moving = false;
					$('a#caru-navi-' + image_current).css({backgroundPosition: '-22px 0'});
				});
			});
		}
	}
}
