/**
 * Changes the source of an image file when a thumbnail is activated.
 * @requires jQuery
 */
var mqGallery = function () {
	var config = {
		sContainerClass: 'in-page', // className of the gallery container
		sImageId: 'in-page-zoom' // id of the image to be replaced
	};
	function init(props)	{
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {
			return;
		}
		// If options were supplied, apply them to the config object.
		for (var key in props) {
			if (config.hasOwnProperty(key)) {
				config[key] = props[key];
			}
		}
		// Check that the target image exists
		var oImage = document.getElementById(config.sImageId);
		if (!oImage) {
			return;
		}
		// Find all links in the gallery ul
		$('.in-page ul a').click(function () {
			var oImg = this.getElementsByTagName('img')[0];
			var oRegExp = /(.*?)-t\.(jpg|gif|png)/
			var sSrc = oImg.src.replace(oRegExp, '$1.$2');
			oImage.src = sSrc;
			return false;
		});
	}
	return {
		init: init
	};
}();

$(document).ready(function() {
	/* Lightbox */
	var $gallery = $(".gallery > ul");
	$gallery.each(function(){
		var settings = {
			imageLoading: '/i/lightbox-ico-loading.gif',
			imageBtnClose: '/i/lightbox-btn-close.gif',
			imageBtnPrev: '/i/lightbox-btn-prev.gif',
			imageBtnNext: '/i/lightbox-btn-next.gif',
			imageBtnDownload: '/i/lightbox-btn-download.gif',
			imageBlank: '/i/blank.gif',
			txtImage: 'Bild',
			txtOf: 'av',
			txtDownload: 'Ladda ner bilden'
		}
		var self = $(this);
		if (self.find("ul").size() > 0) {
			self.find("ul").hide();
			self.children("li").each(function(){
				$(this).find("a").lightBox(settings);
			});
		} else {
			self.find("a").lightBox(settings);
		}
	});
	
	/* Hide h1 on brand pages */
	$(".layout-3 #content-primary .intro img").parents(".module-content").find("h1").addClass("structural");
	/* Temp solution for Club login.. */
	function openTarget(myForm, features, windowName) {
		myForm.target = windowName ? windowName : 'mqform' + (new Date().getTime());
		window.open('', windowName, features);
	}
	var clubForm = jQuery("form", "#club");
	clubForm.attr("onsubmit","openTarget(this,'width=520,height=600,resizable=1,scrollbars=1'); return true;").attr("target","mqclub");
	$(".openclub").click(function(){
		window.open(this.href, 'mqclub' ,'width=520,height=600,resizable=1,scrollbars=1');
		return false;
	});
	/* Hide paragraphs next to a paragraph with a class of "hasmore" */
	$(".hasmore ~ p").hide();
	$(".hasmore").after('<a href="#" class="showmore">Läs hela artikeln</a>');
	$(".showmore").click(function(){
		$(this).hide().siblings().show();
		return false;
	});
	/* Initialize custom gallery */
	mqGallery.init();
	/* Initialize prettyPhoto */
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_square'});
});