var sites = $H({ttela: 'start', bohuslaningen: 'nyheter', nlt: 'startsidan',hn: 'nyheter', hallandsposten: 'nyheter',stromstadstidning: 'nyheter'});


function getStartDepartmentName(){
	var hostName = window.location.host.substring(0, window.location.host.indexOf('.'));
	return sites.get(hostName)
	
}
window.history.back = function(){
	if (typeof fmcn !== 'undefined') {
		var ac = Settings.scope.select('#clickCount')[0];
		if (ac) {
			var c = Cookie.getC(fmcn);
			if (c !== null) {
				var value = (c === 'false' ? 0 : c);
				if (parseInt(value) > 0) {
					value;
				}
				ac.update(value);
			}
			
		}
	}
	return true;
};

var FamiljeLivVignette = (function() {
    var A;
	
    return {
        classes: "ac_familjeliv",
        init: function() {
            if (!this.exists()) {
                return false;
            }
            $$("head")[0].insert(new Element("link", {
                href: "http://korr.adeprimo.se/familjeliv/CrossPublish/html/pres.css",
                rel: "stylesheet",
                media: "screen"
            }));
            var C = new Element("a", {
                id: "byFamiljeLiv",
                href: "http://familjeliv.se",
                target: '_blank'
            }).update("<span></span>Fr\u00E5n familjeliv.se");
            var B = A.select("div.articleFloatContainer");
            if (B.length == 0) {
                var D = new Element("div").addClassName("articleFloatContainer").insert(C);
                A.select(".body")[0].insert({
                    before: D
                });
            } else {
                B.first().insert({
                    top: C
                });
            }
        },
        exists: function() {
            A = $("articleContent");
            if (!A) {
                return false;
            }
            var C = A.select("span.articleCategory");
            if (C.length == 0) {
                return false;
            }
            if (!this.classes || this.classes == "") {
                return false;
            }
            var B = C[0].className.replace(/^\s*/, "").replace(/\s*$/, "").split(" ");
            var F = false;
            for (var E = 0, D = B.length; E < D; E++) {
                if (B[E].match(this.classes) && B[E] != "") {
                    F = true;
                    break;
                }
            }
            return F;
        }
    };
})();


document.observe("dom:loaded", function() {
	FamiljeLivVignette.init();
	
	// Ta bort versionering av bilder pga bildrensningen
	(function() {
		var imgSrc = null;
		var match = null;
		var regExp = /\/image_processor\/1\.\d+(\.\d+)/;
		
		$$("#blog .body img").each(function(imgElement) {
			
			imgSrc = imgElement.getAttribute("src");
			
			if (imgSrc != null) {
				
				match = regExp.exec(imgSrc);
	
				if (match != null) {
					imgSrc = imgSrc.replace(match[1], "");
					imgElement.setAttribute("src", imgSrc);
				}			
			}
		});
	})();
});





	
//// PhotoAlbum ////
// Fallback for IE7, ADEPRIMO-3252
PhotoAlbum.ModalSlideShow.crossfade = function(imageData, item) {
	
	// Change: Begin with item and select
	var ref = item.select('a.photoLink').first();
	var imgUrl = this.getNodeValue(imageData, 'url', item);
	var imgQuerystring = imgUrl.substring(imgUrl.indexOf('?'));
	var maxPixels = imgQuerystring.match(/\d\d*/g);
	var maxHeight = maxPixels[0];
	var maxWidth = maxPixels[1];
	
	var isIE7mode = ref.innerHTML.toLowerCase().indexOf("div") === -1;
	
	if (isIE7mode) {
		var imgElm = item.select("img")[0];
		imgElm.src = imgUrl;
		// Change: Set width/height
		imgElm.width = maxWidth;
		imgElm.height = maxHeight;
	} else {
	
		ref.insert({
			top: new Element('img', {
				src: imgUrl,
				width: maxWidth,
				height: maxHeight
			}).wrap('div')
		});
		
		PhotoAlbum.ModalSlideShow.imageElement(item).parentNode.fade({
			afterFinish: function(effect) {
				effect.element.remove();
			}
		});
	}
};



ModalWindow.initLoader = function(afterFinishFunc) {
	var os = document.viewport.getScrollOffsets();
	var c = this;

	// this.overlay.getStyle('display') == 'none' ||
	// this.overlay.getStyle('display') == null
	if(!this.overlay.style.display || this.overlay.style.display != "block") {
		var hA = document.viewport.getHeight();
		
		// Change: Get correct browser scroll-height //
		var hB = getDocHeight();
		// END //
		this.overlay.setStyle({ display:"block",opacity:0,height:((hB > hA) ? hB : hA)+"px" });
		
		this.overlay.setOpacity(0.7);
	}
	this.loader.setStyle({ 
			display:"block",
			marginTop:((c.loader.getHeight()/2) - os.top)*(-1)+"px",
			marginLeft:((c.loader.getWidth()/2) - os.left)*(-1)+"px"
		});
		
	if (typeof afterFinishFunc == "function") {
		c.wrapper.setOpacity(0);
		afterFinishFunc();
	}
	this.wrapper.className = "";
	
	// Change: New internal func //
	function getDocHeight() {
		var D = document;
		return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		);
	}
	// END //
};


	


ModalWindow.shrinkWindow = function(renderCallFunc) {
	// what should we do?
	// shrink loader to the correct size, fade out and fade in content.		
	var c = this;

	// Change: Quirk mode fix. If not width/height has been correcly set, then fetch values from modalWindow-element //
	if (parseInt(c.width) < 10) {
		
		c.loader.setStyle({
			opacity: 0
		});
		
		var modalWin = document.getElementById("modalWindowInner");
		c.height = modalWin.scrollHeight;
		c.width = modalWin.scrollWidth;
		c.left = "-" + parseInt(c.width/2 ,10) + "px";
		c.top = "0px";
	}
	
	// END //


	var newX, newY;
	newY = parseInt((parseInt(c.height,10) - c.loader.myOffset.height + c.wrapper.myOffset.height),10);
	newX = parseInt((parseInt(c.width,10) - c.loader.myOffset.width + c.wrapper.myOffset.width),10);

	if (newX < 0) {
		newX = 0;
	}

	if (newY < 0) {
		newY = 0;
	}

	newX = newX + "px";
	newY = newY + "px";


	this.currentAnimation = new Effect.Morph(this.loader, {
		style: {
			width: newX,
			height:  newY,
			marginTop: c.top,
			marginLeft: c.left
		},
		duration: this.time.durationShrink,
		afterFinish: function(){
			// Change: Always do this //
			//if (!c.loader.style.display==='') {
				c.wrapper.setStyle({
					width: c.width,
					height: c.height,
					marginTop: c.top,
					marginLeft: c.left,
					display: "block",
					opacity: 0
				});
				c.inner.setStyle({
					width: c.width,
					height: c.height
				});
				new Effect.Opacity(c.wrapper, {
					from: 0,
					to: 1,
					fps: c.time.fpsOverlay,
					duration: c.time.durationOverlay,
					afterFinish: function(){
						c.loader.setStyle({
							display:''
						});
						renderCallFunc();
					}
				});
			//}
		}
	});
};

(function() {
	var modalWindowOverlay = $("modalWindowOverlay");
	
	if (modalWindowOverlay) {
		PhotoAlbum.ModalSlideShow.getMaxHeight = function(item){
			// Change: Hardcoded maxHeight
			return 480;
		};
	}
})();



PhotoAlbum.ModalSlideShow.init = function() {
	if (!this.select('.photoWrapper').length) return;
	
	// Change: Revert of position
	var modalWindowOverlay = $("modalWindowOverlay");
	if (modalWindowOverlay) {
		modalWindowOverlay.setStyle({ position:"absolute" });
	}
	// END //
	
	this.resetPhotoAlbums();
	this.select('.photoWrapper').each(function(item, index){
		var nextLink = item.select('.simplePager .next a').first();
	    var previousLink = item.select('.simplePager .previous a').first();
	    var autoplayLink = PhotoAlbum.ModalSlideShow.autoplayLink(item);
	    var photoLink = item.select('a.photoLink').first();
	    nextLink && nextLink.observe('click', PhotoAlbum.ModalSlideShow.stepAhead.bindAsEventListener(item, index));
	    
		// Change: new variables for easier reading
	    var imgElement = PhotoAlbum.ModalSlideShow.imageElement(item);
	    var maxHeight = PhotoAlbum.ModalSlideShow.getMaxHeight(item) || 460;
	    var maxWidth = PhotoAlbum.ModalSlideShow.getMaxWidth(item) || 650;
	   	
		previousLink && previousLink.observe('click', PhotoAlbum.ModalSlideShow.stepBack.bindAsEventListener(item, index));
	    
	    if (photoLink) {
	      photoLink.observe('click', PhotoAlbum.ModalSlideShow.stepAhead.bindAsEventListener(item, index));
	      var div = imgElement.up('div');
		  if(div && (div.getAttribute('class') || div.getAttribute('id'))){
		  	imgElement.wrap('div');
		  }
	      photoLink.setStyle({ 'width': maxWidth + 'px', 'height': maxHeight + 'px' });
	    }
	    
		
		// Change: Change first image size
	    var imgElementSrc = imgElement.getAttribute("src");
	    imgElementSrc = imgElementSrc.replace(/maxHeight=\d+&maxWidth=\d+/, "maxHeight=" + maxHeight);
	    imgElement.setAttribute("src", imgElementSrc);
		imgElement.removeAttribute("width");
		imgElement.removeAttribute("height");
		// END //
	    
	    if (autoplayLink) {
	      autoplayLink.observe('click', PhotoAlbum.ModalSlideShow.toggleAutoplay.bindAsEventListener(item, index));
		  autoplayLink.insert(new Element('span')); 
	    }
	    
	    [nextLink, previousLink, autoplayLink, photoLink].each(function(link) {
	      link && link.setAttribute('rel', 'enhanced');
	    });
	    
		var parent = item.up(0);
		if(parent.hasClassName('modalWindow')){
			document.observe('mkt:modal:close', function(event){
				PhotoAlbum.ModalSlideShow.close(event,item, index);
			});
		}
	  
	    PhotoAlbum.ModalSlideShow.initializeImages(item, index);		
		
	});
};

/*
(function() {

	if (typeof jQuery === "undefined") {
		return false;
	}
	
	jQuery(document).ready(function($) {

		removeAutocomplete();
		
		var searchForm = $("#fw-searchForm");
		
		if (searchForm) {
			searchForm.submit(function() {
				setTimeout(removeAutocomplete, 100);
			});
		}
	});
	
	function removeAutocomplete() {
	
		(function($) {
			$$("#searchField").each(function(item) {
			
				var elmInput = $(item)[0];
				
				try {
					$(elmInput).autocomplete({ disabled: true })
				}
				catch(ignore) {
				}
			});
		})(jQuery);
	}
})();
*/

//MM.searchClient.relatedSearch = function(params, fn) {};
