
/* *******************************************************************************************
 *         INIT                                                                              *
 ******************************************************************************************* */

function init() {}


/* *******************************************************************************************
 *         get querystring-parameters from document.location                                 *
 ******************************************************************************************* */

function getParam(attrib) {			
	var sParams = location.search;
	var retVal = "";
	if (sParams.indexOf('?') != -1) {
		var pairs = sParams.substring(1, sParams.length).split('&');
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split('=');
			if (nameVal[0] == attrib) {
				retVal = nameVal[1];
			}
		}
	}
	return retVal;
}


/* *******************************************************************************************
 *         Comments - Article                                                                *
 ******************************************************************************************* */
jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};

/* *******************************************************************************************
 *         Document Init                                                                     *
 ******************************************************************************************* */

var windowHeight = 700 ;
var searchHelpIframeHeight = 510 ;
var searchHelpWantedH = 510 ;
var offsetH = 80 ;

jQuery(document).ready(function($){
	
	try {
		var thisUrl = getParam('u') ;
		if (thisUrl.indexOf(':') != -1) {
			$('div.headInRight')
			  .append('<div class="hrText"><input type="button" value="Close the Translator" class="formBtn toolTip"' +
			          ' onclick="top.location=\'' + thisUrl + '\'; return false"' +
			          ' title="Close/exit Google Translator&lt;br/&gt;and return to the site" /></div>') ;
		}
	} catch (e) {}
	
	$('#rssList').each(function() {
		var $this = $(this) ;
		var ajaxParameters = 'action=get_rss_feed&' + $this.find('.waitDiv img').attr('longdesc') ;
		$.ajax({
			url: '/jsp/rsslist/ajax/?' + ajaxParameters,
			timeout: 3000,
			dataType: 'html',
			cache : false,
			success: function(data) {
				if (data.indexOf('rss_list') != -1) {
					$this.html(data) ;
					toolTip($, '#' + $this.attr('id')) ;
				} else {
					$this.remove() ;
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$this.remove() ;
			}
		}) ;
	}) ;
	
	$('a[class*=popup_]').click(function(event) {
		var thisClass = $(this).attr('class') ;
		var arrClasses = thisClass.split(/[\s]/g) ;
		var re = new RegExp('popup_([\\d]+)x([\\d]+)', 'gi') ;
		for (var i in arrClasses) {
			var sClass = arrClasses[i] ;
			if (re.test(sClass)) {
				var w = parseInt(sClass.replace(re, '$1')) ;
				var h = parseInt(sClass.replace(re, '$2')) ;
				event.preventDefault() ;
				popWinOpen(w,h,$(this).attr('href'),sClass + 'Win',1,0,1) ;
			}
		}
	}) ;
	
	if ($("#contactLink")) {
		$("#contactLink").click(function() {
			if ($("#contact_div").is(":hidden")) {
				$("#contact_div").show("slow", function() {
					$("body").scrollTo($("#article_bottom"), 500);
				});
			} else {
				$("#contact_div").hide("slow");
			}
		});
		$("#contactHideLink").click(function() {
			$("#contact_div").hide("slow");
		});
	}
	if ($("#rightContactLink1")) {
		$("#rightContactLink1").click(function() {
			if ($("#right_contact1_div").is(":hidden")) {
				$("#right_contact1_div").show("slow", function() {
					$("body").scrollTo($("#rightcol_contact1"), 500);
				});
			} else {
				$("#right_contact1_div").hide("slow");
			}
		});
		$("#rightContactHideLink1").click(function() {
			$("#right_contact1_div").hide("slow");
		});
	}
	if ($("#rightContactLink2")) {
		$("#rightContactLink2").click(function() {
			if ($("#right_contact2_div").is(":hidden")) {
				$("#right_contact2_div").show("slow", function() {
					$("body").scrollTo($("#rightcol_contact2"), 500);
				});
			} else {
				$("#right_contact2_div").hide("slow");
			}
		});
		$("#rightContactHideLink2").click(function() {
			$("#right_contact2_div").hide("slow");
		});
	}
	if ($("#rightContactLink3")) {
		$("#rightContactLink3").click(function() {
			if ($("#right_contact3_div").is(":hidden")) {
				$("#right_contact3_div").show("slow", function() {
					$("body").scrollTo($("#rightcol_contact3"), 500);
				});
			} else {
				$("#right_contact3_div").hide("slow");
			}
		});
		$("#rightContactHideLink3").click(function() {
			$("#right_contact3_div").hide("slow");
		});
	}
	
	
	if ($("a#inline").length > 0) {
		windowHeight = $(window).height() ;
		if (windowHeight > (searchHelpWantedH + offsetH)) {
			searchHelpIframeHeight = searchHelpWantedH ;
		} else if (windowHeight > 300) {
			searchHelpIframeHeight = (windowHeight - offsetH) ;
		}
		loadFancyBoxEvent($, searchHelpIframeHeight) ;
		$(window).resize(function(){
			windowHeight = $(window).height() ;
			if (windowHeight > (searchHelpWantedH + offsetH)) {
				searchHelpIframeHeight = searchHelpWantedH ;
			} else if (windowHeight > 300) {
				searchHelpIframeHeight = (windowHeight - offsetH) ;
			}
			loadFancyBoxEvent($, searchHelpIframeHeight) ;
		});
	}
	
	toolTip($, 'body');
	
	// FancyBox init
	
	$("a[rel=fancybox]").fancybox({
		'overlayOpacity'     : 0.8,
		'titlePosition'      : 'over',
		'transitionIn'       : 'elastic',
		'transitionOut'      : 'elastic',
		'easingIn'           : 'swing',
		'easingOut'          : 'swing',
		'onComplete'         : function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show('slow') ;
			}, function() { 
				$("#fancybox-title").hide('slow') ;
			}) ;
		}
	}) ;
	
	
	try {
		$('div > div > br').each(function() {
			if ("" == $(this).parent().parent().html().replace(/\s+|<[^>]+?>/g, '')) {
				$(this).parent().parent().remove() ;
			}
		}) ;
	} catch (e) {}
	
	$('#printDocHead').insertBefore('#container') ;
	
	
});


/*HighLite*/

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") ;
	}) ;
}

function resizeIframe(el) {
	var iframeTop = getElementTop(el) ;
	var iframeH = ($(window).height() - iframeTop - 170) ;
	if (iframeH < 200) iframeH = 200 ;
	el.height(iframeH) ;
}

function loadFancyBoxEvent($, iFrameH) {
	try {
		$("a#inline").fancybox({
			'easingIn'      : 'easeOutBounce',
			'easingOut'     : 'easeOutBounce',
			'transitionIn'  : 'elastic',
			'transitionOut' : 'elastic',
			'speedIn'       : 1000,
			'speedOut'      : 600,
			'width'         : 520,
			'height'        : iFrameH,
			'type'          : 'iframe'
		}) ;
	} catch (e) {}
}


/* *******************************************************************************************
 *         Google Translate                                                                  *
 ******************************************************************************************* */



jQuery(document).ready(function($) {
	$(".googleTranslateOpener").click(function(event) {
		event.preventDefault() ;
		GoogleTranslate.Translate.showLanguageDialog($) ;
	}) ;
}) ;


GoogleTranslate = {} ;

GoogleTranslate.Translate = {
	showLanguageDialog : function($) {
		if ($("#googleTranslateDialog").length > 0) {
			$("#googleTranslateDialog").dialog("destroy") ;
			$("#googleTranslateDialog").remove() ;
		}
		$('<div id="googleTranslateDialog"></div>').appendTo($("body")) ;
		$("#googleTranslateDialog").append($("#googleTranslate").html()) ;
		if ($("#googleTranslateDialog div.googleTranslateDiv")) {
			$.getScript(document.location.protocol+"//www.google.com/jsapi", function() {
				google.load("language", "1", {
					callback : GoogleTranslate.Translate.prepareLanguageDialogForm
				}) ;
			}) ;
		}
	},
	prepareLanguageDialogForm : function() {
		var translateDiv = $("#googleTranslateDialog div.googleTranslateDiv") ;
		var elemImg = $(document.createElement("img")) ;
		elemImg.attr("src","/images_gui/google-translate.png") ;
		elemImg.attr("alt","Google Translate") ;
		translateDiv.append(elemImg) ;
		var elemForm = $(document.createElement("form")) ;
		elemForm.attr("action","") ;
		translateDiv.append(elemForm) ;
		var elemFieldset = $(document.createElement("fieldset")) ;
		elemForm.append(elemFieldset) ;
		var elemSelect = $(document.createElement("select")) ;
		elemFieldset.append(elemSelect) ;
		var elemOption = $(document.createElement("option")) ;
		elemOption.val("-1") ;
		elemOption.text("- Select language -") ;
		elemSelect.append(elemOption) ;
		for (var i in google.language.Languages) {
			if (i !== "UNKNOWN" && google.language.isTranslatable(google.language.Languages[i])) {
				elemOption = $(document.createElement("option")) ;
				elemOption.val(google.language.Languages[i]) ;
				elemOption.text(GoogleTranslate.Utils.capitalizeString(i.replace("_"," "))) ;
				elemSelect.append(elemOption) ;
			}
		}
		var elemSubmit = $(document.createElement("input")) ;
		elemSubmit.attr("type","submit") ;
		elemSubmit.attr("class","formBtnSmall") ;
		elemSubmit.attr("value","OK") ;
		elemFieldset.append(elemSubmit) ;
		elemSubmit.click(function(event) {
			event.preventDefault() ;
			var lang = elemSelect.find("option:selected").attr("value") ;
			if (lang != "-1") {
				document.location.href = "http://translate.google.com/translate?hl="+lang+"&tl="+lang+"&sl = sv&u="+escape(document.location.href) ;
			}
		}) ;
		var elemP = $(document.createElement("p")) ;
		elemP.append("Use Google to translate this website. We take no responsibility for the accuracy of the translation.") ;
		translateDiv.append(elemP) ;
		$("#googleTranslateDialog").dialog({
			modal     : true,
			width     : 450,
			height    : 280,
			resizable : false,
			draggable : true,
			show      : 'blind',
			hide      : 'blind',
			title     : "Other Languages",
			buttons   : {
				"Close"  : function() {
					$(this).dialog("close") ;
				}
			}
		}) ;
	}
} ;

GoogleTranslate.Utils = {
	capitalizeString : function(str) {
		if (str.length > 1) {
			var tmpStr = str.toLowerCase() ;
			return tmpStr.substring(0,1).toUpperCase() + tmpStr.substring(1,str.length) ;
		} else {
			return str.toUpperCase() ;
		}
	}
} ;



	
/* *******************************************************************************************
 *         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) ;
		}
	});
};



/* *******************************************************************************************
 *         Popup                                                                             *
 ******************************************************************************************* */

function popWinOpen(winW,winH,sUrl,sName,iResize,iScroll,iStatus) {
	var winX, winY ;
	if (screen) {
		if (winW <= 0) {
			winW = screen.width - 20 ;
		}
		if (winH <= 0) {
			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 {
		window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,scrollbars=" + iScroll + ",status=" + iStatus + ",width=" + winW + ",height=" + winH);
	}
}

function openMail(sId, meta_id) {
	var url = "/jsp/mail_formular.jsp?id=" + sId;
	url += "&amp;" + "meta_id=" + meta_id;

	popWinOpen(500, 650, url , "mailWin", 1, 0, 1) ;
}

//<a href="/path/file.ext" onClick="popWinOpen(600,560,'/path/file.ext','myWin',0,0,0); return false">Linktext</a>

function openPath(event, path) {
	if (event && (event.ctrlKey || event.shiftKey)) {
		window.open(path) ;
	} else {
		document.location = path ;
	}
}


/* *******************************************************************************************
 *         Get absolute position of any element                                              *
 ******************************************************************************************* */

function getElementTop(el) {
	try {
		if (!el && this) {
			el = this ;
		}
		var posTop = el.offsetTop ;
		var elPar = el.offsetParent ;
		while (elPar != null) {
			posTop += elPar.offsetTop ;
			elPar = elPar.offsetParent ;
		}
		return posTop ;
	} catch (e) {
		return 0 ;
	}
}


/* *******************************************************************************************
 *         MISC                                                                              *
 ******************************************************************************************* */

function winStatus(str) {}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}


/* *******************************************************************************************
 *         Set/Get Cookie                                                                    *
 ******************************************************************************************* */

function setCookie(name, value) {
	var sPath = '/';
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 1000*60*60*24*365); // 365 days
	var sCookieCont = name + "=" + encodeURIComponent(value);
	sCookieCont += (expire == null) ? "" : "; expires=" + expire.toGMTString();
	sCookieCont += "; path=" + sPath;
	document.cookie = sCookieCont;
}

function getCookie(name) {
	var search = name + "=";
	if (document.cookie.length > 0) {
		var offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			var end = document.cookie.indexOf(";", offset);
			if (end == -1) {
				end = document.cookie.length;
			}
			return decodeURIComponent(document.cookie.substring(offset, end));
		}
	}
	return "" ;
}


