var speed = 750;
var pDiv, pHeight, pWidth;
var eLeft = new Array();
var eTop = new Array();
var eWidth = new Array();
var eHeight = new Array();
var eImageWidth = new Array();
var eImageHeight = new Array();
var eImageTop = new Array();
var expanders = 0;
function createExpander(parentDiv) 
{
	pDiv = $(parentDiv);
	pDiv.style.position = "relative";
	pDiv.style.overflow = "hidden";
	pHeight = pDiv.offsetHeight;
	pWidth = pDiv.offsetWidth;
}

var currentSection = 0;
var oldCurrent = 0;
var change;
var action = new Array();
var actionImage = new Array();
var newLeft = new Array();
var newTop = new Array();
function createActions(idNum)
{
	action[idNum] = new Fx.Morph('expander'+idNum, {wait: false, duration: speed});
	actionImage[idNum] = new Fx.Morph('expanderImage'+idNum, {wait: false, duration: speed});
}
function getNewLoc(idNum,sectTop,sectLeft)
{
	var leftPer = (($('expander'+idNum).offsetLeft + ($('expander'+idNum).offsetWidth / 2)) - ($('expander'+currentSection).offsetLeft + ($('expander'+currentSection).offsetWidth / 2))) / ($('expander'+currentSection).offsetWidth / 2);
	var topPer = (($('expander'+idNum).offsetTop + ($('expander'+idNum).offsetHeight / 2)) - ($('expander'+currentSection).offsetTop + ($('expander'+currentSection).offsetHeight / 2))) / ($('expander'+currentSection).offsetHeight / 2);

	
	if((($('expander'+idNum).offsetLeft) + ($('expander'+idNum).offsetWidth / 2)) < ($('expander'+currentSection).offsetLeft + ($('expander'+currentSection).offsetWidth / 2)))
	{
			newLeft[idNum] = ((sectLeft - $('expander'+idNum).offsetWidth + 50) + (leftPer * $('expander'+idNum).offsetWidth));
	}
	else if((($('expander'+idNum).offsetLeft) + ($('expander'+idNum).offsetWidth / 2)) > ($('expander'+currentSection).offsetLeft + ($('expander'+currentSection).offsetWidth / 2)))
	{
			newLeft[idNum] = ((sectLeft + (pWidth-30) - 50) + (leftPer * $('expander'+idNum).offsetWidth));
	}
	else 
		newLeft[idNum] = $('expander'+idNum).offsetLeft;
	if((($('expander'+idNum).offsetTop + ($('expander'+idNum).offsetHeight / 2))) < ($('expander'+currentSection).offsetTop + ($('expander'+currentSection).offsetHeight / 2)))
	{
			newTop[idNum] = ((sectTop - $('expander'+idNum).offsetHeight + 30) + (topPer * $('expander'+idNum).offsetHeight));
	}
	else if((($('expander'+idNum).offsetTop + ($('expander'+idNum).offsetHeight / 2))) > ($('expander'+currentSection).offsetTop + ($('expander'+currentSection).offsetHeight / 2)))
	{
			newTop[idNum] = ((sectTop + (pHeight-30) - 30) + (topPer * $('expander'+idNum).offsetHeight));
	}
	else
		newTop[idNum] = $('expander'+idNum).offsetTop;
	action[idNum].start({
		top: newTop[idNum]+'px',
		left: newLeft[idNum]+'px'
	});
}
function resetLocs(idNum)
{
	$('expanderText'+idNum).fade('hide');
	action[idNum].start({
		width: eWidth[idNum]+'px',
		height: eHeight[idNum]+'px',
		top: eTop[idNum]+'px',
		left: eLeft[idNum]+'px'
	});
	actionImage[idNum].start({
		width: eWidth[idNum]+'px',
		height: eHeight[idNum]+'px',
		marginTop: '0px' 
	});
}
function clickAction(idNum)
{
	if(currentSection != 0)
	{
		for(y=1;y<=expanders;y++)
			resetLocs(y);
		oldCurrent = idNum;
		$clear(change);
		change = changeExpand.delay(speed);
	}
	else {
		currentSection = idNum;
		var changeTop = pDiv.scrollTop + 15;
		var changeLeft = pDiv.scrollLeft + 15;
		var topLocation = $('expander'+idNum).offsetTop + ($('expander'+idNum).offsetHeight / 2);
		var leftLocation = $('expander'+idNum).offsetLeft + ($('expander'+idNum).offsetWidth / 2);
		for(y=1;y<=expanders;y++)
		{
			if(y!=idNum)
				getNewLoc(y,changeTop,changeLeft);
		}
		action[idNum].start({
			width: (pWidth-30)+'px',
			height:(pHeight-30)+'px',
			top: changeTop+'px',
			left: changeLeft+'px'
		});
		$('expanderText'+idNum).fade('hide');
		actionImage[idNum].start({
			width: eImageWidth[idNum]+'px',
			height: eImageHeight[idNum]+'px',
			marginTop: eImageTop[idNum]+'px'
		}).chain(function(e){
			$('expanderText'+idNum).fade(1);
		});
	}
}
function createClick(idNum)
{
	var currentZ;
	$('expanderImage'+idNum).addEvent('click', function(e) {
		e.stop();
		clickAction(idNum);
	});
	$('expander'+idNum).addEvent('mouseenter', function(e) {
		e.stop();
		if(currentSection == 0)
		{
			if($('expander'+idNum).style.zIndex < 9999)
				currentZ = $('expander'+idNum).style.zIndex;
			$('expander'+idNum).style.zIndex = 9999;
		}
	});
	$('expander'+idNum).addEvent('mouseleave', function(e) {
		e.stop();
		if(currentSection == 0)
			$('expander'+idNum).style.zIndex = currentZ;
	});
}
var changeExpand = function () {
	if(oldCurrent != currentSection)
	{
		currentSection = 0;
		clickAction(oldCurrent);
	}
	else 
		currentSection = 0;
}
function endExpander()
{
	if($('work-back').getAttribute('rel') != "stopExpand")
	{
		for(x=1;x<=expanders;x++)
			createActions(x);
		for(x=1;x<=expanders;x++)
			createClick(x);
	}
}
function getStyle(idNum)
{
	if($("expanderText"+idNum).offsetWidth != 815 && $("expanderText"+idNum).offsetWidth != 835) 
	{
		var newTextTop = (pHeight - $("expanderText"+idNum).offsetHeight - 30) / 2;
		$("expanderText"+idNum).style.margin = newTextTop + "px 10px 0px 10px";
		eImageTop[idNum] = (pHeight - $("expander"+idNum).offsetHeight - 30) / 2;
	}
	else
		eImageTop[idNum] = 0
	eLeft[idNum] = $("expander"+idNum).offsetLeft;
	eTop[idNum] = $("expander"+idNum).offsetTop;
	eImageWidth[idNum] = $("expander"+idNum).offsetWidth;
	eImageHeight[idNum] = $("expander"+idNum).offsetHeight;
	eWidth[idNum] = $("expanderImage"+idNum).offsetWidth;
	eHeight[idNum] = $("expanderImage"+idNum).offsetHeight;
	$("expander"+idNum).style.left = eLeft[idNum] + "px";
	$("expander"+idNum).style.top = eTop[idNum] + "px";
	$("expander"+idNum).style.width = eWidth[idNum] + "px";
	$("expander"+idNum).style.height = eHeight[idNum] + "px";
	$("expander"+idNum).fade('hide');
	$("expander"+idNum).fade(1);
}


function createSlide() {
	var slideSpeed = 1000;
	var campHeight = $('camp-text').offsetHeight;
	$('camp-back').style.height = campHeight + "px";
	clickRead = new Fx.Morph('camp-info', {wait: false, duration: slideSpeed});
	$('camp-read').addEvent('click',function(e){
		$('camp-first').fade(0);
		$('camp-read').fade(0);
		if($('hide-title'))
			$('hide-title').fade(0);
		clickRead.start({
			height : campHeight + 'px'
		}).chain(function(e){
			$('camp-text').fade('hide');
			$('camp-text').fade(1);
		});
	});
	$('camp-text').addEvent('mouseleave',function(e){
		$('camp-text').fade(0);
		clickRead.start({
			height : 32 + 'px'
		}).chain(function(e){
			$('camp-first').fade(1);
			$('camp-read').fade(1);
			if($('hide-title'))
				$('hide-title').fade(1);
		});
	});
}


window.addEvent('load', function() {
	$$('.expandable').each(function(exp){
		expanders++;
	});
	createExpander('work-back');
	for(z=1;z<=expanders;z++)
		getStyle(z);
	endExpander();
	if($('camp-info'))
		createSlide();
});
