/*** Black backgrounds for everyone ***/
function hideBlack()
{
	$("#bigpopup").fadeOut("fast");
}
function showBlack()
{
	$("#bigpopup").fadeIn("fast");
}


$(function(){
	/***
	 * all links that points to other sites than mensaheating.com should
	 * open in a new window
	 ****/
	$('a').each(function(){
		var ref = $(this).attr('href');
		if(ref && ref.match(/http:/i))
		{
			if(!ref.match(/mensaheating\.com/i))
			{
				$(this).attr('target','_blank');
				//console.log(ref);
			}
		}
	})
	
	
    $("#loadani").ajaxComplete(function(){
        $("#loadani").fadeOut();
        });
    $("#loadani").ajaxStart(function(){
        $("#loadani").fadeIn();
        });
    
    $("div.db_image_wrap img, div.cafefoto img").each(function(){
        $(this).load(function(){
            var imagewidth = parseInt($(this).width());
            var imageheight = parseInt($(this).height());
            $(this).parent().css({width:imagewidth,height:imageheight});
            var imageoffset = $(this).position();
            var corner = $("<img src='/grafik/corners/topright.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)),left:(parseInt(imageoffset.left)+imagewidth-4),position:"absolute"})
            var corner = $("<img src='/grafik/corners/topleft.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)),left:(parseInt(imageoffset.left)),position:"absolute"})
            var corner = $("<img src='/grafik/corners/bottomleft.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)+imageheight-4),left:(parseInt(imageoffset.left)),position:"absolute"})
            var corner = $("<img src='/grafik/corners/bottomright.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)+imageheight-4),left:(parseInt(imageoffset.left)+imagewidth-4),position:"absolute"})
        });
    });
	/* IE doesnt draw the shadows for the content correctly, by hiding and showing
	 * the containing element we force IE to redraw the div. */
	if(!jQuery.support.objectAll)
	{
		$("div.pageshadowrapper").hide();
		window.setTimeout(function(){$("div.pageshadowrapper").show();},250);
	}
});

function hideFactory(jqSelector,delay)
{
    return function()
    {
        if(jqSelector.data("active") === true)
        {
                setTimeout(hideFactory($(jqSelector),delay),delay);
        }
        else
        {
                jqSelector.fadeOut("slow");
        }
    }
}


function updateByResult(json)
{
    for(selector in json)
    {
        $(selector).replaceWith(json[selector]);
    }
}

function loadToLayer(url,data)
{
        showBlack();
        $('#viewer').remove();
        $.post(url,data,function(result)
               {
                newDiv = $(result);
                $("body").append(newDiv);
               })
}

