window.onload=function() {
//makes all thumbs into draggables
	$A($('thumbnails').getElementsByClassName("content")).each(
	function (item)
		{
			new Draggable(
			item,
				{
				revert:true,
				ghosting:true
				}
			);
				//ajax to see if it's in a fave array
				var params=$H({favepic:item.id}).toQueryString();
				new Ajax.Request("http://www.layerspace.com/lightbox/lbfavesfind.php", {
					method: "post",
					parameters:params,
					onSuccess : function(resp){
						if(resp.responseText=='yes'){
						$(item.id).style.backgroundColor='#33ff00';
						}
			 		}
			});

		}
	);
	
$A($('lb_dropbox').getElementsByClassName("content")).each(
	function (item)
		{
			new Draggable(
			item,
				{
				revert:true,
				ghosting:true
				}
			);
			//ajax to see if it's in a fave array
				var params=$H({favepic:item.id}).toQueryString();
				new Ajax.Request("http://www.layerspace.com/lightbox/lbfavesfind.php", {
					method: "post",
					parameters:params,
					onSuccess : function(resp){
						if(resp.responseText=='yes'){
						$(item.id).style.backgroundColor='#33ff00';
						}
			 		}
			});
		}
	);



Droppables.add('lb_dropbox',{
hoverclass:'selectactive',
containment: 'thumbnails',
onDrop: movetoSelection
});

Droppables.add('thumbnails',{
hoverclass:'selectactive',
containment: 'lb_dropbox',
onDrop: removefromSelection
});


createSlider(45,100);

}
//end of onLoad
var thegallref='';
function setthegall(g){
if(g){
thegallref=g;
}

}

//thumbnail highlighting tuff
var highThumbs=new Array();

function removeItems(array, item) {
var i = 0;
while (i < array.length) {
if (array[i] == item) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}

function reloadPage(){
	window.location.href=window.location.href;
}

function highlightThumb(thumbRef){
highThumbs.push(thumbRef);
$(thumbRef).style.backgroundColor='#33ff00';
Element.setInnerHTML( 'highlight', '<br><br><a href="javascript:lowlightThumb(\''+thumbRef+'\')" class="high">unmark as favourite</a><br><br>');
var params=$H(
{favepic:thumbRef}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbfaveset.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			//$('lb_instruct').innerHTML=params;
			//$('lb_instruct').innerHTML= resp.responseText;
			//setTimeout("setInstructions('click X to remove images from selection')",1500)
			setTimeout("reloadPage()",301);
 			},
 		onFailure : function(resp) { 
 			
			//$('lb_instruct').innerHTML=params;
			
 			}
		
	});


}

function lowlightThumb(thumbRef){
removeItems(highThumbs,thumbRef)
$(thumbRef).style.backgroundColor='#f0f0f0';
Element.setInnerHTML( 'highlight', '<br><br><a href="javascript:highlightThumb(\''+thumbRef+'\')" class="low">mark as favourite</a><br><br>');
var params=$H(
{favepic:thumbRef}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbfaveunset.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			//$('lb_instruct').innerHTML=params;
			//$('lb_instruct').innerHTML= resp.responseText;
			//setTimeout("setInstructions('click X to remove images from selection')",1500)
			setTimeout("reloadPage()",301);
 			},
 		onFailure : function(resp) { 
 			
			//$('lb_instruct').innerHTML=params;
			
 			}
		
	});


}

function hstringWorkout(hstring){
	var i = 0;
	var thestring='<br><br><a href="javascript:highlightThumb(\''+hstring+'\')" class="low">mark as favourite</a><br><br>';	
	if (highThumbs.length>0){
				while (i < highThumbs.length) {
					if (highThumbs[i] == hstring) {
						thestring='<br><br><a href="javascript:lowlightThumb(\''+hstring+'\')" class="high">unmark as favourite</a><br><br>';
					}
					i++;
				}
				
			}
	return thestring;
}
////////////////////
function hstringWorkout2(hstring,elmnt){
	
	var params=$H({favepic:hstring}).toQueryString();
	
	new Ajax.Request("http://www.layerspace.com/lightbox/lbfavecheck.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			if (resp.responseText=='yes'){
			var linkString='<br><br><a href="javascript:lowlightThumb(\''+hstring+'\')" class="high">unmark as favourite</a><br><br>';
				$(elmnt).setInnerHTML( 'highlight', linkString);
			}
			
				
 		}	
	});
}

//end of thumb highlighting













function showHigh(){
var t='thumbs: ';
var h=0;
while (h < highThumbs.length){
t+=highThumbs[h]+'/';
h++;
}
alert(t);
}

function changeThumbs(v) {
v=v*9/10;
w=v*90/100;
h=v*110/100;
b=v*20/100;
  var scalePhotos = document.getElementsByClassName("content");
  var scaleButtons=document.getElementsByClassName("buttons");
  for (i=0; i < scalePhotos.length; i++) {
    scalePhotos[i].style.width = w+"px";
    scalePhotos[i].style.height = h+"px";
  }
    for (j=0; j < scaleButtons.length; j++) {
    scaleButtons[j].style.width=b+"px";
    scaleButtons[j].style.height=b+"px";
  }
}


function createSlider(minValue,maxValue){
var values=[];
for (var n=minValue; n<=maxValue; n++) values.push(n);
new Control.Slider(
'sliderHandle',
'sliderTrack',
{
range: $R(minValue,maxValue),
values:values,
sliderValue:maxValue,
onChange:function(value){
changeThumbs(value);

},
onSlide:function(value){
changeThumbs(value);
}

}
);
}
var viewSelection=false;


function movetoSelection(draggable,lb_dropbox){
notdragged=false;

//$('lb_instruct').innerHTML='drag images to the box on the left to remove from selected';
draggable.parentNode.removeChild(draggable);
var clearBox=$('clearBox');
lb_dropbox.insertBefore(draggable,clearBox);
var thumbId=draggable.id.replace('pic_','');
var delBtn=$('delBtn'+thumbId);
delBtn.style.display='inline';
var params=$H(
{thepic:draggable.id}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbselection.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('lb_instruct').innerHTML= resp.responseText;
			setTimeout("setInstructions('click X to remove images from selection')",1500)
			setTimeout("notdragged=true",300);
			setTimeout("reloadPage()",301);
 			},
 		onFailure : function(resp) { 
 			//$('lb_instruct').innerHTML="There has been an error";
			$('lb_instruct').innerHTML=params;
			setTimeout("notdragged=true",300);
 			}
		
	});

}

function setInstructions(thetext){
if (!viewSelection){
$('lb_instruct').innerHTML=thetext;
}
}

function removefromSelection(draggable,thumbnails){
notdragged=false;
var clearBox1=$('clearBox1');
draggable.parentNode.removeChild(draggable);
//need to check whether or not the image is part of the current gallery

if (thegallref=='all'){
thumbnails.insertBefore(draggable,clearBox1);
}
else {
var checkparams=$H(
{thispic:draggable.id,thisgall:thegallref}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbcheckimage.php", {
		method: "post",
		parameters:checkparams,
		onSuccess : function(resp){
			if (resp.responseText=='yes'){
				
			thumbnails.insertBefore(draggable,clearBox1);
			picNo=draggable.id.replace('pic_','');
			$('delBtn'+picNo).style.display='none';
			}
 		}
		
	});//end of unselect request
}

var params=$H(
{thepic:draggable.id}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbunselect.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('lb_instruct').innerHTML= resp.responseText;
			setTimeout("setInstructions('drag images to the box on the right to select')",1500)			
			setTimeout("notdragged=true",300);
			setTimeout("reloadPage()",301);
 			},
 		onFailure : function(resp) { 
 			$('lb_instruct').innerHTML="There has been an error";
			//$('lb_instruct').innerHTML=params;
			setTimeout("notdragged=true",300);
			setTimeout("reloadPage()",301);
 			}
		
	});//end of unselect request

}//end of removeFromSelection


function removeThumb(draggable){
var picNo=draggable;

draggable=$('pic_'+draggable);
notdragged=false;
var clearBox1=$('clearBox1');
draggable.parentNode.removeChild(draggable);
//need to check whether or not the image is part of the current gallery

if (thegallref=='all'){
thumbnails.insertBefore(draggable,clearBox1);
}
else {
var checkparams=$H(
{thispic:draggable.id,thisgall:thegallref}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbcheckimage.php", {
		method: "post",
		parameters:checkparams,
		onSuccess : function(resp){
			if (resp.responseText=='yes'){	
			$('thumbnails').insertBefore(draggable,clearBox1);
			$('delBtn'+picNo).style.display='none';
			
			}
 		}
		
	});//end of unselect request
}

var params=$H(
{thepic:draggable.id}
).toQueryString();
	new Ajax.Request("http://www.layerspace.com/lightbox/lbunselect.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('lb_instruct').innerHTML= resp.responseText;
			setTimeout("setInstructions('drag images to the box on the right to select')",1500)			
			setTimeout("notdragged=true",300);
			setTimeout("reloadPage()",301);
 			},
 		onFailure : function(resp) { 
 			$('lb_instruct').innerHTML="There has been an error";
			//$('lb_instruct').innerHTML=params;
			setTimeout("notdragged=true",300);
			setTimeout("reloadPage()",301);
 			}
		
	});//end of unselect request

}//end of removeThumb

function hideSelection(){
viewSelection=false;
	new Effect.Parallel(
[
new Effect.Fade('selectionHolder',{duration:0.2, sync:true}),
new Effect.Appear('lightboxHolder',{duration:0.2, sync:true})
],
{}
);
$('lb_instruct').innerHTML='click X to remove images from selection';
}

function showSelection(){
viewSelection=true;
$('lb_instruct').innerHTML='click thumbs to edit image options';
new Ajax.Request("http://www.layerspace.com/lightbox/setselection.php", {
		method: "post",
		onSuccess : function(resp){
			$('selectionHolder').innerHTML="<div id='selectionHolder2'><div id='infobar'><span style='float:left'>your selection (click to edit)</span><span style='float:right'><a href='javascript:hideSelection();' class='options'>back to chooser window &raquo;</a></span><br clear='all'><br></div><br><br>"+ resp.responseText;
	new Effect.Parallel(
[		
			new Effect.Fade('lightboxHolder',{duration:0.2, sync:true}),
			new Effect.Appear('selectionHolder',{duration:0.2, sync:true})
			],
			{}
			);
 			},
 		onFailure : function(resp) { 
			$('selectionHolder').innerHTML+="There has been an error";
 			}
		
	});
}

function editimage(imageRef){
$('editimageinner').innerHTML="<a href='javascript:closeeditbox()' class='options'>close box &raquo</a><br><br>";
var params=$H(
{thepic:imageRef}
).toQueryString();
new Ajax.Request("http://www.layerspace.com/lightbox/seteditimage.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('editimageinner').innerHTML+=resp.responseText;
 			},
 		onFailure : function(resp) { 
			$('editimageinner').innerHTML+="There has been an error";
 			}
		
	});

new Effect.Parallel(
[
new Effect.Appear('editimageBox',{from:0.0, to:0.8, duration:0.2, sync:true}),
new Effect.Appear('editimageouter',{from:0.0, to:1.0, duration:0.2, sync:true})
],
{}
);

$('lb_instruct').innerHTML='set options for this image';




}

function closeeditbox(){
new Effect.Parallel(
[
new Effect.Fade('editimageBox',{from:0.8, to:0.0, duration:0.2, sync:true}),
new Effect.Fade('editimageouter',{from:1.0, to:0.0, duration:0.2, sync:true})
],
{}
);
$('lb_instruct').innerHTML='click thumbs to edit image options';
}

function saveimagecomments(imageRef){
var commentForm=$('comments');

new Effect.Appear('commenttimer',{from:0.0, to:1.0, duration:0.1});
$('commentResponse').innerHTML='saving...';
var theComments=commentForm.value;
var params=$H(
{thepic:imageRef,thecomments:theComments}
).toQueryString();
new Ajax.Request("http://www.layerspace.com/lightbox/setimagecomment.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('commentResponse').innerHTML='changes saved';
			new Effect.Fade('commenttimer',{from:1.0, to:0.0, duration:0.3});			
			},
 		onFailure : function(resp) { 
			$('commentResponse').innerHTML="There has been an error";
			new Effect.Fade('commenttimer',{from:1.0, to:0.0, duration:0.3});
 			}
		
	});
	
}
var notdragged=true;


function savedetails(){
var scommentBox=$('sComments');
var nameBox=$('sendername');
var emailBox=$('senderemail');
new Effect.Appear('scommenttimer',{from:0.0, to:1.0, duration:0.1});
$('scommentResponse').innerHTML='saving...';
var theComments=scommentBox.value;
var sendername=nameBox.value;
var senderemail=emailBox.value;
var params=$H(
{thecomments:theComments,thename:sendername,theemail:senderemail}
).toQueryString();
new Ajax.Request("http://www.layerspace.com/lightbox/setdetails.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
			$('scommentResponse').innerHTML='details saved';
			new Effect.Fade('scommenttimer',{from:1.0, to:0.0, duration:0.3});			
			},
 		onFailure : function(resp) { 
			$('scommentResponse').innerHTML="There has been an error";
			new Effect.Fade('scommenttimer',{from:1.0, to:0.0, duration:0.3});
 			}
		
	});
	
}


function submitselection(){
var scommentBox=$('sComments');
var nameBox=$('sendername');
var emailBox=$('senderemail');
var theComments=scommentBox.value;
var sendername=nameBox.value;
var senderemail=emailBox.value;

new Effect.Parallel(
[
new Effect.Appear('editimageBox',{from:0.0, to:0.8, duration:0.1, sync:true}),
new Effect.Appear('submitMessageBox',{from:0.0, to:1.0, duration:0.1, sync:true})
],
{}
);

var params=$H(
{thecomments:theComments,thename:sendername,theemail:senderemail}
).toQueryString();
new Ajax.Request("http://www.layerspace.com/lightbox/submitselection.php", {
		method: "post",
		parameters:params,
		onSuccess : function(resp){
		var responsearray=resp.responseText.split('~');
			var theresponse=responsearray[0];
			if (theresponse=='valid'){
				var theuser=responsearray[1];
				var thespace=responsearray[2];
				var lbref=responsearray[3];
				//clear all windows
				new Effect.Parallel(
				[
				new Effect.Fade('selectionHolder2',{from:0.8, to:0.0, duration:0.2, sync:true}),
				new Effect.Fade('selectionBox2',{from:1.0, to:0.0, duration:0.2, sync:true})
				],
				{}
				);
				$('selectionHolder2').innerHTML='';
				$('selectionBox2').innerHTML='';
				$('lb_instruct').innerHTML='selection submitted';
				//show a message
				//show a link to make a new selection
				//show link to return to main site
				$('submitMessage').innerHTML="<br><br><span style='color:#000;'>THANK YOU!</span><br><br>Your selection has been submitted<br><br><a href='lightboxviewer.php?lno="+lbref+"' class='options'>view selection &raquo;</a><br><br><br><br><br><a href='gallery.php?s="+thespace+"&lb=1' class='options'>new selection &raquo;</a><br><br><a href='gallery.php?s="+thespace+"' class='options'>main website &raquo;</a><br><br>";
			}
			else if(resp.responseText=='name'){
	
				$('submitMessage').innerHTML="<br><br><span style='color:red;'>ERROR</span><br><br>please enter a name before submitting<br><br><br><br><a href='javascript:closesubmitbox()' class='options'>close box &raquo;</a>";
				}
			else if(resp.responseText=='email'){
	
				$('submitMessage').innerHTML="<br><br><span style='color:red;'>ERROR</span><br><br>your email address does not appear to be valid, please try again<br><br><br><br><a href='javascript:closesubmitbox()' class='options'>close box &raquo;</a>";
			}
			else {
	$('submitMessage').innerHTML=resp.responseText;

			}		
			},
 		onFailure : function(resp) { 
 			$('submitMessage').innerHTML="<br><br><span style='color:red;'>ERROR</span><br><br>sorry, there has been an error, please try again<br><br><br><br><a href='javascript:closesubmitbox()' class='options'>close box &raquo;</a>";

 			}
		
	});
	
}

function closesubmitbox(){
				new Effect.Parallel(
				[
				new Effect.Fade('editimageBox',{from:0.8, to:0.0, duration:0.1, sync:true}),
				new Effect.Fade('submitMessageBox',{from:1.0, to:0.0, duration:0.1, sync:true})
				],
				{}
				);
}


