//jQuery 
var jq = jQuery.noConflict();

/*
Configuration
This is for a relatively fixed number of images with a relatively fixed w/h-ratio for images
Changing either means changing sliderWidth and/or maxHeight also
*/

// Width of entire slider
var sliderWidth = 730;

// Maximum height of images (px)
var maxHeight = 183;

// Zoom factor (px pr. image)
var zoomFactor = 20;

// Caption height relative to item's height 
//var captionHeight = 0.15;
var captionHeight = 0;

// Font-size for item in focus (maximum font-size)
var fontSize = 11;

// Maximum reduction of images (actually number of image sizes)
var maxReduction = 5;

// Speed of animation (ms pr. transition)
var animationSpeed = 150;

// ----- no configuration below this line
var slider, items, itemcount;
var curfocus=0;
var imgrs = new Array();
var imgLoaded = new Array();

function start () {
	items.each(
	function (number) {
			img = jq(this).find('img')[0];
			imgrs[number] = (jq(img).width() / jq(img).height());	
 			jq(this).mouseover(function(){update(number);});
	});
	update(Math.floor(itemcount/2));

	items.each(
	function (number) {
			img = jq(this).find('img')[0];
			jq(img).css({'width' : '100%'});		
	});

  jq(slider).fadeTo(1000, 1);			
}
 
function imgLoad (number) {
	imgLoaded[number]=1;
	for (var n=0; n<itemcount; n++){
  		if (imgLoaded[n]!=1){
  			return;
  		}
  	}
  start();  	
}
function init () {
	slider= jq("#slider");
  jq(slider).fadeTo(0, 0);			
	items = jq(slider).find("div.item");
	itemcount = items.length;
	items.each(function(number) {
		img = jq(this).find('img')[0];
		var s = img.src;			
		img.src = "";
		jq(img).bind("load", function(){imgLoad(number);});
		img.src = s;
	
	});
}

function update(focus) {
	var dist, size, w, h, imgh, caph, l, t, cssObj, item, fs;
	var l=0;
	if (focus==curfocus) {
		return false; 
	}
	
	curfocus=focus;
	for (var number=0; number<items.length; number++) {  
  		size = Math.min(Math.abs(number-focus), maxReduction-1);			
  		h = Math.round(maxHeight-zoomFactor*size);
  		caph = Math.round(h * captionHeight);
  		imgh = h-caph;
			w = Math.round(imgh * imgrs[number]);
			l = l + w;   		   		      
	}	
	ol = (l-sliderWidth)/(itemcount-1);
	l=0;
	for (var number=0; number<items.length; number++) {
  
  		dist = Math.abs(number-focus);
  		size = Math.min(dist, maxReduction-1);
			
  		h = Math.round(maxHeight-zoomFactor*size);
  		t = Math.round((maxHeight-h)/2);
  		caph = Math.round(h * captionHeight);
  		imgh = h-caph;
		w = Math.round(imgh * imgrs[number]);
		fs = Math.floor(h * fontSize/maxHeight);
	
		item = items[number];			 
		img = jq(item).find("img")[0];
		div = jq(item).find("div")[0];
			
		jq(item).removeClass('focusimg');
      	
      	jq(item).css({
        	'z-index' : 100-dist,
			left: l + "px",				
        	top: t + "px",
	        width: w + "px",
    	    height: (h + 26) + "px"
      	});
      	
      	if(dist==0){
      		jq(item).addClass('focusimg');
      		jq(item).css({
		        width: "143px",
		        height: (h + 33) + "px"
	        });
      	}	
      	
      /*
   	jq(item).animate({ 
				left: l + "px",				
        top: t + "px",
        width: w + "px",
        height: h + "px"
      }, animationSpeed );
      */
   	
			l = l + w - ol;		   		
      
	}	
}

jq(document).ready(function(){init();});

