var o=0,s=new Date(),t=$('#timer'),w=p.w,z=p.h,u;
function D(e){
	if(o==-1)
		return X(e);
	c=A(e).attr('className');
	o='unk';
	if(c=='unk'||c=='')
		if((e.button==0&&!document.all)||(e.button==1&&document.all))
			o='on';
		else
			o='off';
	return O(e);
}
function U(e){
	if(o!=-1)
		o=0;
	return X(e);
}
function O(e){
	if(!(o<1))
		A(e).removeClass().addClass(o);
	return X(e);
}
function A(e){
	c=$(e.target);
	while(c.is("div"))
		c=c.children().get(0);
	return c.parent();
}
function T(){
	if(o==-1)
		return clearInterval(u);
	e=Math.floor(((new Date()).getTime()-s.getTime())/1000);
	h=Math.floor(e/3600);
	m=Math.floor(e/60)%60;
	e=e%60;
	if(h>23) {
		clearInterval(u);
		return t.text("Time limit of 1 day has exceeded!");
	}
	t.text(h+":"+(m<10?"0":"")+m+":"+(e<10?"0":"")+e);
}
function H(){
	g=1;
	a="";
	$('.puzcell div div').each(
		function(){
			a+=$(this).hasClass('on')?"1":"0"
		}
	);
	for(i=0;i<z&&g;i++){
		x=0;
		y=0;
		for(j=0;j<w&&g;j++)
			if(a.substr(i*w+j,1)=='1')
				x++;
			else if(y>p.l[i].length)
				g=0;
			else if(x==p.l[i][y]){
				x=0;
				y++;
			}else if(x!=0)
				g=0;
		if(y<p.l[i].length&&x!=p.l[i][y])
			g=0;
	}
	for(i=0;i<w&&g;i++){
		x=0;
		y=0;
		for(j=0;j<z&&g;j++)
			if(a.substr(i+j*w,1)=='1')
				x++;
			else if(y>p.t[i].length)
				g=0;
			else if(x==p.t[i][y]){
				x=0;
				y++;
			}else if(x!=0)
				g=0;
		if(y<p.t[i].length&&x!=p.t[i][y])
			g=0;
	}
	if(g){
		t.text("Correct ("+t.text()+")");
		if(ulogged){
			jQuery.post('/times/post/',{i:p.i,d:a});
			f="Top Time Submitted.";
		}else
			f="Login to submit to top times.";
		o=-1
	}else
		f="Errors Found";
	$('#feedback').text(f);
}
function X(e){
	e.stopPropagation();
	return F();
}
function F(){
	return false
}
function C(){
	$('.puzcell div div').attr('class','')
}
function B(a,b){
	$('#tab'+b+' ~ div').addClass('hide');
	if($('#tab'+b).hasClass('tabbed2'))$('#tab'+b).parent().children().each(function(){if($(this).hasClass('tabbed2')) return; $(this).addClass('hide');});
	$(a).removeClass('hide');
	$('#tab'+b+' li').removeClass('chosen');
	$('#tab'+b+' td').removeClass('chosen');
	$("#tab"+b+" [href="+a+"]").parent().addClass('chosen');
	return F()
}
$('.puzcell').html('<div align="center"><div><img src="/static/images/a.gif" /></div></div>');
$('.puzcell div').attr('align','center');
$('.rh,.out,.puztop,.topLeft').mouseover(U);
$('.puzcell div div').mousedown(D).mouseover(O);
$('#puzzle').mouseup(U);
u=setInterval(T,1000);
document.oncontextmenu=F;
document.onselectstart=F;
document.ondragstart=F;
if(jQuery.browser.mozilla){
	document.write( '<style type="text/css">div.off { position:relative; left:37%; }</style>' );
}

