﻿jQuery(document).ready(

	/*
	This function gets loaded when all the HTML, not including the portlets, is
	loaded.
	*/

	function() {
		jQuery("#infoMsg").hide();
		jQuery(".searchInstructionHead").click(function(){
    			jQuery(this).next("#infoMsg").slideToggle('fast');			
  		});
		jQuery("#close").click(function(){
    			jQuery("#infoMsg").hide();			
  		});
		
		var v = jQuery("#LoggedInName");
		if (v.length){
			v.load("/portal/UserNameServlet");
		}else {
			v.empty();
		}
		
		
});

Liferay.Portlet.ready(

	/*
	This function gets loaded after each and every portlet on the page.

	portletId: the current portlet's id
	jQueryObj: the jQuery wrapped object of the current portlet
	*/

	function(portletId, jQueryObj) {
	}
);

jQuery(document).last(

	/*
	This function gets loaded when everything, including the portlets, is on
	the page.
	*/

	function() {
		
		initConfirmBoxes();
		
		jQuery("#ms td.remove a").click(function() {
			return confirm("Är du säker på att du vill ta bort?");
		});
		jQuery("#ms td.remove input").click(function() {
			return confirm("Är du säker på att du vill ta bort?");
		});
		jQuery("#ms td.acol4 form input").click(function() {
			return confirm("Är du säker på att du vill ta bort?");
		});		

		/*
		jQuery("#breadCrumbsAndUser a.logout").click(function() {
			confirmLogout("Är du säker på att du vill logga ut?", this);
			return false;
		});
		*/
	}
);

function initConfirmBoxes() {
	
	var boxes = jQuery(".vl-confirm-javascript");

	jQuery.each(boxes, function() { 
			  	
	  	var js = jQuery(this).attr("onclick");
		var newclick=function(event) {
			event.preventDefault();
			removeSavedData(null,js);
		}
			
		jQuery(this).removeAttr("onclick");
		jQuery(this).click(newclick);
	})
};

function removeSavedData(pText,linkInput) {
	
	jQuery("#confirmBox").empty();
	if (pText != null) {
		jQuery("#confirmBox").append("<p>" + pText + "</p>");
	} else {
		jQuery("#confirmBox").append("<p>Är du säker på att du vill ta bort?</p>");
	}
	jQuery("#confirmBox").dialog({ 
	        autoOpen: true, 
	        modal: true,
	        resizable: false,
	        draggable: false,
	        height: 100, 
	        width: 240,
	        title: "Bekräfta Ta bort",
	        overlay: { 
	                opacity: 0.4, 
	                background: '#9F9F9F'
	        },			        
	        buttons: {
                      // a button invoking an anonymous function when clicked
			// a button invoking an other function when clicked
			"Ja": function() {
				jQuery("#confirmBox").empty();
				jQuery(this).dialog("close");
				return onOk(linkInput);
			},
			"Nej": function() {
				jQuery("#confirmBox").empty();
				jQuery(this).dialog("close");
			}
		}
	});
	return false;
}

function confirmLogout(pText,linkInput) {
	jQuery("#confirmBox").empty();
	if (pText != null) {
		jQuery("#confirmBox").append("<p>" + pText + "</p>");
	}
	
	jQuery("#confirmBox").dialog({ 
	        autoOpen: true, 
	        modal: true,
	        resizable: false,
	        draggable: false,
	        height: 100, 
	        width: 240,
	        title: "Bekräfta utloggning",
	        overlay: { 
	                opacity: 0.4, 
	                background: '#9F9F9F'
	        },			        
	        buttons: {
                      // a button invoking an anonymous function when clicked
				"Nej": function() {
					jQuery("#confirmBox").empty();
					jQuery(this).dialog("close");
				},
				// a button invoking an other function when clicked
					"Ja": function() {return onOk(linkInput);}
		}
	});
	return false;
}

function vlConfirm(pText) {
	jQuery("#confirmBox").empty();
	if (pText != null) {
		jQuery("#confirmBox").append("<p>" + pText + "</p>");
	}
	jQuery("#confirmBox").dialog({ 
	        autoOpen: true, 
	        modal: true,
	        resizable: false,
	        draggable: false,
	        height: 100, 
	        width: 240,
	        title: "Bekr&auml;fta",
	        overlay: { 
	                opacity: 0.4, 
	                background: '#9F9F9F'
	        },			        
	        buttons: {
            	"Ja": function() {
				return true;}
	        	,
	        	"Nej": function() {
					jQuery("#confirmBox").empty();
					jQuery(this).dialog("close");}
	        }
	});
	return false;
}

function onOk(linkInput) {
	if (linkInput instanceof Function) {
		linkInput();
	} else if (linkInput.href != null) {
		location.href=linkInput.href;
		return true;
	} else {
		linkInput.parentNode.submit();
		return true;
	}
}
