// jq_foto.js
//
 var gDuration = 400;
 var gFotoList = new List();
 var gLoadCnt = 0;
 
//---------------------------------
$.fn.fotoNew = function(o)
	{
	$(this).fotoShowCtls();
	$(this).attr("info", gFotoList.add(o));
	if (o.duration) gDuration = o.duration;
gDuration=4000;
	var foto = $(this)
	.addClass("foto")
	.bind("resume",function(){foto.fotoAnimate(o);})
	.attr("duration", gDuration);
	foto.fotoNewImg(101);
	foto.fotoNewImg(102);
	$("<div></div>").css({zIndex:100})
	.addClass("transImg").appendTo(foto)
	.copyRectFromEl(foto);

	//foto.fotoBoxFrame(o);
	if (o.slider)
		{
		$("<div></div>").addClass("fotoSlider")
		.appendTo(foto).slider({min:0,max:3000,value:gDuration})
		.bind('slide', fotoSliderFunc);
		}
	// Slideshow Controls
	foto.bind("next", function(){
		$(this).attr("cmd","next")
		.fotoSetSlide(o)})
	.bind("prev", function(){
		$(this).attr("cmd","prev")
		.fotoSetSlide(o)})
	.bind("auto", function(){
		$(this).attr("cmd","next")
		.fotoAnimate(o)});
	if (o.getNextImg) foto.fotoSetSlide(o);

	return foto;
	}
//---------------------------------
$.fn.fotoGetFilename = function()
	{
	return $(this).attr("file");
	}
//---------------------------------
$.fn.fotoStart = function()
	{
	$(this).attr("on", true);
	$(this).fotoAnimate(gFotoList.get($(this).attr("info")));
	return $(this);
	}
//---------------------------------
$.fn.fotoStop = function()
	{
	$(this).removeAttr("on");
	return $(this);
	}
//---------------------------------
$.fn.fotoNewImg = function(zindex)
	{
	var foto = $(this);
	var div = $("<div></div>")
	.addClass("fotoImgBox")
	.css({zIndex:zindex}).attr("zi",zindex)
	.copyRectFromEl(foto).move(0,0)
	.css({opacity:1});
	if (zindex==102) div.css({opacity:0});
	div.appendTo(foto);
	
	var img = $("<img />")
	.addClass("fotoImg")
	.appendTo(div);
	
	var imgRec = gTransDir.randImg();
	img.attr({
		onload: function(){foto_loaded(img);},
		src:imgRec.file,
		imgDim:{height:imgRec.h,width:imgRec.w}
	});
	return $(this);
	}
//---------------------------------
$.fn.fotoSetSlide = function(o)
	{
	var foto = $(this);
	var imgbox1 = foto.children(".fotoImgBox:[zi=101]");
	var imgbox2 = foto.children(".fotoImgBox:[zi=102]");
	
	imgbox2.css({zIndex:101}).attr("zi",101);
	imgbox1.css({zIndex:102}).attr("zi",102);
	imgbox2.css({opacity:1});
	imgbox1.css({opacity:1});

	var imgRec = o.getNextImg();
	
	var img = imgbox2.children();
	img.attr("loaded","no")
	.attr("onload",function(){foto_loaded(img);});
	image_fix({img:img, src:imgRec.file, parElem:imgbox1,
				imgDim:{height:imgRec.h,width:imgRec.w}
				});

	foto.attr("file", basename(stripfile(imgRec.file)))
	.fotoUpdateIdx(imgRec.idx, imgRec.total)
	.drawPeople();	
	foto.fotoAnimate(o);
	return $(this);
	}
	
//---------------------------------
$.fn.fotoAnimate = function(o)
	{
	var foto = $(this);
	var imgbox1 = foto.children(".fotoImgBox:[zi=101]");
	var imgbox2 = foto.children(".fotoImgBox:[zi=102]");
	var img1 = imgbox1.children();
	var img2 = imgbox2.children();
	if (img1.attr("loaded")=="yes" && img2.attr("loaded")=="yes") 
		{
		//var dur = parseInt(foto.attr("duration"));
		var dur = 250;
		imgbox2.stop().animate({
			opacity: 0
			}, dur, "linear", function(){
//				sheildOn(img1);
			//if (foto.attr("on")) foto.fotoAnimate(o);
			});
		}
	else 
		{
		setTimeout(function(){foto.fotoAnimate(o);}, 100);
		}
	return foto;
	}

//---------------------------------
function foto_loaded(img)
	{
	img.attr("loaded", "yes");
	}

//---------------------------------
$.fn.fotoBoxFrame = function(o)
	{
	var foto = $(this);
	var fotobox = new Rect().getElem(foto)
	.move(foto.position().top, foto.position().left)
	var h = 96;
	var w = 128;
	var box = new Rect(0,0,h,w);
	for (x = fotobox.left-w; x<=fotobox.right; x+= w)
		{
		$("<div></div>")
		.css({position:"absolute", top:fotobox.top-h, left:x, height:h, width:w})
		.css("backgroundColor",RandColor())
		.appendTo($("body"));
		}
	return foto;
	}
//---------------------------------
$.fn.drawPeople = function(box)
	{
	if (!box) box = $(".peopleInFoto");
	box.html("");
	var data = {};
	data.command = "photo_getPlayers";
	data.filename = shuf_getPhotoFilename();
	$.post("shuf_ratings.php", {data: $.toJSON(data)}, function(res)
		{
		var obj = $.evalJSON(res);
		if (obj.recs)
			{
			var recs = $.evalJSON(obj.recs);
			var total = recs.length;
			var i;
			var s = "";
			for (i=0; i<total; ++i)
				{
				var r = recs[i];
				s += "<a>"+r.first+" "+r.last+"</a>";
				if (i<(total-1)) s += ", ";
				}
			box.html(s);
			box.children("a")
			.hover(function(){$(this).css({color:"yellow"});},
			function(){$(this).css({color:"white"})})
			.bind("mousedown", selectPerson)
			.css({cursor:"pointer"})
			.css({cursor:"hand"});
			}
		updatePeopleQuest();
		});
	return $(this);
	}
//---------------------------------
function selectPerson()
	{
	var name = unescape($(this).html());
	var n = name.split(" ");
	wnd_players_pick(n[0],n[1]);
	}
//---------------------------------
$.fn.fotoHasPlayer = function(name)
	{
	var found = false;
	$(".peopleInFoto").children("a").each(function(){
		if (name == $(this).html()) 
			{
			found = true;
			return false;
			}
		});
	return found;
	}
//---------------------------------
$.fn.fotoUpdateIdx = function(idx, total)
	{
	var foto = $(this);
	if (foto.length>0)
		{
		var ctls = foto.children(".fotoCtls");
		if (ctls.length>0)
			{
			var itm = ctls.children("a.[photoIdx=true]");
			if (itm.length>0) itm.html((idx+1)+" of "+total);	
			}
		}
	return $(this);
	}
//---------------------------------
$.fn.fotoShowCtls = function()
	{
	var ctls = wnditm_div_add({klass:"fotoCtls"},$(this))
	.css({zIndex:32200, opacity:.5})
	.menuNew({horiz:true, items:[{label:"Prev", onmousedown:"foto_prev"},
								{label:"Next", onmousedown:"foto_next"},
								{label:"Auto", onmousedown:"foto_auto"},
								{
								 label:"Order:",
								 	type:"choice",
								 	info:{
										itms:[	{label:"[Time]"},
												{label:"Random"}]
										}
								},
								{
								 label:"Filter:",
								 	type:"choice",
								 	info:{
										itms:[	{label:"[All]"},
												{label:"John Doe"}]
										}
								},
								{label:"0 of 0",
								klass:"photoIdx"}
							  ]});
/*
	ctls.hover(function(){},function(e){
		$(this).parent()
		.fotoMoveCtls(-8+e.pageY-parseInt($(this).css("top")));
	});
 
 */
	}
//---------------------------------
$.fn.fotoHideCtls = function()
	{
	$(this).children(".fotoCtls").remove();
	}
//---------------------------------
$.fn.fotoMoveCtls = function(top)
	{
	var ctls = $(this).children(".fotoCtls");
	var t = parseInt(ctls.css("top"));
	if (top>(t+4) || top<(t-4)) ctls.css({top:top});
	}

//---------------------------------
function fotoSliderFunc(event, ui)
	{
	var foto = $(this).parent();
	foto.attr("duration", ui.value)
	.trigger("resume");
	}
//---------------------------------
function animateFotoFrame(foto)
	{
	foto.animate({
		borderColor: RandColor()
	}, 888,"linear",function(){animateFotoFrame(foto);});
	}
//------------------------------------------------------------
function fotoResize(event, ui)
{
	//	zoom_updateImgFromMap();
	/*
	 ui.helper - a jQuery object containing the helper element
	 ui.originalPosition - {top, left} before resizing started
	 ui.originalSize - {width, height} before resizing started
	 ui.position - {top, left} current position
	 ui.size - {width, height} current size
	 */
	var h = ui.size.height;
	var w = ui.size.width;
	
	$(this).children().css({
		height: h,
		width: w
	});
	
	$(this).children(".fotoImgBox").each(function()
	{
		image_resetPhotoSrc($(this).children())
	});
}

function foto_prev(mitm){$(mitm).parClass("foto").trigger("prev");}
function foto_next(mitm){$(mitm).parClass("foto").trigger("next");}
function foto_auto(mitm){$(mitm).parClass("foto").trigger("auto");}


function sheildOn(img)
	{
	var imgRec = gTransDir.randImg();
	img.attr({
		onload: function(){foto_loaded(img);},
		src:imgRec.file,
		imgDim:{height:imgRec.h,width:imgRec.w}
	}).css("opacity",.5);

	}
