/**
 * jQuery mousehold plugin - fires an event while the mouse is clicked down.
 * Additionally, the function, when executed, is passed a single
 * argument representing the count of times the event has been fired during
 * this session of the mouse hold.
 *
 * @author Remy Sharp (leftlogic.com)
 * @date 2006-12-15
 * @example $("img").mousehold(200, function(i){  })
 * @desc Repeats firing the passed function while the mouse is clicked down
 *
 * @name mousehold
 * @type jQuery
 * @param Number timeout The frequency to repeat the event in milliseconds
 * @param Function fn A function to execute
 * @cat Plugin
 */

jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 100;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		})
	}
}

jQuery.fn.debug = function() {
	alert("New Debug " +  this);
}


var imgs = new Array();

var currentPic;

var posx, posy;
var w0;
var h0;
var H, W;
var panx;
var relx;
var absx;
var mousedown = false;
var overlap = 0.25;

var minScale;
var minWidth;
var maxWidth;
var maxHeight;
var falloff;
function init() {
	maxHeight = 250.0*maxWidth/400.0;

	minScale = minWidth/maxWidth;
	w0 = 2.5*W/(pics.length);
	//alert("W = "+ W + "w0 = "+w0);
	h0 = 250.0*w0/400.0;
	panx = minWidth/2;
	relx = 0;
	posx = posy = -100;
}

var myId;
function showPic(i,z) {
	var id = "#img"+i;
	var x = i*w0;
	var dx = Math.abs(x+w0/4-relx);
	var dy = Math.abs(H/2+h0/4-posy);
	var d = Math.sqrt(dx*dx);
	scale = Math.max(minScale, 1.0-d/falloff);
	var w = scale*maxWidth;
	//var h = scale*maxHeight;
	var h = $(myId).height()*scale;
	var x0 = panx + $(myId).offset().left;
	var y0 = $(myId).offset().top;

	var positionx = x0+x-w/2;
	var positiony = y0+H/2-h+H/2;
	$(id).css({'width':w});
	$(id).css({'left':positionx-relx+W/2});
	$(id).css({'top':positiony});
	$(id).css({'z-index':z});
	
	//alert(positiony);
	//	if(x+w0/4-relx>400) return;
	//var id2 = "#title";
	//$(id2).css({'position':'absolute'});
	//$(id2).css({'left':positionx});
	//$(id2).css({'top':y0+H});
	//$(id2).css({'z-index':50});
}


var numImgs = 0;
var myOptions;

jQuery.fn.setup = function(options) {
	myId = 	"#"+this.attr("id");
	var w = $(myId).width();
	var h = $(myId).height();
	var settings = {
		minWidth: 50,
		maxWidth: 100,
		falloff: 200,
		width: w,
		height: h,
		clicked : function(id) { alert("default:" + id);}
	};
	if(options) {
		jQuery.extend(settings, options);
	}
	minWidth = settings.minWidth;
	maxWidth = settings.maxWidth;
	falloff = settings.falloff;
	W = settings.width;
	H = settings.height;
	if(W==0)  W = 760;
	if(H==0) H = 400;
	//alert("size = "+W + "x"+ H);
	init();

	myOptions = options;
	jQuery(this).find("img").each(function() {
		id = "img"+numImgs;
		$(this).attr('id', id);
		$(this).css({'position':'absolute'});
		$(this).css({'border':'1px solid #D0D0D0'});
		imgs[numImgs++] = this;
		$(this).click(function() {
			id = $(this).attr("id").substring(3);
			//settings.clicked(id);
		});
	});
	relx=0;
	currentPic = 0;
	showPics();
};

function showPics() {
    
	 for(var i=0;i<currentPic;i++) {
		showPic(i,i);
	 }
	 for(var i=imgs.length-1;i>currentPic;i--) {
		showPic(i,imgs.length-i);
	 }
	 showPic(currentPic,imgs.length);
	 myOptions.clicked(currentPic);
}

function on_mousemove(e) {
    //$("info").html("Current Pic = "+currentPic);
	//if(mousedown) panx+=e.pageX - $(myId).offset().left -posx;
	posx = e.pageX - $(myId).offset().left;
	posy = e.pageY - $(myId).offset().top;
	//if(posx>0 && posx<W && posy>0 && posy<H) {
	//relx = posx-panx;
		var w = w0;
		currentPic = Math.floor((relx+w/4)/w);
		//if(currentPic>=0 && currentPic<pics.length) {
		//	$("#title").html("<span>"+pics[currentPic].title+"</span>");
		//}
	//}
	showPics();
}

jQuery(document).ready(function(){
	jQuery().mousemove(function(e){
		absx = e.pageX - $(myId).offset().left;		
	  	on_mousemove(e);
	});
		
	jQuery().mousehold(25, function(e){
		var d = absx - W/2;
		if(Math.abs(d)<10) d = 0;
		relx = relx + d/2;
		if(relx<0) relx = 0;
		var w = w0;
		currentPic = Math.floor((relx+w/4)/w);
		if(relx>numImgs*w-w/4) relx = numImgs*w-w/4;
		showPics();
	});
	/*
	jQuery().click(function(e){
		absx = e.pageX - $(myId).offset().left;				
		var d = absx - W/2;
		relx = relx + d;
		if(relx<0) relx = 0;
		var w = w0;
		//alert("w0 = "+w0);
		//alert("numImgs*w="+(numImgs*w-w/4.0));
		currentPic = Math.floor((relx+w/4)/w);
		//alert("d="+d + " relx="+relx + "numImgs*w-w/4="+(numImgs*w-w/4));
		if(relx>numImgs*w-w/4) relx = numImgs*w-w/4;
		showPics();
		//alert("d="+d + " relx="+relx);
	});
		*/
});