// Gallery lightbox 
function exproGallery (obj) {
	$(obj).nyroModalManual({
		type: "image",
		minWidth: 100,
		minHeight: 100,
		endShowContent: function (e) {
			var img_height = parseInt($(e.content).children("img").attr("height"));
			if (img_height < 100) {
				$(e.content).children("img").css("marginTop",((100-img_height)/2)+"px");
			}
		}
	});
}
//Rolltext control
function rolltext_body(control){	
	if($(control).children('.control_text_long').css('display') =='none'){
		$(control).children('.control_text_long').show('slow');
		$(control).children('.control_text_short').hide('slow');
		$(control).children(".rolltext_more").children('a').text(rolltextmsg['hide']);
	}else{
		$(control).children('.control_text_long').hide('slow');
		$(control).children('.control_text_short').show('slow');	
		$(control).children(".rolltext_more").children('a').text(rolltextmsg['show']);		
	}
	return 0;
};

function rolltext(ctrl){
	$(ctrl).parent().unbind('rolltext');
	$(ctrl).parent().bind('rolltext',rolltext_body($(ctrl).parent().parent(".control_body")));
};
//Newsletter control
function sentAddress(id){
	var group = $('#newslettergroup_'+id).val();
	var email = $('#newslettermail_'+id).val();
	$.post(baseUrl+"/index/savemail/", 
		new Array(email, group), 
		function(msg){
			var id1 = id;
			$('#newsletter_form_'+id1).hide('slow');
			if(msg<2){
				$('#newsletter_msg_'+id1).attr('style','color:green;');
			}else{
				$('#newsletter_msg_'+id1).attr('style','color:red;');
			}
			if(msg == '0'){
				$('#newsletter_msg_'+id1).html($('#newsletteracceptmsg_'+id1).val());
			}else{
				$('#newsletter_msg_'+id1).html(newsletter["control"][msg]);
			}
			
			$('#newsletter_msg_'+id1).show('slow');
		},
		"html"
	);
	
};

var ParserYMD = function( unparsedDate ) {
	this.unparsedDate = unparsedDate;
	this.exploded = (this.unparsedDate == null ? new Array() : this.unparsedDate.split('-')),
	this.getYear = function() {
		return parseInt(this.exploded[0]);
	},
	this.getMonth = function() {
		return parseInt(this.exploded[1]);
	},
	this.getDay = function() {
		return parseInt(this.exploded[2]);
	},
	this.getDate = function() {
		if ( this.unparsedDate == null ) {
			return new Date();
		} else {
			return new Date(this.getYear(), (this.getMonth() - 1), this.getDay());
		}
	}
}

$(document).ready(function(){
	$('.form-workspace-elementui-draggableui-resizable *').attr('class', '');
	$('[required="false"]').removeAttr('required');
	$('input[run="submit"]').click(function(){
		var postData = [];
		var thisObject = this;
		var isValid = true;
		var notValidElements = [];
		$('[field="element"]').each(function(index, element){
			if ( $(element).attr('required') == "false" ) {
				$(element).removeAttr('required');
			}
			if ( $(element).attr('required') == "true" && !($(element).is('[value]')) ) {
				if ( !$(element).find('[checked]').is('[checked]') ) {
					isValid = false;
					$(element).animate({ opacity: 0 }, 500, function(){ $(this).animate({ opacity: 1 }, 500); });
					var notValidElem = { name: $(element).attr('shortname'), error: [] };
					if ( $(element).is('.required') ) notValidElem.error.push('jest wymagane');
					notValidElements.push(notValidElem);
				}
			} 
//			else if ( $(element).attr('required') == "true" ) {
//				if ( $(element).valid() == 0 ) {
//					isValid = false;
//					$(element).animate({ opacity: 0 }, 500, function(){ $(this).animate({ opacity: 1 }, 500); });
//					var notValidElem = { name: $(element).attr('shortname'), error: [] };
//					if ( $(element).is('.required') ) notValidElem.error.push('jest wymagane');
//					if ( $(element).is('.email') ) notValidElem.error.push('musi zawierać poprawny adres e-mail');
//					if ( $(element).is('.numeric') ) notValidElem.error.push('może zawierać tylko i wyłącznie cyfry');
//					notValidElements.push(notValidElem);
//				}
//			}
		});
		var formid = $(this).parents('form[method="post"]').attr('formid');
		if ( isValid ) {
			$(this).parents('.form-workspace').find('[field="element"]').each(function(index, element){
				if ( $(element).find(':checked').is(':checked') ) {
					postData.push({ alias: $(element).attr('shortname'), value: $(element).find(':checked').val() });
				} else {
					postData.push({ alias: $(element).attr('shortname'), value: $(element).val() });
				}

			});
			$("#DynamicForm").parents('form').find('[field="element"]').find('div').val('');
			$.post('/index/form/id/' + formid, { form: JSON.encode(postData) }, function() {
				$.nyroModalManual({
			 		url: baseUrl + '/index/form/id/' + formid + '/message/thankyou',
			 		width: 600,
			 		minHeight:1,
			 		autoSizable: true,
			 		resizeable: false,
			 		closeButton: '',
			 		modal: true,
			 		endShowContent: function () {
						$('.lightbox-control .button-close').click(function(){
							$.nyroModalRemove();
							window.location.reload(false);
						});
				 	}
				});
			});
		} else {
			var messageData = '';
			$.each(notValidElements, function(index, element){
				var message = '- pole "' + element.name + '" ' + element.error.join(', ') + '.';
				messageData += '<p class="lightbox-label lightbox-label-select to-left long" style="margin: 0px; padding: 0px; padding-left: 20px;">' + message + '</p>';
			});
			if ( $(this).parents('.form-workspace').find('[element="validate"]').attr('element') == 'validate' ) {
				$(this).parents('.form-workspace').find('[element="validate"]').find('div').html('<p class="lightbox-label lightbox-label-select to-left long" style="margin: 0px; padding: 0px; padding-left: 5px;">Formularz zawiera następujące błędy:</p>' + messageData);
			} else {
				$.nyroModalManual({
			 		url: baseUrl + '/index/form/id/' + formid + '/message/notvalid',
			 		width: 600,
			 		minHeight: 1,
			 		autoSizable: true,
			 		resizeable: false,
			 		closeButton: '',
			 		modal: true,
			 		endShowContent: function () {
						$('.lightbox-control .button-close').click(function(){
							$.nyroModalRemove();
						});
						$('#nyroModalContent').css('height', (parseInt($('#nyroModalContent').css('height')) + (notValidElements.length * 20)) + 'px');
						$('#nyroModalWrapper').css('height', (parseInt($('#nyroModalWrapper').css('height')) + (notValidElements.length * 20)) + 'px');
						$('#messages').append(messageData);
				 	}
				});
			}
		}
	});
	
});

