/**
 * @fileoverview Global functions
 */
/**
 * Hack to reduce background image flickering in IE 6
 */
/*@cc_on
	@if (@_jscript_version == 5.6)
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	@end
@*/

/* Create NetR namespace */
if(typeof NetR == "undefined"){ var NetR = {}; }

/**
 * Creates a link that triggers the browser's window.print function.
 */

NetR.addPrintLink = function () {
	var options = {
		targetEl: 'content-primary', // Id of the element the link is appended to
		linkText: 'Skriv ut sidan',
		linkId: 'print-link',
		prepend: false
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		var oTarget = document.getElementById(options.targetEl);
		if (!oTarget) {return;}
		if (!window.print) {return;}
		var oLink = document.createElement('a');
		oLink.id = options.linkId;
		oLink.href = '#';
		oLink.appendChild(document.createTextNode(options.linkText));
		oLink.onclick = function() {
			window.print();
			return false;
		};
		if (options.prepend) {
		    oTarget.insertBefore(oLink, oTarget.firstChild);
		} else {
		    oTarget.appendChild(oLink);
		}
	}
	return {
		init: init
	};
}();

NetR.addPrintLinkEnglish = function () {
	var options = {
		targetEl: 'content-primary', // Id of the element the link is appended to
		linkText: 'Print page',
		linkId: 'print-link',
		prepend: false
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		var oTarget = document.getElementById(options.targetEl);
		if (!oTarget) {return;}
		if (!window.print) {return;}
		var oLink = document.createElement('a');
		oLink.id = options.linkId;
		oLink.href = '#';
		oLink.appendChild(document.createTextNode(options.linkText));
		oLink.onclick = function() {
			window.print();
			return false;
		};
		if (options.prepend) {
		    oTarget.insertBefore(oLink, oTarget.firstChild);
		} else {
		    oTarget.appendChild(oLink);
		}
	}
	return {
		init: init
	};
}();

/**
 * @requires jQuery
 * Hide contact form and make it expand when a link is clicked.
 */
jQuery(function() {
	// Hide contact form
	var contactFormWrapper = $('#contact-form:not(".static")');
	var contactForm = $('#contact-form:not(".static") form:first');
	var contactHeading = $('#contact-form:not(".static") h2:first');
	if (contactForm.size() && contactHeading.size()) {
	    contactFormWrapper.addClass("active");
	    var contactLink = contactHeading.wrapInner('<a href="#"></a>');
		if (window.location.hash != '#contact-form') {
			contactForm.hide();
			contactForm.isHidden = true;
			contactFormWrapper.addClass('hidden');
		}
		contactLink.click(function (e) {
			e.preventDefault();
			if (contactForm.isHidden) {
				contactForm.animate({
					height: 'show',
					opacity: 'show'
				}, 'slow', function () {
					var targetOffset = contactForm.offset().top;
					$('html,body').animate({
						scrollTop: targetOffset
					}, 1000, function () {
						window.location.hash = '#contact-form';
					});
					contactForm.isHidden = false;
					contactFormWrapper.removeClass('hidden');
				});
			} else {
				contactForm.animate({
					height: "hide",
					opacity: "hide"
				}, 'slow', function () {
					contactForm.isHidden = true;
					contactFormWrapper.addClass('hidden');
				});
			}
		});
	}
});

// Init on document ready
$(document).ready(function() {
    NetR.addPrintLink.init({
        targetEl: 'page-footer',
        linkText: 'Skriv ut sidan',
        prepend: true
    });
NetR.addPrintLink.init({
        targetEl: 'page-footer-en',
        linkText: 'Print page',
        prepend: true
    });
/*
NetR.addTipLink.init({
        targetEl: 'page-footer',
        linkText: 'Tipsa en vän',
        prepend: true
    });
NetR.addTipLink.init({
        targetEl: 'page-footer-en',
        linkText: 'Tell a friend',
        prepend: true
    });*/

    /*if (jQuery().datepicker) {
        $('input.has-datepicker').datepicker($.extend({}, $.datepicker.regional["sv"],{
        	showOn: "both",
        	buttonText: "Välj från almanacka",
        	buttonImage: "/i/icons/calendar.gif",
        	buttonImageOnly: true
        }));
    }*/
});