// JavaScript Document

var MOVEMENT_ID;
var CURRENT_CHARACTER;

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function initGraffiti()
{
	var characterDiv;
	var characters = new Array('meg', 'brad', 'brian', 'jade', 'kelly', 'matt', 'gary');
	for (var i=0; i < characters.length; i++)
	{
		characterDiv = document.getElementById(characters[i]);
		if (characterDiv.addEventListener) {
			characterDiv.addEventListener('mouseover', changeGraffiti, false);
			characterDiv.addEventListener('mouseout', changeGraffitiBack, false);
		} else if (characterDiv.attachEvent) {
			characterDiv.attachEvent('onmouseover', changeGraffiti);
			characterDiv.attachEvent('onmouseout', changeGraffitiBack);
		};
	};
};

leftBoxMover = {
	init: function() {
		el = document.getElementById('leftpanel');
		el.boxClip = [0, 456, 152, 0];
		el.maxHeight = 152;
		el.currentHeight = 152;
		el.state = 'open';
		el.stopMoving = 'N';
		el.newCycle = 'N';
	},
	setNewText: function(header, text, url, urltext) {
		el = document.getElementById('leftpanel');
		el.newHeader = header;
		el.newText = text;
		el.newUrl = url;
		el.newUrltext = urltext;
	},
	doMove: function() {
		el = document.getElementById('leftpanel');
		if (el.stopMoving == 'Y') {
			clearTimeout(MOVEMENT_ID);
			el.stopMoving = 'N';
			el.state = 'open';
			if (el.newCycle == 'Y') { el.newCycle = 'N'; } else { return; };
		};
		var direction = 'none';
		if (el.state == 'movingup' || el.state == 'open') { direction = 'up'; };
		if (el.state == 'movingdown' || el.state == 'closed') { direction = 'down'; };
		if (el.state == 'closed') {
			el.getElementsByTagName('h3')[0].firstChild.nodeValue = el.newHeader;
			el.getElementsByTagName('p')[0].firstChild.nodeValue = el.newText;
			el.getElementsByTagName('a')[0].href = el.newUrl;
			el.getElementsByTagName('a')[0].firstChild.nodeValue = el.newUrltext;
			el.state = 'movingdown';
			direction = 'down';
		};
		if (direction == 'up') {
			if (el.state != 'closed') {
				el.currentHeight -= 5;
				el.boxClip[2] = el.currentHeight;
				el.style.clip = 'rect(' + el.boxClip.join('px ') + 'px)';
				if (el.currentHeight <= 0) { el.state = 'closed'; } else { el.state = 'movingup'; };
				MOVEMENT_ID=setTimeout(leftBoxMover.doMove, 10);
			};
		};
		if (direction == 'down') {
			if (el.state != 'open') {
				el.currentHeight += 5;
				el.boxClip[2] = el.currentHeight;
				el.style.clip = 'rect(' + el.boxClip.join('px ') + 'px)';
				if (el.currentHeight >= el.maxHeight) { el.state = 'open'; el.stopMoving = 'Y'; } else { el.state = 'movingdown'; };
				MOVEMENT_ID=setTimeout(leftBoxMover.doMove, 10);
			};
		};
	},
	mOver: function(header, text, url, urltext) {
		el = document.getElementById('leftpanel');
		if (el.state == 'movingup' || el.state == 'movingdown') {
			el.stopMoving = 'Y';
			el.newCycle = 'Y';
		};
		leftBoxMover.doMove();
	}
};

function changeGraffiti()
{
	if (window.event) {
		var element = window.event.srcElement;
	} else {
		var element = this;
	};
	var graffiti = document.getElementById('graffitipic');
	var elementid = element.id;
	if (elementid.indexOf("pic") != -1) { elementid = elementid.substring(0,elementid.indexOf("pic")); };
	graffiti.parentNode.style.display = "none";
	switch(elementid)
	{
		case 'meg':
			graffiti.src = 'Images/hivgraffiti.png';
			graffiti.parentNode.style.left = '605px';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('Hi, I\'m Meg!', 'I have HIV. I\'m still discovering what that means for me, and I\'m a bit scared. ', 'STIs.php?sti=HIV', 'Find out more about HIV here.');
				leftBoxMover.mOver();
			};
			break;
		case 'brad':
			graffiti.src = 'Images/herpesgraffiti.png';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('\'Sup? I\'m Brad!', 'My doc just told me I got herpes. That really sucks. I dunno what it means though... guess I\'d better find out more. ', 'STIs.php?sti=Herpes', 'Find out more about herpes here.');
				leftBoxMover.mOver();
			};
			break;
		case 'brian':
			graffiti.src = 'Images/gongraffiti.png';
			graffiti.parentNode.style.left = '575px';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('\'Sup man? Brian. That\'s me.', 'Quack says I got gonorrhoea. Thought that was an olden days type thing. I ain\'t bothered though... don\'t even know what it means. ', 'STIs.php?sti=Gonorrhoea', 'Find out more about gonorrhoea here.');
				leftBoxMover.mOver();
			};
			break;
		case 'jade':
			graffiti.src = 'Images/chlamgraffiti.png';
			graffiti.parentNode.style.left = '605px';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('Hey people... I\'m Jade.', 'I just had some real bad news... looks like I\'ve gone and caught chlamydia. I guess I haven\'t been safe enough. What do I do now though? ', 'STIs.php?sti=Chlamydia', 'Find out more about chlamydia here.');
				leftBoxMover.mOver();
			};
			break;
		case 'kelly':
			graffiti.src = 'Images/syphilisgraffiti.png';
			graffiti.parentNode.style.bottom = '220px';
			graffiti.parentNode.style.left = '615px';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('Hey there... Kelly\'s the name.', 'You got to be kidding me... I have syphilis? Didn\'t Henry the Eighth have that? No way... this ain\'t happening. ', 'STIs.php?sti=Syphilis', 'Find out more about syphilis here.');
				leftBoxMover.mOver();
			};
			break;
		case 'matt':
			graffiti.src = 'Images/wartsgraffiti.png';
			graffiti.parentNode.style.bottom = '200px';
			graffiti.parentNode.style.left = '615px';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('Alright? I\'m Matt.', 'Genital warts. I mean, I have em. So embarrassing... why me? I thought I was pretty safe. What happens now... is there a cure? ', 'STIs.php?sti=Genitalwarts', 'Find out more about genital warts here.');
				leftBoxMover.mOver();
			};
			break;
		case 'gary':
			graffiti.src = 'Images/hepcgraffiti.png';
			if (CURRENT_CHARACTER != elementid) {
				CURRENT_CHARACTER = elementid;
				leftBoxMover.setNewText('Hey, I\'m Gary!', 'Just found out I have hep C. Something to do with my liver, I think. I feel ok, but they say this could kill me. Scary stuff. ', 'STIs.php?sti=HepC', 'Find out more about hepatitis C here.');
				leftBoxMover.mOver();
			};
			break;
		default:
			break;
	};
	graffiti.parentNode.style.display = "block";
	graffiti.parentNode.style.zIndex = 25;
};

function changeGraffitiBack()
{
	var graffiti = document.getElementById('graffitipic');
	graffiti.src = 'Images/STI.png';
	graffiti.parentNode.style.left = '595px';
	graffiti.parentNode.style.bottom = '160px';
	graffiti.parentNode.style.zIndex = 10;
};

function preloadGraffitiImages()
{
	image1 = new Image();
	image1.src = "Images/hivgraffiti.png";
	
	image2 = new Image();
	image2.src = "Images/hepcgraffiti.png";
	
	image3 = new Image();
	image3.src = "Images/herpesgraffiti.png";
	
	image4 = new Image();
	image4.src = "Images/gongraffiti.png";
	
	image5 = new Image();
	image5.src = "Images/chlamgraffiti.png";
	
	image6 = new Image();
	image6.src = "Images/wartsgraffiti.png";

	image7 = new Image();
	image7.src = "Images/syphilisgraffiti.png";
};


addLoadListener(initGraffiti);
addLoadListener(leftBoxMover.init);
addLoadListener(preloadGraffitiImages);
