// global vars
var emailReg 		= /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var blnHasVideo 	= false;
var jackpotResult 	= 0;
// init
$(document).ready(function(){    	
	// ambassadeur
	$('#ambassadeur').animate({width: '60px'}, 500);
	$('#ambassadeur').animate({width: '34px'}, 500);
	$('#ambassadeur').mouseenter(function(event) {
		$(this).animate({width: '60px'}, 500);
	});
	$('#ambassadeur').mouseleave(function(event) {	
		$(this).animate({width: '34px'}, 500);		
	});	
	$('#ambassadeur').click(function(event) {	
		window.open('http://www.ondernemenversterktlimburg.be/leden/');		
	});	
	
	// winnaars
	$('#winnaars').animate({width: '70px'}, 500);
	$('#winnaars').animate({width: '34px'}, 500);
	$('#winnaars').mouseenter(function(event) {
		$(this).animate({width: '60px'}, 500);
	});
	$('#winnaars').mouseleave(function(event) {	
		$(this).animate({width: '34px'}, 500);		
	});	
	$('#winnaars').click(function(event) {
		event.preventDefault();								  
		$.fancybox(			
			{
				'href'				: 'winnaars.php',
				'autoDimensions'	: false,
				'width'         	: 700,
				'height'        	: 446,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			}
		);
	});
	
	// speel en win
	$('#reischeque').click(function() {
	  	$('#reischeque').attr('class','NoScreenDisplay');
		$('#speel-win').attr('class','speel-win');
	});
	$('#speelwinClose').click(function() {
		$('#reischeque').attr('class','reischeque');
		$('#speel-win').attr('class','NoScreenDisplay');
	});	
	$('#submitA').click(function() {
		testAnswer();
	});
	// formulier
	$('#jpVerderWin').click(function() {
		startForm();
	});
	$('#jpVerderLose').click(function() {
		startForm();
	});
	$('#wForm').submit(function() {								
		submitForm();		
		return false;
	});
	// share fb + tw
	$('#shareFb').click(function() {
		$('#kans_2').removeAttr('disabled');
	});
	$('#shareTw').click(function() {
		$('#kans_3').removeAttr('disabled');
	});
	
	// slideshow	
	$('#slideshow').cycle({
		fx: 'scrollLeft',		
		speed:  'fast', 
	    timeout: 0,
	    prev:   '#slideDown',
		next:   '#slideUp',
		after:   onAfterCycle
	});
	// onAfterCycle
	function onAfterCycle(obj){			
		if(this.id == 'intro'){
			document.getElementById('introTv').className 	  = 'introTv';
			document.getElementById('videowrapper').className = 'NoScreenDisplay';

		}else{		
			document.getElementById('introTv').className 	  = 'NoScreenDisplay';
			document.getElementById('videowrapper').className = 'videowrapper';
			stepName = String(this.id).split('_')[1];				
			jwplayer('videocontainer').setup({
				id: 'testvideo',									 
				flashplayer: 'jwplayer/player.swf',				
				controlbar: "none",
				file: 'jwplayer/film/VKW-'+stepName+'.f4v',
				image: 'jwplayer/film/VKW-'+stepName+'.png',
				height: 270,
				width: 480
			});
		}					
	}
	
});
// testAnswer
function testAnswer(){	
	answer		= -1;
	$('input').each(function(){				 
		answer = ($(this).is(':checked'))?$(this).val():answer;							 	
	});
	if(answer == cAnswer){		
		$('#speelwinError').attr('class','NoScreenDisplay');
		showJackpot();
	}else{		
		$('#speelwinError').attr('class','speelwinError');
	}
}
// showJackpot
function showJackpot(){
	$('#video').attr('class','NoScreenDisplay');
	$('#speel-win').attr('class','NoScreenDisplay');
	
	$('#jackpot').attr('class','jackpot');
	$('#jackpotPrijzen').attr('class','jackpotPrijzen');
}
// testJackpot
function testJackpot(jpResult){
	$('#jackpotPrijzen').attr('class','NoScreenDisplay');
	$('#jackpotResult').attr('class','jackpotResult');	
	if(jpResult == 0){
		$('#jackpotResultWin').attr('class','NoScreenDisplay');
		$('#jackpotResultLose').attr('class','jackpotResultLose');
	}else{
		$('#jackpotResultWin').attr('class','jackpotResultWin');
		$('#jackpotResultLose').attr('class','NoScreenDisplay');
		$('img[name^="jpWin_"]').each(function(){				
			$(this).attr('class','NoScreenDisplay');				 	
		});
		$('img[name^="jpWin_'+jpResult+'"]').attr('class','jpWin');
	}	
	jackpotResult = jpResult;
}
// startForm
function startForm(){
	$('#jackpot').attr('class','NoScreenDisplay');
	$('#jackpotResult').attr('class','NoScreenDisplay');
	$('#formulier').attr('class','formulier');
	$('#reischeque_groot').attr('class','reischeque_groot');		
	$('#kans_2').attr('disabled', 'disabled');
	$('#kans_3').attr('disabled', 'disabled');	
}
// submitForm
function submitForm(){
	var arrFields 		= new Array("voornaam","achternaam","email","straat","woonplaats","postcode");
	var arrErrorFields 	= new Array;
	$(arrFields).each(function(i){
		if($('input[name="'+arrFields[i]+'"]').val()==''){
			arrErrorFields.push(arrFields[i]);
		}else if(arrFields[i] == 'email' && !emailReg.test($('input[name="'+arrFields[i]+'"]').val())){
			arrErrorFields.push(arrFields[i]);
		}
	});
	if(isNaN($('input[name="kans_1"]').val())){
		arrErrorFields.push('kans 1');
	}
	if($('input[name="kans_2"]').val() != '' && isNaN($('input[name="kans_2"]').val())){
		arrErrorFields.push('kans 2');
	}
	if($('input[name="kans_3"]').val() != '' && isNaN($('input[name="kans_3"]').val())){
		arrErrorFields.push('kans 3');
	}
	
	if(arrErrorFields.length > 0){
		var txtError = '';
		$(arrErrorFields).each(function(j){
			txtError += "Geef een correcte waarde voor "+arrErrorFields[j]+".\r\n";				   			
		});								   
		alert(txtError);
	}else{	
		arrFields.push("kans_1");
		arrFields.push("kans_2");
		arrFields.push("kans_3");
		
		var dataString = '';
		$(arrFields).each(function(k){
			dataString += (dataString == '')?'':'&';
			dataString += arrFields[k]+'='+$('input[name="'+arrFields[k]+'"]').val();
		});			
		dataString += "&jackpotresult="+jackpotResult;
		$.ajax({
			url: "submitForm.php",		
			data: dataString,
			success: function(r){
				$('#formulier').attr('class','NoScreenDisplay');
				$('#formulierResult').attr('class','formulierResult');
				
				if(r > 0){					
					result = '<div id="bedankt"><h3>Proficiat, je hebt gewonnen.</h3><br><h4>Je krijgt binnen enkele dagen een e-mail van VKW Limburg. Daarin staan alle details over hoe je jouw prijs kan ontvangen.</h4><br><h3>Waag morgen opnieuw je kans!</h3><br><br><a href="http://www.ondernemenversterktlimburg.be" target="_self">&raquo; Terug naar home</a></div>';
				}else{
					result = '<div id="bedankt"><h3>Bedankt dat je aan onze wedstrijd deelnam.</h3><br><h4>De winnaar van de reischeque krijgt op 8 juni een persoonlijk bericht. Ook op onze website maken we de winnaar bekend. Alvast veel succes!</h4><br><h3>Waag morgen opnieuw je kans!</h3><br><br><a href="http://www.ondernemenversterktlimburg.be" target="_self">&raquo; Terug naar home</a></div>';					
				}
				$('#formulierResult').html(result);
				Cufon.replace('h3, h4',  { fontFamily: 'Helvetica Neue LT Std' });							
			}
		});
	}
}

function showReglement(){
	$(document).ready(function() {
		$.fancybox(			
			{
				'href'				: 'reglement.php',
				'autoDimensions'	: false,
				'width'         	: 700,
				'height'        	: 446,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			}
		);
	});
}

// F L A S H

// 1. Flash comm. methods
	function flashReady() {
		// flash can receive input now
		//alert("flashready");
	}
	
	function spinResult(r) {
		testJackpot(r);
	}

	var flashvars = {};
	flashvars.preloader_swf = "slotmachine.swf";
	flashvars.preloader_cleanup = "true";
	flashvars.service_url = "getJackpot.php";
	flashvars.error_txt = "U mag slechts 1x per dag meespelen.";
	var params = {};
	params.SeamlessTabbing = "false";
	params.allowFullScreen = "true";
	/*params.bgcolor = "#656565";*/
	params.wmode = "transparent";
	params.scale = "noscale";
	params.align = "TL";
	var attributes = {};
	swfobject.embedSWF("preloader.swf", "flashContent", "520", "446", "9.0.0", false, flashvars, params, attributes);
	
