function nop(){}
$(function(){
	var loc = window.location + "";
	var matches = loc.match(/^http:\/\/(www\.)?([^\/]*)/i);
	currentDomain = matches[2];
	$('a[href]').each(function(){
		var ref = $(this).attr('href');
		if(ref.match(/http:\/\//i) && ref.match(/^http:\/\/(www\.)?([^\/]*)/i))
		{
			var matches = ref.match(/^http:\/\/(www\.)?([^\/]*)/i);
			remoteDomain = matches[2];
			if(remoteDomain !== currentDomain)
			{
				$(this).attr('target','_blank');
			}
		}
	});
    $("#loadani").ajaxComplete(function(){
        $("#loadani").fadeOut();
        });
    $("#loadani").ajaxStart(function(){
        $("#loadani").fadeIn();
        });
    
     //Select all input fields that has the "rel" attribute
	$("input[rel],textarea[rel]").each(function(){
		//Bind a function to the focus event
		$(this).focus(function(){
			//If the value of the field is the same as the text in the "rel" attribute
			if($(this).attr('rel') == $(this).val())
			{
				//Clear the field of its current value
				$(this).val("");
			}
			
			//Additionally, check if the rel contains the word Password
			//And check if the input field is type text
			//Note; this roundabout way is because it isnt legal to change the "type" attribute
			if($(this).attr('rel').match(/password/i) && $(this).attr('type') == "text")
			{
				//Hide field and show the password field, then pass on focus
				$(this).hide();
				//We use siblings() so we only target the field closest to the current
				//This way we dont have to worry about other fields with the same "rel"
				//Note; we have to set a new focus since it isnt the original field
				$(this).siblings("input[rel=" + $(this).attr('rel') + "]").show().focus();
			}
			
			});
		//Bind a function to the blur event
		$(this).blur(function(){
			//If the value of the field is the empty string
			if($(this).val() === "")
			{
				//Set the field to the "rel" attribute
				$(this).val($(this).attr('rel'));
				//Additionally, check if the rel contains the word Password
				//And check if the input field is type password
				//Note; this roundabout way is because it isnt legal to change the "type" attribute
				if($(this).attr('rel').match(/password/i) && $(this).attr('type') == "password")
				{
					//Hide field and show the text field, then pass on focus
					$(this).hide();
					//We use siblings() so we only target the field closest to the current
					//This way we dont have to worry about other fields with the same "rel"
					//Note; we have to set the value again since it isnt the same field
					$(this).siblings("input[rel=" + $(this).attr('rel') + "]").show().val($(this).attr('rel'));;
				}
			}
			});
		
		});
	 
});

function adminWithMenu(url,data)
{
    $('#hiddenpanel').fadeOut('fast');
    showBlack();
    $.post(url+"/true/",data,function(result)
    {
        $("#tabmenu").html(result.menu);
        $("#formblockcontent").html(result.edit);
        showAdmin();
        $('li.tab a[title=' + url + ']').parent().eq(0).addClass("active");
        paneltitel = $("#tabmenu li.active").text();
		if($("li.tab").length === 1)
		{
			paneltitel = $("#tabmenu li").text();
		}
        //if($("#tabmenu li").length == 1)
		if(true)
        {
            $("#popupheader").hide();
        }
        else
        {
            $("#popupheader").show();
        }
    },"json");
    //return false;
}

function showAdmin()
{
    $("#notinvisible").fadeIn();
	var ni = $("#notinvisible")[0];
	setTimeout(function(){ni.scrollIntoView(false)},50)
	var bodywidth = parseInt($("body").width(),10) - 190;
	var notinvleft = (bodywidth - 700)/2; 
	//Chrome doesnt want to read #notinvisible's width... so we have to go with the 700 we know it is
	$("#notinvisible").css({left:notinvleft});
}
function hideAdmin(callback)
{
    $("#notinvisible").fadeOut();
    if(typeof callback != "undefined" )
    {
        callback();
    }
}

function panelHide()
{
	$("#hiddenpanel").fadeOut('fast');
	hideBlack();
}

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);
				if(!data || !data.removeBlackBackground)		// add by mayank
        	 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,position:"absolute"});
        $("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();
		}
        });
}

function getStyles(element)
{
		var types = ['font-family','font-size','color','font-weight','line-height','font-style','font-variant'];
		var styles = {};
		$(types).each(function(index,val){
				styles[val] = $(element).css(val);
				});
		
		return styles;
}
function logout()
{
	createPopup("confirm",{
		title:'Log ud',
		message:'Er du sikker p&aring; du vil logge ud?',
		action:'/admin/logout',
		hideAdmin:true
	});
};

function askQuestion()
{
	var width = 400;
	var left = ($("body").width() - width)/2;
	var top = "10%";
	showBlack();
	$("#askquestion").css({width:width,position:"absolute",left:left,top:top}).fadeIn();
	var ele = $('#askquestion')[0];
	ele.scrollIntoView(false);
	
}

function addFavorite(type,id)
{
	//Post the data to the proper controller
	$.post("/user/add_favorite/",{type:type,id:id},function(response){
		//check the response for confirmation
		if(response.success)
		{
			window.location = window.location;
		}//In case of error show it to the user
		else
		{
			//Create a popupmessage with the contents of the error message.
			createPopup("message",{message:response.message,title:"Fejl",hideAdmin:true});
		}
		
		},"json");//Set the return type to json so jQuery will parse it correctly
}

