////////////////////////////////////////////////////////////////////////////
// Jaredwilliam.Com Javascript Calls
////////////////////////////////////////////////////////////////////////////


// include other JS...
document.write('<script src="/assets/javascript/curvycorners.js"><\/script>');
document.write('<script src="/assets/javascript/scriptaculous/prototype.js"><\/script>');
document.write('<script src="/assets/javascript/scriptaculous/scriptaculous.js"><\/script>');

// call a global variable  (rather than sending it out and parsing it back in)
var theID = '';
var tArray = new Array();

// insert at cursor
function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue 
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}

// Capture Escape
document.onkeydown = function(e){ 
	if (e == null) { // ie 
  keycode = event.keyCode; 
 } else { // mozilla 
  keycode = e.which; 
 } 
 if(keycode == 27){ // escape, close box, esc 
  cancel();
 } 
};

function updateStyle(tag,cls) {
	seltext = (document.all)? document.selection.createRange() : document.getSelection();
	if (seltext == "")
		seltext == "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
	replacetext = '<'+tag;
	if (cls !="")
		replacetext += ' class="'+cls+'"';
	replacetext += '>'+seltext+'</'+tag+'>';
	document.execCommand('insertHTML', false,replacetext);
}

// insert a link for an element
function el() {
	var title=prompt('Enter link title (e.g. The Link):','');var link=prompt('Enter link location (e.g. http://www.gravitybear.com):','http://');var str='<a href="'+link+'">'+title+'</a>';insertAtCursor(document.getElementById('ef'),str);
}

// this function toggles between two visible divs
function toggleLayer(whichLayerShow, whichLayerHide)
{
	document.getElementById(whichLayerHide).style.display = "none";
	document.getElementById(whichLayerShow).style.display = "";
}

// this function shows a div
function showLayer(whichLayer)
{
	//$(document.getElementById(whichLayer)).fadeIn("slow");
	//document.getElementById(whichLayer).style.display = "block";
  new Effect.Appear(document.getElementById(whichLayer));
}


// this function allows the visible toggle for just one div
function showHideLayer(whichLayer)
{
	theLayer = document.getElementById(whichLayer);
	theLayer.style.display = (theLayer.style.display != "none") ? "none" : "";
}

// this function hides one div
function hideLayer(whichLayer)
{
	document.getElementById(whichLayer).style.display = "none";
	//new Effect.Fade( document.getElementById(whichLayer));
}

function hideLayerSmooth(whichLayer)
{
	new Effect.Fade(document.getElementById(whichLayer));
}

// this function hides many div
function hideLayers(whichLayers)
{
	var layersArray = whichLayers.split("::");
	for (var i = 0; i < layersArray.length; i++) {
		document.getElementById(layersArray[i]).style.display = "none";		
	}
}

function minAdmin() {
	document.getElementById('chooser').style.display= "none";
	document.getElementById('bannerSpacer').style.height="20px";
	document.getElementById('bannerSpacer').style.top="-50px";
	document.getElementById('bannerBGMiddle').style.marginTop="50px";
	document.getElementById('bannerBG').style.top="-50px";
}
function maxAdmin() {
	document.getElementById('chooser').style.display= "";
	document.getElementById('bannerSpacer').style.height="256px";
	document.getElementById('bannerSpacer').style.top="-300px";
	document.getElementById('bannerBGMiddle').style.marginTop="300px";
	document.getElementById('bannerBG').style.top="-300px";
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Elements Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// relign the height of the popup... 
function alignPopup(div) {
	if (window.pageYOffset)
		topValue = 10 + window.pageYOffset;
	else
		topValue = 120;
	//scroll(0,0);
	showLayer('screen');
	document.getElementById('popup').style.top = topValue+'px';
	//document.getElementById('screen').style.height = (window.innerHeight + window.pageYOffset)+'px'; 
}

// close popup div... 
function cancel() {
	hideLayer('popup');
	hideLayer('screen');
}

// find position of an object
function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
 
// browser check
if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4) {
	var ie = 1;
}
else {
	var ie = null;
}
if (navigator.userAgent.indexOf("Safari")!=-1) {
	var safari = 1;
}
else {
	var safari = null;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Image Rotator
/////////////////////////////////////////////////////////////////////////////////////////////////////////
var total = 0;
var currentRI = 0;
function rotator(t) {
	total = t;
	rotateTimeout = window.setTimeout("rotate();",3000);
}
function rotate() {
	new Effect.Fade(document.getElementById('ri'+currentRI),{ duration: 3.0 });
	if (currentRI == (total - 1))
		currentRI = 0;
	else
		currentRI = (currentRI + 1);
	new Effect.Appear(document.getElementById('ri'+currentRI),{ duration: 3.0 });
	rotateTimeout = window.setTimeout("rotate();",7000);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Scrolling Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// these variables control the movement
xTarget = 0;
xCoord = 0;
yTarget = 0;
yCoord = 0;
count  = 0;
currentThumb = 0;
var div = '';
// these functions move the contents of a div
function moveH (distance,theDiv) {
	div = theDiv;
	//new Effect.Fade(document.getElementById('caption'+currentThumb),{ duration: 1 });
	xTarget = xTarget + distance;
	if (xTarget > 0) {
		xCoord = (count*-800);
		xTarget = (count*-800)+800;
	}
	if (xTarget < (count*-800)) {
		xCoord = 0;
		xTarget = -800;
	}
	moveHorizontal();
}
function goToH(target,theDiv) {
	//new Effect.Fade(document.getElementById('caption'+currentThumb),{ duration: 2 });
	div = theDiv;
	xTarget = target;
	moveHorizontal();
}
function moveHorizontal() {

	var xDiff = xTarget - xCoord;
	xCoord += xDiff/4;
	$(div).style.left = xCoord + "px";
	if (Math.round(xDiff*10) == 0) {	
		window.clearTimeout(move_timeout);
		xCoord = xTarget;
		document.getElementById(div).style.left = xCoord + "px";
		document.getElementById('thumb'+currentThumb).className='thumb';
		//currentThumb = (xTarget/-690);
		if (xTarget == (count*-800))
			currentThumb = 0;
		
		document.getElementById('caption'+currentThumb).style.display='none';
			
		currentThumb = (xCoord / 800) * -1;
		document.getElementById('galleryProscenium').style.height = document.getElementById('image'+currentThumb).height+'px';
		//alert(document.getElementById('image'+currentThumb).height);
			
		new Effect.Appear(document.getElementById('caption'+currentThumb),{ duration: 1 });
		document.getElementById('thumb'+currentThumb).className='thumbSelected';
		move_timeout = null;
	} else {
		move_timeout = window.setTimeout("moveHorizontal();",27);
	}
}
function focusY(focusElement,theDiv) {
	target = document.getElementById(focusElement).offsetTop * -1;
//	if (ie) {
//		y = Math.round(y);
//		target = y + target;
//	}
	moveY(target,theDiv);
}
function moveY(target,theDiv) {
	div = theDiv;
	yTarget = target;
	moveVertical();
}
function yScroll(velocity, theDiv) {
	if (y < 0 || velocity < 0) {
		div = theDiv;
		yTarget = (y + velocity);
		moveVertical();
	}
}
function initiateMouseScrollY(theDiv,divToScroll) {
	div = divToScroll;
	if (theDiv.addEventListener && !safari)
     theDiv.addEventListener('DOMMouseScroll', scrollWheelMoveV, false);
	else
		theDiv.onmousewheel = document.onmousewheel = scrollWheelMoveV;
}
function removeMouseScrollY(theDiv,divToScroll) {
	if (theDiv.addEventListener && !safari)
     theDiv.removeEventListener('DOMMouseScroll', scrollWheelMoveV, false);
	else
		theDiv.onmousewheel = document.onmousewheel = null;
}
function scrollWheelMoveV(e) {
	if (window.event || window.Event) {
		if (!e) e = window.event;
		if (e.wheelDelta <= 0 || e.detail > 0 ) 
			velocity = -100;
		else 
			velocity = 100;
		if (y < 0 || velocity < 0) {
			yTarget = (y + velocity);
			moveVertical();
		}
	}
}
function moveVertical() {
	var yDiff = yTarget - y;
	y += yDiff/4;
	if (y > 0) {
		y = 0;
		yTarget = 0;
	}
	document.getElementById(div).style.top = y + "px";
	if (Math.round(yDiff) == 0) {
		window.clearTimeout(move_timeout);
		move_timeout = null;
	} else {
		move_timeout = window.setTimeout("moveVertical();",25);
	}
}

// these are generic AJAX functions
var req;

function aj(data,div) 
{
	// hope to deprecate this soon... here, we assign a universal
	// variable  for the div id
	theID = div;
	if (div !='') {
		document.getElementById(theID).innerHTML = '<img src="/images/spinner.gif" alt="loading..." />';
		//if (div !='status')
		//	showLayer('screen');
		showLayer(theID);
	}
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.open("POST", "/assets/ajax/ajax.php", true);
				req.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
        req.send(data);
        req.onreadystatechange = processReqChange;
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST", "/assets/ajax/ajax.php", true);
						req.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
            req.send(data);
        }
    }
}
function processReqChange() 
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200 && theID !='') {
			// set the id's HTML to the result
			document.getElementById(theID).innerHTML = req.responseText;
			//showLayer(theID);
		} 
		if (req.status != 200) {
			// explain the problem (we need error checking here)
			//alert("Ajax Javascript Request Error\n" + req.statusText);
		}
	}
}
