var fibernocciOne = 2;
var fibernocciTwo = 3;
var emailToSend;

////////////////////////////////////////////////////////////////////////////////////
// ARTICLE

var articleStatus = "stopped";
var articleHeight = 0;

function showArticle(id) {
	if (articleStatus != "in transit") {
		fibernocciOne = 2;
		fibernocciTwo = 3;
		if (document.getElementById("article_"+id).style.height == "1px") {
			articleStatus = "in transit";
			articleHeight = 1;
			openArticleLoop=setInterval("openArticle("+id+")",100);
		} else {
			articleStatus = "in transit";
			articleHeight = 1;
			closeArticleLoop=setInterval("closeArticle("+id+")",100);
		}
	}
}

function openArticle(id) {
	articleHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = articleHeight;

	if (articleHeight >= 150) {
		document.getElementById("article_"+id).style.height = "auto";
		clearInterval(openArticleLoop);
		articleStatus = "stopped";
	} else {
		document.getElementById("article_"+id).style.height = articleHeight + "px";
	}
}

function closeArticle(id) {
	articleHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = articleHeight;
	
	if (articleHeight >= 150) {
		document.getElementById("article_"+id).style.height = "1px";
		clearInterval(closeArticleLoop);
		articleStatus = "stopped";
	} else {
		document.getElementById("article_"+id).style.height = (150 - articleHeight) + "px";
	}
}

////////////////////////////////////////////////////////////////////////////////////
// EMAIL

var emailStatus = "stopped";
var emailHeight = 0;

function showEmail(id) {
	if (emailStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("email_"+id).style.height == "1px") {
			emailStatus = "in transit";
			emailHeight = 1;
			openEmailLoop=setInterval("openEmail("+id+")",100);
		} else {
			emailStatus = "in transit";
			emailHeight = 1;
			closeEmailLoop=setInterval("closeEmail("+id+")",100);
		}
	}
}

function openEmail(id) {
	emailHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = emailHeight;

	if (emailHeight >= 50) {
		document.getElementById("email_"+id).style.height = "auto";
		clearInterval(openEmailLoop);
		emailStatus = "stopped";
	} else {
		document.getElementById("email_"+id).style.height = emailHeight + "px";
	}
}

function closeEmail(id) {
	emailHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = emailHeight;
	
	if (emailHeight >= 50) {
		document.getElementById("email_"+id).style.height = "1px";
		clearInterval(closeEmailLoop);
		emailStatus = "stopped";
	} else {
		document.getElementById("email_"+id).style.height = (50 - emailHeight) + "px";
	}
}

function sendEmail(id) {
	if (!isWorking && http) {
		emailToSend = id;
		http.open("GET", "ajax/send_email.html?id=" + id + "&name=" + document.forms['emailForm'+id]['name'].value + "&email=" + document.forms['emailForm'+id]['email'].value, true);
		http.onreadystatechange = handleHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			document.getElementById("emailInner_"+emailToSend).innerHTML = "<DIV STYLE='font-size:9pt;padding:9px;color:#786b05;'><CENTER>This article has been mailed</CENTER></DIV>";
			isWorking = false;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////
// SUBSCRIBE

var subscribeStatus = "stopped";
var subscribeHeight = 0;

function showSubscribe() {
	if (subscribeStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("subscribe").style.height == "1px") {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			openSubscribeLoop=setInterval("openSubscribe()",100);
		} else {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			closeSubscribeLoop=setInterval("closeSubscribe()",100);
		}
	}
}

function openSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;

	if (subscribeHeight >= 70) {
		document.getElementById("subscribe").style.height = "70px";
		clearInterval(openSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height =subscribeHeight + "px";
	}
}

function closeSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;
	
	if (subscribeHeight >= 70) {
		document.getElementById("subscribe").style.height = "1px";
		clearInterval(closeSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height = (70 - subscribeHeight) + "px";
	}
}

function sendSubscribe() {
	if (!isWorking && http) {
		if (document.forms['subscribeForm']['email'].value) {
			http.open("GET", "ajax/subscribe.html?email=" + document.forms['subscribeForm']['email'].value, true);
			http.onreadystatechange = handleHttpResponseTwo;
			isWorking = true;
			http.send(null);
		}
	}
}

function handleHttpResponseTwo() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			
			rexp = /success/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>Thank you for signing up</CENTER></DIV>";
			}
			
			rexp = /duplicate/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>You are already signed up</CENTER></DIV>";
			}
			
			isWorking = false;
		}
	}
}


////////////////////////////////////////////////////////////////////////////////////
// SLIDES

if (navigator.appVersion.indexOf('Mac') != -1) mac = 1;
else mac = 0;

var quotes=new Array();
//var size=new Array();

startImage = 1;


function switchImage(direction) {
	endImage = quotes.length-1;
	
	if (direction == "next"){startImage++}
	else {startImage--}
	if (startImage == endImage+1){startImage = 1};
	if (startImage == 0){startImage = 1};
	
	if (!mac){document.getElementById('main').filters.blendTrans.Apply()}
	
	document.getElementById('main').src = "/images/slides/"+slideCategory+"_"+startImage+".jpg";

	document.getElementById('quote').innerHTML = quotes[startImage];
//	document.getElementById('quote').style.height = (size[startImage] * 16);
//	document.getElementById('quote').style.top = -21 - (16*size[startImage]);

	if (!mac){document.getElementById('main').filters.blendTrans.Play()}
	
	document.getElementById('progressMarker').style.marginLeft = ((306 / endImage) * (startImage-1)) + "px";
	
	// PRELOAD NEXT IMAGE
	ImageNext = new Image();
	ImageNext.src = "/images/slides/"+slideCategory+"_"+(startImage+1)+".jpg";
}