function img_loader(size){
	return '<img src="/images/loader_48.gif" width="'+size+'" height="'+size+'" border="0" id="img_loader"/>';
}

//main function loads all necessary aspects of the popin
function createAndShowPopin(page_to_show, template_to_use, template_message){
	clearTimeout(timers);
	
	if($('.popin_Darkener').length==0){
		$('body').prepend('<div class="popin_Darkener"></div><table border="0" cellspacing="0" cellpadding="0" class="new_popin" id="new_popin_table"><tr><td class="tl"></td><td class="hbar"></td><td class="tr"></td></tr><tr><td class="vbar"></td><td class="content"></td><td class="vbar"></td></tr><tr><td class="bl"></td><td class="hbar"></td><td class="br"></td></tr></table>');
	}
	
	loadingPopin();
	
	if(page_to_show!=''){
		$.ajax({
			type: "GET",
			url: '/'+page_to_show,
			cache: false,
			success: function(data){
				if(data!='error' && data!=''){
					loadPopinContent(data);
					centerPopin();
				}else{
					showPopinError(popin_template['generic_error_header']+'<div class="body">An error has occured while loading data.<br/><br/>Please <a href="javascript:void(0);" onclick="javascript:destroyAndRemovePopin();createAndShowPopin(\''+page_to_show+'\',\''+template_to_use+'\');">try again</a> or <a href="javascript:void(0);" onclick="javascript:destroyAndRemovePopin();">quit</a>.</div>',10000);
				}
			}
		});
	}else if(template_to_use!=''){
		if(template_message == null)
		{
			template_message = "Something went wrong.";
		}
		if(popin_template[template_to_use]!=null){
			var temp_template = popin_template[template_to_use].replace('{MESSAGE}', template_message);
			loadPopinContent(temp_template);
		}else{
			showPopinError(popin_template['developer_error_null_template'],3000);
		}
	}else{
		showPopinError(popin_template['developer_error'],3000);
	}
	
	return false; //render logins' href obsolete
}

//adjust to childrens height
function adjustPopinHeightToChildren(){
	var sumHeight = 0;
	$.each($('.new_popin .content').children(),function(){
		sumHeight += $(this).outerHeight();
	});
	$('.new_popin').css('height',sumHeight+'px');
}

//loads HTML in the popin
function loadPopinContent(content){
	$('.new_popin .content').html(content);
	centerPopin();
}

var timers; //keeps track of activated timers
//show an error for "delay" miliseconds
function showPopinError(error,delay){
	clearTimeout(timers);
	$('.new_popin .content').html(error);
	timers = setTimeout(destroyAndRemovePopin,delay);
	centerPopin();
}

//shows "loading..."
function loadingPopin(){
	loadPopinContent('<div class="new_popin_loading">Loading...<br/>'+img_loader(36)+'</div>');
	centerPopin();
}

//center the popin
function centerPopin(){
	adjustPopinHeightToChildren();
	
	var new_top = Math.round(($('body').innerHeight() - $('.new_popin').outerHeight()) / 2);
	if (new_top < 0) new_top = 0;
	if (new_top > 120) new_top = 120;

	var cssObj = {
		//'width':$('.new_popin').outerWidth()+10+'px',
		'width':'300px',
		'height':$('.new_popin').outerHeight()+'px',
		'top': new_top+'px',
		'left':'50%',
		'margin-top':'0px',
		'margin-left':'-'+Math.round(($('.new_popin').outerWidth()+10)/2)+'px'
	}
	$('.new_popin').css(cssObj);
}

//remove the popin and shader
function destroyAndRemovePopin(redirect){
	$('.popin_Darkener').remove();
	$('#new_popin_table').remove();
	if(redirect) document.location.href = redirect;
}

$(document).keyup(function(e) {
	if (e.keyCode == 27) { destroyAndRemovePopin(); }   // esc
});
