﻿var nextSWF;
var thisSWF;

function _resize() {
	var locH = (typeof window.innerHeight == 'number') ? window.innerHeight : document.documentElement.clientHeight;	
	$('body').height(locH);
	locH = $('body').height();		
	var locT = (locH/2)-(590/2)-30;			
	if(locT < 70) {
		locT = 70;
		$('body').css('background-position','center -'+205+'px');
	} else {
		$('body').css('background-position','center center');
	}
	$('#wrapper').css('padding-top',''+locT+'px')
}

function publishOnFacebook(result) { 				
	var cardID = window.location.search;
	cardID = cardID.substring(1);
	var attachment = {
		'name':'Zając Mroku życzy...',
		'href':'http://wielkanoc.playmobile.pl/TwojaKartka.html?'+cardID,
		'description':'Arcymroczne życzenia wielkanocne składa Zając Mroku ;-)',
		'media':[{
			'type':'image',
			'src':'http://wielkanoc.playmobile.pl/_img/cardIco'+cardID+'.jpg',
			'href':'http://wielkanoc.playmobile.pl/TwojaKartka.html?'+cardID
		}]
	};

	FB.ensureInit(				
		function () 
		{ 
			FB.Connect.streamPublish('', attachment, null); 
		}
		
	); 
} 

$(document).ready(function(){		
	_resize();

	$(window).bind('resize',_resize);	
	$('a').focus(function() {
		$(this).trigger('blur');
	});
	$('#btn_next').click(function() {		
		var d = new Date();				
		window.location = 'kartka.html?'+nextSWF;		
		return false;
	});
	$('#btn_wyslij').click(function(){
		window.location = 'wyslij.html?'+thisSWF;		
		return false;
	});
	var locID = window.location.search;
	locID = locID.substring(1);
	
	$('#cs'+locID).hide();
	$('#showMyCard').css({'background-image':'url(_img/k'+locID+'.png)'});	
	
	$('#btn_wyslijEnd').click(function() {
		var that = this;
		$('label').find('span').remove();
		$('#tresc_error,#imie_error').html('&#160;').hide();
		$('#kartka_id').val(locID);
		$.ajax({
			url: $(that).attr('href'),
			data: $(that).siblings('form').serialize(),
			dataType: 'json',
			type: 'post',
			success: function(re) {							
				if(!re.result) { //error
					$.each(re.errors,function(i,o) {
						if(i != 'tresc' && i != 'imie') {
							$('input[name="'+i+'"]').parent().append('<span><br />'+o+'</span>');
						}
						else {
							$('#'+i+'_error').html(o).show();
						}
					});
				}
				else {
					window.location.href = 'end.html?'+locID;
					return false;
				}
			},
			error: function() {
				$('#tresc_error').html('Problemy techniczne. Prosze spróbowac pózniej.').show();				
			}
		});
		return false;
	});
	$('#soundButton').click(function() {
		document.getElementById('kartka').toggleSound();		
		// alert('Object: '+document.getElementById('kartka').id+' -> toggleSound');
		// $(this).trigger('blur');
		return false;
	});	
});