//image preloader
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

// has attribute AND is not empty
jQuery.fn.hasAttr = function(attr){
	return $(this).attr(attr) !== undefined && $(this).attr(attr) !== ""; //XXX (should be a jQuery fn)
}

// topimages
var currentTop = 0;
var topImages = new Array();
for (i=0;i<=9;i++) {
	var incr = i+1; 
	topImages[i]="images/top-" + incr + ".jpg";
}

$(window).load(function(){
	$('#content img').each(function(){
		addImgWrapper($(this));
	});
	
	$("#content a.fancybox").fancybox({
		titlePosition 	: 'inside',
		titleShow       : true,
	    overlayShow     : true,
	    overlayOpacity	: 0.75,
	    overlayColor	: '#000000',
	    hideOnOverlayClick: true,
	    hideOnContentClick:	true
	    });

	$('#content a.fancybox img').each(function(){
		addMagnifier($(this), $(this).parents('a.fancybox'));	
	});
	
	// preload images
	for(i=0; topImages.length > i; i++) {
		$.preLoadImages(topImages[i])
	}
	
	setTimeout("changeTop()",5000); 
});

function changeTop() {
	// randomized top image order
	var newTop = currentTop;
	while(currentTop == newTop) {
		newTop = Math.floor(Math.random()*topImages.length);
	}
	currentTop = newTop;	

	// set bildet under likt det over
	$("#top_under").attr("src",$("#top_over").attr("src"));
	$("#top_under").show();
	
	// skjul bildet over
	$('#top_over').hide();
	
	// set nytt bildet over
	$("#top_over").attr("src",topImages[currentTop]);
	
	// vis bildet over (og samtidig skjul det under for å få "flash" effekt mot bakgrunn)
	$("#top_over").fadeIn(2000);
	$("#top_under").fadeOut(2000);
	
	// start på nytt	
	setTimeout("changeTop()",8000);
}

// imagewrapper
function addImgWrapper(img) {
	// if linked image
	var imgOrLink = img;
	$.each(img.parents("a"), function() {
		imgOrLink = $(this);
		imgOrLink.attr('title',img.attr('title')); // XXX: copy title to a-tag (fancybox hack) 
	});

	// wrapper
	var wrapper = $("<div/>");
	wrapper.css('width', img.width()); // same as image
	wrapper.css('height', img.height()); // same as image	
	wrapper.insertAfter(imgOrLink);

	// move link/image
	imgOrLink.appendTo(wrapper);
	
	// move css from old img to wrapper
	wrapper.attr("class", img.attr("class"));
	img.removeAttr("class");
	wrapper.addClass("imgwrapper");

	// align as float
	if(img.hasAttr('align')) {
		wrapper.css("float", img.attr('align'));
		img.removeAttr("align");
	} 

	if(img.hasAttr('title')) {	
		// background		
		var background = $("<div class='background'/>");
		background.hide();
		background.css('opacity', 0.75);
		background.appendTo(wrapper);
		background.width(img.width());
		
		// description
		var description = $("<div class='description'/>");
		description.hide();
		description.appendTo(wrapper);
		var title = $("<p>" + img.attr("title") + "</p>");
		description.width(img.width());
		title.appendTo(description);

		// background height
		background.height(description.height());
		
		// remove title-attr (annoying tooltip)
		img.removeAttr('title');

		// show
		background.fadeIn(750);
		description.fadeIn(750);

		// remove title
		// img.removeAttr("title");
	}
}

function addMagnifier(img, parent) {
	var magnifier = $("<img src='/images/magnifier.png' class='magnifier' title='Klikk for å se større utgave'./>");
	magnifier.css('opacity', 0);
	magnifier.appendTo(parent);

	// mouseover (image and the magnifier)
	$.each([img, magnifier], function() {
		this.hover(function(){
			magnifier.stop().fadeTo(200, 0.75); // mouse over
		},function(){
			magnifier.stop().fadeTo(200, 0); // mouseout
		});
	}); 
}