/*Costa Rica Dream Villas *By: Festa Technologies LLC, 2006 ********************************* *File: functions.js *Summary: This page conntains all of the * javascript functions used on the site */ /*Global Variables*/
var currentpicture=1;
var folder='rental/';
var ox,oy;
var lockmouseover = false;
var gap = new Array();
var insert = 0; 

function checkEmail(elmnt){
	var emailinfo = elmnt.value;
	var atpoint = emailinfo.lastIndexOf("@");
	var dotpoint = emailinfo.lastIndexOf(".");
	if(atpoint<1 || dotpoint==-1 || atpoint>dotpoint || dotpoint==emailinfo.length-1|| dotpoint-atpoint<2){
		return false;
	}
	else{
		return true;
	}
}
/*Edit Pictures*/
function changecategory(elmnt,picID){
	newcat = elmnt.options[elmnt.selectedIndex].value;
	location.href='php/changecategory.php?picID='+picID+'&newcat='+newcat;
} 

/*Slideshow Functions*/
function slideshowBack(){	
	currentpicture--;	
	if(currentpicture < 1){		
		currentpicture = imagetotal;	
	}
	setImage();
}
function slideshowForward(){
	currentpicture++;
	if(currentpicture > imagetotal){
		currentpicture = 1;
	}
	setImage();
}
function showslide(number){
	currentpicture = number;
	setImage();
	location.href='#picture';
}
function setImage(){
	image = document.getElementById('viewer');
	text = document.getElementById('caption');
	image.src='images/'+folder+eval('imagesrc'+currentpicture);	text.firstChild.nodeValue=eval('imagecaption'+currentpicture);
	for(x=1;x<imagetotal+1;x++){
		document.getElementById('imagethumb'+x).className='thumbnail';
	}
	document.getElementById('imagethumb'+currentpicture).className='thumbnailactive';
}

/*Image Order Functions*/
function dragstart(e,elmntref){
	lockmouseover = true;
	elmnt = document.getElementById('moveable'+elmntref.id.substr(6,elmntref.id.length));
	elmnt.className='drag';
	if(!e){
		var e = window.event;
	}
	ox = e.clientX;
	if(document.body.scrollTop!=0){
		oy = e.clientY +  document.body.scrollTop;
	}
	else{
		oy = e.clientY + document.documentElement.scrollTop;
	}
	parentoffset = 104;
	for(var x=1;x<imagetotal+1;x++){
		gap[x] = document.getElementById('gap'+(x)).offsetTop + 2 + parentoffset + (document.getElementById('moveable'+(x)).offsetHeight/2);
	}
	document.onmousemove = function(e){drag(e,elmnt)}
	document.onmouseup = function(e){release(e,elmnt)}
	document.body.ondrag = function () { return false; };
	document.body.onselectstart = function () { return false; };
	document.body.className = "noselect";}

function drag(e,elmnt){	
	if(elmnt.className!='drag'){elmnt.className='drag';}	
	if(!e){var e = window.event;}	
	found = false; 

	/*Indicates if the mouse is in position to show an 'insert' div*/	
	if(document.body.scrollTop!=0){		
		ycoord = e.clientY +  document.body.scrollTop;
	}
	else{
		ycoord = e.clientY + document.documentElement.scrollTop;
	}	
	for(var x=0;x<imagetotal+1;x++){ 
	/*Cycle through each gap*/		
		if(x==0){
			/*First Div*/
			if(ycoord < gap[1]){
				/*Mouse is above the first element*/
				if(insert != 1 && insert != 0){
					closeGap(insert);
				}
				insert = 1;
				found = true;			
			}
		}		
		else if(x==imagetotal){
			/*Last Div*/			
			if(ycoord > gap[x]){/*Mouse is below the last element*/	
				if(insert != x+1 && insert != 0){
					closeGap(insert);
				}
				insert = x+1;
				found = true;
			}		
		}
		else{ 
			/*The rest of the div's*/
			if(ycoord > gap[x] && ycoord < gap[x+1]){
				if(insert != x+1 && insert != 0){
					closeGap(insert);
				}
				insert = x+1;
				found = true;
			}
		}
	}
	if(insert != 0 && found){
		openGap(insert);
	}
	else if(insert!=0 && !found){
		closeGap(insert);
		insert = 0;
	}
	elmnt.style.left = (e.clientX-14)+'px';
	elmnt.style.top = (ycoord-40)+'px';
}
function openGap(num){
	if(document.getElementById('gap'+num).className!='gap'){
		document.getElementById('gap'+num).className='gap';	
	}
}
function closeGap(num){
	if(document.getElementById('gap'+num).className!='gaphidden'){
		document.getElementById('gap'+num).className='gaphidden';
	}
}
function release(e,elmnt){
	lockmouseover = false;
	elmnt.className='adminimage';
	document.onmousemove = null;
	document.onmouseup = null;
	document.body.ondrag = null;
	document.body.onselectstart = null;
	document.body.className = "body";
	if(insert!=0){
		elnumber = elmnt.id.substr(8,elmnt.id.length);
		if(elnumber!=insert&&elnumber!=insert-1){
			if(elnumber<insert){insert--;}
			location.href="php/setimageorder.php?oldorder="+elmnt.id.substr(8,elmnt.id.length)+"&order="+insert+"&category="+category;
		}
		else{
			window.location.reload();
		}
	}
}
