

var Cake = {
	JTween: new JTweenController(),
	active:0,
	aboutActive:false,
	over:false,
	xmlHttp:  Juliet.AJAX.createXMLHTTPObject(),
	Questions:new Array(
	
		{cake:"cake",cakecount:1515,cakehint:"cake, the dessert",death:"death",deathcount:501,deathhint:"death, the end of life."},{cake:"helvetica",cakecount:742,cakehint:"the font you most likely know",death:"din",deathcount:474,deathhint:"the font on this site"},{cake:"obama",cakecount:931,cakehint:"(and biden)",death:"mccain",deathcount:224,deathhint:"(and palin)"},{cake:"jerry seinfeld",cakecount:627,cakehint:"master of your domain",death:"larry david",deathcount:496,deathhint:"curb your enthusiasm"},{cake:"oreo",cakecount:425,cakehint:"delicious cookie",death:"chocolate chip",deathcount:634,deathhint:"delicious cookie"},{cake:"iphone",cakecount:730,cakehint:"omg (with a full keyboard)",death:"i don't care",deathcount:619,deathhint:"wtf (with T9)"},{cake:"pulp fiction",cakecount:817,cakehint:"marsellus wallace",death:"reservoir dogs",deathcount:479,deathhint:"mr. orange"},{cake:"the king",cakecount:383,cakehint:"elvis",death:"the man in black",deathcount:888,deathhint:"johnny cash"},{cake:"SFO",cakecount:443,cakehint:"san francisco",death:"NYC",deathcount:804,deathhint:"new york"},{cake:"mp3",cakecount:810,cakehint:"the single",death:"vinyl",deathcount:447,deathhint:"the album"},{cake:"coffee",cakecount:1061,cakehint:"coffee, addiction",death:"cigarettes",deathcount:317,deathhint:"cigarette, addiction"},{cake:"peanut butter",cakecount:1233,cakehint:"pb",death:"jelly",deathcount:424,deathhint:"j"},{cake:"maverick",cakecount:704,cakehint:"you're dangerous, maverick.",death:"iceman",deathcount:574,deathhint:"that's right, iceman."},{cake:"mcdreamy",cakecount:706,cakehint:"derek, that handsome doctor from grey's anatomy",death:"mcsteamy",deathcount:490,deathhint:"sloan, that handsome doctor from grey's anatomy"},{cake:"leica",cakecount:722,cakehint:"35mm",death:"hasselblad",deathcount:462,deathhint:"medium format"},{cake:"coke",cakecount:815,cakehint:"coke, the cola beverage",death:"pepsi",deathcount:362,deathhint:"pepsi, the cola beverage"},{cake:"bond",cakecount:1019,cakehint:"bond, james bond",death:"bauer",deathcount:336,deathhint:"jake bauer, 24 hero"},{cake:"the office",cakecount:756,cakehint:"michael, dwight, jim and pam",death:"30 rock",deathcount:385,deathhint:"liz, jack, tracy, and kenneth"},{cake:"shaken",cakecount:125,cakehint:"shaken, not stirred",death:"stirred",deathcount:124,deathhint:"stirred"}
	),
	Delegate : function(scope,method){
		var args = new Array() ;
		var l = arguments.length ;
		for(var i = 2 ; i < l ; i++) args[i - 2] = arguments[i] ;
		return (function(){method.apply(scope,args)})
	},
	initialize:function(){
	
		document.getElementById("btn_chart").style.display="block"
		document.getElementById("btn_i").style.display="block"
		document.getElementById("btn_help").style.display="block"
		
		setTimeout(function(){
		
			document.getElementById("cake_1_count").style.backgroundImage='url(data/images/sys/dyn_image.php?text=' + Cake.Questions[Cake.active].cakecount + '&style=count)';
			document.getElementById("death_1_count").style.backgroundImage='url(data/images/sys/dyn_image.php?text=' + Cake.Questions[Cake.active].deathcount + '&style=count)';
		
		
		Cake.JTween.Tween(
			"cake_1_count",
			{tweenProperty:"width",tweenDuration:1.5,tweenEase:"easeInOutQuint",tweenStart:"1%",tweenEnd:"100%"}
		)
		
		Cake.JTween.Tween(
			"death_1_count",
			{tweenProperty:"width",tweenDuration:1.8,tweenEase:"easeInOutQuint",tweenStart:"1%",tweenEnd:"100%"}
		)
		
		
		
			}
			,2000)
		
		//this.active=1;
		
		this.JTween.Tween(
			"cake_q0",
			{tweenProperty:"top",tweenDuration:2,tweenEase:"easeInOutQuint",tweenStart:"125%",tweenEnd:"50%"}
		)
		
		this.JTween.Tween(
			"death_q0",
			{tweenProperty:"top",tweenDuration:2.6,tweenEase:"easeInOutQuint",tweenStart:"125%",tweenEnd:"50%"}
		)
		
	},
	next:function(ele,dbid){
		ele.blur()
		ele.onclick = function(){}
		
		if (this.active<this.Questions.length-1){
		
		var id = (ele.id.indexOf("cake")>-1) ? "cake" : "death"
		
		this.register(id,dbid)
		
		var id2 = (ele.id.indexOf("cake")>-1) ? "death" : "cake"
		
		
		var cStyle = document.getElementById(id+"_1_count").style.backgroundImage
		var c = cStyle.split("?")[1].split("&")[0].split("=")[1]
		document.getElementById(id+"_1_count").style.backgroundImage='url(data/images/sys/dyn_image.php?text=' + (parseInt(c)+1) + '&style=count)';
		document.getElementById(id2+"_1_count").style.backgroundImage=''
		
		setTimeout(function(){
				document.getElementById("cake_1_count").style.backgroundImage='';
				document.getElementById("death_1_count").style.backgroundImage='';
			}
			,1500
		)
			
		setTimeout(function(){
		
			document.getElementById("cake_1_count").style.backgroundImage='url(data/images/sys/dyn_image.php?text=' + Cake.Questions[Cake.active].cakecount + '&style=count)';
			document.getElementById("death_1_count").style.backgroundImage='url(data/images/sys/dyn_image.php?text=' + Cake.Questions[Cake.active].deathcount + '&style=count)';
		
		
			Cake.JTween.Tween(
				"cake_1_count",
				{tweenProperty:"width",tweenDuration:1.5,tweenEase:"easeInOutQuint",tweenStart:"1%",tweenEnd:"100%"}
			)
		
			Cake.JTween.Tween(
				"death_1_count",
				{tweenProperty:"width",tweenDuration:1.8,tweenEase:"easeInOutQuint",tweenStart:"1%",tweenEnd:"100%"}
			)
			
			}
			,3000
		)
			
		this.dispose();
		this.seek(this.active+1)
		
		}else{
			this.dispose()
			this.over=true
			this.toggleAbout(true);
		}
	},
	seek:function(id){
		this.active=id
		this.JTween.Tween(
			"cake_q" + id,
			{tweenProperty:"top",tweenDuration:2,tweenEase:"easeInOutQuint",tweenStart:"125%",tweenEnd:"50%",tweenDelay:1}
		)
		
		this.JTween.Tween(
			"death_q" + id,
			{tweenProperty:"top",tweenDuration:2.6,tweenEase:"easeInOutQuint",tweenStart:"125%",tweenEnd:"50%",tweenDelay:1}
		)
	},
	dispose:function(){
		this.JTween.Tween(
			"cake_q" + this.active,
			{tweenProperty:"top",tweenDuration:2.6,tweenEase:"easeInOutQuint",tweenStart:"50%",tweenEnd:"-50%"}
		)
		
		this.JTween.Tween(
			"death_q" + this.active,
			{tweenProperty:"top",tweenDuration:2,tweenEase:"easeInOutQuint",tweenStart:"50%",tweenEnd:"-50%",tweenDelay:.5}
		)
	},
	help:function(t){
		if (t==false){
			document.getElementById("hint").style.height='0';
			document.getElementById("hint").innerHTML = '';
		}else{
			document.getElementById("hint").innerHTML = '<div class="desc"><p>' + this.Questions[this.active].cakehint + '</p><p>or</p><p>' + this.Questions[this.active].deathhint + '</p></div>' ;
			document.getElementById("hint").style.height='259px';
		}
	},
	register:function(id,dbid){
	
		this.xmlHttp		= Juliet.AJAX.createXMLHTTPObject();
	
		var params = {
			action:'answer',
			id:dbid,
			vote:id
		}
	
				
		Juliet.AJAX.query('_gateway/gateway.php',params,this.xmlHttp,this.Delegate(this,this.stateChanged_processQuery))

	
	},

	stateChanged_processQuery : function(){
		if (this.xmlHttp.readyState==4 || this.xmlHttp.readyState=="complete"){ 
			this.bindResponse(this.xmlHttp.responseXML)
		} 
	},

	bindResponse : function (response){
		var gatewayresponse = Juliet.AJAX.getNodeValue("gatewaydata",response)
	},
	
	toggleAbout:function(force){
	
		if (this.aboutActive == false || force==true){
			this.aboutActive=true;
			document.getElementById("btn_i").style.display="none"
			this.JTween.Tween(
				"info",
				{tweenProperty:"top",tweenDuration:1.5,tweenEase:"easeOutQuint",tweenStart:"155%",tweenEnd:"50%"}
			)
		}else{
			
			//else
			//{
				this.aboutActive=false;
				document.getElementById("btn_i").style.display="block"
				this.JTween.Tween(
					"info",
					{tweenProperty:"top",tweenDuration:1.5,tweenEase:"easeOutQuint",tweenStart:"50%",tweenEnd:"155%"}
				)
			//}
			
			if (this.over){
				if (Chart.active==false) Chart.toggle()
				alert("You're at the end of the questions. Come back next week for 5 more!");
				
			}

		}
	},
	
	link:function(ele){
		window.open(ele.href)
	}
	
}	


var Chart = {
	active:false,
	init:false,
	activeWeek:0,
	JTween: new JTweenController(),

	toggle:function(){
	
		if (this.init==false){
			var xhtml = '';
			if (document.getElementById("charts_wrapper"))
			for (var i=0;i<Cake.Questions.length;i++){
				
				var cakeTitle = Cake.Questions[i].cake;
				var cakeCount = parseInt(Cake.Questions[i].cakecount);
				
				var deathTitle = Cake.Questions[i].death;
				var deathCount = parseInt(Cake.Questions[i].deathcount);
				
				if (deathCount>=cakeCount ){
					var deathHeight = 125
					var cakeHeight = (cakeCount*125)/deathCount
				}else{
					var cakeHeight = 125
					var deathHeight = (deathCount*125)/cakeCount
				}
				
				xhtml += '<div class="chart"><div class="barchart"><div class="bars"><em class="bar1" style="height:' + cakeHeight + 'px" title="' + cakeCount + '">' + cakeCount + '</em></div><div class="bars"><em class="bar2" style="height:' + deathHeight + 'px" title="' + deathCount + '">' + deathCount + '</em></div></div><div class="charttitles"><h3 class="title1">' + cakeTitle + ' <span class="count">' + cakeCount + '</span></h3><h3 class="title2">' + deathTitle + ' <span class="count">' + deathCount + '</span></h3></div></div><!--chart-->'
			}
			this.init=true
			document.getElementById("charts_wrapper").innerHTML = xhtml
		}
		
		document.getElementById("chartwrapper").style.backgroundImage = 'url(data/images/sys/dyn_image.php?text=WEEK%20' + (this.activeWeek+1) + '&style=chart)';
	
		
		document.getElementById("btn_chart").style.backgroundPosition = (this.active) ? '0 0' : '0 100%'
		
		document.getElementById("chartwrapper").style.display = (this.active) ? 'none' : 'block';
		
		this.JTween.Tween(
			"chartmask",
			{tweenProperty:"top",tweenDuration:2,tweenEase:"easeOutQuint",tweenStart:"125%",tweenEnd:"50%"}
		)
		
		
		
		
		this.active=!this.active
	},
	
	prev:function(){
		var maxCharts = Math.ceil(Cake.Questions/5);
		document.getElementById("charts_wrapper").style.height=200*maxCharts + "px"
		var cWeek = this.activeWeek
		this.activeWeek = this.activeWeek - 1
		if (this.activeWeek < 0){
			this.activeWeek = 0
			alert("At the start! Onward and upward, my friend.")
		}else{
			
			this.moveCharts(cWeek)
			
			
		}
	},
	
	next:function(){
		var maxCharts = Math.ceil(Cake.Questions.length/5);
		document.getElementById("charts_wrapper").style.height=200*maxCharts + "px"
		var cWeek = this.activeWeek
		this.activeWeek = this.activeWeek + 1
		if (this.activeWeek > maxCharts-1){
			this.activeWeek = maxCharts-1
			alert("All done! Check back next week for 5 more")
		}else{
			
			this.moveCharts(cWeek)
			
			
		}
	},
	
	moveCharts:function(cWeek){
			
			document.getElementById("chartwrapper").style.backgroundImage = 'url(data/images/sys/dyn_image.php?text=WEEK%20' + (this.activeWeek+1) + '&style=chart)';
			var tStart = cWeek * -175
			var tEnd = 	this.activeWeek * -175
			this.JTween.Tween(
				"charts_wrapper",
				{tweenProperty:"margin-top",tweenDuration:2,tweenEase:"easeInOutQuint",tweenStart:tStart,tweenEnd:tEnd}
			)
	}

}









function init(){
	
	Cake.initialize();
}
window.onload = init;