// (c) Damien Debin

var compt, comptstar;

//cf. http://www.quirksmode.org/viewport/compatibility.html
function getWindowWidth()
{
	if (self.innerWidth) { return self.innerWidth; }
	else if (document.documentElement && document.documentElement.clientHeight) { return document.documentElement.clientWidth; }
	else if (document.body) { return document.body.clientWidth; }
	else { return 0; }
}

function setActiveStylesheet(title, enabled)
{
	var a = $('link[title=' + title + ']').get(0);
	if (a !== null)
	{
		if (enabled == 2) { enabled = a.disabled; }
		a.disabled = !enabled;
		$.cookie(title, enabled?'1':'0', {expires: 365, path: '/'});
	}
}

function showHintbox()
{
	if ($('#hintbox').css('display') == 'block') { return; }
	clearTimeout(compt);
	$('#hintbox').show();
}

function hideHintbox()
{
	compt = setTimeout(function() { $('#hintbox').hide(); }, 1000);
}

function showStars(id)
{
	clearTimeout(comptstar);
	var num = id.slice(4);
	for (var i = 1; i <= 10; i++)
	{
		$('#vote' + i).css('background-position', (i<=num)?'center right':'center left');
	}
}

function hideStars()
{
	//delayed hide stars (to prevent blinking)
	comptstar = setTimeout(function() { $('#votebar input').css('background-position','center left'); },300);
}

function onResizeHideBox(e)
{
	if (getWindowWidth() < 1005)
	{
		$('#gallerylist').hide()
		$('#photozone').css('left','53%');
		$.cookie('narrowbrowser', '1', {expires: 365, path: '/'});
	}
	else
	{
		$('#gallerylist').show()
		$('#photozone').css('left','46%');
		$.cookie('narrowbrowser', '0', {expires: 365, path: '/'});
	}
}

function toggleBlackTheme()
{
	setActiveStylesheet('blacktheme', 2);
	return false;
}

function toggleCommentsList(e)
{
	$('#commentslist').toggle();
	return false;
}

function closeAllBoxes(e)
{
	$('#commentslist').hide();
	$('#thanksbox').hide();
	return false;
}

function onLoad()
{
	onResizeHideBox();
	
	$(window).resize(onResizeHideBox);
	$('#scl1').click(toggleCommentsList);
	$('#scl2').click(toggleCommentsList);
	$('#scl4').click(toggleCommentsList);
	$('#scf1').click(toggleCommentsList);
	$('#scf3').click(toggleCommentsList);
	$('#clo1').click(closeAllBoxes);
	$('#clo3').click(closeAllBoxes);
	$('#colorswitch').click(toggleBlackTheme);
	$('#mainphoto').focus(showHintbox);
	$('#mainphoto').blur(hideHintbox);
	$('#mainphoto').hover(showHintbox,hideHintbox);

	$('#votebar input').blur(hideStars);
	$('#votebar input').focus(function(){showStars(this.id);});
	$('#votebar input').hover(function(){showStars(this.id);},hideStars);
}

if ($.cookie('blacktheme') !== null)
{
	setActiveStylesheet('blacktheme', ($.cookie('blacktheme') == '1'));
}
else
{
	setActiveStylesheet('blacktheme', false);
}

if (document.styleSheets[0].cssRules)
{
	document.styleSheets[0].cssRules[0].style.display = 'none';
}
else
{
	document.styleSheets[0].rules[0].style.display = 'none';
}

$(onLoad);
