/*
if (typeof OAS_RICH === "undefined") {
	function OAS_RICH(ignore) {};
}

if (typeof OA_show === "undefined") {
	function OA_show(ignore) {};
}

if (typeof OAS_AD === "undefined") {
	function OAS_AD(ignore) {};
}

if (typeof initClickHeat === "undefined") {
	function initClickHeat() {};
}

if (typeof citygate_simpleSearch === "undefined") {
	function citygate_simpleSearch() {};
}

if (typeof Admeta === "undefined") {
	function Admeta() {};
}
*/

// Kommentars-buggen
CommentCitation.init = function() {
	var commentForm = $('commentForm');
	
	if (commentForm) {
		Element.observe(commentForm, 'submit', function(event) {
			if ($$("#bodyQuote[name='bodyQuote']").length === 0) {
				commentForm.insert("<input type='hidden' name='bodyQuote' value='' />");
			}
		});
	}
	
	
	
  CommentCitation.scrollToMessage();
  if (!CommentCitation.exists()) {
  $$('div.posting_reply').each(function(each) { 
	each.hide();
	each.style.overflow = 'hidden';
	each.style.height = '0px';
  });
	return;
  }

var x;
if (x = $('bodyQuote')) { x.hide(); }
if (x = $('bodyQuoteLabel')) { x.hide(); }

	$$('div.posting_reply a').each(function(link) {
		link.observe('click', function(event) {
			CommentCitation.citeComment(link);
			Event.stop(event);
		});
	});
	
	if (document.location.hash == '#commentForm') {
		this.focusBody();
	}
};



document.observe("dom:loaded", function(){
	
	// 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);
				}			
			}
		});
		
		
		// Fix för paginering
		var links = $$(".extraCommentsCommentsWrapper .scrollerlinks a");
		var newHref;
		
		links.each(function(item) {
			newHref = item.getAttribute("href");
			if (!newHref.match(/article_full_discussion/)) {
				newHref = newHref + "&articleRenderMode=article_full_discussion";
				item.setAttribute("href", newHref);
			}
		});
		
	})();
	
});


/**
 * 
 * Buggfix för MKtWebbFB i 1.13.1 (ADEPRIMO-3562), kan tas bort i release 1.14.0)
 * 
 */

if(typeof MktWebbFB !== 'undefined'){
	MktWebbFB.publishComment = function(frm){
		if (MktWebbFB.sessionUid > 0) {
			eval("props = {'" + MktWebbFB.Comment.props1 + "':{'text':'" + MktWebbFB.Comment.props2 + "','href':'" + location.href + "'}};");
			var body = $('body');
			var bodyText = "";
			if (body) {
				bodyText = body.value.replace(/\n/g, "");
				MktWebbFB.Comment.message = bodyText;
			}
			//Use popup for security reasons
			FB.ui({
				method: 'feed',
				display: (isIE ? 'iframe' : 'popup'),
				message: MktWebbFB.Comment.message,
				name: MktWebbFB.Comment.name,
				link: MktWebbFB.Comment.link,
				picture: MktWebbFB.Comment.picture,
				caption: MktWebbFB.Comment.caption,
				description: MktWebbFB.Comment.description,
				properties: props
			
			}, function(response){
				var ok = response && response.post_id ? true : false;
				var aborted = ok === true ? 'false' : 'true';
				var hiddenAborted = new Element('input', {
					type: 'hidden',
					name: 'fb-aborted',
					id: 'fb-aborted',
					value: aborted
				});
				var hiddenSessionUid = new Element('input', {
					type: 'hidden',
					name: 'fb-sessionUID',
					id: 'fb-sessionUID',
					value: MktWebbFB.sessionUid
				});
				frm.insert(hiddenSessionUid);
				frm.insert(hiddenAborted);
				frm.submit();
			});
		}
		else {
			frm.submit();
		}
	}
}



// Findwise-fixar för IE (9), ADEPRIMO-3537
// Med i 1.15 
MM.searchClient = (function($J){
    function log( message ) {
        var console = window['console'];
        if (console && console.log) {
            console.log(message);
        }
    }

    function time( message, fn, params ) {
        var start = new Date().getTime();
        if ( $J.isArray( params) ) {
            fn.apply( this, params );
        } else {
            fn.call( this, params );
        }
        var end = new Date().getTime();
        log ( message + ": " + (end - start) + " ms" );
    }

    function requestData( url, ident, profile, params, fn, debugEnabled ) {
        var startTime = new Date().getTime();
        if ( profile !== null && profile !== "" ) {
            $J.ajax({
                type: "GET",
                dataType:       ( MM.cfg.crossDomain ? "jsonp" : "json" ),
                url:            url + "/" + ident + "/" + profile + "/search.json",
                data:           params,
                success: function( json ) {
                    if ( fn && json !== null) {
                        if ( debugEnabled ) {
                            // Log debug messages to console
                            log ( "QT: " + ((json !== null) ? json.searchTimeInMillis + " ms" : " N/A ") );
                            log ( "RTT: " + (new Date().getTime() - startTime) + " ms" );
                            time ( "Result rendering", fn, json );
                        } else {
                            fn ( json );
                        }
                    }
                },
                complete : function( XMLHttpRequest, textStatus ){
                    //MM.util.showLoad( false );
                }
            });
        }
    }

     return {

        simpleSearch: function( term, fn ) {
           requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.main_search_profile, "q=" + encodeURIComponent(term), fn );
        },

        search: function( params, fn ) {
			requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.main_search_profile, MM.searchClient.encodeQuery(params), fn );
        },

        relatedSearch: function( params, fn ) {
            requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.related_search_profile, MM.searchClient.encodeQuery(params), fn );
        },

        backfillSearch: function( params, fn ) {
            requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.backfill_search_profile, MM.searchClient.encodeQuery(params), fn );
        },
		
		// Enkodningsproblem i IE9, ADEPRIMO-3537
		encodeQuery: function(params) {
		
			var arrParams = params.split("&");
			var paramName = "";
			var paramValue = "";
			
			arrParams.each(function(elm, index) {
				var arrNameValue = elm.split("=");
		
				paramName = arrNameValue[0];
				paramValue = arrNameValue[1];
				paramValue = decodeURIComponent(paramValue);
				paramValue = encodeURIComponent(paramValue);
			
				arrParams[index] = paramName + "=" + paramValue;
			});
			
			params = arrParams.join("&");
			
			return params;
		}

    };
})(jQuery.noConflict());


ui.renderResultInfoText = function( meta,c ) {
    	var wrapper = c.select("#resultInfoText")[0];
    	if(!wrapper){
    		wrapper = c.select(".resultInfo")[0].select("span")[0];
    	}
    	wrapper.update( MM.cfg.resultInfoText.replacePlaceholders(meta) );
    };

ui.renderResultQueryTimeText = function( meta,c ) {
    	var wrapper = c.select("#resultQueryTimeText")[0];
    	if(!wrapper){
    		wrapper = c.select(".resultInfo")[0].select("span")[1];
    	}
    	wrapper.update( MM.cfg.resultQueryTimeText.replacePlaceholders(meta) );
    };
	

// Fallback for IE7, ADEPRIMO-3252
// Med i 1.15
PhotoAlbum.ModalSlideShow.crossfade = function(imageData, item) {
	
	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 = $$(".photoWrapper")[0].select("img")[0];
		imgElm.src = imgUrl;
	} 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();
			}
		});
	}
};


// IE8 uses "arguments" instead of "args"
// In 1.15
PhotoAlbum.ModalSlideShow.navigateWithArrowKeys =  function(event, item, index) {
	
	if (!args && arguments) {
		var args = arguments;
	}

	if (!args) {
		return; // EXIT
	}
	
	var item = args[1];
	if (!([37, 39].indexOf(event.keyCode) > -1)) return;
	PhotoAlbum.ModalSlideShow.stopAutoplay(item, index);
	event.keyCode == 39 ? PhotoAlbum.ModalSlideShow.stepAhead(item, index) : PhotoAlbum.ModalSlideShow.stepBack(item, index);
	Event.stop(event);
};
