if(defined(window.addEventListener)) window.addEventListener('load', function(){return quiz.onLoad();}, false);
else if(defined(window.attachEvent)) window.attachEvent('onload', function(){return quiz.onLoad();});
else window.onload = function(){return quiz.onLoad();};

var quiz = new function()
{
	this.questions = {};
	this._currquestion = null;
	this._currresponse = null;
	
	this.setQuestion = function(sQuestionID,sQuestion,image)
	{
		var oQuestion = {};
		oQuestion.id = sQuestionID;
		oQuestion.html = sQuestion;
		oQuestion.image = if_undef(image,null);
		oQuestion.responses = [];
		
		this._currquestion = oQuestion.id;
		this._currresponse = null;
		this.questions[oQuestion.id] = oQuestion;
		return true;
	}
	this.setResponse = function(sResponse)
	{
		if(null == this._currquestion) return false;
		var oQuestion = this.questions[this._currquestion];
		
		var oResponse = {};
		
		oResponse.id = oQuestion.responses.length;
		oResponse.html = sResponse;
		oResponse.leanings = [];
		
		this._currresponse = oResponse.id;
		oQuestion.responses.push(oResponse);

		return true;
	}
	this.setLeaning = function(sCode,iValue)
	{
		if(null == this._currquestion) return false;
		if(null == this._currresponse) return false;


		var oResponse = this.questions[this._currquestion].responses[this._currresponse];
				
		oResponse.leanings[sCode] = iValue;

		return true;
	}
	
	
	
	
	this.leanings = {'current':{},'currentmax':{},'currentmin':{},'max':{},'min':{}};
	
	this.question = -1;
	
	this.question_ids = [
		'quiz01','quiz02','quiz03','quiz04','quiz05','quiz06','quiz07','quiz08','quiz09','quiz10','quiz11','quiz12','quiz13','quiz14','quiz15','quiz16','quiz17','quiz18'
	];
	
	this.move_bar = null;
	
	this.onLoad = function()
	{
		this.preloadNextImage();
		
		this.http = 'undefined' != typeof XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
		
		//lb.dhtml.loadLayer('progress_bar');
		//this.move_bar = lb.dhtml.transform('move_bar');
		
	}
	
	this.postResults = function()
	{
		var body = [];
		
		body.push('correct%5Bcurrent%5D='+this.getLeaning('current','correct'));
		body.push('correct%5Bmax%5D='+this.getLeaning('max','correct'));
		
		if(navigator.appName == "Microsoft Internet Explorer")
			var http = new ActiveXObject("Microsoft.XMLHTTP");
		else
			var http = new XMLHttpRequest();
		
		http.open('POST','results.php',false);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				
		body = body.join('&');
		http.send(body);
		return 200 == http.status;
	}
	
	this.nextQuestion = function()
	{
		++this.question;
		if(this.question < this.question_ids.length)
		{
			this.showQuestion(this.question);
			this.preloadNextImage();
			return true;
		}
		return this.showResult();
	}
	
	this.showQuestion = function(iQuestionID)
	{
		var oQuestion = this.questions[this.question_ids[this.question]];
		
		var sQuestionOf = 'Photo '+(this.question+1)+' of '+this.question_ids.length;
		var sContent = '';
		sContent += '<div style="text-align: left; padding: 17px 10px;">';
		sContent += '<div style="color: rgb(102,204,51); font-size: 10px; line-height: normal; font-weight: bold;">'+sQuestionOf+'</div>';
		
		sContent += '<table class="format" cellspacing="0" style="width: 100%; margin-top: 6px;">';
		sContent += '<tr>';
		sContent += '<td style="width: 284px; text-align: center; vertical-align: middle;">';
		if(null != oQuestion.image)
		{
			sContent += '<img src="_images/butts/'+oQuestion.image.filename+'" style="width: '+oQuestion.image.width+'px; height: '+oQuestion.image.height+'px;" alt="" />';
		}
		sContent += '</td><td style="text-align: left; vertical-align: top; padding-left: 36px;">';
		
		sContent += '<p>'+oQuestion.html+'</p>';
		sContent += '<table class="format" cellspacing="0">';
		for(var i = 0; i < oQuestion.responses.length; ++i)
		{
			sContent += '<tr>';
			sContent += '<td style="text-align: right; padding-right: 8px; padding-top: 5px;">'+(i+1)+'.</td>';
			sContent += '<td style="padding-top: 5px;"><span class="link" onclick="quiz.submitResponse('+i+');">'+oQuestion.responses[i].html+'</span></td>';
			sContent += '</tr>';
		}
		sContent += '</table>';
		
		sContent += '</td>';
		sContent += '</tr>';
		sContent += '</table>';
		
		sContent += '</div>';
		
		$('quiz_content').innerHTML = sContent;
	}
	this.showResult = function()
	{
		this.postResults();
		window.document.location.href = 'results.html';
	}
	this.submitResponse = function(iSubmittedResponseID)
	{
		var oResponse = null;
		var maxleanings = {};
		var minleanings = {};
		
		for(var iResponseID in this.questions[this.question_ids[this.question]].responses)
		{
			var oResponse = this.questions[this.question_ids[this.question]].responses[iResponseID];
			
			for(var sLeaning in oResponse.leanings)
			{
				if(!defined(maxleanings[sLeaning]) || oResponse.leanings[sLeaning] > maxleanings[sLeaning])
					maxleanings[sLeaning] = oResponse.leanings[sLeaning];
				
				if(!defined(minleanings[sLeaning]) || oResponse.leanings[sLeaning] < minleanings[sLeaning])
					minleanings[sLeaning] = oResponse.leanings[sLeaning];

				if(iResponseID == iSubmittedResponseID)
					this.addLeaning('current', sLeaning, oResponse.leanings[sLeaning]);
			}
		}
		
		for(var sLeaning in maxleanings)
			this.addLeaning('currentmax', sLeaning, maxleanings[sLeaning]);
		
		for(var sLeaning in minleanings)
			this.addLeaning('currentmin', sLeaning, minleanings[sLeaning]);
		
		this.updateBar(this.getLeaning('current','correct')/this.getLeaning('max','correct'));
		this.nextQuestion();
	}
	
	this.updateBar = function(fPercent)
	{
		var width = Math.round(fPercent*403);
/*
		var current_width = lb.dhtml.layers.progress_bar.get('width');
		
		var distance = Math.abs(width - current_width);
		var time = (distance / 403) * 1000
*/
		if(width  < 1)
		{
			//this.move_bar.layer('progress_bar','width','to',1);
			//this.move_bar.run(time,function(){lb.dhtml.layers.progress_bar.hide();});
			$('quiz_progress_bar').style.width = '0px';
			$('quiz_progress_bar').style.display = 'none';
		}
		else
		{
			//this.move_bar.layer('progress_bar','width','to',width);
			//lb.dhtml.layers.progress_bar.show();
			//this.move_bar.run(time);
			$('quiz_progress_bar').style.width = width+'px';
			$('quiz_progress_bar').style.display = '';
			
		}
	}
	
	this.addLeaning = function(type,leaning,value)
	{
		if(!defined(this.leanings[type][leaning])) this.leanings[type][leaning] = value;
		else this.leanings[type][leaning] += value;
	}

	this.getLeaning = function(type,leaning)
	{
		if(!defined(this.leanings[type])) return 0;
		if(!defined(this.leanings[type][leaning])) return 0;
		return this.leanings[type][leaning];
	}
	this.preloadNextImage = function()
	{
		var question_id = this.question + 1;
		
		var image = new Image();
		
		if(question_id < this.question_ids.length)
		{
			image.src = '_images/butts/'+this.questions[this.question_ids[question_id]].image.filename;
		}
	}
	this.start = function()
	{
// Total Up possible leanings
//		for(var i in this.question_ids)
	        for(var i=0; i < this.question_ids.length; ++i)
		{
			var maxleanings = {};
			var minleanings = {};
			
			for(var iResponseID in this.questions[this.question_ids[i]].responses)
			{
				var oResponse = this.questions[this.question_ids[i]].responses[iResponseID];
				
				for(var sLeaning in oResponse.leanings)
				{
					if(!defined(maxleanings[sLeaning]) || oResponse.leanings[sLeaning] > maxleanings[sLeaning])
						maxleanings[sLeaning] = oResponse.leanings[sLeaning];
					
					if(!defined(minleanings[sLeaning]) || oResponse.leanings[sLeaning] < minleanings[sLeaning])
						minleanings[sLeaning] = oResponse.leanings[sLeaning];
				}
				
			}
			
			for(var sLeaning in maxleanings)
				this.addLeaning('max', sLeaning, maxleanings[sLeaning]);
			for(var sLeaning in minleanings)
				this.addLeaning('min', sLeaning, minleanings[sLeaning]);
		}
		
		this.updateBar(0);
		this.nextQuestion();
	}
	
	this.send_to_friends_http = null;
	this.friends_timeout = null;
	this.sending_timeout = null;
	this.sending_count = 0;
	
	this.sending = function()
	{
		var states = [
			'Sending',
			'Sending',
			'Sending .',
			'Sending . .',
			'Sending . . .',
			'Sending . . .'
		];
		
		this.sending_count %= states.length;
		
		$('quiz_friend_msg').innerHTML = states[this.sending_count];
		
		++this.sending_count;
	}
	this.sendToFriends = function()
	{
		if(null != this.send_to_friends_http) return false;
		
		this.clearFriendsMessage();
		
		var textarea = $('quiz_friend_emails');
		var button = $('quiz_friend_button');
		var message = $('quiz_friend_msg');
		
		textarea.disabled = true;
		button.disabled = true;
		
		this.sending_timeout = window.setInterval('quiz.sending();',175);
		//return;
		
		
		var body = [];
		
		body.push('to_list='+escape(textarea.value));
		
		if(navigator.appName == "Microsoft Internet Explorer")
			this.send_to_friends_http = new ActiveXObject("Microsoft.XMLHTTP");
		else
			this.send_to_friends_http = new XMLHttpRequest();
		
		this.send_to_friends_http.open('POST','send_to_friends.php');
		this.send_to_friends_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		this.send_to_friends_http.onreadystatechange = function()
		{
			if(quiz.send_to_friends_http.readyState == 4)
				quiz.sentToFriends();
		}
		
		body = body.join('&');
		this.send_to_friends_http.send(body);
		
		//this.sentToFriends(http);
	}
	
	this.sentToFriends = function()
	{
		var textarea = $('quiz_friend_emails');
		var button = $('quiz_friend_button');
		var message = $('quiz_friend_msg');
		var http = this.send_to_friends_http;
		
		
		if(null != this.sending_timeout)
			window.clearInterval(this.sending_timeout);
		
		if(200 == http.status)
		{
			message.innerHTML = http.responseText;
			textarea.value = '';
		}
		else
		{
			message.innerHTML = 'Request failed, please try again';
		}
		
		textarea.disabled = false;
		button.disabled = false;
		
		this.send_to_friends_http = null;
		this.friends_timeout = window.setTimeout("$('quiz_friend_msg').innerHTML = '';",10000);
	}
	
	this.clearFriendsMessage = function()
	{
		if(null != this.friends_timeout) window.clearTimeout(this.friends_timeout);
		$('quiz_friend_msg').innerHTML = '';
	}
	
	this.notify_timeout = null;
	this.notifyMe = function()
	{		
		this.clearNotifyMessage();
		var text = $('quiz_notify_email');
		var button = $('quiz_notify_button');
		var message = $('quiz_notify_msg');
		
		text.disabled = true;
		button.disabled = true;
		
		
		var body = [];
		
		body.push('email='+escape(text.value));
		
		if(navigator.appName == "Microsoft Internet Explorer")
			var http = new ActiveXObject("Microsoft.XMLHTTP");
		else
			var http = new XMLHttpRequest();
		
		http.open('POST','notify.php',false);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		body = body.join('&');
		http.send(body);
		
		if(200 == http.status)
		{
			message.innerHTML = http.responseText;
			text.value = '';
		}
		else
		{
			message.innerHTML = 'Request failed, please try again';
		}
		
		text.disabled = false;
		button.disabled = false;
		
		this.notify_timeout = window.setTimeout("$('quiz_notify_msg').innerHTML = '';",10000);
	}
	this.clearNotifyMessage = function()
	{
		if(null != this.notify_timeout) window.clearTimeout(this.notify_timeout);
		$('quiz_notify_msg').innerHTML = '';
	}
	
	
	return this;
}

quiz.setQuestion('quiz01','Which famous cartoon character&#8217;s butt is this?',{filename:'cartoon.jpg',width:284,height:231});
	quiz.setResponse('Bart');
	quiz.setResponse('Beavis');
	quiz.setResponse('Butt-head');
	quiz.setResponse('Cartman');
		quiz.setLeaning('correct',1);

quiz.setQuestion('quiz02','This Spice Girl is too hot to handle. Whose behind is it?',{filename:'spice.jpg',width:284,height:231});
	quiz.setResponse('Baby');
	quiz.setResponse('Ginger');
	quiz.setResponse('Posh');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Scary');
	quiz.setResponse('Sporty');

quiz.setQuestion('quiz03','People Magazine thinks he&#8217s one of the sexiest men alive. Whose butt is this?',{filename:'sexiest.jpg',width:284,height:231});
	quiz.setResponse('Brad Pitt');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Matt Damon');
	quiz.setResponse('Matthew McConaughey');
	quiz.setResponse('Tom Cruise');

quiz.setQuestion('quiz04','Which cute yellow animated character&#8217;s butt is this?',{filename:'yellow.png',width:284,height:231});
	quiz.setResponse('Jr Pacman');
	quiz.setResponse('Pacman');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Pikachu');
	quiz.setResponse('SpongeBob SquarePants');

quiz.setQuestion('quiz05','Whoa! Is this photo even real? Which famous Jessica&#8217;s bottom is this?',{filename:'jessica.jpg',width:284,height:231});
	quiz.setResponse('Jessica Alba');
	quiz.setResponse('Jessica Biel');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Jessica Rabbit');
	quiz.setResponse('Jessica Simpson');

quiz.setQuestion('quiz06','What phrase is most likely to be said by the person whose butt is shown?',{filename:'phrase.jpg',width:284,height:231});
	quiz.setResponse('Can we fix it?');
	quiz.setResponse('Doh!');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Hola, Soy Butts!');
	quiz.setResponse('WIIIILLLLLLMMMMMAAAAAA');

quiz.setQuestion('quiz07','This tennis player has a great backhand, but an even better back side. Who is it?',{filename:'tennis.jpg',width:284,height:231});
	quiz.setResponse('Anna Kournikova');
	quiz.setResponse('Gabriella Sabatini');
	quiz.setResponse('Maria Sharapova');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Monica Seles');

quiz.setQuestion('quiz08','The butt below is featured on the cover of a children&#8217;s book. Which book is it??',{filename:'book.jpg',width:284,height:231});
	quiz.setResponse('Dumbo Picture Book');
	quiz.setResponse('Ella The Elephant');
	quiz.setResponse('The Gas We Pass');
		quiz.setLeaning('correct',1);
	quiz.setResponse('The Story of Babar');

quiz.setQuestion('quiz09','He wins golf tournaments, lands endorsement deals, starts charities, and even married a hot model. On top of that, he&#8217;s got a great butt.  Who is he?',{filename:'golf.jpg',width:284,height:231});
	quiz.setResponse('Jack Nicklaus');
	quiz.setResponse('John Daly');
	quiz.setResponse('Phil Mickelson');
	quiz.setResponse('Tiger Woods');
		quiz.setLeaning('correct',1);

quiz.setQuestion('quiz10','This celebrity can&#8217;t seem to keep herself out of the news and the police station. Whose butt is it?',{filename:'police.jpg',width:284,height:231});
	quiz.setResponse('Britney Spears');
	quiz.setResponse('Lindsay Lohan');
	quiz.setResponse('Nicole Richie');
	quiz.setResponse('Paris Hilton');
		quiz.setLeaning('correct',1);

quiz.setQuestion('quiz11','This photo was taken from the cover of a rap album. Which album is it?',{filename:'rap.jpg',width:284,height:153});
	quiz.setResponse('Baby Got Back');
		quiz.setLeaning('correct',1);
	quiz.setResponse('I Want My Baby Back');
	quiz.setResponse('My Adidas');
	quiz.setResponse('Stinky Feet');

quiz.setQuestion('quiz12','Who performed this famous head butt?',{filename:'head.jpg',width:284,height:231});
	quiz.setResponse('David Beckham');
	quiz.setResponse('Diego Maradona');
	quiz.setResponse('Ronaldo');
	quiz.setResponse('Zinedine Zidane');
		quiz.setLeaning('correct',1);

quiz.setQuestion('quiz13','American Idol is on top of the charts. But whose bottom is this?',{filename:'idol.jpg',width:284,height:231});
	quiz.setResponse('Carrie Underwood');
	quiz.setResponse('Kellie Pickler');
	quiz.setResponse('Kelly Clarkson');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Kimberly Caldwell');

quiz.setQuestion('quiz14','This actress made her acting debut as the Coppertone girl in a commercial when she was 3 years old.',{filename:'debut.jpg',width:284,height:231});
	quiz.setResponse('Cameron Diaz');
	quiz.setResponse('Drew Barrymore');
	quiz.setResponse('Jodie Foster');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Kate Hudson');

quiz.setQuestion('quiz15','Which celebrity&#8217;s butt is this?',{filename:'burlyman.jpg',width:284,height:231});
	quiz.setResponse('Bo Jackson');
	quiz.setResponse('Nacho Libre');
	quiz.setResponse('Junk Yard Dog');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Mr. T');

quiz.setQuestion('quiz16','Which famous Jennifer&#8217;s bottom is this?',{filename:'jennifer.jpg',width:284,height:231});
	quiz.setResponse('Jennifer Aniston');
	quiz.setResponse('Jennifer Garner');
	quiz.setResponse('Jennifer Lopez');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Jennifer Love Hewitt');

quiz.setQuestion('quiz17','His bottom is often seen flying above the rim. Whose butt is it?',{filename:'basketball.jpg',width:284,height:231});
	quiz.setResponse('Allen Iverson');
	quiz.setResponse('Kobe Bryant');
	quiz.setResponse('LeBron James');
	quiz.setResponse('Vince Carter');
		quiz.setLeaning('correct',1);

quiz.setQuestion('quiz18','This photo of a Friend gives the lyrics &#8220;I&#8217;ll be there for you&#8221; new meaning. Whose bottom is it?',{filename:'friends.jpg',width:284,height:231});
	quiz.setResponse('Courteney Cox Arquette');
	quiz.setResponse('Matt LeBlanc');
	quiz.setResponse('Jennifer Aniston');
		quiz.setLeaning('correct',1);
	quiz.setResponse('Matthew Perry');
	quiz.setResponse('Lisa Kudrow');
	quiz.setResponse('David Schwimmer');
