//         _.-'~~~~~~`-._
//        /      ||      \
//       /       ||       \
//      |        ||        |
//      | _______||_______ |
//      |/ ----- \/ ----- \|
//     /  (  |  )  (  |  )  \
//    / \  ----- () -----  / \
//   /   \      /||\      /   \
//  /     \    /||||\    /     \
// /       \  /||||||\  /       \
///_        \o========o/        _\
//  `--...__|`-._  _.-'|__...--'
//          |    `'    |

//          DARTH FADER !!!!       \\


var currentOP=0;
var direction="none";
var fading="";
var objectopacity=0;
var opacity=0;
var fadingup="";
var display="none";
var visibility="hidden";
var currente="";
var safetycatch=0;


// UP \\
function dfadeup(e,faintest,strongest) {
	
	clearInterval(fading); // clears any existing fading \\
	
	if (currente && currente!=e) {
		//document.getElementById(currente).style.display="none";
		setopacity(currente,faintest);
		//display="none";
		currentOP=faintest;
	}
	
	currente=e;
	
// 	if (display=="none") {
// 		display="block";
// 		visibility="visible";
// 		document.getElementById(e).style.display=display;
// 		document.getElementById(e).style.visibility=visibility;
// 	}
	
	direction="up";
	if (!currentOP) { currentOP=faintest; }
	window.currentOP=currentOP;
	
	if (currentOP<strongest) {
		fadingup = setInterval("fadeup('"+e+"','"+faintest+"','"+strongest+"')", 20);
	}

}

function fadeup(e,faintest,strongest) {
	
	currentOP=window.currentOP;
	
	if (currentOP>=strongest || direction=="down") {
		clearInterval(fadingup);
		direction="down";
		//display="none";
		direction="none";
	}
	else {
		currentOP+=10;
		setopacity(e,currentOP);
	}
	
}

// DOWN \\
function dfadedown(e,faintest,strongest) {
	
	clearInterval(fading); // clears any existing fading \\
	
	if (currente && currente!=e) {
		//document.getElementById(currente).style.display="none";
		setopacity(currente,0);
		//display="none";
		currentOP=0;
	}
	
	currente=e;
	
	if (direction) {
		clearInterval(fadingup);
	
		direction="down";

		if (currentOP>=faintest) {
			fading = setInterval("fadedown('"+e+"','"+faintest+"','"+strongest+"')", 20);
		}
	}

}

function fadedown(e,faintest,strongest) {

	if (currentOP<=faintest) {
		clearInterval(fading);
		direction="none";
	}
	else {
		currentOP-=10;
		setopacity(e,currentOP);
	}
	
}

// SHOW AND SET OPACITY \\
function showorhidethensetopacity (whattofade, targetopacity, showorhide) {

	if (showorhide=="show") {
		display="block";
		visibility="visible";
	}
	else {
		display="none";
		visibility="hidden";
	}
	
	document.getElementById(whattofade).style.display=display;
	document.getElementById(whattofade).style.visibility=visibility;
	setopacity(whattofade, targetopacity);
}

function showorhidethenfade (whattofade, targetopacity, showorhide) {

	if (showorhide=="show") {
		display="block";
		visibility="visible";
		
		document.getElementById(whattofade).style.display=display;
		document.getElementById(whattofade).style.visibility=visibility;
		
		fade(whattofade,0,100,5);
		
	}
// 	else {
// 		display="none";
// 		visibility="hidden";
// 	}
	
	//setopacity(whattofade, targetopacity);

// 	if (showorhide=="show") {
// 		
// 	}

	else {
		fade(whattofade,100,0,5,'zap');
	}

}

// OPACITY FUNCTION \\
function setopacity(whattofade, targetopacity) { // replaces so() \\
	
	var objectopacity = document.getElementById(whattofade).style;
	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.999; }
	}
	objectopacity.filter = "alpha(opacity=" + targetopacity + ")";
	objectopacity.KhtmlOpacity = (targetopacity / 100); 
	objectopacity.MozOpacity = (targetopacity / 100);
	objectopacity.opacity = (targetopacity / 100);
}



// X-FADE : 


function xFade(filetoload, wheretoloadit) { // needs a safety-catch \\
	
	if (safetycatch<1) {
		
		if (filetoload && document.getElementById(wheretoloadit)) {
			fade(wheretoloadit,100,0,5,filetoload);
		}
		else {
			alert('the target element, (`'+wheretoloadit+'`) does not exist');
		}
	}
}


function fade(wheretoloadit,startingopacity,endingopacity,increment,filetoload) {
	
	if (safetycatch<1) {
		safetycatch=1;

		fs = increment;
		fc = 0;
		fa = (startingopacity - endingopacity) / fs;
		
		if (filetoload) {
			ft = setInterval("controlfade('"+wheretoloadit+"','"+filetoload+"')", 60);
		}
		else {
			ft = setInterval("controlfade('"+wheretoloadit+"')", 60);
		}
	}
}

function fadeImage(wheretoloadit,startingopacity,endingopacity,increment,filetoload) {
	
	if (safetycatch<1) {
		safetycatch=1;

		fs = increment;
		fc = 0;
		fa = (startingopacity - endingopacity) / fs;
		
		if (filetoload) {
			ft = setInterval("controlfadeImage('"+wheretoloadit+"','"+filetoload+"')", 60);
		}
		else {
			ft = setInterval("controlfadeImage('"+wheretoloadit+"')", 60);
		}
	}
}

// XFADE IMAGES \\
var previousimage="";
var galleryimagesarray=[];

function setPreviousimage(imageid) {
	previousimage="image"+imageid;
}

function xFadeImage(imageid) {

	if (safetycatch<1) {
		
		if (previousimage) {
			//alert(previousimage);
			//parseInt(startingwidth)
			makeUnTaggable(previousimage);
		}
		
		if (imageid!=previousimage) {
			
			if (document.getElementById('tagshape')) {
				document.getElementById('tagshape').style.display="none";
				document.getElementById('tagshape').style.visibility="hidden";
			}

			galleryimagesarray=getElementsByClassName('galleryimage'); // thanks to Robert Nyman for the getElementsByClassName function, required for IE and other slackers \\

			
			for(var i=0; i<galleryimagesarray.length; i++) {
				if (!previousimage) {
					previousimage=galleryimagesarray[i].id;
				}
				galleryimagesarray[i].style.zIndex = 1;
			}
			
			if (imageid!=previousimage) {
			
				if (previousimage) {
					document.getElementById(previousimage).style.zIndex=50;
				}
	
				setopacity(imageid,0);
				
				document.getElementById(imageid).style.zIndex=100;
				document.getElementById(imageid).style.visibility="visible";
				
				if (document.getElementById(imageid)) {
					fadeImage(imageid,0,100,5);
				}
				else {
					alert('the target element, (`'+wheretoloadit+'`) does not exist');
				}
			}
		}
	}
}



function loadXMLDoc(filetoload, wheretoloadit,fesr) {

	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", filetoload, true);
		req.target=wheretoloadit;
		req.send(null);
		req.fesr=fesr;
	}
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", filetoload, true);
			req.send();
			req.fesr=fesr;
		}
	}
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			document.getElementById(req.target).innerHTML = req.responseText;
			if (req.fesr) {
				fade(req.target,0,100,5);
			}
		}
		else {
			//alert("There was a problem retrieving the file");
		}
	}
}

function controlfadeImage(whattofade,filetoload) {

	fc++; // goes from 1 to 5 (normally)
	
	if (fa < 0) { setopacity(whattofade, Math.abs(fc * fa)); }
	else { setopacity(whattofade, 100 - (fc * fa)); }
	
	if (fc == 100) {
		alert('hi');
	}

	if (fc == fs) {
		clearInterval(ft);
		safetycatch=0;
		if (previousimage && previousimage!=whattofade) {
			setopacity(previousimage,0);
		}
		
		if (filetoload=="zap") {
			document.getElementById(whattofade).style.display="none";
			document.getElementById(whattofade).style.visibility="hidden";
		}
		
		else if (filetoload) {
			loadXMLDoc(filetoload, whattofade,'fesr');
		}
		
		previousimage=whattofade;
	}
}


function controlfade(whattofade,filetoload) {

	fc++; // goes from 1 to 5 (normally)
	
	if (fa < 0) { setopacity(whattofade, Math.abs(fc * fa)); }
	else { setopacity(whattofade, 100 - (fc * fa)); }
	
	if (fc == 100) {
		alert('hi');
	}

	if (fc == fs) {
		clearInterval(ft);
		safetycatch=0;
		
		if (filetoload=="zap") {
			document.getElementById(whattofade).style.display="none";
			document.getElementById(whattofade).style.visibility="hidden";
		}
		
		else if (filetoload) {
			loadXMLDoc(filetoload, whattofade,'fesr');
		}
	}
}


/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};











function reSize(whattoresize,targetwidth,targetheight,extra) {
	
	if (safetycatch<1) {
		
		sizeoftag=targetwidth;
		
		if (document.getElementById(whattoresize)) {
			
			startingwidth=document.getElementById(whattoresize).style.width;
			startingheight=document.getElementById(whattoresize).style.height;
			
			if (startingwidth) { startingwidth=parseInt(startingwidth); }
			else { startingwidth=100; }
			
			if (startingheight) { startingheight=parseInt(startingheight); }
			else { startingheight=100; }
			
			if (targetwidth!=startingwidth) {
			
				rescale(whattoresize,startingwidth,startingheight,targetwidth,targetheight,4,extra); // INCREMENT SHOULD BE AN EVEN NO. \\
			}		
		}
		
	}
	
}	

function rescale (whattorescale,startingwidth,startingheight,targetwidth,targetheight,increment,extra) {
	if (safetycatch<1) {
		safetycatch=1;
		
		
 		difference = increment;
 		stepcount = 0;
// 		fa = (startingopacity - endingopacity) / fs;
		widthtarget=targetwidth;
		
		direction=startingwidth-targetwidth;
		newwidth=startingwidth;
		newheight=startingheight;
		
		if (extra) {
			scaler = setInterval("controlscale('"+whattorescale+"','"+extra+"')", 10);
		}
		else {
			scaler = setInterval("controlscale('"+whattorescale+"')", 10);
		}
		//controlscale(whattorescale);
	}
}

function controlscale(whattorescale,extra) {

 	stepcount++;

	currentmarginLeft=parseInt(document.getElementById(whattorescale).style.marginLeft);
	currentmarginTop=parseInt(document.getElementById(whattorescale).style.marginTop);
	
	if (direction<0) { // EXPAND \\
	
		newwidth=newwidth+(difference);
		newheight=newheight+(difference);
		
		newmarginTop=currentmarginTop-(difference/2);
		newmarginLeft=currentmarginLeft-(difference/2);
		
		if (newmarginLeft<0) { newmarginLeft=0; }
		
		if (imagewidth<((newmarginLeft+newwidth)+10)) {
			newmarginLeft=newmarginLeft-(difference/2);
		}
		
		if (newmarginTop<0) { newmarginTop=0; }
		
		if (imageheight<((newmarginTop+newheight)+10)) {
			newmarginTop=newmarginTop-(difference/2);
		}
		
	}
	
	else { // CONTRACT \\
	
		newwidth=newwidth-(difference);
		newheight=newheight-(difference);
		
		newmarginTop=currentmarginTop+(difference/2);
		newmarginLeft=currentmarginLeft+(difference/2);
	}
	
	document.getElementById(whattorescale).style.height = newheight+"px" ;
	document.getElementById(whattorescale).style.width = newwidth+"px" ;
	
	document.getElementById(whattorescale).style.marginLeft = newmarginLeft+"px" ;
	document.getElementById(whattorescale).style.marginTop = newmarginTop+"px" ;
	
	topmargin=newmarginTop;
	leftmargin=newmarginLeft;

	if ((direction<1 && newwidth >= widthtarget) || (direction>1 && newwidth <= widthtarget)) {
		document.getElementById(whattorescale).style.width = widthtarget+"px" ;
		
		if (scaler) {
			clearInterval(scaler);
			
			if (extra) {
				tagformid="tagimageform"+extra;
				document.forms[tagformid].elements['pos_x'].value=newmarginLeft;
				document.forms[tagformid].elements['pos_y'].value=newmarginTop;
				document.forms[tagformid].elements['tag_width'].value=newwidth;
			}
			
		}
		safetycatch=0;
	}
}


var safetycatch=0;

function xFadePanel(panelid) {

	if (safetycatch<1) {
		
		//if (imageid!=previousimage) {
			
			//for(var i=0; i<galleryimagesarray.length; i++) {
			
			if (panelid=="propertysearchsales") {
				document.getElementById("propertysearchrentals").style.zIndex = 10;
				//galleryimagesarray[i].style.zIndex = 10;
				document.getElementById("propertysearchrentals").style.visibility="hidden";
				document.getElementById("propertysearchrentals").style.display="none";
			}
			else if (panelid=="propertysearchrentals") {
				document.getElementById("propertysearchsales").style.zIndex = 10;
				//galleryimagesarray[i].style.zIndex = 10;
				document.getElementById("propertysearchsales").style.visibility="hidden";
				document.getElementById("propertysearchsales").style.display="none";
			}
		

			setopacity(panelid,0);
			
			document.getElementById(panelid).style.zIndex=100;
			document.getElementById(panelid).style.visibility="visible";
			document.getElementById(panelid).style.display="block";
			
			if (document.getElementById(panelid)) {
				fade(panelid,0,100,5);
			}
			else {
				alert('the target element, (`'+wheretoloadit+'`) does not exist');
			}
		//}
	}
}

