function sendform(f) {
	try {
		var querystr = $(f).serialize();
		$.post('popup-contact.php', querystr, function (r) {
			$(f).html("<p class='center'><strong>Your message has been sent</strong></p>");
		});
	} catch (e) {
		alert(e);
	}
	return false;
}
function show_overlay() {
	hide_overlay();
	$('body').prepend('<div id="overlay">&nbsp;</div>');
	setup_overlay_height();
	$('#overlay').fadeIn('fast');
}
function hide_overlay() {
	$('#overlay').remove();
}
function setup_overlay_height() {
	body_height = $(document).height();
	$('#overlay').css('height', body_height);
}
function hide_popups() {
	if ($('.popup').length) { $('.popup').fadeOut('fast') };
	$('#ajax-holder').remove();
	hide_overlay();
}
$(function(){
	
	$('.popup').center();
	
	$('.popup .close').live('click', function() {
		hide_popups();
		return false;
	});
	
	$('#overlay').live('click', function() {
		hide_popups();
	});
	
	$('a[rel=popup]').live('click', function() {
		$('#ajax-holder').remove();
		var href = $(this).attr('href');
		$('body').append('<div id="ajax-holder">&nbsp;</div>');
		var $this = $(this);
		show_overlay();
		$('#ajax-holder').load(href, function() {
			$('.popup').center();
		});
		return false;
	});
	
	$(document).keypress(function(e){  
		if(e.keyCode==27){
			hide_popups();
		}
	});
})

jQuery.fn.center = function(loaded) {
    var obj = this;
    body_width = parseInt($(window).width());
	body_height = parseInt($(window).height());
	block_width = parseInt(obj.width());
	block_height = parseInt(obj.height());
	
	left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
	if (body_width<block_width) { left_position = 0 + $(window).scrollLeft(); };
	top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
	if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
	
	
    if(!loaded) {
    	
    	obj.css({'position': 'absolute'});
		obj.css({'left': left_position, 'top': top_position});
        $(window).bind('resize', function() { 
        	obj.center(!loaded);
    	});
		$(window).bind('scroll', function() { 
        	obj.center(!loaded);
    	});
    	
    } else {
        obj.stop();
        obj.css({'position': 'absolute'});
        obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
    }
}
jQuery.preloadImages = function(path) {
	var imagesPath = path;
  	for(var i = 1; i<arguments.length; i++) {
  		$(document.body).append($('<img src="' + imagesPath + arguments[i] + '" style="position: absolute; top: -50000px; left: -50000px; " />'));
  	}
}