$(function(){
    $("#loadani").ajaxComplete(function(){
        $("#loadani").fadeOut();
        });
    $("#loadani").ajaxStart(function(){
        $("#loadani").fadeIn();
        });
    $("a").each(function(){
		var link = $(this).attr('href');
		if(!link.match(/(http:\/\/)(www\.)?stregeriluften/i))
		{
			$(this).attr('target','_blank');
		}
		});
    //Lang select images
    /*$("div.langselect a").hover(function()
    {
        var imgurl = $(this).attr("rel");
        $(this).html("<img src='" + imgurl + ".png' />");
    },
    function()
    {
        var imgurl = $(this).attr("rel");
        $(this).html("<img src='" + imgurl + "_sh.png' />");
    });*/
       //black cover hover effect
     $("img.galleryimg").hover(function()
	   {
	    $(this).fadeTo("fast",0.7);
	   },function()
	   {
	    $(this).fadeTo("fast",1);
	});
    //Gallery images, place the edit buttons and produce hover effect
    $("div.db_image_wrap 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/admingrafik/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/admingrafik/corners/topleft.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)),left:(parseInt(imageoffset.left)),position:"absolute"})
            var corner = $("<img src='/grafik/admingrafik/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/admingrafik/corners/bottomright.png'>");
            $(this).parent().append(corner);
            $(corner).css({top:(parseInt(imageoffset.top)+imageheight-4),left:(parseInt(imageoffset.left)+imagewidth-4),position:"absolute"})
        });
    });
});

function hideFactory(jqSelector,delay)
{
    return function()
    {
        if(jqSelector.data("active") === true)
        {
                setTimeout(hideFactory($(jqSelector),delay),delay);
        }
        else
        {
                jqSelector.fadeOut("fast");
        }
    };
}


function updateByResult(json)
{
    for(selector in json)
    {
        $(selector).replaceWith(json[selector]);
    }
}

function hideBlack()
{
	$("#bigpopup").fadeOut("fast");
}
function showBlack()
{
	$("#bigpopup").fadeIn("fast");
}

function loadToLayer(url,data)
{
        showBlack();
        $('#viewer').remove();
        $.post(url,data,function(result)
               {
                newDiv = $(result);
                $("body").append(newDiv);
               });
}

function createPopup(type,data,callback)
{
    $.post("/popup/" + type,data,function(result){
        var newpopup = $(result);
        showBlack();
        var toppos = ($(".popup").length) * 25;
		toppos += (parseInt($("#bigpopup").height(),10)*0.3);
        var leftpos = (parseInt($("#bigpopup").width(),10) - 300)/2;
        leftpos += $(".popup").length * 20;
        $(newpopup).css({display:"block",top:toppos,left:leftpos});
        $("body").append(newpopup);
        var draghandle  = $(newpopup);
        $(newpopup).draggable({handle:draghandle});
        $(newpopup).children(".popupheader").children(".close").click(function()
        {
			
           $(newpopup).remove();
		   if(data.hideAdmin)
		   {
			hideBlack();
		   }
           });
		if(typeof callback === "function")
		{
			callback();
		}
        });
}