function init_new_gallery(only_gallery, width){
	$("img").removeAttr("height").removeAttr("width").removeClass("size-thumbnail");
	
	if(typeof(only_gallery) == 'undefined'){
		only_gallery = false;
	}
	
	if(typeof(width) == 'undefined'){
		width = '';
	}
	
	$(".wordpress_post").each(function(){
		//window.console.log("wodpress post with " + $(this).find("img").length + " images");
		if($(this).find(".gallery").length !== 0 || only_gallery == true){
			//window.console.log("looking for gallery...");
			if($(this).find("img").length == 1){
				// one image
			}
			else{
				// regular gallery
				generate_gallery($(this), width);
				update_gallery_caption($(this).find(".slideshow"));
			}
		}
	});
	
	$(".gallery").remove();

	$(".slideshow .prev").click(function(){
		
		if($(this).siblings("ul").find("li.active").prev().length !== 0)
		{
			$(this).siblings("ul").find("li").hide();
			$(this).siblings("ul").find("li.active").removeClass("active").prev().addClass("active").show();
		}
		else
		{
			$(this).siblings("ul").find("li").hide().removeClass("active");
			$(this).siblings("ul").find("li:last-child").addClass("active").show();
		}
		
		update_gallery_caption($(this).parent());
		emediateReload();
		
		GA.log();
		RI.log();		
		return false;
	});

	$(".slideshow .next").click(function(){
		
		if($(this).siblings("ul").find("li.active").next().length !== 0)
		{
			$(this).siblings("ul").find("li").hide();
			$(this).siblings("ul").find("li.active").removeClass("active").next().addClass("active").show();
		}
		else
		{
			$(this).siblings("ul").find("li").hide().removeClass("active");
			$(this).siblings("ul").find("li:first-child").addClass("active").show();
		}
		
		update_gallery_caption($(this).parent());
		emediateReload();
		GA.log();
		RI.log();
		
		return false;
	});
}

function update_gallery_caption(selector){
	var html = "Bild <b>" + $(selector).find("ul li.active img").attr("rel") + "/" + $(selector).find("ul li").length + "</b> " + $(selector).find("ul li.active img").attr("alt");
	$(selector).find(".caption").html(html);
}

function generate_gallery(selector, width){
	var list = "";
	var i = 1;
	var img_selector = "div.wp-caption, p a img, p img:not(.wp-smiley),.gallery dl dt a img";
	
	//window.console.log("matched " +  $(selector).find(img_selector).length);
	
	if($(selector).find(img_selector).length !== 0){
		$(selector).find(img_selector).each(function(){
			if($(this).find("p.wp-caption-text").length == 1){
				list = list + '<li><img src="' + $(this).find("img").attr("src") + '" rel="' + i + '" alt="' + $(this).find("p").html() + '" class="exclude_lazyload" /></li>';
			}
			else if($(this).parent().parent().siblings().length == 1){
				list = list + '<li><img src="' + $(this).attr("src") + '" rel="' + i + '" alt="' + $(this).parent().parent().siblings().html() + '" class="exclude_lazyload" /></li>';
			}
			else{
				list = list + '<li><img src="' + $(this).attr("src") + '" rel="' + i + '" alt="" class="exclude_lazyload" /></li>';
			}
			
			i = i + 1;
			$(this).remove();
		});
		
		$(selector).find("h2").after("<div class='slideshow width_" + width + "'><a href='#' onClick='return false' class='prev'></a><ul>" + list + "</ul><a href='#' onClick='return false' class='next'></a><div style='clear: both;'></div><div class='caption'>hello</div></div>");
		
		$(selector).find(".slideshow ul li:first-child").addClass("active");
	}
}


/* Statistics */

var GA = {
	obj : null,
	log : function() {
		if (GA.obj == null) {
			GA.obj = _gat._getTracker("UA-5773852-1");
		};
		
//		var url = "/" + type + "/" + id + "/" + field + "/" + value;
		var url = window.location;
		
		GA.obj._trackPageview(url);
	}
}

var RI = {
	log: function(){
		var imgTag = "<img src=\"http://sifomedia.nyheter24.se/RealMedia/ads/adstream_nx.ads/nyheter24/12345@TopRight?XE&amp;JSFLAG=noscript&amp;Sajt=nyheter24&amp;";
		imgTag += "Sektion=NotUsed&amp;Grupp1=nyheter24natverket&amp;XE\" alt=\"Tracking image\"/>";
	
		$('#RILog').html(imgTag);
	}
}



