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

//          DARTH FADER !!!!       \\


var currentOP=0;
var direction="none";
var fading="";
var objectopacity=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() \\
	//alert(whattofade+":"+targetopacity);
	
	if (document.getElementById(whattofade)) {
		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 (imageid!=previousimage) {
		

			galleryimagesarray=getElementsByClassName('testimonial'); // 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;
				name=galleryimagesarray[i].id;
				setopacity(name, 0)
				//alert(name);
			}
			
			if (imageid!=previousimage) {
			
				if (previousimage) {
					document.getElementById(previousimage).style.zIndex=50;
				}
				
				setopacity(imageid,0);
				
				if (document.getElementById(imageid)) {
					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);
};









// GALERY IMAGE TAGGING BY MJB \\

var taggable=0;
var imagewidth=0;
var imageheight=0;
var lasttaggedid=0;
var lasttaggedimageid=0;

function makeTaggable(image,tagsize) {
	taggable=1;
	sizeoftag=tagsize;
	imageid="gi"+image;
	imagewidth=document.getElementById(imageid).width;
	imageheight=document.getElementById(imageid).height;
	document.getElementById(imageid).className="crosshair";

	tagformid="tagimage#"+image;
	showorhidethenfade (tagformid, 100, 'show');
	//tagimage#image
}

function makeUnTaggable(image) {
	taggable=0;

	image=image.replace('image','');
	//image_number=imageid.replace('gi','');
	imageid="gi"+image;

//	imagewidth=document.getElementById(imageid).width;
//	imageheight=document.getElementById(imageid).height;
	document.getElementById(imageid).className="";

 	tagformid="tagimage#"+image;

 	//fade(tagformid,100,0,5,'zap');
 	
	if (document.getElementById(tagformid)) {
	 	document.getElementById(tagformid).style.display="none";
 		document.getElementById(tagformid).style.visibility="hidden";
	 	setopacity(tagformid,0);
	}
	
 	document.getElementById('tagshape').style.display="none";
 	document.getElementById('tagshape').style.visibility="hidden";

}

function tagContact(id,imageid) {
	
	if (taggable==1 && sizeoftag>0) {
		
		//alert(document.getElementById(imageid).width);
		image_number=imageid.replace('gi','');
		tagformid="tagimageform"+image_number;
		
		imagewidth=document.getElementById(imageid).width;
		imageheight=document.getElementById(imageid).height;
		
		pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("id").offsetLeft;
		pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("id").offsetTop;
		
		document.getElementById('tagshape').style.height = sizeoftag+"px" ;
		document.getElementById('tagshape').style.width = sizeoftag+"px" ;
		
		topmargin=pos_y-(sizeoftag/2)-4;
		if (topmargin<0) { topmargin=0; }
		else if (topmargin>(imageheight-sizeoftag)) { topmargin=imageheight-sizeoftag-10; }
		
		leftmargin=pos_x-(sizeoftag/2)-4;
		if (leftmargin<0) { leftmargin=0; }
		else if (leftmargin>(imagewidth-sizeoftag)) { leftmargin=imagewidth-sizeoftag-10; }
		
		document.getElementById('tagshape').style.marginLeft = leftmargin+"px" ;
		document.getElementById('tagshape').style.marginTop = topmargin+"px" ;
		
		document.forms[tagformid].elements['pos_x'].value=leftmargin;
		document.forms[tagformid].elements['pos_y'].value=topmargin;
		document.forms[tagformid].elements['tag_width'].value=sizeoftag;
		
		setopacity('tagshape',0);
		document.getElementById('tagshape').style.display="block";
		document.getElementById('tagshape').style.visibility="visible";
		document.getElementById('tagshape').style.zIndex=150;
		fade('tagshape',0,50,5);
		
		lasttagged=id;
		lasttaggedimageid=imageid;
		
		
	}
}

function reTag(e) {
	
	if (lasttaggedimageid) {

		image_number=lasttaggedimageid.replace('gi','');
		tagformid="tagimageform"+image_number;
		
		imagewidth=document.getElementById(imageid).width;
		imageheight=document.getElementById(imageid).height;
		
		pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("e").offsetLeft;
		pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("e").offsetTop;
		
		//document.getElementById('tagshape').style.height = sizeoftag+"px" ;
		//document.getElementById('tagshape').style.width = sizeoftag+"px" ;
		

		moveX=pos_x-sizeoftag/2;
		moveY=pos_y-sizeoftag/2;
		
		topmargin=topmargin+moveY-4;
		if (topmargin<0) { topmargin=0; }
		else if (topmargin>(imageheight-sizeoftag)) { topmargin=imageheight-sizeoftag-10; }
		
		leftmargin=leftmargin+moveX-4;
		if (leftmargin<0) { leftmargin=0; }
 		else if (leftmargin>(imagewidth-sizeoftag)) { leftmargin=imagewidth-sizeoftag-10; }
 		
 		document.getElementById('tagshape').style.marginLeft = leftmargin+"px" ;
 		document.getElementById('tagshape').style.marginTop = topmargin+"px" ;

 		document.forms[tagformid].elements['pos_x'].value=leftmargin;
 		document.forms[tagformid].elements['pos_y'].value=topmargin;
// 		document.forms[tagformid].elements['tag_width'].value=sizeoftag;
		

		document.getElementById('tagshape').style.zIndex=150;
	}

}

function showTag(image_id,pos_x,pos_y,tag_width) {
	
	if (pos_x && pos_y && tag_width) {
		
		imageid="gi"+image_id;
		imagewidth=document.getElementById(imageid).width;
		imageheight=document.getElementById(imageid).height;
		
		document.getElementById('tagshape').style.marginLeft = pos_x+"px" ;
		document.getElementById('tagshape').style.marginTop = pos_y+"px" ;
		
		document.getElementById('tagshape').style.height = tag_width+"px" ;
		document.getElementById('tagshape').style.width = tag_width+"px" ;
		
		setopacity('tagshape',0);
		document.getElementById('tagshape').style.display="block";
		document.getElementById('tagshape').style.visibility="visible";
		document.getElementById('tagshape').style.zIndex=150;
		fade('tagshape',0,100,5);
		
	}	
}

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;
	}
}





// MJB TABS \\

function showTab(tab) {
	if (document.getElementById(tab)) {
		// hide all
		
		var tabsarray=document.getElementsByClassName('tab');
		for(var i=0; i<tabsarray.length; i++) {
			tabsarray[i].style.display="none";
			tabsarray[i].style.visibility="hidden";
		}
		
		document.getElementById(tab).style.display="block";
		document.getElementById(tab).style.visibility="visible";

		document.getElementById(tab).style.filter = "alpha(opacity=100)";
		document.getElementById(tab).style.KhtmlOpacity = 100; 
		document.getElementById(tab).style.MozOpacity = 100;
		document.getElementById(tab).style.opacity = 100;
		
	}
}


