////////////////////////////////////////////////////////////////////////////
// Jared Alessandroni's Javascript Calls
////////////////////////////////////////////////////////////////////////////


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

// call a global variable  (rather than sending it out and parsing it back in)
var theID = '';
var theID2 = '';
var menuOpen = 0;

// cookie functions
function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
	if (document.cookie.length>0) {
 	c_start=document.cookie.indexOf(cookieName + "=");
  if (c_start!=-1)
    {
    c_start=c_start + cookieName.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

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

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


function popperClose() {
	hideLayer('popper');
	hideLayer('popperUnder');
}

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

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

function showSub(whichLayer)
{
	if (document.getElementById(whichLayer).style.display=='none')
		$(document.getElementById(whichLayer)).fadeIn(300,showSubCallback());
}

function showSubCallback() {
	menuOpen = 2;
}

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

// 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));
	$(document.getElementById(whichLayer)).fadeOut(600);
}

function hideSub(whichLayer) {
	setTimeout('hideSubWrapped(\''+whichLayer+'\')',350);
}
function hideSubWrapped(whichLayer) {	
	if (menuOpen !=2) {
		hideLayer(whichLayer);
		menuOpen=0;
	}
}

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



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

// admin functions
function multiAdmin(id,action) {
	document.getElementById('id').value = id;
	document.getElementById('lDo').value = action;
	document.multiAdminForm.submit();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Scrolling Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// these variables control the movement
xTarget = 0;
x = 0;
yTarget = 0;
y = 0;
var div = '';
floor = 0;


var playing = 1;
var current = 0;
var next = 1;
// these functions move the contents of a div
function startPlayFeature(quantity) {
	floor = ((quantity)*-410);
	window.setTimeout("playFeature();",8000);
}
function playFeature() {
	if (next !=current) {
		document.getElementById('feature0').style.zIndex=0;
		document.getElementById('feature1').style.zIndex=0;
		document.getElementById('feature2').style.zIndex=0;
		document.getElementById('feature3').style.zIndex=0;
		document.getElementById('feature4').style.zIndex=0;
		document.getElementById('featureMenu0').className='featureMenuItem';
		document.getElementById('featureMenu1').className='featureMenuItem';
		document.getElementById('featureMenu2').className='featureMenuItem';
		document.getElementById('featureMenu3').className='featureMenuItem';
		document.getElementById('featureMenu4').className='featureMenuItem';
		document.getElementById('featureMenu'+next).className='featureMenuItemSelected';
		document.getElementById('feature'+current).style.zIndex=4;
		document.getElementById('feature'+next).style.display='none';
		document.getElementById('feature'+next).style.zIndex=5;
		showLayer('feature'+next);
		current = next;
	}
	if (playing == 1) {
		if (current < 4)
			next = current + 1;
		else
			next = 0;
		window.setTimeout("playFeature();",4000);
	}
}


function moveH (distance,theDiv) {
	div = theDiv;
	xTarget = Math.round(xTarget) + distance;
	if (xTarget <= floor) {
		x = 0;
		xTarget = -410;	}
	if (xTarget > 0) {
		x = (floor + 410);
		xTarget = (floor + 820);	}
	moveHorizontal();
}
function goToH(target,theDiv) {
	div = theDiv;
	xTarget = target;
	moveHorizontal();
}
function moveHorizontal() {
	var xDiff = xTarget - x;
	x += xDiff/4;
	document.getElementById(div).style.left = x + "px";
	if (Math.round(xDiff) == 0) {
		window.clearTimeout(move_timeout);
		move_timeout = null;
		document.getElementById(div).style.left = xTarget + "px";
	} else {
		move_timeout = window.setTimeout("moveHorizontal();",25);
	}
}
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;
var req2;

function aj(data,div) 
{
	if (!div)
		div = 'popper';
	// hope to deprecate this soon... here, we assign a universal
	// variable  for the div id
	theID = div;
	if (theID == 'popper') {
		showLayer('popper');
		showLayer('popperUnder');	
	}
	if (div !='') {
		document.getElementById(theID).innerHTML = '<br /><br /><img src="/images/spinner.gif" alt="loading..." />';
	}
	// 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;
			//new Effect.Appear( document.getElementById(theID));
			$(document.getElementById(theID)).fadeIn(1000);
			if (theID == 'adminWrapper')
				widgInit();
		} 
		if (req.status != 200) {
			// explain the problem (we need error checking here)
			alert("Ajax Javascript Request Error\n" + req.statusText);
		}
	}
}

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

function messageCompose(who) {
	aj('f=ajaxMessageCompose&r='+who,'popper');
}

