function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function reset_errors() {
    var f = $$("input.error");
    for(var i=0; i<f.length; i++) {
	Element.removeClassName(f[i], 'error');
    }
    var f = $$("div.errors");
    for(var i=0; i<f.length; i++) {
	f[i].innerHTML = '';
    }
}

function do_login() {
    reset_errors();
    var ajax = new Ajax.Request("/session.js", { method:'post', parameters: Form.serialize($('login_form')), onComplete: function(o){
    }});
}

function do_register() {
    reset_errors();
    var ajax = new Ajax.Request("/users.js", { method:'post', parameters: Form.serialize($('register_form')), onComplete: function(o){
    }});
}



function init(self)
{
    if(get_cookie( "_photoshare_user" )!=null || get_cookie( "auth_token" )!=null) {
	Element.show($('user_form')); 
	Element.hide($('guest_form')); 
    }
    else {
	Element.hide($('user_form')); 
	Element.show($('guest_form')); 
    }
    if(location.hash=="#login") Element.show($('login-form')); 
    if(location.hash=="#register") Element.show($('register-form')); 


    anime = new iAnime();
    
    sx = 28;
    sy = 93;
    
    count = 32;
    wCell = 32; hCell = 32;
    columns = 30; rows = 7;
    cells = columns * rows;
    pos = [];
    for (i=0; i<cells; i++) {
	switch(i) {
	case 8:
	case 17:
	case 21:
	case 22:
	case 28:
	case 29:
	    break;
	default:
	    pos.push(i);
	    break;
	}
    }
    
    for (i=0; i<cells-6; i++) {
	j = i+Math.floor(rows*10*Math.random());
	if (j<cells-6) {
	    t = pos[i]; pos[i]=pos[j]; pos[j]=t;
	}
    }
    
    htmlText = "";
    seq = [{duration:500}];
    for (i=0; i<cells-6; i++) {
	x = sx + wCell * Math.floor(pos[i] / rows);
	y = sy + hCell * (pos[i] % rows);
	px = ((i % count) % 8)*30;
	py = Math.floor((i % count) / 8)*30;
	htmlText += "<div id='cell"+i+"' class='cell' ";
	htmlText += "style='background-position:"+px+"px "+py+"px' ></div>";
	var duration = (x+0.1*wCell)*0.6;
	var delay = 2000*i/cells;
	if (i>cells-36) { 
	    duration += (i-(cells-36))*80; 
	    delay += (i-(cells-36))*30; 
	    if (i>cells-10) {
		duration += 500;
		delay += 800;
	    }
	}
	seq.push([{duration:delay}, 
		  {id:'cell'+i, duration:duration,
		   cp: [{x:x,y:102}], 
		   effect:'easeout', x:x, y:y}]);
	// cp: [{x:x,y:102}], 
    }
    $("cellarea").innerHTML = htmlText;
    anime.addSequence(seq);
}

