jQuery().ready(function(){

	window.countpopups=0;
	jQuery(".js_popupwin").click(function() {
	var url=this.href;
	var winname='';
	window.countpopups++;
	var winname="win"+window.countpopups;
	var w=url.replace(/.*[\&\?]w=(\d+).*?$/,"$1")*1;
	var h=url.replace(/.*[\&\?]h=(\d+).*?$/,"$1")*1;
	w=(typeof w == "number" && !isNaN(w)) ? w : 500 ;
	h=(typeof h == "number" && !isNaN(h)) ? h : 500 ;
	var popupWin = window.open(url, winname, 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,dependent=no,width='+w+',height='+h+'');
	return false;
	});


	// text size
	fixTextSize();
	jQuery(".textsize").click(function() {
		var ts=window.name;
		if (ts && ts=="big") {
			var val="small";
		} else {
			var val="big";
		}
	
		window.name=val;
		fixTextSize();
		return false;
	});
	 // set classname for png on IE 6 (Alpha tranparency)
	if (navigator.userAgent.indexOf("MSIE 6")!=-1) {
		jQuery("img").each(function() {
			var reg=/\.png$/;
			if (this.src.match(reg)) {
				var imgsrc=this.src;
				this.style.width=this.width+"px";
				this.style.height=this.height+"px";
				this.src = "/x.gif";
				this.runtimeStyle.backgroundImage = "none";
				this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgsrc + "', sizingMethod='scale')";
			}
		});
	}	
	// accordion function for menu
	jQuery(".js_shownext").showHide({
		"accordion" : true
	}); 
	// arena guide
	var fadeSpeed=800;
	jQuery(".js_layer").not(".open").css("display","none"); // hide layers not active
	jQuery(".js_layer .overlay a").click(function() {
		var layerName=this.className;
		jQuery(this).parents(".js_layer").fadeOut(fadeSpeed,function() {
			jQuery("#"+layerName).fadeIn(fadeSpeed);
		});
		return false;
		
	});
	jQuery("#plan a").click(function() {
		var lay=getCurrentPlane(jQuery(this));
		if (!jQuery("#"+lay+":visible").size()) { // if it's not the current layer visible
			jQuery("#plan a").removeClass("current");
			jQuery("#plan a."+lay).addClass("current");
			jQuery(".js_layer:visible").fadeOut(fadeSpeed,function() {
				jQuery("#"+lay).fadeIn(fadeSpeed);
			});
		}
		return false;
	});
	jQuery(".tillbaka").click(function(){
		var lay=getCurrentPlane(jQuery("#plan a.current"));
		jQuery(".js_layer:visible").fadeOut(fadeSpeed,function() {
			jQuery("#"+lay).fadeIn(fadeSpeed);
		});
		return false;	
	});
	function getCurrentPlane(JQobj) {
		if (JQobj.hasClass("plan1")) var lay="plan1";
		else if (JQobj.hasClass("plan2")) var lay="plan2";
		else if (JQobj.hasClass("plan3")) var lay="plan3";
		return lay;
	}
	// picture slideshow at arena guide, handles many slides/different navigation - only visible ones should switch pict
	var countgallery=1;
	jQuery(".pictguide").each(function() {
		if (jQuery(this).children("img").size()>1) {
			this.id="gallery_"+countgallery;
			var countpics=0;
			jQuery(this).children("img").each(function() {
				jQuery(this).css("position","absolute").css("display","none");
				this.id="pic_"+countpics;
				countpics++;
			});
			jQuery(this).children("img:first").css("display","block");
			setInterval("showNextId('"+this.id+"')",5000);
			countgallery++;
		}
	});
	// picture slideshow, actually - there can be only one of these, so classnames is not recommended
	pictureSlideShow('.pictsubsite',5000,1000);
	pictureSlideShow('.pictsubsite_small',5000,1000);
	pictureSlideShow('.pictstartpage',5000,1000);
	if (navigator.userAgent.indexOf("MSIE")!=-1) { // this one contains png - so it can't be faded in IE6
		// we need to hide img-s from ie6:
		//jQuery("#js_bubble1").children("img").not("img:first").css("display","none");
		// another approach
		pictureSlideShow("#js_bubble1",7000,5000,true);
	} else { 
		pictureSlideShow("#js_bubble1",7000,5000);
	}
	// öh - this is the scroll thingie on the start page. wierd stuff.		
	jQuery(".js_flub_outer").flub({
		topnav : "#gradient_top",
		botnav : "#gradient_bot",
		animspeed : 2000,
		easingout : "easeOutExpo",
		easing : "easeInOutSine",
		url : "../besokare/"
	});
});
// simple slideshow
/*			
	nod=node that jQuery can find (css), 
	t=time in milliseconds between picture-change, 
	at=animation of fade time 
*/
function pictureSlideShow(nod,t,at,simple) { 
	if (typeof(at)=="undefined") at=1000;
	jQuery(nod).each(function() {
		if (jQuery(this).children("img").size()>1) {
			jQuery(this).children("img").css("position","absolute").css("display","block").not("img:first").css("display","none");
			if (simple==true) {
				setInterval("showNextSimple('"+nod+"',"+at+")",t);
			} else {
				setInterval("showNext('"+nod+"',"+at+")",t);
			}
		}
		//break; // first is created, the rest is ignored.
	});	
} 
var ind=new Array(); // index of current place in slideshow
function showNext(container,at) {
	if (typeof(ind[container])=="undefined") ind[container]=0;
	if (jQuery(container+" img").size()>ind[container]+1) ind[container]++;
	else ind[container]=0;
	jQuery(container+" img:visible").fadeOut(at);
	jQuery(container+" img:eq("+ind[container]+")").fadeIn(at);
}
function showNextSimple(container,at) {
	if (typeof(ind[container])=="undefined") ind[container]=0;
	if (jQuery(container+" img").size()>ind[container]+1) ind[container]++;
	else ind[container]=0;
	jQuery(container+" img:visible").hide();
	jQuery(container+" img:eq("+ind[container]+")").show();
}
// end simple slideshow

// picture slideshow at arena guide
function showNextId(ID) {
//	var ID=jQuery(".pictguide img:visible")[0].id;
	if (jQuery("#"+ID).parents(".js_layer:visible").size()>0) {
		var i=parseInt(jQuery("#"+ID+" img:visible")[0].id.replace("pic_",""))+1;
		if (i>=jQuery("#"+ID).children("img").size()) i=0;
		//console.debug(ID + " " +i);
		jQuery("#"+ID+" img:visible").fadeOut();
		jQuery("#"+ID+" img:eq("+i+")").fadeIn();
	}
}

// check size chosen by user and changes
function fixTextSize() {
	var ts=window.name;
	if (ts && ts=="big") {
		jQuery("body").css("font-size","85%");
		jQuery(".textsize").html("Mindre text");
	} else {
		jQuery("body").css("font-size","70%");
		jQuery(".textsize").html("St&ouml;rre text");
	}
}

/* 
	plugin: show/hide
	(c) 2008- Elin Tjerngren, Artopod
html example: 
	<div class="js_shownext"><a href="#"></a></div>
	<div class="js_showhidearea"></div>
	(  class="open" is added to "a" tag if not overridden )
	js:
	jQuery(".js_shownext").showHide();
	note: method findobj overrides property area
*/
(function(){
jQuery.fn.showHide = function(opt) {
	opt = jQuery.extend({
		accordion : false,
		area : ".js_showhidearea",
		openselector : "a",
		openclass : "open", 
		eventtype : "click",
		effect : "slide",
		defaultstate : { "display" : "none" }, // css values
		findobj : function(obj) { 
				return jQuery(obj).next(opt.area); 
			}
	}, opt);
	var $obj=this;
	return $obj.each(function() {
		var el=opt.findobj(this);
		var $h=el.get(0).offsetHeight;
		el.not("."+opt.openclass).css( opt.defaultstate ); // class="open" also overrides the default state...hm thats wierd maybe.
		jQuery(this).bind(opt.eventtype, function(e) {
			if (opt.effect=="slide") {
				el.slideToggle();
				if (opt.accordion) jQuery(opt.area).not(el).slideUp();
			} else if (opt.effect=="none") {
				el.toggle(); 
				if (opt.accordion) jQuery(opt.area).not(el).hide();
			}
			jQuery(this).children(opt.openselector).toggleClass(opt.openclass);
			return false;
		});
	});
};
})();
/* 
	plugin: flub
	(c) 2008- Elin Tjerngren, Artopod
	don't ask
*/
(function(){
jQuery.fn.flub = function(opt) {
	opt = jQuery.extend({
		innerobj : ".js_flub",
		url : "#",
		animspeed : 2000,
		autoscroll : false, // overrides easing and more
		dividefact : 4,
		topnav : false,
		botnav : false,
		easing : "linear",
		easingout : "linear",
		findobj : function(obj) { 
				return jQuery(obj).children(opt.innerobj); 
			}
	}, opt);
	return this.each(function() {
		var $outer=jQuery(this);
		//$outer.click(function() { location.href=opt.url; })
		var $inner=opt.findobj(this);
		var innerh=$inner.get(0).offsetHeight;
		var outerh=$outer.get(0).offsetHeight;
		var lastpos=$inner.get(0).offsetTop;
		setInterval(function() {
			lastpos=$inner.get(0).offsetTop;
		},500);
		$inner.animate({ "top" : "0px" },opt.animspeed,opt.easingout);
		$inner.css({
			"position":"absolute"
		});
		var h=parseInt(outerh/opt.dividefact);
		var w=$outer.get(0).offsetWidth;
		var intrvl=false;
		if (innerh>outerh){
			if (opt.autoscroll==false) {
				var stoppoint=(parseInt(outerh)-parseInt(innerh));   // when using botnav, otherwise 0
				// bottom area
				if (opt.botnav) {
					$botnav=jQuery(opt.botnav);
				} else {
					$botnav=$outer.append('<div></div>').children("div:last").css({ 
						"position" : "absolute",
						"width" : w+"px",
						"height" : h+"px",
						"top" : h*(opt.dividefact-1)+"px",
						"z-index" : 6
					});
				}
				$botnav.hover(function() {
					if ($inner.get(0).offsetTop>stoppoint) {
						var diff=($inner.get(0).offsetTop-stoppoint); // distance left
						var speed=parseInt(opt.animspeed*diff/outerh);
						$inner.stop().animate({ "top" : stoppoint+"px" },speed,opt.easing);
					}
				},function(){
					// onmouseout
					var curpos=$inner.get(0).offsetTop;
					var stoppos=curpos-(lastpos-curpos);
					if (stoppos<stoppoint) stoppos=stoppoint;
					var speed=parseInt(opt.animspeed*($inner.get(0).offsetTop-stoppos)/outerh);
					$inner.stop().flubOut(stoppos,speed,opt.easingout);
				});
				// upper area
				if (opt.topnav) {
					$topnav=jQuery(opt.topnav);
				} else {
					$topnav=$outer.append('<div></div>').children("div:last").css({ 
						"position" : "absolute",
						"width" : w+"px",
						"height" : h+"px",
						"top" : "0",
						"z-index" : 6
					});
				}
				$topnav.hover(function() {
					if ($inner.get(0).offsetTop<0) {
						var diff=(0-$inner.get(0).offsetTop);
						var speed=parseInt(opt.animspeed*diff/outerh);
						$inner.stop().animate({ "top" : "0px" },speed,opt.easing);
					}
				},function(){
					var curpos=parseInt($inner.css("top"));
					var stoppos=curpos+(curpos-lastpos);
					if (stoppos>0) stoppos=0;
					var speed=parseInt(opt.animspeed*(stoppos-$inner.get(0).offsetTop)/outerh);
					$inner.stop().flubOut(stoppos,speed,opt.easingout);
				});
			} else { // autoscroll, linear
				var dir="up";
				var speed=1;// guessing 1 pixel at the time is optimal
				var int=parseInt(opt.animspeed/(innerh-outerh)*speed);
				//console.debug(int);
				setInterval(function(e) {
					var cur=$inner.get(0).offsetTop;
					var stoppoint=(parseInt(outerh)-parseInt(innerh));
					if (cur <= stoppoint) {
						dir="down";
					} else if (cur > 0) {
						dir="up";
					}
					if (dir=="up") cur-=speed;
					else cur+=speed;
					$inner.css("top",cur+"px");
					},
					int);
			}
		} 
	});
}
	jQuery.fn.flubOut = function(stoppos,t,e) {
		return jQuery(this).animate({ "top" : stoppos + "px" }, t, e);
	};
})();

