/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 236 2010-03-09 06:04:40Z emartin24 $
 *
 */

jQuery(function ($) {
	var contact = {
		message: null,
		init: function () {
			$('a#button_prihlaseni_id').click(function (e) {
				e.preventDefault();

				// load the contact form using ajax
				$.get("ajax.php?modul=login", function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Zavřít' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'contact-overlay',
						containerId: 'contact-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
                    });
				});
			});

			// preload images
			var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
			$(img).each(function () {
				var i = new Image();
				i.src = 'images/extern/jquery/login/' + this;
			});
		},
		open: function (dialog) {

			// definovani vysky
			var h = 219;
	
			
            Cufon.replace('h1#nadpis_login', { fontFamily: 'DINPro-Regular' });
            
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#contact-container .contact-content').animate({
							height: h
						}, function () {
							$('#contact-container form').fadeIn(200, function () {
								$('#contact-container #uzivatelske_jmeno_id').focus();
							});
						});
					});
				});
			});
		},
		show: function (dialog) {
			$('#contact-container #btn_odeslat_id').click(function (e) {
				e.preventDefault();
				
				var msg = $('#contact-container .contact-message');
				msg.fadeOut(function () {
					msg.removeClass('contact-error').empty();
				});


				$('#contact-container .contact-loading').fadeIn(200, function () {
					$.ajax({
						url: 'ajax.php?modul=login',
						data: $('#contact-container form').serialize() + '&action=login_action',
						type: 'post',
						cache: false,
						dataType: 'html',
						success: function (data) {
							$('#contact-container .contact-loading').fadeOut(200, function () {
							
							    if ( data=='logged_ok' )
							    {
							        // uspesne zalogovani
							        contact.closeWithReload(dialog);
								}
								else
								{
									msg.html(data).fadeIn(200);

									var contact_container 	= $('#contact-container')

									var new_height          = 300;

									$('#contact-container').height(new_height);

									$('#contact-container #simplemodal-data').animate({
										height: new_height
									}, function () {
									
									});
								}

							});
						},
						error: contact.error
					});
				});
			});
		},
		close: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			$('#contact-container form').fadeOut(200);
			$('#contact-container .contact-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		closeWithReload: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			$('#contact-container form').fadeOut(200);
			$('#contact-container .contact-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
							window.location = window.location;
						});
					});
				});
			});
		},
		error: function (xhr) {
			//
		}
	};

	contact.init();


	// --- L O G O U T ---
	
	var contact_off = {
		message: null,
		
		init: function () {

			$('a#button_odhlaseni_id').click(function (e) {
				e.preventDefault();

				// load the contact form using ajax
				$.get("ajax.php?modul=logout", function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Zavřít' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'contact-overlay',
						containerId: 'contact-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
                    });
                    
                    $('#link_storno_id').click(function(){
						$.modal.close();
					});
                    
				});
			});

			// preload images
			var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
			$(img).each(function () {
				var i = new Image();
				i.src = 'images/extern/jquery/login/' + this;
			});

		},

		open: function (dialog) {

			// definovani vysky
			var h = 219;


            Cufon.replace('h1#nadpis_logout', { fontFamily: 'DINPro-Regular' });

			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#contact-container .contact-content').animate({
							height: h
						}, function () {
							$('#contact-container form').fadeIn(200);
						});
					});
				});
			});
		},

		show: function (dialog) {
			$('#contact-container #btn_odhlasit_id').click(function (e) {
				e.preventDefault();
				
			});
		},

		close: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			$('#contact-container form').fadeOut(200);
			$('#contact-container .contact-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},

		closeWithReload: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			$('#contact-container form').fadeOut(200);
			$('#contact-container .contact-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
							window.location = window.location;
						});
					});
				});
			});
		},

		error: function (xhr) {}
	};

	contact_off.init();

	//----------------------------------------------------------------------------------------

});
