/*
* SimpleModal Contact Form
* http://www.ericmmartin.com/projects/simplemodal/
* http://code.google.com/p/simplemodal/
*
* Copyright (c) 2008 Eric Martin - http://ericmmartin.com
*
*/

$(document).ready(function () {
	$('#weiter').click(function (e) {
		e.preventDefault();
		// load the note using ajax
		$.get(window.location.hostname + "/index.php?article_id=70&clang=0", function(data){    		
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["0%",],
				overlayId: 'note-overlay',
				containerId: 'note-container',
				onOpen: note.open,
				onShow: note.show,
				onClose: note.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 = 'css/files/' + this;
	});
});

var note = {
	message: null,
	open: function (dialog) {

		// dynamically determine height
		var chars_per_row = 95;
		var char_height = 20; 
		var caption_height = 100; 
		var caption_max = 70;   //Anzahl Zeichen bis Überschrift umbricht
		
		var anzp = 0; //chars in paragraphs
		var anzh = 0; //chars in headlines
		var p = 0;    //paragraphs
		var hl = 0;   //headlines
		var im_h = 0; //image height + space above and below
		$('div.wichtig p').each(function() {
		  p++;
		  anzp += $(this).text().length;
		  anzp += chars_per_row;    //für jeden Paragraphen eine Leerzeile extra
    })
		$('h2').each(function() {
		  hl++;
      //Muss die Titelzeile umgebrochen werden, dann noch mal Hälfte der Höhe dazurechnen    
		  if ($(this).text().length > caption_max){
        hl += Math.floor($(this).text().length / caption_max) / 2; 		  
      }
    })
    $('p.fancy').each(function() {
//        alert ('Bild ist ' + $('p.fancy').height() + ' Pixel hoch.'); // height funktioniert vermutlich nicht, weil das Bild noch nicht geladen ist
//       man könnte ermitteln, wie das Bild heißt und es preloaden und dann die Größe ermitteln - zu kompliziert
//       wahrscheinlich sollen die Bilder eine feste Göße haben   
      im_h = 157 + 50; 
    })
//  		alert(anzp + ' Zeichen in ' + p + ' paragraphs. ' + hl  + ' headlines.');
    var h = (anzp / chars_per_row * char_height) + (hl * caption_height) + im_h;
//     alert('Ergibt Hoehe in Pixeln: ' + h);
    // Höhe fester Titel
    h += 85;

		var title = $('#note-container .note-title').html();
		$('#note-container .note-title').html('Loading ...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#note-container .note-content').animate({
						height: h
					}, function () {
						$('#note-container .note-title').html(title);
						$('#note-container #note').fadeIn(200, function () {
							$('#note-container #note-name').focus();

							$('#note-container .note-cc').click(function () {
								var cc = $('#note-container #note-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#note-container .note-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")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#note-container .note-send').click(function (e) {
			e.preventDefault();
		});
	},
	close: function (dialog) {
		$('#note-container .hinweis-message').fadeOut();
		$('#note-container .note-title').html('Goodbye ...');
		$('#note-container #note').fadeOut(200);
		$('#note-container .note-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
						window.location.href = window.location.hostname + "/index.php?article_id=2&clang=0";
					});
				});
			});
		});
	},
};
