var xypos = function(xy, wh) {
  return (xy) ? (xy - wh)  / 2 : 0;
}
 
var attr = function(x, y, w, h) {
  return 'menubar=no,toolbar=no,status=no,resizable=yes,width='+w+',height='+h+',left='+x+',top='+y;
}
 
var popup = function(url, w, h) {
    var x = xypos(screen.width, w);
    var y = xypos(screen.height, h);
    var win = window.open (url, '_popup', attr(x, y, w, h));
    win.focus();
}

$(document).ready(function() {

	/* ************************************************************************ */
	/* redraw document */
	/* ************************************************************************ */

	var pt = 'pt';
	var px = 'px';

	function redraw() {
	  if ($.cookie('fontsize'))
	    $('.main').css({ 'font-size': $.cookie('fontsize')+px });
	  if ($.trim($('.left').text()) == "" && $.trim($('.right').text()) == "") {
	    $('.left').remove();
	    $('.right').remove();
	    $('.main').css({ 'width' : 900+px, 'margin-left' : -450+'px' });
	  } else {
	    if ($.trim($('.left').text()) == "") {
	      $('.left').remove();
	      $('.main').css({ 'width' : 668+px, 'margin-left' : -450+px });
	    }
	    if ($.trim($('.right').text()) == "") {
	      $('.right').remove();
	      $('.main').css({ 'width' : 668+px, 'margin-right' : -450+px });
	    } 
	  }
	  return $('.main').height();
	}

	function resize(h, m, n, o) {
	  $('.footer').css({     'top': h+m+px });
	  $('.background').css({ 'top': h+px, 'height' : m+px });
	  $('.footnote').css({   'top': h+m+n+px });
	  $('.base').css({       'top': h+m+o+px, 'display' : 'block' });
	}

	var header = $('.header').offset();
	var h = $('.header').height()+header.top;
	var m = redraw();
	var n = 350;
	var o = 225;
	resize(h, m, n, o);

	$('a[rel=mung]').each(function() {
	    $(this).html($(this).attr('title')+'@'+$(this).text());
	});

	$('a').css({ 'color' : '#8c301e', 'text-decoration' : 'none' });

	$('.footnote a').css({ 'color' : '#ffffff', 'text-decoration' : 'none' });

	$('.top span').each(function(i) {
	    if (!i) $(this).css({ 'border' : 'none' });
	});

	/* ************************************************************************ */
	/* events */
	/* ************************************************************************ */

	$('a[rel=mouseover] img').mouseover(function() {
	    var str = $(this).attr('src');
	    var pat = /jpg$/i;
	    if (str.match(pat))
	      var result = '.jpg';
	    else
	      var result = '.gif';
	    $(this).attr({ 'src': $(this).attr('src').replace(result,'')+'_h'+result });
	});

	$('a[rel=mouseover] img').mouseout(function() {
	    $(this).attr({ 'src': $(this).attr('src').replace('_h', '') });
	});

	$('a[name=popup]').click(function() {
	    var args = $(this).attr('title');
	    var size = args.split(',');
	    popup($(this).attr('href'), $.trim(size[0]), $.trim(size[1]));
	});

	$('a[rel=larger]').click(function() {
	    if ($.cookie('fontsize'))
	      var fontSize = $.cookie('fontsize');
	    else
	      var fontSize = $('body').css('fontSize');
	    var finalNum = parseFloat(fontSize, 10);
	    fontSize.slice(-2);
	    finalNum *=1.1;
	    if (finalNum < 18) {
	      $.cookie('fontsize', finalNum, { path : '/' });
	      $('.main').css({ 'font-size': finalNum+px });
	      var m = redraw();
	      resize(h, m, n, o);
	    }
	});

	$('a[rel=smaller]').click(function(){
	    if ($.cookie('fontsize'))
	      var fontSize = $.cookie('fontsize');
	    else
	      var fontSize = $('body').css('font-size');
	    var finalNum = parseFloat(fontSize, 10);
	    fontSize.slice(-2);
	    finalNum /=1.1;
	    if (finalNum > 11) {
	      $.cookie('fontsize', finalNum, { path : '/' });
	      $('.main').css({ 'font-size': finalNum+px });
	      var m = redraw();
	      resize(h, m, n, o);
	    }
	});

	$('a[rel=mung]').click(function() {
	    window.location.href= 'mailto:'+$(this).attr('title')+'@'+$(this).text();
	});

	if (FlashDetect.installed) {
	    $('.flash').html('');
	    var swf = '';
	    switch ($.cookie('id')) {
		  case '6':
		  case '10':
		  case '77':
			swf = 'history';
		  	break;
		  case '19': 
			swf = 'golfpark';
			break;
		  case '25':
			swf = 'restaurant';
			break;
		  case '29':
			swf = 'cafe';
			break;
		  case '228': 
			swf = 'pcenter';
			break;
		  default:
			swf = 'default';
			break;
	    }
	    $('.flash').flash({ src: 'http://rittergut-birkhof.de/swf/'+swf+'.swf', width: 806, height: 269 }, { update : false });
	}

	/* ************************************************************************ */
	/* form handler */
	/* ************************************************************************ */

	var subject = '';
	var req = new Array();

	if ($.trim($('#error').text()) == "") {
	    $('#error').html($('#default').html());
	    $('#send').attr({ 'disabled': 'disabled' });
	}

	$("input[name=subject]").click(function() {
	    subject = $(this).attr('title');
	    $('#addfields').html('');
	    switch($(this).val()) {
	        case 'callback':
		    $('#addfields').html($('#PHONE').html());
		    break;
	        case 'infopost':
		    $('#addfields').html($('#ADDRESS').html());
		    break;
	        case 'infomail':
	        case 'mailback':
		    $('#addfields').html($('#MAIL').html());
		    break;
	    }

	    var i = 0;
	    $('#required input').each(function() {
		req[i++] = $(this).attr('name');
	    });

	    var m = redraw();
	    resize(h, m, n, o);
	});

	$('#required').mouseout(function() {
	    var error = null;
	    if (!$('#addfields').html()) {
		error = true;
	    }
	    $('#required input').each(function(i) {
		if (!$(this).val())
		  error = true;
	    });
	    if (!error) {
	      $('#send').attr({ 'disabled': false });
	    }
	});

	$('#send').click(function() {
	    var required = req.join(',');
	    var post = {
	        name: $('#name').val(),
		subject: subject,
		street: $('#street').val(),
		no: $('#no').val(),
		postcode: $('#postcode').val(),
		city: $('#city').val(),
		phone: $('#phone').val(),
		email: $('#email').val(),
		username: 'witt@birkhof.de',
		message: $('#message').val(),
		required: required
	    };
	    $.post("http://rittergut-birkhof.de/mod/response.php", post, function(data) {
		if (data != 'success') {
		  $('#error').addClass('error');
		}
		else {
		  $('#error').addClass('success');
		  $('input').each(function() { $(this).attr({ disabled: true }) });
		  $('textarea').attr({ disabled: true });
		  $('#send').remove();
		}
		$('#error').html($('#'+data).html());
	    });
	  });

});
