

jQuery.fn.idle = function(time) {
	return this.each(function() {
		var i = $(this) ;
		i.queue(function() {
			setTimeout(function() {
				i.dequeue() ;
			}, time) ;
		}) ;
	}) ;
} ;
String.prototype.trim = function() {
	return this.replace(/^\s|\s$/g, '') ;
} ;


$(document).ready(function() {
	
	addHiliteToFormElements("#mainContainer") ;
	if ($('a.showMorePublications')) {
		$('a.showMorePublications').click(function() {
			$('div.morePublications').toggle();
		});
	}
	
	var iMaxBoxH = 0 ;
	$('.boxBlue div.boxContent').each(function() {
		if ($(this).height() > iMaxBoxH) iMaxBoxH = $(this).height() ;
	}) ;
	if (iMaxBoxH > 100) {
		$('.boxBlue div.boxContent').each(function() {
			$(this).height(iMaxBoxH) ;
		}) ;
	}
	iMaxBoxH = 0 ;
	$('.boxBrownLarge').each(function() {
		if ($(this).hasClass('searchBox') || $(this).hasClass('sortBox')) {
			var oBrownInnerBox = $(this).find('div.boxContent')[0] ;
			if ($(oBrownInnerBox).height() > iMaxBoxH) iMaxBoxH = $(oBrownInnerBox).height() ;
		}
	}) ;
	if (iMaxBoxH > 80) {
		$('.boxBrownLarge').each(function() {
			if ($(this).hasClass('searchBox') || $(this).hasClass('sortBox')) {
				var oBrownInnerBox = $(this).find('div.boxContent')[0] ;
				$(oBrownInnerBox).height(iMaxBoxH) ;
			}
		}) ;
	}
	if ($("#boxIntro") && $("#boxIntroRight")) {
		iMaxBoxH = 0 ;
		$("#boxIntro, #boxIntroRightContent").each(function() {
			if ($(this).height() > iMaxBoxH) iMaxBoxH = $(this).height() ;
		}) ;
		if (iMaxBoxH > 100) {
			$('#boxIntro, #boxIntroRightContent').each(function() {
				var newBoxIntroH = ("boxIntroRightContent" == $(this).attr("id")) ? iMaxBoxH - 2 : iMaxBoxH ;
				$(this).height(newBoxIntroH) ;
			}) ;
		}
	}
	$('.boxRedLarge').each(function() {
		var oRedInnerBox = $(this).find('div.boxContent')[0] ;
		if ($(oRedInnerBox).height() < 300) {
			$('.boxRedLarge .topleft,.boxRedLarge .topright').each(function() {
				$(this).css("background-image", "url(/images_gui/box-small.jpg)") ;
			}) ;
		}
	}) ;
	
	
	$('div.workarea div.workareaText p.workareaTextText').each(function() {
		var waTextLink = "" ;
		var oWaSpanSection = $(this).find('span.sektion') ;
		if (oWaSpanSection.length) {
			var tempWaSection = "" ;
			for (var i = 0; i < oWaSpanSection.html().length; i++) {
				tempWaSection += "M" ;
			}
			tempWaSection += "¤" ;
			$(this).html(tempWaSection + $(this).html()) ;
			$(this).find('span.sektion').remove() ;
		}
		if ($(this).find('a').length) {
			waTextLink = '<a href="' + $(this).find('a').attr('href') + '" target="' + $(this).find('a').attr('target') + '" />' ;
		}
		$(this).excerpt({ lines: 2, end: '...' }) ;
		if ("" != waTextLink) {
			$(this).wrapInner(waTextLink) ;
		}
		$(this).html($(this).html().replace(/M+¤/, '')) ;
		$(this).prepend(oWaSpanSection) ;
	}) ;
	
	$('p.inFocusText').each(function() {
		$(this).excerpt({ lines: 4, end: '...' }) ;
	}) ;
	$('div.news p.waNewsText').each(function() {
		var waNewsTextLink = "" ;
		if ($(this).find('a').length) {
			waNewsTextLink = '<a href="' + $(this).find('a').attr('href') + '" target="' + $(this).find('a').attr('target') + '" />' ;
		}
		$(this).excerpt({ lines: 3, end: '...' }) ;
		if ("" != waNewsTextLink) {
			$(this).wrapInner(waNewsTextLink) ;
		}
	}) ;
	
	$('div.truncateText').each(function() {
		var oTruncateTextDiv = $(this) ;
		var oTruncateTextDivSection = $(this).find('div.section:first') ;
		var oSectionDiv = null ;
		var sSectionText = "" ;
		if (oTruncateTextDivSection.length) {
			oSectionDiv = oTruncateTextDivSection ;
			sSectionText = oTruncateTextDivSection.html().trim() + "M" ;
			oTruncateTextDivSection.remove() ;
			oTruncateTextDiv.html(sSectionText + " " + oTruncateTextDiv.html().trim()) ;
		}
		oTruncateTextDiv.excerpt({ lines: 2, end: '...' }) ;
		if (null != oSectionDiv) {
			var newTruncateTextString = oTruncateTextDiv.html().trim().substring(oTruncateTextDiv.html().trim().indexOf(sSectionText) + sSectionText.length, oTruncateTextDiv.html().trim().length) ;
			oTruncateTextDiv.html(newTruncateTextString) ;
			oTruncateTextDiv.prepend(oSectionDiv) ;
		}
	}) ;
	
	try {
		$('.bannerImg').each(function() {
			var oBannerImg = $(this).find('img')[0] ;
			var hOffset = ($(this).next("div").hasClass("boxRedLarge")) ? 32 : 20 ;
			$(this).css("height", $(oBannerImg).height() + hOffset) ;
		}) ;
	} catch (e) {}
	try {
		$('.news').each(function() {
			if ($(this).next("div").hasClass("boxRedLarge20")) {
				$(this).css("height", $(this).height() + 12) ;
			}
		}) ;
	} catch (e) {}
	
	$('.autoCompleteOff').each(function() {
		$(this).attr("autocomplete", "off") ; 
	}) ;
	try {
		$(".autoCompleteKeyWords").autocomplete("/jsp/get_keywords_for_jquery.jsp", {
			minChars: 2
		});
	} catch (e) {}
	
	$('.hideIcons A').each(function() {
		$(this).removeClass("hideIcons").addClass("hideIcons") ;
	}) ;
	
	var fileInRelPattern = new RegExp(".*(pdf|doc|excelikon)\\.(gif|jpg).*", "gim") ;
	
	$('#mainContent A:not(.hideIcons,.formBtnImg)').each(function() {
		var relData = "" ;
		try {
			var linkTagImages = $(this).find('img') ;
			relData  = $(this).attr("rel") ;
			var isRelFil        = (null != relData && relData.length > 5 && relData.indexOf("FIL") != -1) ;
			var isRelUrl        = (null != relData && relData.indexOf("URL") != -1) ;
			var isRelUrlWithUrl = (isRelUrl && relData.length > 5 && relData.indexOf("URL:") != -1) ;
			var isTargetBlank = ("_blank" == $(this).attr("target")) ;
			if (isTargetBlank && (null == relData || "" == relData)) {
				var href = $(this).attr("href") ;
				if (href.indexOf(":") != -1) {
					relData = "URL:" + href ;
					$(this).attr("rel", relData) ;
					isRelUrl = true ;
					isRelUrlWithUrl = true ;
				}
			}
			try {
				var oldTitle = $(this).attr("title") ;
				var newTitle = oldTitle ;
				if (null != oldTitle && "" != oldTitle) {
					newTitle += (/.*\.$/.test(newTitle)) ? " " : ". " ;
				}
				var brIfNotEmpty = ("" != newTitle) ? "&lt;br/&gt;" : "" ;
				var hasOtherTitle = false ;
				if ("" == relData && null != linkTagImages && linkTagImages.length > 0) {
					var imgSrc  = $(linkTagImages[0]).attr("src") ;
					if (fileInRelPattern.test(imgSrc)) {
						var fileImg  = imgSrc.replace(fileInRelPattern, "$1").toLowerCase() ;
						var fileType = (fileImg.indexOf("excel") != -1) ? "Excel" : ("doc" == fileImg) ? "Word" : fileImg ;
						var fileExt  = (fileImg.indexOf("excel") != -1) ? "XLS" : ("doc" == fileImg) ? "DOC" : fileImg.toUpperCase() ;
						newTitle += brIfNotEmpty + "Fil: " + fileType.toUpperCase() + ".&lt;br/&gt;Öppnas i nytt fönster." ;
						$(this).attr("rel", "FIL:" + fileExt) ;
						hasOtherTitle = true ;
					}
				}
				if (!hasOtherTitle || isRelUrl) {
					newTitle +=
						(isRelUrlWithUrl) ?
							brIfNotEmpty + "Länk: " + relData.substring(4) + (isTargetBlank ? "&lt;br/&gt;Öppnas i nytt fönster." : "") :
							(isRelFil) ?
								brIfNotEmpty + "Fil: " + relData.substring(4) + (isTargetBlank ? ".&lt;br/&gt;Öppnas i nytt fönster." : "") :
								(isTargetBlank && isRelUrl) ?
									brIfNotEmpty + "Länk till annan webbplats.&lt;br/&gt;Öppnas i nytt fönster." :
									(isTargetBlank) ?
										brIfNotEmpty + "Öppnas i nytt fönster." :
										(isRelUrl) ?
											brIfNotEmpty + "Länk till annan webbplats." :
											"" ;
					if (newTitle.indexOf(" OO-")) {
						newTitle = newTitle.replace(" OO-", " OpenOffice ") ;
					}
				}
				$(this).attr("title", newTitle) ;
				if ("" != newTitle) {
					$(this).addClass("toolTip") ;
					if (newTitle.indexOf("till annan webbplats") != -1) {
						$(this).attr("rel", "URL") ;
					}
					if (null != linkTagImages && linkTagImages.length > 0) {
						$(linkTagImages[0]).attr("title", "") ;
					}
				}
			} catch (ex) {}
		} catch (e) {}
	}) ;
	
	
	var oldBtnIdCount = 0 ;
	$('.formBtn').each(function(){
		var oldBtn   = $(this) ;
		var btnText  = oldBtn.text() || oldBtn.val() ;
		var oldBtnId = oldBtn.id || 'oldBtn' + oldBtnIdCount ;
		var newBtnW  = (btnText.length > 5) ? ((btnText.length + 1) * .6) + 'em' : (btnText.length + 1) + 'em' ;
		if ($(':submit,:button,:reset',this)) {
			var newLink = $('<a>')
				.insertAfter(this)
				.addClass(this.className)
				.addClass(/.*(sök|beställ).*/i.test(btnText) ? 'formBtnImgSpecial' : this.className)
				.removeClass('formBtn')
				.addClass('formBtnImg')
				.css('width',newBtnW)
				.attr('id',oldBtnId)
				.bind('click', function() {
				  document.getElementById(oldBtnId + 'Old').click() ;
					return false ;
				});

			$(this)
				.attr('id',oldBtnId + 'Old')
				.attr('style','position:absolute !important; left:-10000px !important; visibility:hidden !important;') ;
			newLink
				.text(btnText)
				.wrapInner('<div><span><b></b></span></div>') ;
		}
		oldBtnIdCount++ ;
	});
	
	
	
	$('#subContentCol .news A').each(function() {
		if (!$(this).parent().parent().hasClass('news_item')) {
			$(this).addClass('normalLink') ;
		}
	});
	
	
});




jQuery.fn.outerHTML = function() {
	return $('<div>').append( this.eq(0).clone() ).html();
};

jQuery.fn.orderedList = function(showNumbers, appendString) {
	var iCount = 0 ;
	var oList ;
	return this.each(function() {
		oList = $(this) ;
		var oTable = $('<table class="olTable" border="0" cellspacing="0" cellpadding="0"></table>') ;
		$(this).find('li').each(function() {
			iCount++ ;
			$(oTable).append('<tr><td class="number">' + (showNumbers ? iCount + '' : '') + appendString + '</td><td class="data">' + $(this).html() + '</td></tr>') ;
		});
		$(oList).replaceWith($(oTable)) ;
	});
};

$(document).ready(function() {
	$('#article > ol').each(function(){
		$(this).orderedList(true, ')') ;
	});
	$('#article > ul').each(function(){
		if ('list-style-type: none;' == $(this).attr('style')) {
			$(this).orderedList(false, '&ndash;') ;
		} else {
			$(this).orderedList(false, '<b>&#149;</b>') ;
		}
	});
});


function addHiliteToFormElements(selector) {
	$(selector + ' input[type="text"],' + selector + ' input[type="file"],textarea,select').addClass("formFieldBlur") ;
	$(selector + ' input[type="text"],' + selector + ' input[type="file"],textarea,select').focus(function() {
		$(this).removeClass("formFieldBlur").addClass("formFieldFocus") ;
	}) ;
	$(selector + ' input[type="text"],' + selector + ' input[type="file"],textarea,select').blur(function() {
		$(this).removeClass("formFieldFocus").addClass("formFieldBlur") ;
	}) ;
}



/* *******************************************************************************************
 *         ToolTip                                                                           *
 ******************************************************************************************* */
/*
 * Tooltip script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * modified by Tommy Ullberg, imCode Partner AB (http://www.imcms.net/)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

var oToolTipTimer = null ;

this.toolTip = function(){
	if (oToolTipTimer) {
		window.clearTimeout(oToolTipTimer) ;
	}
	xOffset = 10;
	yOffset = 20;
	$(".toolTip").hover(function(e){
		this.t = this.title;
		this.title = "";
		var fileData = $(this).attr("rel") ;
		this.iconClass = "" ;
		if (null != fileData && fileData.length > 7 && fileData.indexOf("FIL") != -1) {
			var fileExt = fileData.substring(4,7) ;
			if (/^(PDF|DOC|ZIP|JPG|PNG|GIF|MP3|AVI|MPG)$/i.test(fileExt)) {
				this.iconClass = "toolTipIcon_" + fileExt.toUpperCase() ;
			}
		} else if (null != fileData && fileData.indexOf("URL") != -1) {
			this.iconClass = "toolTipIcon_EXT_LINK" ;
		}
		$("body").append('<div id="toolTipPop">'+ this.t.replace(/&lt;/g, "<").replace(/&gt;/g, ">") + '</div>');
		$("#toolTipPop")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn(document.all ? "fast" : "slow");
	},
	function(){
		this.title = this.t;
		$("#toolTipPop").remove();
	});
	$(".toolTip").mousemove(function(e){
		$("#toolTipPop")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px") ;
		if ("" != this.iconClass) {
			$("#toolTipPop").addClass(this.iconClass) ;
		}
	});
};

$(document).ready(function(){
	//oToolTipTimer = window.setTimeout("toolTip()", 200) ;
	toolTip() ;
});


/* *******************************************************************************************
 *         Functions                                                                         *
 ******************************************************************************************* */

function easterEgg(event, metaId) {
	if (event && (event.shiftKey || event.ctrlKey)) {
		document.location = '/login?next_meta=' + metaId ;
		return false ;
	}
	return true ;
}

function openPath(event, path) {
	if (event && (event.ctrlKey || event.shiftKey)) {
		window.open(path) ;
	} else {
		document.location = path ;
	}
}

function popWinOpen(winW,winH,sUrl,sName,iResize,iScroll,iStatus) {
	var winX, winY ;
	if (screen) {
		if (winH < 10) winH = screen.height - 80 ;
		if ((screen.height - winH) < 150) {
			winX = (screen.width - winW) / 2;
			winY = 0;
		} else {
			winX = (screen.width - winW) / 2;
			winY = (screen.height - winH) / 2;
		}
		var popWindow = window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,scrollbars=" + iScroll + ",status=" + iStatus + ",width=" + winW + ",height=" + winH + ",top=" + winY + ",left=" + winX + "");
		if (popWindow) popWindow.focus();
	} else {
		if (winH < 10) winH = 400 ;
		window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,scrollbars=" + iScroll + ",status=" + iStatus + ",width=" + winW + ",height=" + winH);
	}
}


function addEvent() {
	popWinOpen(380,550,"/calendar_admin/","EventWin",1,0,1) ;
}

function openApplyToEvent() {
	popWinOpen(775,600,"http://www.arbetsgivarverket.se/utvecklingsradet/aktiviteter/form/oppna_utbildningar_small.asp","seminarium",1,1,1) ;
}


