// subdomain stuff

var subdomains=['fonts','people','cars','places','meat'];

var subdomain_labels={
  'fonts':'font',
  'people':'person',
  'cars':'car',
  'places':'place',
  'meat':'meat'
};

var subdomain_prompts={
  'fonts':'Which font is this?',
  'people':'Who is this?',
  'cars':'What kind of car is this?',
  'places':'Where is this?',
  'meat':'What kind of meat is this?'
};

var root_prompt="What/who is this?";

function build_subdomain_links(){
  var l=document.getElementById('subdomain-links');
  for(var i in subdomains){
    var cs=subdomains[i];
    if(cs!=subdomain){
      var a=document.createElement('a');
      a.setAttribute('href', 'http://'+cs+'.'+domain+(port?':'+port:''));
      a.appendChild(document.createTextNode(cs));
      l.appendChild(a);
    }else{
      var s=document.createElement('span');
      s.appendChild(document.createTextNode(cs));
      l.appendChild(s);
    }
    l.appendChild(document.createTextNode(' '));
  }
  if(subdomain){
    var aa=document.createElement('a');
    aa.setAttribute('href', 'http://'+domain+(port?':'+port:''));
    aa.appendChild(document.createTextNode('anything'));
    l.appendChild(aa);
  }
}

function build_subdomain_menu(){
  var select=document.createElement('select');
  select.setAttribute('name','sub_domain');
  select.onchange=function(){return subdomain_menu_change(this,domain,port);};

  var goption=document.createElement('option');
  goption.setAttribute('value','');
  goption.appendChild(document.createTextNode('(general)'));
  select.appendChild(goption);
  if(!subdomain){
    goption.setAttribute('selected',true);
  }

  for(var i in subdomains){
    var cs=subdomains[i];
    var option=document.createElement('option');
    option.setAttribute('value',cs);
    option.appendChild(document.createTextNode(subdomain_labels[cs]));
    if(cs==subdomain){
      option.setAttribute('selected',true);
    }
    select.appendChild(option);
  }
  document.getElementById('subdomain-menu').appendChild(select);
}

var domain;
var subdomain;
var port;
function init_domain(){
  if(document.location.port){
    port=document.location.port;
  }

  var hostname=String(document.location.hostname);
  var q=hostname.split('.');
  if(q.length>2){
    subdomain=q[0];
    domain=hostname.substring(subdomain.length + 1);
  }else{
    domain=hostname;
  }

  var l=document.getElementById('logo-link');
  l.setAttribute('href', 'http://'+domain+(port?':'+port:''));

  build_subdomain_menu();
  build_subdomain_links();
}

// end subdomain stuff


// location loop stuff

var current_location;
var location_loop;
var base_page_title=document.title;

function load_location(l){
  clearInterval(location_loop);
  alert("ERROR: You must override load_location() in your page-specific JavaScript.");
}

function check_location(){
  var new_location=String(document.location);
  if (new_location.indexOf('?')>=0){
    new_location=new_location.replace(/\?/,'#');
    document.location.replace(new_location);
  }else if(new_location!=current_location){
    load_location(new_location);
  }
}

function set_current_hash(h){
  if(document.location.hash===h){
    return;
  }
  if(!document.location.hash){
    document.location.replace(String(document.location)+'#'+h);
  }else{
    document.location='#'+h;
  }
  current_location=String(document.location);
}

// end location loop stuff


// overlay & dialog stuff

function show_overlay(name){
  show('overlay');
  show_detail(name);
}

function hide_overlay(name){
  hide('overlay');
  hide_detail(name);
}

// end overlay stuff


// about stuff 

function show_about(){
  show_overlay('about');
}

function hide_about(){
  hide_overlay('about');
}

// end about stuff


// auth stuff
var current_user_id=undefined;

function show_auth_info(){
  show('you-label');
  current_user_id=get_cookie('userid');
  var user_name=decodeURIComponent(get_cookie('username'));
  var you=document.getElementById('you-link');
  you.innerHTML=user_name;
  you.setAttribute('href','/user/#'+current_user_id);
}

function show_login(){
  show_overlay('login');
  document.getElementById('login-email').focus();
}

function show_signup(){
  show_overlay('signup');
  document.getElementById('signup-email').focus();
}

function hide_login(){
  hide_overlay('login');
  if(!get_cookie('userid')){
    show('show-login');
  }
}

function hide_signup(){
  hide_overlay('signup');
}

function signup_callback(response){
  if(!alert_errors(response)){
    hide_signup();
    hide('show-signup');
    hide('show-login');
    alert("Click on the link in the verification message\nemailed to you to verify your email address.");
  }
}

function signup(){
  var e=document.getElementById('signup-email');
  var p1=document.getElementById('signup-password-1');
  var p2=document.getElementById('signup-password-2');
  e.className='';
  p1.className='';
  p2.className='';
  if(!e.value){
    notice_this(e);
    alert("You must enter a valid email.");
  }else if(!p1.value){
    notice_this(p1);
    alert("You must choose a password.");
  }else if(!p2.value){
    notice_this(p2);
    alert("You must enter your password twice.");    
  }else if(p1.value!=p2.value){
    notice_this(p1);
    alert("Your passwords do not match.");
  }else if(p1.value.length<4){
    notice_this(p1);
    alert("Your password must be longer than four characters.");
  }else{
    var params='email='+encodeURIComponent(e.value);
    params=params+'&password1='+encodeURIComponent(p1.value);
    params=params+'&password2='+encodeURIComponent(p2.value);
    XMLDoc('POST','/signup',params,signup_callback);
  }
}

function login_callback(response,email){
  if(!alert_errors(response)){
    hide_login();
    hide('show-login');
    hide('show-signup');
    if(get_cookie('userid')){
      show_auth_info();
      show_points();
      load_location(String(document.location));
    }
  }
}

function login(){
  var e=document.getElementById('login-email');
  var p=document.getElementById('login-password');
  e.className='';
  p.className='';
  if(!e.value){
    notice_this(e);
    alert("You must enter your email.");
  }else if(!p.value){
    notice_this(p);
    alert("You must enter your password.");
  }else if(p.value.length<4){
    notice_this(p);
    alert("Your password must be longer than four characters.");
  }else{
    var params='email='+encodeURIComponent(e.value);
    params=params+'&password='+encodeURIComponent(p.value);
    XMLDoc('POST','/login',params,function (response){login_callback(response, e.value);});
  }
}

// end auth stuff 


// points 

function format_points(points){
  return Math.round(points*1000)/100;
}

function show_points(){
  var points=get_cookie('points');
  if(points&&parseFloat(points)){
    document.getElementById('total-points').innerHTML=format_points(points)+' points';
  }else{
    document.getElementById('total-points').innerHTML='no points yet.';
  }
}

// end points


// upload widget

function subdomain_menu_change(menu){
  var sd=menu.value;
  var action='http://';
  if(sd){
    action+=sd+'.';
  }
  action+=domain;
  if(port){
    action+=':'+port;
  }
  action+='/upload';
  document.getElementById('upload-form').action=action;
}

function file_chosen(file){
  var filename=file.value.toLowerCase();
  if((filename.substring(filename.length-4)=='.jpg')||
     (filename.substring(filename.length-4)=='.png')||
     (filename.substring(filename.length-4)=='.gif')||
     (filename.substring(filename.length-5)=='.jpeg')){
    document.getElementById('upload-form').submit();
  }else{
    file.value='';
    notice_this(file);
    alert("Only .jpg, .png, or .gif files allowed.");
  }
}

// end upload widget


// status checker
function check_status(){
  var status_id=get_cookie('status');
  if(status_id=='duplicate_image'){
    alert("That image has already been posted.");
  }else if(status_id=='email_verified'){
    show_login();
  }
  delete_cookie('status');
}


// site-wide page-load function
function page_load(){
  if(isInternetExplorer){
    if(navigator.appVersion.indexOf('7.0')==-1){
      alert("This site doesn't work in Internet Explorer 6.0.\n"+
	    "Use a real web browser.  The site works in\n"+
	    "Firefox, Safari, and Internet Explorer 7.0.");
      return;
    }
  }
  if(!navigator.cookieEnabled){
    show('nocookies');
    return;
  }
  if(get_cookie('userid')){
    show_auth_info();
  }else{
    show('show-signup');
    show('show-login');
  }
  show_points();
  init_domain();
  check_location();
  location_loop=setInterval(check_location, 333);
  check_status();
}

