var demo_url = 'assets/templates/naturagame/imgs/';
jQuery(function ($) {
	// contact
	var planmaps = {
		message: null,
		init: function () {
			$('.clickPlan').click(function (e) {
				e.preventDefault();
				// load the contact form using ajax
				$.get(getIdPlan, function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'nodal-overlay',
						containerId: 'nodal-container',
						onOpen: planmaps.open,
						onShow: planmaps.show,
						onClose: planmaps.close
					});
				});
				//initialize();
			});

			// preload images
			var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'ajax-loader.gif', 'send.png'];
			$(img).each(function () {
				var i = new Image();
				i.src = demo_url + this;
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#nodal-container .nodal-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#nodal-container .nodal-input').css({
					'font-size': '.9em'
				});
			}
	
			// dynamically determine height
			var h = 320;	
			var title = $('#nodal-container .nodal-title').html();
			$('#nodal-container .nodal-title').html('Loading...');
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#nodal-container .nodal-content').animate({
							height: h
						}, function () {
							$('#nodal-container .nodal-title').html(title);
							$('#nodal-container #contentMaps').fadeIn(200, function () {	
								// fix png's for IE 6
								if ($.browser.msie && $.browser.version < 7) {
									$('#nodal-container .nodal-button').each(function () {
										if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
											var src = RegExp.$1;
											$(this).css({
												backgroundImage: 'none',
												filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
											});
										}
									});
								}
							});
						});
					});
				});
			});
			/*var maps = $('#map_canvas').html();//"toto"; //
			$('#nodal-container #mapcanvas').html(maps);*/
			//initialize();
									 
			$(this).oneTime("3s", function() {
				$('#nodal-container #buttonMainGpsSubmit').css("display","block");
				if(geo == null) {
					$('#nodal-container #buttonMainGpsLocal').css("display","none");
				} else {
					$('#nodal-container #buttonMainGpsLocal').css("display","block");
				}
				initialize();
			});
		},
		show: function (dialog) {
			//$('#nodal-container .nodal-send').click(function (e) {
				
				//$('#nodal-container .nodal-title').html('toto');
				$('#nodal-container .nodal-send').click(function (e) {
				e.preventDefault();
					$('#nodal-container #thisMyMap').css("display","block");
					
					$('#nodal-container .nodal-content').animate({
						height: '540px'
					});	
					
					$('#nodal-container #thisMyMap').fadeIn(200);
					
				});
				
				$('#nodal-container .nodal-Submit').click(function (e) {
					//$('#nodal-container .nodal-title').html("Trop buttonMainGps "+$('input:text[name=address]').val());
					var addForm = $('input:text[name=address]').val();
					if(addForm!="" && addForm!=messForm) { 
						showMyAddress(addForm);
						$(this).oneTime("1s", function() {
							$('#nodal-container #buttonintinairenWiewMpas').css("display","block");
						});
						
						e.preventDefault();
						$('#nodal-container #thisMyMap').css("display","block");
						
						$('#nodal-container .nodal-content').animate({
							height: '540px'
						});	
						
						$('#nodal-container #thisMyMap').fadeIn(200);
					}
				});
				
				
				$('#nodal-container .nodal-Local').click(function (e) {
					initializeMy();
					$(this).oneTime("3s", function() {
						//$('#nodal-container .nodal-title').html("Trop Local ");
						mainGps();
						$('#nodal-container #buttonintinairenWiewMpas').css("display","block");
					});
				});
				
				$('#nodal-container .formadd').focus(function (e) {
					$('#nodal-container .formadd').css("color","#000");
				});											   
				
				$('#nodal-container .nodal-gps').click(function (e) {
				e.preventDefault();
				
				//$('#nodal-container .nodal-title').html("Trop intinaire ");
					$('#nodal-container').css("width",'870px');
					$('#nodal-container #rightMpas').css("display","block");
					$('#nodal-container #rightMpas').animate({
							width: '210px'
						});
					$(this).oneTime("3s", function() {
						intinaire();
						$('#nodal-container #rightMpas').css("background","none");
					});
				//intinaire
				});
		},
		close: function (dialog) {
			$('#nodal-container .nodal-message').fadeOut();
			$('#nodal-container .nodal-title').html('Goodbye...');
			$('#nodal-container #contentMaps').fadeOut(200);
			$('#nodal-container .nodal-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		showError: function () {
			$('#nodal-container .nodal-message')
				.html($('<div class="nodal-error"></div>').append(contact.message))
				.fadeIn(200);
		}
	};
	planmaps.init();
});

