(function() {
	var $ = jQuery;
	$(document).ready(function() {
		// Make enter key on input elements fire the click event of the closest submit button.
		$('input,select,textarea,fieldset').live('keypress', function(e) {
			if (e.which != 13) return true;
			if (this.tagName && this.tagName == 'TEXTAREA') return true;

			var root = $(this);
			while (root.length > 0) {
				var button = $(":submit,:button", root);
				if (button.length > 0) {
					button[0].click();
					return false;
				}
				root = root.parent();
			}
		});

		//Arrows
		$('.general-portlet a').arrowAfter();
		$('#navigation .middle ul li a').arrowAfter();
		$('.rootLink a').arrowBefore();
		$('.faqCategory li a').arrowAfter();

		$("#main-nav ul li:last").addClass('last');

		$('form.noscript').removeClass('noscript');

		// start - toggle page hidden heading area
		$(".brandName").hide();
		$('ul li a').bind("mouseenter", function() {
			$(this).children(".brandName").slideDown(300);
		}).bind("mouseleave", function() {
			$(this).children(".brandName").slideUp(300);
		});

		/* toggles the content div */
		$(".newHiddenArea").hide();
		$(".mainHiddenArea a.readmore").click(function() {
			$(".newHiddenArea").slideToggle();
			if ($("#contentArea").hasClass("up")) {
				$("#contentArea").removeClass("up");
			} else {
				$("#contentArea").addClass("up");
			}
			return false;
		});
		// end - function for the page hidden heading area

		/* faq */
		$(".faqAnswer").hide();
		$(".faqItem a.faqQuestion").click(function() {
			if ($(this).hasClass("minus")) {
				$(this).siblings(".faqAnswer").slideUp("slow");
				$(this).removeClass("minus");
			} else {
				$(this).siblings(".faqAnswer").slideDown("slow");
				$(this).addClass("minus");
			}
			return false;
		});

		/* validate */
		$("fieldset.validate button").click(function() {
			$("#aspnetForm").validate();
		});

		// Animate the anchor links
		$('a[href*=#]:not(a[href=#])').click(function() {
			if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({ scrollTop: targetOffset }, 600, "swing");
					return false;
				}
			}

			return false;
		});

		//Puts width on image wrappers that have image and text
		$(".rightImage img").each(function() {
			var imageWidth = $(this).width();
			$(this).parent(".rightImage").css("width", "" + imageWidth + "px");
		});
		$(".leftImage img").each(function() {
			var imageWidth = $(this).width();
			$(this).parent(".leftImage").css("width", "" + imageWidth + "px");
		});

		// Setup slideshows
		$(".x-portlet ul.slideshow").each(function() {
			$(this)
                .children('li').addClass('hide')
                .children(':not(.colorbox)').hide();
			$(this).children('li:first').addClass('first-item');
			var title = $(this).children('li:first').children('.colorbox').attr("title");
			var number = $(this).children('li').size();


			$(this).parent('.x-portlet').append("<div class='imageNumber'>Bild 1/" + number + "</div>");
			$(this).parent('.x-portlet').append("<div class='imageSlideshowText'>" + title + "</div>");

		});

		$('ul.slideshow .colorbox').colorbox({
			maxWidth: "80%",
			current: "Bild {current} av {total}",
			title: function() {
				var text = "";
				$(this).siblings().each(function() {
					text += "<" + this.nodeName + ">" + $(this).html() + "</" + this.nodeName + ">";
				});
				return text;
			}
		});

		//IE6
		if ($.browser.msie && $.browser.version == "6.0") {
			$("body").addClass("ieSix");
			$("body").append("<div class='iesixDiv'>Du anv&auml;nder en webbl&auml;sare som denna sida ej &auml;r utvecklad f&ouml;r, vi ber er d&auml;rf&ouml;r anv&auml;nda en senare <a href='http://www.microsoft.com/downloads/details.aspx?FamilyId=9AE91EBE-3385-447C-8A30-081805B2F90B&displaylang=en'>version</a> av denna webbl&auml;sare eller en annan typ av <a href='http://www.mozilla.com/sv-SE/'>webbl&auml;sare</a></div>");
		}

		$(".movieList ul li:odd").addClass('odd');

		// Calendar
		if ($(".calendarView").length == 0) {
			$(".dynamicCalendar .gradient").hide();
		} else {
			$(".dynamicCalendar .less").hide();
			$(".listView").hide();

			$(".dynamicCalendar a.more").click(function() {
				$(this).hide();
				$(this).siblings(".less").show();
				$(this).parent(".gradient").siblings(".listView").slideDown("slow");
			});
			$(".dynamicCalendar a.less").click(function() {
				$(this).hide();
				$(this).siblings(".more").show();
				$(this).parent(".gradient").siblings(".listView").slideUp("slow");
			});
		}

		$(".dynamicCalendar td a.events").each(function() {
			$(this).parent("td").addClass("eventWrapper");
		});

		// Flash install message on top
		if (!FlashDetect.installed || !FlashDetect.versionAtLeast(7)) {
			$("#noflash").addClass("active");
		}

		// Fix external links
		externalLinks();
	});

	var externalLinks = function() {
		var anchors = document.getElementsByTagName("a");

		for (var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			var href = anchor.getAttribute("href");
			if (href != null) {
				if (IsIE()) {
					AddMailtoClass(anchor, href);
				}
				var rel = anchor.getAttribute("rel");
				var email = IsEmailLink(href);
				var file = IsFileLink(href, rel);
				var external = IsExternalLink(href, rel);
				if (!email && (external || file)) {
					if (anchor.target != null) {
						anchor.target = "_blank";
						if (IsIE()) {
							if (file) {
								anchor.className += " file";
								AddExtensionClass(anchor, href);
							} else {
								anchor.className += " external";
							}
						}
					}
				}
			}
		}
	};

	var IsIE = function() {
		var agt = navigator.userAgent.toLowerCase();
		return ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	};

	var AddMailtoClass = function(anchor, href) {
		if (IsEmailLink(href))
			anchor.className += " mailto";
	};

	this.AddExtensionClass = function(anchor, href) {
		var index = href.lastIndexOf(".");
		if (index > 0) {
			anchor.className += " ext-" + href.slice(index + 1);
		}
	};

	var IsEmailLink = function(href) {
		return href.indexOf("mailto:") == 0;
	};

	var IsFileLink = function(href, rel) {
		var extension = "";
		var dotIndex = href.lastIndexOf(".");
		var questionIndex = href.indexOf("?");
		if (dotIndex > 0 && questionIndex < 0) {
			extension = href.slice(dotIndex + 1);
			var slashIndex = extension.indexOf("/");
			if (slashIndex > 0) {
				extension = "";
			}
			var hashIndex = extension.indexOf("#");
			if (hashIndex > 0) {
				extension = extension.substring(0, hashIndex);
			}
		}
		return (rel == "file" || (extension != "" && extension != "htm" && extension != "html" && extension != "aspx" && extension != "asp" && extension != "jsp" && extension != "php"));
	};

	var IsExternalLink = function(href, rel) {
		if (document.location.pathname != null) {
			var host = document.location.href.substring(0, document.location.href.indexOf(document.location.pathname));
			if (href.indexOf(host) == 0) {
				return false;
			}
		}
		var s = href.substring(0, 1);
		return (rel == "external" || (s != "/" && s != "#"));
	}

	$.fn.arrowBefore = function() {
		if (this.size() > 0) {
			this.each(function() {
				$(this).prepend("<span class='arrow'>&laquo;&nbsp;</span>");
			});
		}
	};
	$.fn.arrowAfter = function() {
		if (this.size() > 0) {
			this.each(function() {
				$(this).append("<span class='arrow'>&nbsp;&raquo;</span>");
			});
		}
	};

	var FlashDetect = new function() {
		var self = this; self.installed = false; self.raw = ""; self.major = -1; self.minor = -1; self.revision = -1; self.revisionStr = ""; var activeXDetectRules = [{ "name": "ShockwaveFlash.ShockwaveFlash.7", "version": function(obj) { return getActiveXVersion(obj); } }, { "name": "ShockwaveFlash.ShockwaveFlash.6", "version": function(obj) {
			var version = "6,0,21"; try { obj.AllowScriptAccess = "always"; version = getActiveXVersion(obj); } catch (err) { }
			return version;
		}
		}, { "name": "ShockwaveFlash.ShockwaveFlash", "version": function(obj) { return getActiveXVersion(obj); } }]; var getActiveXVersion = function(activeXObj) {
			var version = -1; try { version = activeXObj.GetVariable("$version"); } catch (err) { }
			return version;
		}; var getActiveXObject = function(name) {
			var obj = -1; try { obj = new ActiveXObject(name); } catch (err) { obj = { activeXError: true }; }
			return obj;
		}; var parseActiveXVersion = function(str) { var versionArray = str.split(","); return { "raw": str, "major": parseInt(versionArray[0].split(" ")[1], 10), "minor": parseInt(versionArray[1], 10), "revision": parseInt(versionArray[2], 10), "revisionStr": versionArray[2] }; }; var parseStandardVersion = function(str) { var descParts = str.split(/ +/); var majorMinor = descParts[2].split(/\./); var revisionStr = descParts[3]; return { "raw": str, "major": parseInt(majorMinor[0], 10), "minor": parseInt(majorMinor[1], 10), "revisionStr": revisionStr, "revision": parseRevisionStrToInt(revisionStr) }; }; var parseRevisionStrToInt = function(str) { return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision; }; self.majorAtLeast = function(version) { return self.major >= version; }; self.minorAtLeast = function(version) { return self.minor >= version; }; self.revisionAtLeast = function(version) { return self.revision >= version; }; self.versionAtLeast = function(major) { var properties = [self.major, self.minor, self.revision]; var len = Math.min(properties.length, arguments.length); for (i = 0; i < len; i++) { if (properties[i] >= arguments[i]) { if (i + 1 < len && properties[i] == arguments[i]) { continue; } else { return true; } } else { return false; } } }; self.FlashDetect = function() { if (navigator.plugins && navigator.plugins.length > 0) { var type = 'application/x-shockwave-flash'; var mimeTypes = navigator.mimeTypes; if (mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description) { var version = mimeTypes[type].enabledPlugin.description; var versionObj = parseStandardVersion(version); self.raw = versionObj.raw; self.major = versionObj.major; self.minor = versionObj.minor; self.revisionStr = versionObj.revisionStr; self.revision = versionObj.revision; self.installed = true; } } else if (navigator.appVersion.indexOf("Mac") == -1 && window.execScript) { var version = -1; for (var i = 0; i < activeXDetectRules.length && version == -1; i++) { var obj = getActiveXObject(activeXDetectRules[i].name); if (!obj.activeXError) { self.installed = true; version = activeXDetectRules[i].version(obj); if (version != -1) { var versionObj = parseActiveXVersion(version); self.raw = versionObj.raw; self.major = versionObj.major; self.minor = versionObj.minor; self.revision = versionObj.revision; self.revisionStr = versionObj.revisionStr; } } } } } ();
	}; FlashDetect.JS_RELEASE = "1.0.4";
})();

var XD = function() {
    var interval_id,
        last_hash,
        cache_bust = 1,
        attached_callback,
        window = this;

    return {
        postMessage: function(message, target_url, target) {
            if (!target_url) {
                return;
            }

            target = target || parent;  // default to parent

            if (window['postMessage']) {
                // the browser supports window.postMessage, so call it with a targetOrigin
                // set appropriately, based on the target_url parameter.
                target['postMessage'](message, target_url.replace(/([^:]+:\/\/[^\/]+).*/, '$1'));

            } else if (target_url) {
                // the browser does not support window.postMessage, so set the location
                // of the target to target_url#message. A bit ugly, but it works! A cache
                // bust parameter is added to ensure that repeat messages trigger the callback.
                target.location = target_url.replace(/#.*$/, '') + '#' + (+new Date) + (cache_bust++) + '&' + message;
            }
        },

        receiveMessage: function(callback, source_origin) {
            // browser supports window.postMessage
            if (window['postMessage']) {
                // bind the callback to the actual event associated with window.postMessage
                if (callback) {
                    attached_callback = function(e) {
                        if ((typeof source_origin === 'string' && e.origin !== source_origin)
                        || (toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                            return !1;
                        }
                        callback(e);
                    };
                }
                if (window['addEventListener']) {
                    window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                } else {
                    window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                }
            } else {
                // a polling loop is started & callback is called whenever the location.hash changes
                interval_id && clearInterval(interval_id);
                interval_id = null;

                if (callback) {
                    interval_id = setInterval(function() {
                        var hash = document.location.hash,
                        re = /^#?\d+&/;
                        if (hash !== last_hash && re.test(hash)) {
                            last_hash = hash;
                            callback({ data: hash.replace(re, '') });
                        }
                    }, 100);
                }
            }
        }
    };
   } ();

   function openReseplanerare() {
   	if ($('#hiddenLink').length > 0) {
   		var url = $('#hiddenLink').val();
   		if ($('#ctl00_ctl00_bodyRegion_pageRegion_colPortlet2_ctl01_ctl03_startLocation').val() === "") return;
   		url += $('#ctl00_ctl00_bodyRegion_pageRegion_colPortlet2_ctl01_ctl03_startLocation').val();
   		window.open(url);
   	}
   }