


$(document).ready(function() {
	$('#topmenu_links A').each(function() {
		if (!$(this).hasClass("active")) {
			$(this).hover(function() {
	      $(this).addClass("active") ;
	    }, function() {
	      $(this).removeClass("active") ;
	    });
		}
	}) ;
	
	if ($('#customSelectbox')) {
		try {
			$('#customSelectbox').selectbox() ;
		} catch (e) {}
	}
	
	$('.hideIcons A').each(function() {
		$(this).removeClass("hideIcons").addClass("hideIcons") ;
	}) ;
	
	var fileInRelPattern = new RegExp(".*(pdf|doc|excelikon)\\.(gif|jpg).*", "gim") ;
	
	$('#content_container A:not(.eventLink, .previewZoom, .hideIcons)').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 ;
				}
			}
			if (isTargetBlank && isRelUrl && !isRelFil) {
				var linkContentHtml = $(this).html().replace(/\s+/g,"") ;
				if (/.*<\/span>$/i.test(linkContentHtml.replace(/\s+/g,""))) {
					$(this).find('span').append('<i class="newWinIconSpan newWinIconSpanMenu">&nbsp;<img src="/images_gui/icons/icon_new_win.gif" class="newWinIcon" title="Öppnas i nytt fönster" alt="" /></i>');
				} else if ("" != linkContentHtml && (null == linkTagImages || 0 == linkTagImages.length)) {
					$(this).append('<i class="newWinIconSpan">&nbsp;<img src="/images_gui/icons/icon_new_win.gif" class="newWinIcon" title="Öppnas i nytt fönster" alt="" /></i>');
				}
			}
			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) {}
	}) ;
	
	if ($("#commentBtn")) {
		$("#commentBtn").click(function() {
			if ($("#tip_friend").is(":visible")) {
				$("#tip_friend").hide("slow", function() {
					$("#comment_page").show("slow", function() {
						$("body").scrollTo($("#article_bottom"), 500);
					});
				});
			} else {
				if ($("#comment_page").is(":hidden")) {
					$("#comment_page").show("slow", function() {
						$("body").scrollTo($("#article_bottom"), 500);
					});
				} else {
					$("#comment_page").hide("slow");
				}
			}
		});
		$("#commentHideBtn").click(function() {
			$("#comment_page").hide("slow");
		});
	}
	
	if ($("#tipFriendBtn")) {
		$("#tipFriendBtn").click(function() {
			if ($("#comment_page").is(":visible")) {
				$("#comment_page").hide("slow", function() {
					$("#tip_friend").show("slow", function() {
						$("body").scrollTo($("#article_bottom"), 500);
					});
				});
			} else {
				if ($("#tip_friend").is(":hidden")) {
					$("#tip_friend").show("slow", function() {
						$("body").scrollTo($("#article_bottom"), 500);
					});
				} else {
					$("#tip_friend").hide("slow");
				}
			}
		});
		$("#tipFriendHideBtn").click(function() {
			$("#tip_friend").hide("slow");
		});
	}
	
	if ($("a.calendarAddLink")) {
		try {
			$("a.calendarAddLink").fancybox({
				'zoomSpeedIn' :500,
				'zoomSpeedOut':500,
				'showCloseButton':true,
				'enableEscapeButton':true,
				'frameWidth':400,
				'frameHeight':500
			}) ;
		} catch (e) {}
	}
	
	if ($("#newsSubscribeBtn")) {
		$("#newsSubscribeBtn").click(function() {
			if ($("#news_subscribe").is(":hidden")) {
				$("#news_subscribe").show("slow", function() {
					$("body").scrollTo($("#news_subscribe"), 500);
				});
			} else {
				$("#news_subscribe").hide("slow");
			}
		});
		$("#newsSubscribeHideBtn").click(function() {
			$("#news_subscribe").hide("slow");
		});
	}
	
	if ($("#iframeWide")) {
		resizeIframe($("#iframeWide")) ;
		$(window).resize(function(){
			resizeIframe($("#iframeWide")) ;
		});
	}
	
	addHiliteToFormElements("#content_container") ;
	
	$('#rssList1').each(function() {
		var oThis = $(this) ;
		var ajaxParameters = 'action=get_rss_feed&' + $(oThis).find('.waitDiv img').attr('longdesc') ;
		$.get('/servlet/AjaxServlet', ajaxParameters, function(data) {
			if (data.indexOf('rss_list') != -1) {
				$(oThis).html(data) ;
				toolTip($, '#' + $(oThis).attr('id')) ;
			} else {
				$(oThis).remove() ;
			}
		}) ;
	}) ;
}) ;


function addHiliteToFormElements(selector) {
	if ($(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") ;
		}) ;
	}
}

function resizeIframe(el) {
	try {
		var lockedH = $(el).css('min-height') ;
		if (/\d+/.test(lockedH) && parseInt(lockedH) >= 100 && parseInt(lockedH) <= 2000) {
			$(el).height(parseInt(lockedH)) ;
		} else {
			var iframeTop = $(el).position().top ;
			var iframeH = ($(window).height() - iframeTop - 170) ;
			if (iframeH < 200) iframeH = 200 ;
			$(el).height(iframeH) ;
		}
	} catch (e) {}
}


/* *******************************************************************************************
 *         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 iconFileTypes = [ "PDF", "DOC", "XLS", "PPT", "ZIP", "IMAGE", "SWF", "VIDEO", "AUDIO",
                      "TXT", "RTF", "HTM", "OO-WRITE", "OO-CALC", "OO-DRAW", "OO-IMPRESS",
                      "ODS", "OTS", "SXC", "STC", "ODT", "OTT", "SXW", "STW", "ODP", "OTP", "SXI", "STI", "ODG", "OTG", "SXD", "STD" ] ;
 
this.toolTip = function($, selector){
	xOffset = 10;
	yOffset = 20;
	$(selector + " .toolTip").hover(function(e){
		this.t = this.title;
		this.title = "";
		var fileData = $(this).attr("rel") ;
		this.iconClass = "" ;
		var fileExt ;
		var reFile = new RegExp("^FIL:(" + iconFileTypes.join("|") + ")", "gi") ;
		if (null != fileData && reFile.test(fileData)) {
			fileExt = fileData.replace(reFile, "$1") ;
			this.iconClass = "toolTipIcon_" + fileExt.toUpperCase() ;
		} else if (null != fileData && fileData.length >= 7 && fileData.indexOf("FIL") != -1) {
			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();
	});
	$(selector + " .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(){
	toolTip($, 'body') ;
});

/* *******************************************************************************************
 *         Functions                                                                         *
 ******************************************************************************************* */

jQuery.fn.outerHTML = function() {
	return $('<div>').append( this.eq(0).clone() ).html();
};

function goDirectTo(path) {
	if ("" != path) {
		top.location.href = path ;
	}
}

function openPath(event, path) {
	if (event && (event.ctrlKey || event.shiftKey)) {
		window.open(path) ;
	} else {
		document.location = path ;
	}
}

function easterEgg(event, metaId) {
	if (event && (event.shiftKey || event.ctrlKey)) {
		document.location = '/login?next_meta=' + metaId ;
		return false ;
	}
	return true ;
}

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 openBuyItPopup(path) {
	popWinOpen(600, 490, path, 'buyItWin', 1, 1, 1);
}

function addEvent(sectionId) {
	$("#addEventDummy").attr("href", "/calendar_admin/?" + (sectionId > 0 ? "section_id=" + sectionId + "&" : "") + "iframe") ;
	$("#addEventDummy").click() ;
}

function adminCalendar() {
	popWinOpen(950,700,'/jsp/admin/popup_admin_events.jsp','adminEvents',1,1,1) ;
}



/* *******************************************************************************************
 *         Get absolute position of any element                                              *
 ******************************************************************************************* */
 
function getElementLeft(el) {
	return $(el).position().left ;
}

function getElementTop(el) {
	return $(el).position().top ;
}
