/* GLOBAL INIT */
$(document).ready(function(){ 
	
	// top menu
	$("ul.sf-menu").supersubs({ 
		minWidth:    9,   // minimum width of sub-menus in em units 
		maxWidth:    27,   // maximum width of sub-menus in em units 
		extraWidth:  0.2     // extra width can ensure lines don't sometimes turn over 
						   // due to slight rounding differences and font-family 
	}).superfish({
		delay:0
	}); 


	// hide empty DIV #sectionPerex if parent resource content is empty
	if($("#sectionPerex").length){
		var scDiv = $("#sectionPerex");
		if(scDiv.html().length < 2){
			scDiv.hide();
		}
	}


	// init text field default values (Serch form etc.)
	$.defaultText();
	
	
	// decode obfuscated e-mails
	decodeObfuscatedEmails();
	
	
	// init modal windows
	$('#mwRecommend').jqm();
	$('#mwRegister').jqm();

}); 


/* PRINT */
function printContent() {
	window.print();
}


/* RECOMMEND */
var recommendFormInited = false;
function showRecommendModal() {
	// init form once
	
	if(!recommendFormInited){
		var options = { 
			dataType: 'json',
			beforeSubmit: validateRecommendForm,
			success: recommendResponse,
			resetForm: true
		}; 
		$('#emailRecommendForm').ajaxForm(options); 

		recommendFormInited = true;
	}
	
	// show
	$('#mwRecommend .step1').show();
	$('#mwRecommend .step2').hide();
	$('#mwRecommend').jqmShow();
}

function validateRecommendForm() {
	var formValid = true;
	var errMessage = 'Vyplňte prosím všechna pole formuláře.';

	// fields
	if(!validateEmail($('#regForm_senderEmail').val())){
		formValid = false;
	}
	if(!validateEmail($('#regForm_recipientEmail').val())){
		formValid = false;
	}
   
	// message
	if(!formValid){
		alert(errMessage);
	}

	return formValid;
}

function recommendResponse(data) {
	if(data.status == 1){
		$('#mwRecommend .step2').show();
		$('#mwRecommend .step1').hide();
		
		// fill email address to message
		$('#mwRecommend .step2 span.recipientEmail').html(data.recipientEmail);
	}
}



/* REGISTRATION */
var regFormInited = false;
function showRegisterModal() {
	// init form once
	
	if(!regFormInited){
		var options = { 
			dataType: 'json',
			beforeSubmit: validateRegistrationForm,
			success: registrationResponse,
			resetForm: true
		}; 
		$('#newsRegistrationForm').ajaxForm(options); 

		regFormInited = true;
	}
	
	// show
	$('#mwRegister .step1').show();
	$('#mwRegister .step2').hide();
	$('#mwRegister').jqmShow();
}

function validateRegistrationForm() {
	var formValid = true;
	var errMessage = 'Vyplňte prosím všechna povinná pole formuláře.';

	// fields
	if($('#regForm_firstname').val().length < 2){
		formValid = false;
	}
	if($('#regForm_lastname').val().length < 2){
		formValid = false;
	}
	if(!validateEmail($('#regForm_email').val())){
		formValid = false;
	}
	if(!$('#regForm_occupation1').attr('checked') &&
		!$('#regForm_occupation2').attr('checked') &&
		!$('#regForm_occupation3').attr('checked') &&
		!$('#regForm_occupation4').attr('checked') &&
		!$('#regForm_occupation5').attr('checked')
		){
		formValid = false;
	}

	// message
	if(!formValid){
		alert(errMessage);
	}

	return formValid;
}

function registrationResponse(data) {
	if(data.status == 1){
		$('#mwRegister .step2').show();
		$('#mwRegister .step1').hide();
	}
}
