$(document).ready( function() {

	// Init menu ukat functions
	initMenuHoverEffects();
	
	// Init imageslides in start.
	initImgSlide();
	
	
	// Check if navigationarrows should be placed.
	if($("img[imgId]").size() < 2){
		$(".moveForward").hide();
		$(".moveBack").hide();
	}
});



function popup(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=1,statusbar=0,menubar=0,resizable=1,width=740,height=560');");
}

edit = 'false';
function showedit(editurl) {
	if (edit == 'true'){
		//	alert('test');
		edit = 'false';
		frames['edit1'].location.href='includes/loading.html';
		$('#editframe').hide();
		$('#centerframe').hide();
		max = 'false';
		$('#editiframe').css('width','700px');
		$('#editiframe').css('height','400px');
		$('#editiframe').css('margin','50px auto');
		$('#editiframe').css('border','1px solid white');
		$('#edit1').css('height','400px');

	}
	else {
	  edit = 'true';
	  //document.getElementById('edit1').src=editurl;	
		frames['edit1'].location.href=editurl;
		$('#editiframe').css('width','700px');
		$('#editiframe').css('height','400px');
		$('#editiframe').css('margin','50px auto');
		$('#editframe').show();
		$('#centerframe').show();
	}
}



function changeImg(forward){
	var currentId = parseInt($(".imgNav").attr("currentId"));
	if(forward){
		var fullSrc = $("img[imgId=" + (currentId + 1) + "]").attr("fullSrc");
		$(".imgNav").attr("currentId", (currentId + 1));
		$(".imgNav").attr("src",fullSrc);
		$(".moveBack").show();
		
		if($("img[imgId=" + (currentId + 2) + "]").size() == 0)
			$(".moveForward").hide();
		else
			$(".moveForward").show();
		
	}
	else
	{
		var fullSrc = $("img[imgId=" + (currentId - 1) + "]").attr("fullSrc");
		$(".imgNav").attr("currentId", (currentId - 1));
		$(".imgNav").attr("src",fullSrc);
		$(".moveForward").show();
		
		if($("img[imgId=" + (currentId - 2) + "]").size() == 0)
			$(".moveBack").hide();
		else
			$(".moveBack").show();
	}
	
	$(".imgNav").attr("onClick","showedit('image.lasso?src=images/_700x/" + $(".imgNav").attr("src").replace("images/_660x440/","") + "&print=false&width=700')");
}


function updateFullPath(){
	$(".imgNav").attr("onClick","showedit('image.lasso?src=images/_700x/" + $(".imgNav").attr("src").replace("images/_660x440/","") + "&print=false&width=700')");
}

function displayArrows(){
	var currentId = parseInt($(".imgNav").attr("currentId"));
	
	if(currentId > 1){
		$(".moveBack").show();
		
		if($("img[imgId=" + (currentId + 1) + "]").size() > 0)
			$(".moveForward").show();
		else
			$(".moveForward").hide();
	}
	else
	{
		$(".moveBack").hide();
		
		if($("img[imgId=" + (currentId + 1) + "]").size() > 0)
			$(".moveForward").show();
		else
			$(".moveForward").hide();
	}

	
	
	if($("img[imgId]").size() < 2){
		$(".moveForward").hide();
		$(".moveBack").hide();
	}

}


function initMenuHoverEffects() {
	
	$(".kat_item").hover( function() {
		var count = parseInt($(this).attr("count"));
		$(".ukat_list_up[count=" + count + "]").css("display","block");
		
	}, function() {
		var count = parseInt($(this).attr("count"));
		$(".ukat_list_up[count=" + count + "]").css("display","none");
	});
	
	$(".kat_item_top").hover( function() {
		var count = parseInt($(this).attr("count"));
		$(".ukat_list_down[count=" + count + "]").css("display","block");
		
	}, function() {
		var count = parseInt($(this).attr("count"));
		$(".ukat_list_down[count=" + count + "]").css("display","none");
	});
}	


function initImgSlide() {
	
	$(".bildspel").each(function() {
		var interval = $(this).attr("slide_interval");
		var slide_count = $(this).find("img").size();
		$(this).attr("slide_count", slide_count);
		if(slide_count > 1) {
			$(this).find("img").fadeTo(0, 0);
			bildspel_nextslide("#" + $(this).attr("id"));
		}
	});
}


function bildspel_nextslide(elem) {
	var div = $(elem);
	var current_slide = parseInt(div.attr("current_slide"));
	var slide_count = div.attr("slide_count");

	if(current_slide > -1) {
		div.find("img").eq(current_slide).fadeTo(1000, 0);
		div.find("img").eq(current_slide).css("z-index","0");
	}

	current_slide++;
	if(current_slide >= slide_count)
		current_slide = 0;
		
	if(current_slide < slide_count) {
		div.find("img").eq(current_slide).fadeTo(1000, 1);
		div.find("img").eq(current_slide).css("z-index","200000");
		div.attr("current_slide", current_slide);
		setTimeout("bildspel_nextslide('" + elem + "');", div.attr("slide_interval"));
	}
	
}


