var historyStore;
var historyInfo;
var historyImgOn = '/static/brow/images/history_on.png';
var historyImgOff = '/static/brow/images/history_off.png';
var aboutText;
var waiting;



function setupHistory()
{
	historyStore =

		[

		{

			'moment' : null,

			'historyImg' : 'history1'

			

		},

		{

			'moment' : null,

			'historyImg' : 'history2'

			

		},

		{

			'moment' : null,

			'historyImg' : 'history3'

			

		},

		{

			'moment' : null,

			'historyImg' : 'history4'

			

		},

		{

			'moment' : null,

			'historyImg' : 'history5'

			

		}

	];

	historyInfo = {

		'active' : -1

		

	};

}



function resetHistoryImages()

{

	var i;

	for(i = 0; i < historyStore.length;i++)

		document.getElementById(historyStore[i].historyImg).src = historyImgOff;

}





function storeHistory(moment)

{

	var i;

	i = historyStore.length -1;

	while(i > 0)

	{

		historyStore[i].moment = historyStore[i-1].moment;

		i--;

	}

	

	historyStore[0].moment = moment;

}



function loadHistory(index)

{

	resetHistoryImages();

	curHistory = historyStore[index];

	if (curHistory.moment == null)

		return(false);

		

	document.getElementById(curHistory.historyImg).src = historyImgOn;

	document.getElementById('momentText').innerHTML = curHistory.moment.body;

	document.getElementById('momentPoster').innerHTML = curHistory.moment.poster;

	document.getElementById('momentID').value = curHistory.moment.id;

	return(true);

}



function historyNext()

{

	var active = historyInfo.active + 1;

	if (active >= historyStore.length)

		active = 0;

	

	if (loadHistory(active))	

		historyInfo.active = active;

	else

	{

		loadHistory(0);

		historyInfo.active = 0;

	}

}



function historyPrev()

{

	var active = historyInfo.active - 1;

	if (active < 0)

	{

		var i;

		for(i = historyStore.length -1; i >= 0; i--)

		{

			if (historyStore[i].moment != null)

				break;

		}

		active = i;

	}



	if (loadHistory(active))	

		historyInfo.active = active;

	else

	{

		loadHistory(0);

		historyInfo.active = 0;

	}

}





function setSizes()

{

	var windowHeight =   YAHOO.util.Dom.getViewportHeight();

	var newHeight = windowHeight - 260;

	if (newHeight > 200)

		YAHOO.util.Dom.setStyle(['momentContainer'], 'height', newHeight +"px");

}

function loadNextMoment(o)

{

	newMoment = o.responseText.parseJSON();

	historyInfo.active = 0;

	storeHistory(newMoment);

	loadHistory(0);

	

	document.getElementById('hitstotal').innerHTML = newMoment['hitstotal'];

	document.getElementById('hitsyours').innerHTML = newMoment['hitsyours'];

}

function clearWait()

{

	document.getElementById('reload').innerHTML = 'Continue';

	waiting = false;

}

function nextMoment()

{

	if(!waiting)

		waiting = true;

	else

		return;

	

	document.getElementById('reload').innerHTML = 'Hold it';

	var callback= 

	{

		success: function(o) { 

			clearWait();

			loadNextMoment(o) 

		},

		

		failure: function(o) {  

			clearWait();

			alert(':('); 

		}

	}

	YAHOO.util.Connect.setForm("theForm");

	var cObj = YAHOO.util.Connect.asyncRequest('POST', 'getrandom/',callback);

}



function turnOffSelect()

{

	YAHOO.util.Dom.removeClass('sizeSmall', 'selected');

	YAHOO.util.Dom.removeClass('sizeBig', 'selected');

	YAHOO.util.Dom.removeClass('sizeMedium', 'selected');

	YAHOO.util.Dom.removeClass('sizeAny', 'selected');

}



function selectSmallMoments(e)

{

	turnOffSelect();

	YAHOO.util.Dom.addClass('sizeSmall', 'selected')

	

	var storage = document.getElementById('momentSize');

	storage.value = 'S';

}





function selectMediumMoments()

{

	

	turnOffSelect();

	

	YAHOO.util.Dom.addClass('sizeMedium', 'selected')

	

	var storage = document.getElementById('momentSize');

	storage.value = 'M';

}





function selectBigMoments()

{

	turnOffSelect();

	YAHOO.util.Dom.addClass('sizeBig', 'selected')

	

	var storage = document.getElementById('momentSize');

	storage.value = 'B';

}



function selectAllMoments()

{

	turnOffSelect();

	YAHOO.util.Dom.addClass('sizeAny', 'selected')



	var storage = document.getElementById('momentSize');

	storage.value = 'A';

}



function showAbout()

{

	aboutText.show();

}

function setupAboutText()

{

	aboutText = new YAHOO.widget.Panel("wait",  

								{ width:"600px", 
								  fixedcenter:true, 
								  close:true, 
								  draggable:false, 
								  modal:true,
								  visible:false,
								  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
								} 

							);



	aboutText.setHeader("About this junk");

	aboutText.setBody("<div style=\"text-align:center\"><strong>TLDR: read, reload, repeat</strong></div>");
	aboutText.render(document.body);

}



function handleKeyPress(e)

{

	var keyCode = e.keyCode;

	switch(keyCode)

	{

		case 37:

			historyNext();

			return;

		case 39:

			historyPrev();

			return;

	}

	

	

	var keyPress = String.fromCharCode(e.keyCode);

	

	switch(keyPress)

	{

		case 'R':

			nextMoment();

			break;

		case 'S':

			selectSmallMoments();

			break;

		case 'M':

			selectMediumMoments();

			break;

		case 'B':

			selectBigMoments();

			break;

		case 'A':

			selectAllMoments();

			break;

	}

}



function handleHistoryClick(e)

{

	var target = YAHOO.util.Event.getTarget(e);

	var histId = target.id.substring(7,8);

	loadHistory(histId-1);

}





function init()

{

	YAHOO.util.Event.addListener("reload","click", nextMoment);

	

	

	YAHOO.util.Event.addListener("sizeSmall", "click", selectSmallMoments);

	YAHOO.util.Event.addListener("sizeMedium", "click", selectMediumMoments);

	YAHOO.util.Event.addListener("sizeBig", "click", selectBigMoments);

	YAHOO.util.Event.addListener("sizeAny", "click", selectAllMoments);

	

	YAHOO.util.Event.addListener("showsubmit", "click", function() { alert('not yet')});

	

	YAHOO.util.Event.addListener("leftImg", "click", showAbout);

	

	YAHOO.util.Event.addListener(["history1",

		"history2",

		"history3",

		"history4",

		"history5",

	], "click", handleHistoryClick);

	

	

	YAHOO.util.Event.addListener(document,"keyup", handleKeyPress);

	

	setSizes();

	setupHistory();

	nextMoment();

	//setupAboutText();

}

YAHOO.util.Event.onDOMReady(init);

window.onresize  = setSizes;



