 $(document).ready(function() {
 	
 	
 	
 	
 	var $eventCal = $('#eventCalAjax'),
	$eventsInfo = $('#eventsInfoAjax');
	var now = new Date();
	$eventCal.EventCalendar({
		// Ajax Variables
		/** The JSON variable that will be return on the AJAX request which will contain our entries */
		ajaxEntriesVariable: 'entries',
		/** The AJAX url to request for our entries */
		ajaxEntriesUrl: $('#form_SN').val() + 'index.php?event=geteventcalendardata',
		/** The JSON Object to send via POST to our AJAX request */
		ajaxPostData: {},
		/** Whether or not to Cache the AJAX data */
		ajaxCache: true,
		
		// Data Variables
		/* If you are not using AJAX, you can define your entries here */
		//calendarEntries: [],
		
		// Customisation Variables
		/** The CSS class which will be assigned to the Event Calendar */
		calendarClass: 'hasEventCalendar',
		/** The CSS class which will be assigned to day when the day contains a entry */
		dayEventClass: 'ui-state-active hasEvent',
		/** The standard options to send to the datepicker */
		datepickerOptions: {
			firstDay: 1 /* Monday */,
			dayNamesMin:   ['N', 'Pn', 'Wt', 'Śr', 'Cz', 'Pi', 'So'],
			monthNames:  ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień']
		},
		/** Whether or not to disable the datepicker date selection click */
		disableClick: true,
		/**
		 * The domEvents option contains all the events which you would like to assign to a $day
		 * 
		 * It will send the following arguments back to you:
		 * - domEvent
		 * - details
		 * 
		 * The details of the details argument contains the following:
		 * - {Number} year
		 * - {Number} month
		 * - {Number} day
		 * - {String} date
		 * - {Array} dayEntries
		 * - {Array} monthEntries
		 * - {Element} datepicker
		 */
		domEvents: {
			mouseenter: function(domEvent, details) {
				// Prepare
				var $day = $(this),
				dayEntries = details.dayEntries;
				// Output
				$eventsInfo.append('<div class="ui-widget-header ui-corner-top center-header">Kalendarium</div><div class="ui-helper-clearfix spacer"></div>');
				$.each(dayEntries,function(i,entry){
					$eventsInfo.append('<div class="cms"><p>' +
							'<strong>'+entry.title+'</strong><br/>'+
							'<i>'+entry.date+'</i><br/>'+
							''+entry.content+'<br/>'+
						'</p></div>'
					);
				});
			},
			mouseleave: function(domEvent, details) {
				// Clear
				$eventsInfo.empty();
			},
			click: function(domEvent, details) {
				window.location = $('#form_SN').val() + 'aktualnosci/1/0.html';
			}
		}
	});
					
 	
 	$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'light_square',slideshow:3000, autoplay_slideshow: false, hideflash: false, wmode: 'opaque',opacity: 1, show_title: false});
	
 	// menu
 	$("ul.sf-menu").superfish({
	 	speed:       'normal',                          // faster animation speed 
        autoArrows:  false,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows
	 });
	 
	// --------------->
	// Button -------->
	// --------------->
	
	$("a", ".club_home").button({
        icons: {
            primary: 'ui-icon-carat-1-e'
        },
        text: true
	});
	
	$("a", ".club_home").css({ width: '228px', 'padding-top': '4px', 'padding-bottom': '3px', 'text-align': 'left', 'margin-bottom': '1px', 'font-weight': 'bold' });
	
	
 	$("#login").click(function() {
		$('#divLogin').dialog('open');
		$('#formLoginEmail').focus();
		return false; 
	});
	
	$("a", "#loginNoIcon").button({
        text: true
	});
	$("a", "#loginNoIcon").click(function() {
		$('#divLogin').dialog('open');
		$('#formLoginEmail').focus();
		return false; 
	});
 	
	$(".contact").click(function() {
		$('#divContact').dialog('open');
		$('#formContactName').focus();
		return false; 
	});
	
	$("input:submit").button();
	
	$("a", ".newsletter").button({
        text: true
	});
	
	$("a", ".search").button({
        text: true
	});
	
	$("a", ".save").button({
        text: true
	});
	
	$("#formNewsletterEmail").focus(function() {
		$("#formNewsletterEmail").val("");
	});
	
	$("#formNewsletterButton").click(function() {
		$("#formNewsletterForm").submit();
	});
	
	$("a", "#Book").button({
        text: true
	});
	
	
	// --------------->
	// Preloader ----->
	// --------------->
	$("#isLoading").ajaxStart(function() {
		//$(this).show();
	});
	
	$("#isLoading").ajaxStop(function() {
		//$(this).hide();
	});
	
	$("#showPreloader").click(function() {
		//$("#isLoading").show();
	});
	
	$("#hidePreloader").click(function() {
		//$("#isLoading").hide();
	});
	
	$("#isLoading").css("position", "absolute");
	 
	$(window).scroll(function() {
	    $("#isLoading").css("top", $(window).scrollTop() + "px");
	});
	
	// --------------->
	// Dialog Forms -->
	// --------------->
	function updateTips(t) {
		$(".validateTips").show();
		$(".validateTips").text(t).addClass('ui-state-highlight');
		setTimeout(function() {
			$(".validateTips").removeClass('ui-state-highlight', 500);
		}, 500);
	}
	
	function removeFieldsHighlight(fields) {
		$(".validateTips").hide();
		fields.removeClass('ui-state-error');
	}
	
	function removeFieldsContent(fields) {
		$(".validateTips").hide();
		fields.val('');
	}
	
	// Login Form Starts -->
	var formLoginFields = $([]).add($("#formLoginEmail")).add($("#formLoginPassword"));
	$('#divLogin').dialog('destroy');
	$('#divLogin').dialog({
		autoOpen: false,
		height: 280,
		width: 350,
		modal: true,
		resizable: false,
		open: function(event, ui) {
			$('#selectedRowId').val();
		},
		buttons: {
			
			"Anuluj": function() {
				removeFieldsHighlight(formLoginFields);
				removeFieldsContent(formLoginFields);
				$("#divLogin").dialog("close"); 
			},
			"Logowanie": function() {
				divLoginSubmit();
			}			
		}
	});
	
	$('#divLogin').find('input').keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			divLoginSubmit();
		}
	});
	
	function divLoginSubmit() {
		$.post(  
			$("#form_SN").val() + "index.php?event=executeLogin",  
			$("#formLogin").serialize(),  
			function(data){
		   	  if (data.validationResult) {
		   		    removeFieldsHighlight(formLoginFields);
		   		    removeFieldsContent(formLoginFields);
		   		  	$("#divLogin").dialog("close");
		   		  	if (data.userType == "admin") {
		   	  			$('#executeLoginAdmin').submit();
		   		  	}
		   		  	if (data.userType == "client_profil") {
		   		  		$('#executeLoginClientProfile').submit();
		   		  	}
		   		  	if (data.userType == "client_basket") {
		   		  		$('#executeLoginClientBasket').submit();
		   		  	}
				} else {
					removeFieldsHighlight(formLoginFields);
					updateTips(data.errorMessage);
					$("#" + data.fieldName + "").addClass('ui-state-error');
				}  
		  	},  
		  	"json"  
		 );
	}
	// Login Form End -->
	
	// Contact Form Starts -->
	var formContactFields = $([]).add($("#formContactName")).add($("#formContactEmail")).add($("#formContactMessage"));
	$('#divContact').dialog('destroy');
	$('#divContact').dialog({
		autoOpen: false,
		height: 420,
		width: 380,
		modal: true,
		resizable: false,
		open: function(event, ui) {
			$('#selectedRowId').val();
		},
		buttons: {
			"Anuluj": function() {
				removeFieldsHighlight(formContactFields);
				removeFieldsContent(formContactFields);
				$("#divContact").dialog("close");				
			},
			"Wyślij": function() {
				divContactSubmit();
			}			
		}
	});
	
	$('#divContact').find('input').keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			divContactSubmit();
		}
	});
	
	function divContactSubmit() {
		$.post(  
			$("#form_SN").val() + "index.php?event=executeContact",  
			$("#formContact").serialize(),  
			function(data){
		   	  if (data.validationResult) {
		   		  	removeFieldsHighlight(formContactFields);
		   		  	removeFieldsContent(formContactFields);
		   		  	$('#divContactConfirm').dialog('open');
					$("#divContact").dialog("close");
				} else {
					removeFieldsHighlight(formContactFields);
					updateTips(data.errorMessage);
					$("#" + data.fieldName + "").addClass('ui-state-error');
				}  
		  	},  
		  	"json"  
		  );
	}
	
	// Contact Form Ends -->
	
	// Contact Form Confirm Starts -->
	$('#divContactConfirm').dialog('destroy');
	$('#divContactConfirm').dialog({
		autoOpen: false,
		height: 180,
		width: 350,
		modal: true,
		resizable: false,
		buttons: {
			"Zamknij": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	// Contact Form Confirm Ends -->
			
});

function search_box_submit() {
	var wpisana = document.getElementById('search_keyword').value;
	if(wpisana == ''){
		alert('Nie wprowadzono slowa kluczowego dla wyszukiwania.');
	} else {
		document.getElementById('search_form').submit()
	}
}

function save_box_submit() {
	var email = document.getElementById('email').value;
	if(email == ''){
		alert('Nie wprowadzono adresu email.');
	} else {
		document.getElementById('formNewsletter').submit()
	}
}
