$(document).ready(function() {
	$("#right").append('<div class="collapsibleWrap"></div>');
	$("body").append('<div id="modalOverlay"></div><div id="modalWindow"></div>');
	$("object").hide();
	
	$.each($("object"), function(i){
		i = i+1;
		var video = $(this);
		
		$(this).parent("p").html('<p class="videoLink" id="nr_' + i + '"><img src="userFiles/myImages/icon-video.png" alt="Video" /> Video ' + i + '</p>');
		$("#modalWindow").append('<span class="video nr_' + i + '"></span>');
		$('.video.nr_' + i).append(video);
	});
	
	$(".video").hide();
	
	$(".videoLink").live("click", function(){
		var target = $(this).attr("id");
		$(".video").hide();
		$("#modalOverlay, #modalWindow, ." + target).fadeIn();
		$("." + target).children("object").show();
	});
	
	$("#modalOverlay").live("click", function(){
		$(".video").hide();
		$("#modalOverlay, #modalWindow").fadeOut("fast");
	});
	
	$(".collapsibleImages").html("");
	$("#containerGallery").appendTo(".collapsibleImages");
	
	$.each($("#containerGallery").children("a"), function(i){
		if(i > 2)
		{
			$(this).hide();
		}
	});
	
	if($("#containerGallery").children("a").length > 2)
	{
		$(".collapsibleImages").append('<span id="moreImages">Fler bilder</span>');
	}
	
	$("#moreImages").live("click", function(){
		var gallery = $("#containerGallery").clone();
		gallery.attr("id", "gallery2");
		gallery.children("a").show();
		$("#right").children().hide();
		$("#right").append(gallery);
		$('a[rel^="lightbox"]').lightBox();
		$("#gallery2").prepend('<span id="backRight">Tillbaka</span>');
	});
	
	$("#backRight").live("click", function(){
		$("#gallery2").remove();
		$("#right").children().show();
	});
	
	$.each($(".collapsible"), function(){
		var html = $(this).html();
		$(this).html("");
		$(this).css("background-color", "#fff");
		$(".collapsibleWrap").append('<div class="collapsible">' + html + '</div>');
	});
	
	$.each($(".collapsible:not(:first-child)"), function(){
		$(this).children(":not(h3)").hide();
	});
	
	$(".collapsible h3").live("click", function(){
		$(".collapsible").children(":not(h3)").slideUp("fast");
		if($(this).parent("div").children(":not(h3)").is(":hidden"))
			$(this).parent("div").children(":not(h3)").slideDown("fast");
		else
			$(this).parent("div").children(":not(h3)").slideUp("fast");
	});
	
	if($("#containerGallery").length > 0)
		$('a[rel^="lightbox"]').lightBox();
		
});
