jQuery.extend({
	url: function(strParamName){
		var strReturn = "";
		var strHref = window.location.href;
		var bFound=false;
		var cmpstring = strParamName + "=";
		var cmplen = cmpstring.length;
		if ( strHref.indexOf("?") > -1 ){
			var strQueryString = strHref.substr(strHref.indexOf("?")+1);
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
				if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					bFound=true;
					break;
				}
			}
		}
		if (bFound==false) return null;
		return strReturn;
	}
});






// starting the script on page load
$(document).ready(function(){



	var windowwidth = $(document.body).width();

	// Basic setup
	tooltip_xOffset = -10;
	tooltip_yOffset = -10;	
	
	tooltipargs = {
		style: {
			name: 'light',
			border: {
				color: '#666',
				radius: 5,
				width: 3
			},
			tip: true
		},
		show: {
			delay: 0,
			effect: {
				length: 0
			}
		},
		content: {
			text: false,
			title: {
				text: false
			}
		},
		position: {
			corner: {
				target: "bottomMiddle"
			},
			adjust: {
				screen: true
			}
		}
	};

	if (browser != "Mobile Safari"){
		$('.tooltip').each(function(){
			var a = tooltipargs;
			var text = $(this).attr("title").split("::");
			$(this).attr("title", "");
			if (text.length == 2){
				a["content"]["text"] = text[1];
				a["content"]["title"]["text"] = text[0];
			} else {
				a["content"]["text"] = text[0];
				a["content"]["title"]["text"] = false;
			}
			$(this).qtip(a);
		});
	}

	
	$("body").click(function(){
		// Reset stuff
		$("#atlaspopupmenu").html("");
		$("#atlaspopupmenu").hide();
		$(".atlaspopupmenu").hide();
		$(".popupmenu_button").removeClass("popupmenu_button");
		popupmenutarget = false;
		
	});
	
	
	$().mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	}); 	
	


	$("#inlineditbutton").click(function(){
		var id = inline_edit_element;
		var parts = id.split("::");
		var theElement = $("#" + id);
		dialog("/atlas/ajax/admin/inline/field.php?identifier=" + parts[0] + "&field=" + parts[1] + "&id=" + parts[2]);
		return false;
	});
	
	var hide_edit_button_timer;
	var inline_edit_element;
	
	$(".inline_edit").live("click", function(){
		var id = $(this).attr("id");
		var parts = id.split("::");
		//console.log("/atlas/ajax/admin/inline/field.php?identifier=" + parts[0] + "&field=" + parts[1] + "&id=" + parts[2]);
		dialog("/atlas/ajax/admin/inline/field.php?loadjs=yes&identifier=" + parts[0] + "&field=" + parts[1] + "&id=" + parts[2]);
	});
	
	
	// Popupmenyer!
	
	//$("textarea.textarea").elastic();
	
	//$("textarea.textarea").keyup(function(){
	//	var h = $(this).scrollHeight;
	//	console.log(h);
	//});
	

	$("a.popupmenu").live("click", function(){
		var what = $(this).attr("href");
		var ap = qtipargs;
		ap["content"]["title"]["text"] = false;
		ap["content"]["title"]["button"] = false;
		if (document.getElementById(what)){
			ap["content"]["url"] = false;
			ap["content"]["text"] = $("#" + what);
		} else {
			ap["content"]["text"] = "<img src='/bilder/spinner.gif' />";
			ap["content"]["url"] = what;
		}
		$(this).qtip(ap);
		return false;
	});
	
	$(".pointer").each(function(){
		var apo = qtipargs;
		apo["content"]["title"]["text"] = "Information";
		apo["content"]["title"]["button"] = "Stäng";
		apo["content"]["text"] = $(this).attr("title");
		apo["hide"] = false;
		
		$(this).qtip(apo);
	});
	

	

// 	$("a.popupmenu").click(function(){
// 		if ($("#atlaspopupmenu")){
// 			var alreadyopen = true;
// 		}
// 		
// 		
// 		$("#atlaspopupmenu").remove();
// 		$(".atlaspopupmenu").hide();
// 		//$(".atlaspopupmenu").removeClass("atlaspopupmenu");
// 		$(".menu_under").removeClass("menu_under");
// 		$(".popupmenu_button").removeClass("popupmenu_button");
// 
// 		var what = $(this).attr("href");
// 		
// 		var t = $(this);
// 		
// 		if (what == popupmenutarget){
// 			// We're clicking on the same item again, so we reset the popupmenutarget, and that's all.
// 			
// 			popupmenutarget = "";
// 		} else if (document.getElementById(what)){
// 
// 			popupmenutarget = what;
// 			var m = $("#" + what);
// 			m.addClass("atlaspopupmenu");
// 			positionmenu(m, t);
// 			m.show();
// 			
// 		} else {
// 			$("body").append("<div id='atlaspopupmenu' class='atlaspopupmenu'><" + "/div>");
// 			$.get(what, function(data){
// 				$("#atlaspopupmenu").html(data);
// 				positionmenu($("#atlaspopupmenu"), t);
// 				$("#atlaspopupmenu").show();
// 				t.addClass("menu_under");
// 				t.addClass("popupmenu_button");
// 			});
// 			popupmenutarget = what;
// 		}
// 
// 
// 		return false;
// 	});


	
	if ($("a.adminmode.atlasbutton").hasClass("active")){
		$(".admin").show();
	} else {
		$(".admin").hide();
	}
	
});