
var defaultFontSize = 75;
var currentFontSize = defaultFontSize;

function changeFontSize(sizeDifference){
	if (sizeDifference == 0) {
		currentFontSize = defaultFontSize;
	} else {
		currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 10);
	}

	if(currentFontSize > 200){
		currentFontSize = 200;
	}else if(currentFontSize < 50){
		currentFontSize = 50;
	}

	document.body.style.fontSize = currentFontSize + '%';
	createCookie('fontSize', currentFontSize +'%', 60);

	return false;
};
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

//*******************************************************************************************
// Fetch RSS/ATOM and parse
//
// Parameters:
// serial		string	serial to Zope object (can be used to fetch eg. URL to RSS/ATOM feed)
// target		string	Name of HTML element to send result to, eg a DIV
// title		string	Optional title
// feed_limit	int		Max number of feed items to fetch
//
// Howto use:
// First make a div to contain the result...
// <div id="rssResult"></div>
//
// Then put a javascript element with reference to the function...
// <script type="text/javascript">
// DifiGetFeed('http://localhost:8082/index.html/rss', 'rssResult',5);
// </script>
//*******************************************************************************************
function DifiGetFeed(serial, target, title, feed_limit) {
	jQuery.getFeed({
		url: '/proxy.html?serial=' + serial,
		success: function(feed) {

			var feedTitle;
			if (title != '') {
				feedTitle = title;
			} else {
				feedTitle = feed.title;
			}

			if(feed.items.length==0 && feedTitle=='Ledige stillingar i Difi') {
				jQuery('#'+target).append('<strong>Det er for tida ingen ledige stillingar i Difi</strong>');
			}
			else {
				jQuery('#'+target).append('<h2 class="pagetitle">'
				+ feedTitle
				+ '</h2>');

				var html = '';

				for(var i = 0; i < feed.items.length && i < feed_limit; i++) {

					var item = feed.items[i];

					var feedDate = Date.parse(item.updated);

					var description = item.description;

					if (feedDate) {
						feedDate = "(" + feedDate.toString('dd.MM.yyyy') + ")";
					} else {
						if (feedDate) {
							feedDate = "(" + item.updated + ")";
						} else {
							feedDate = "";
						}
					}

					if (description == item.title) {
						description = '';
					}
					else if (description.length > 300) {
						description = item.description.slice(0,300);
						whitespace=description.lastIndexOf(" ");
						description=description.slice(0,whitespace+1)+ "[...]"; //wordpress gjer eigen concatinering på litt over 50 ord og legg på [...] sjølv
					} else {
						description = item.description;
					}

					html += '<div class="date">'
						+ feedDate
						+ '</div>';

					html += '<h2 class="headline">'
						+ '<a href="'
						+ item.link
						+ '" '
						+ 'title="'
						+ item.title
						+ '" class="headline">'
						+ item.title
						+ '</a>'
						+ '</h2>';

					html += '<div class="fpabstract"><p>'
						+ description
						+ '</p></div>';
				}
				html+='<div class="rssListSeperator">&nbsp;</div>';

				jQuery('#'+target).append(html);
			}
		}
	});
}


//*******************************************************************************************
// Fetch RSS/ATOM and parse
//
// Parameters:
// serial	string	serial to Zope object (can be used to fetch eg. URL to RSS/ATOM feed)
// target	string	Name of HTML element to send result to, eg a DIV
// title	string	Optional title
//
// Howto use:
// First make a div to contain the result...
// <div id="rssResult"></div>
//
// Then put a javascript element with reference to the function...
// <script type="text/javascript">
// DifiGetFeed('http://localhost:8082/index.html/rss', 'rssResult',5);
// </script>
//*******************************************************************************************
function DifiGetFeedForProjects(serial, target, title,feed_limit) {
	jQuery.getFeed({
		url: '/proxy.html?serial=' + serial,
		success: function(feed) {

			var feedTitle;
			if (title != '') {
				feedTitle = title;
			} else {
				feedTitle = feed.title;
			}

			jQuery('#'+target).append('<h2 class="pagetitle">'
			+ feedTitle
			+ '</h2>'
			+ '<hr />');

			var html = '';
			html+='<ul>';
			for(var i = 0; i < feed.items.length && i < feed_limit; i++) {

				var item = feed.items[i];

				var feedDate = Date.parse(item.updated);

				var description = item.description;

				if (feedDate) {
					feedDate = "(" + feedDate.toString('dd.MM.yyyy') + ")";
				} else {
					if (feedDate) {
						feedDate = "(" + item.updated + ")";
					} else {
						feedDate = "";
					}
				}

				if (description == item.title) {
					description = '';
				}
				else if (description.length > 300) {
					description = item.description.slice(0,300);
					whitespace=description.lastIndexOf(" ");
					description=description.slice(0,whitespace+1) + "[...]"; //wordpress gjer eigen concatinering på litt over 50 ord og legg på [...] sjølv
				} else {
					description = item.description;
				}

				html +='<li>';

				html += '<h4 class="headline">'
					+ '<a href="'
					+ item.link
					+ '" '
					+ 'title="'
					+ item.title
					+ '" class="headline">'
					+ item.title
					+ '</a>'
					+ '</h4>';

				html += '<div class="fpabstract"><p>'
					+ description
					+ '</p></div>';

				html += '<div class="date">'
					+ feedDate
					+ '</div>';

				html +='</li>';
			}
			html +='</ul>';


			jQuery('#'+target).append(html);
		}
	});
}

//On Hover Over
function megaHoverOver(){
	if ($(this).find(".sub").length == 1) {
		//$("ul#submenu").hide();
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		$(this).addClass('hover');
	}
}
//On Hover Out
function megaHoverOut(){
	if ($(this).find(".sub").length == 1) {
		$(this).removeClass('hover');
		//$("ul#submenu").show();
		$(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
			$(this).hide();  //after fading, hide it
		});
	}
}
//On Hover Over
function megaHoverOver2(){
	$(this).addClass('megamenu2hover');
}
//On Hover Out
function megaHoverOut2(){
	$(this).removeClass('megamenu2hover');
}
$(function() {
	//Dirty ugly hack for IE7 or if IE runs in IE7 Document Mode
	if (($.browser.msie && parseInt($.browser.version, 10) == 7) || document.documentMode == 7) {
		$("ul#mainmenu > li").each(function(idx){
		    $(this).find(".sub").css({
		    	'left'	: $(this).position().left,
		    	'top'	: 30
				}
			);
		});
	}

	var config = {
	     sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)
	     interval: 300, // number = milliseconds for onMouseOver polling interval
    	 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     	 timeout: 200, // number = milliseconds delay before onMouseOut
     	 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("ul#mainmenu li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#mainmenu li").hoverIntent(config); //Trigger Hover intent with custom configurations

	var config2 = {
	     sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)
	     interval: 300, // number = milliseconds for onMouseOver polling interval
    	 over: megaHoverOver2, // function = onMouseOver callback (REQUIRED)
     	 timeout: 200, // number = milliseconds delay before onMouseOut
     	 out: megaHoverOut2 // function = onMouseOut callback (REQUIRED)
	};

	$(".megamenu2").hoverIntent(config2); //Trigger Hover intent with custom configurations
});
