// jq_icon.js

//---------------------------------------------
$.fn.icon = function(o)
	{
	var imgbox = $(_div).addClass("iconImgBox");
	o.img = $(_img).addClass("iconImg");
	imgbox.append(o.img);
//	var txt = $(_div).addClass("iconTxt");
	var icon = $(this).addClass("icon").css({opacity:0});

	icon.draggable({
				stack: {group: ".icon",min: 50},
				appendTo: $("body"),
		//		helper: "clone",
				zIndex: 30000,
				start:icon.iconDragStart,
				drag: icon.iconDrag,
				stop: icon.iconDragStop,
				distance: 6
				})
	.droppable({
			drop:function(event, ui){
				$(this).before(ui.draggable);
			},
			hoverClass:"iconOver",
			accept:".icon",
			greedy:true
			})
	.append(imgbox);	//.append(txt);
	
	if (isMediaFile(o.src)) {
		o.img.attr({
			media: o.src
		});
		if (o.iconSrc) o.src = o.iconSrc;
		else o.src = GetMediaIcon(o.src);
		icon.popup({
			items: 	[
						{
						label: "Upload Icon for this movie",
						click: function(){$(this)
						.modal({draw:iconDrawMediaDlog,
						 		data:icon, close:function(){
							--_uploadIDCnt;
							},
							width:270,height:102
						});}
						},
					{label: "Info...",	click: function(){$(this).modal({height:111, draw:iconDrawInfoMediaDlog, data:icon});}},
					{label:"Delete...", click:function(){$(this).modal({height:125, draw:iconDrawDelMediaDlog, data:icon});}}
					],
			width:170
		});
	}
	else {
		o.src = ps_fixpathFromSize(o.src, {w: o.iconImgW,	h: o.iconImgH});
		icon.popup({items:[
			{label:"Delete...", click:function(){$(this).modal({height:120, draw:iconDrawDelMediaDlog, data:icon});}},
			{label: "Info...",	click: function(){$(this).modal({height:120, draw:iconDrawInfoMediaDlog, data:icon});}}
			]});
	}
	o.img.attr({src:o.src+image_getSrcArg(),imgW:o.imgW, imgH:o.imgH});
//	if (o.title) txt.html(o.title);
//	else txt.html(o.orig_filename);
	icon
	.attr({visitor_id: o.visitor_id,user_id: o.user_id})
	.hover(function(){$(this).iconImg().css({border:"1px dashed yellow"});
	},function()
	{$(this).iconImg().css({border:"1px solid #cccccc"});})
	.animate({opacity:1},1000);
	
	icon.bind("mousedown",function(){$(this).iconImg().css({border:"1px solid yellow"})});
	if (o.click) icon.bind("click", o.click);
	else icon.bind("click", $(this).iconStdClick);
	if (o.folder_id) icon.attr({folder_id:o.folder_id});
	if (o.photo_id) icon.attr({photo_id:o.photo_id});
	return $(this);
	}
//---------------------------------------------
$.fn.iconStdClick = function()
	{
	var icon = $(this);
	var iconbox = icon.parent();
	if (inAdminMode()) {
		icon.iconDelete();
		return;
	}
	else if (!icon.attr("photo_id")) {
			icon.parent().iconbox_photos(icon.attr("folder_id"));
		}
	else if (icon.iconImg().attr("media"))
		{
		icon.unbind("click");
		var img = $(this).iconImg().hide();
//		if ($.browser.msie) videoClass = "videoIE";
//		else videoClass = "video";
		videoClass = "video";
		var videobox = $(_div).addClass(videoClass).appendTo(img.parent());
		$(_div).addClass("videoclose").html("X")
		.hover(function(){$(this).removeClass("videocloseDown").addClass("videocloseOver");}
				,function(){$(this).removeClass("videocloseDown").removeClass("videocloseOver");})
		.bind("mousedown", function(){$(this).removeClass("videocloseOver").addClass("videocloseDown");})
		.bind("mouseup", function(){$(this).removeClass("videocloseDown");})
		.bind("click", function(){
			var vidbox = $(this).parent();
			var img = vidbox.parent().iconImg();
			vidbox.remove();
			img.show();
			icon.bind("click", icon.iconStdClick);
			return false;
		})
		.appendTo(videobox);
	
		videobox.mymedia({
			src: img.attr("media")
			});
		}
	else $(this).iconToggleSize();
	}

//---------------------------------------------
$.fn.iconImgBox = function()
	{
	return $(this).find("div.iconImgBox");
	}
//---------------------------------------------
$.fn.iconImg = function()
	{
	if ($(this).hasClass("iconImg")) return $(this);
	if ($(this).hasClass("iconImgBox")) return $(this).children("img.iconImg");
	return $(this).find("div.iconImgBox").children("img.iconImg");
	}
//---------------------------------------------
$.fn.iconTxt = function(txt)
	{
	return $(this).children("div.iconTxt");
	}
//---------------------------------------------
$.fn.iconToggleSize = function()
	{
	ps_toggleSize($(this).iconImg());
	return $(this);
	}

//---------------------------------------------
$.fn.iconIncSize = function()
	{
	ps_incSize($(this).iconImg());
	return $(this);
	}
//---------------------------------------------
$.fn.iconDecSize = function()
	{
	ps_decSize($(this).iconImg());
	return $(this);
	}
//---------------------------------------------
$.fn.iconDelete = function()
	{
	var icon = $(this);
	if (!icon.attr("photo_id"))
		{
		var id = icon.attr("folder_id");
		if (id > 0)
			{
		$.post("photo.php",{data:$.toJSON({command:"delete_folder", folder_id:id})}, function(res){
				icon.parent().iconbox_folders();
			});
			return icon;	
			}
		}
	if (icon.attr("photo_id"))
		{
		var id = icon.attr("photo_id");
		if (id > 0)
			{
			$.post("photo.php",{data:$.toJSON({command:"delete_photo", photo_id:id})}, function(res){
				icon.parent().iconbox_photos();
			});
			return icon;	
			}
		}
	}

//---------------------------------------------
function iconDrawMediaDlog(div, data)
	{
	var src = data.iconImg().attr("src");
	var img = data.iconImg();
	var mediaSrc = img.attr("media");
	$(_div).appendTo(div)
	.fotobox({uploadOne:true,
			uploadCmd:"upload_mediaIcon",
			noicons:true, 
			type_id:data.parent().attr("type_id"),
			src:escape(mediaSrc),
			doneUploadAll: function(src){
				img.attr({src:src+image_getSrcArg(),imgW:64, imgH:48});
				ShutModal();
			}
			});
	$(_img).addClass("mediaIconDlogImg")
	.attr({src:src})
	.appendTo(div);
	$(_div).addClass("mediaIconDlogTxt")
	.html("Upload a photo to use as an icon for this video.")
	.appendTo(div);
	$(_a).addClass("uploadStatus")
	.addClass("mediaIconDlgUploadStatus")
	.appendTo(div);
	}

//---------------------------------------------
function iconDrawDelMediaDlog(div, icon)
	{
	var iOwnIcon = user_isThisMe(icon.attr("user_id"), icon.attr("visitor_id"));
	var src = icon.iconImg().attr("src");
	var img = icon.iconImg();
	var mediaSrc = img.attr("media");
	$(_img).addClass("mediaIconDlogImg")
	.attr({src:src})
	.appendTo(div);
	if (!icon.attr("photo_id")) var f = "folder";
	else var f = "file";
	
	if (iOwnIcon) var txt = "Click the Delete button if you wish to permanently remove this "+f+".";
	else var txt = "You can only delete "+f+"s that you uploaded."
	$(_div).addClass("mediaIconDlogTxt").html(txt).appendTo(div);
	
	var okBut = $(_button).addClass("modalOKBut");
	if (iOwnIcon)
		{
		okBut.attr({value:"Delete"})
		.bind("click", function(){icon.iconDelete();})
		$(_button).addClass("modalCancelBut").attr({value: "Cancel"})
		.bind("click", ShutModal).appendTo(div);
		}
	else
		{
		okBut.attr({value:"OK"}).bind("click", ShutModal)
		}
	okBut.appendTo(div);
	}
//---------------------------------------------
function iconDrawInfoMediaDlog(div, icon)
	{
	}

//------------------------------------------------------------
$.fn.iconDragStart = function(event, ui)
	{
/*
	box = $(this).attr("anchor", true);
	startPos = box.position();
	boxes = $(this).wnd().find(".photos").children(".slct")
	dboxes = boxes.clone();
	dboxes.each(function()
		{
		dbox = $(this);
		var top = parseInt(dbox.css("top"));
		var left = parseInt(dbox.css("left"));
		dbox.attr({top1:top,left1:left});
		}).appendTo($("body"))
		.css("zIndex",32700);
	boxes.css({opacity:.25});
*/
	}

//------------------------------------------------------------
$.fn.iconDrag = function(event, ui)
	{
/*
	var pos = ui.offset;
	var offtop = pos.top - startPos.top;
	var offleft = pos.left - startPos.left;
	
	dboxes.each(function(idx){
		var dbox = $(this);
		var origtop = parseInt(dbox.attr("top1"));
		var origleft = parseInt(dbox.attr("left1"));
		dbox.css({
			top: origtop + offtop,
			left:origleft + offleft
		});
	});
*/
	}
//------------------------------------------------------------
$.fn.iconDragStop = function(event, ui)
	{
	$(this).css({top:"", left:""});
/*
	boxes.css("opacity", 1);
	drop_End();
	$("body").children(".slct").remove();
*/
	}
