function scroll_image(image){
  // scroll the (overflowing) image horizontally to the middle
  var v=document.getElementById('view');
  v.scrollLeft=(image.width - v.offsetWidth)/2;
}


/* Flagging */

function flag_callback(response){
  if(!alert_errors(response)){
    previous_image=null;
    next();
    //alert("Thanks for flagging!");
  }
}

function flag(){
  hide('voted');
  document.getElementById('flag').href='javascript:;';
  XMLDoc('POST','/flag/'+current_image.id,'',flag_callback);
}

/* End flagging */


/* Skipping */

function skip_callback(response){
  if(!alert_errors(response)){
    previous_image=null;
    next();
  }
}

function skip(){
  hide('voted');
  document.getElementById('skip').href='javascript:;';
  XMLDoc('POST','/skip/'+current_image.id,'',skip_callback);
}

/* End skipping */


/* Image stuff */

var current_image=null;
var previous_image=null;

function voted(response){
  if(!alert_errors(response)){
    previous_image=current_image;
    Results_from_xml(previous_image,response);
  }
  next();
}

function disable_vote_buttons(){
  var c=document.getElementById('choices');
  for(var i=0;i<c.childNodes.length;i++){
    var n=c.childNodes[i];
    if(n.nodeName=='BUTTON'){
      n.disabled=true;
    }
  }
  document.getElementById('new-vote').disabled=true;
  document.getElementById('show-other').disabled=true;
}

function vote(choice){
  if(current_image){
    disable_vote_buttons();
    current_image.choice=choice;
    var controller='/vote/'+current_image.id+'/';
    // Have to double-escape '/' for the controller route to work,
    // and JavaScript automatically decodes '%XX' strings.
    controller+=encodeURIComponent(choice).replace(new RegExp('%'+'2F','g'),'%'+'252F');
    XMLDoc('POST',controller,'',voted);
  }
}

function new_vote(){
  var choice=document.getElementById('new-choice');
  choice.className='';
  if(choice.value){
    vote(choice.value);
  }else{
    alert("Pick an option or type a new option in the text box.");
    notice_this(choice);
  }
}

function choice_keypress(event, field){
  var e=event||window.event; // IE sucks
  var code=e.charCode||e.keyCode; // IE sucks
  var future_value=_future_value(code,field);

  if(!future_value){
    document.getElementById('new-vote').disabled=true;
  }else if((code==10||code==13)&&future_value){
    // enter, submit the vote
    new_vote();
  }else if((code<32)||e.charCode===0||e.ctrlKey||e.altKey){
    // do nothing
    return true;
  }else if(future_value){
    document.getElementById('new-vote').disabled=false;
  }
}

function button_click_scope_factory(choice){
  // This function creates a new scope containing 'choice'
  // For IE, onclick has have a real function, not a string
  return function (){ vote(choice); };
}

function clear(){
  document.getElementById('prompt').innerHTML='';
  hide('results-blurb');
  hide('no-results-blurb');

  var choices=document.getElementById('choices');
  var i;
  for(i=choices.childNodes.length;i>0;i--){
    choices.removeChild(choices.childNodes[i-1]);
  }
  var presults=document.getElementById('previous-results');
  for(i=presults.childNodes.length;i>0;i--){
    presults.removeChild(presults.childNodes[i-1]);
  }
  var cresults=document.getElementById('current-results');
  for(i=cresults.childNodes.length;i>0;i--){
    cresults.removeChild(cresults.childNodes[i-1]);
  }
  var comments=document.getElementById('comments');
  for(i=comments.childNodes.length;i>0;i--){
    var ccn=comments.childNodes[i-1];
    if(ccn.nodeName=='DIV'){
      comments.removeChild(ccn);
    }
  }
  var cip=document.getElementById('current-image-poster');
  var cipa=cip.getElementsByTagName('a');
  if(cipa.length){
    cip.removeChild(cipa.item(0));
  }
  var pip=document.getElementById('previous-image-poster');
  var pipa=pip.getElementsByTagName('a');
  if(pipa.length){
    pip.removeChild(pipa.item(0));
  }
  document.getElementById('new-choice').disabled=true;
  document.getElementById('new-vote').disabled=true;
  hide_detail('other');
}

function show_other(){
  show_detail('other');
  document.getElementById('new-choice').focus();
}

function image_load(response){
  clear();

  if(previous_image){
    var p_image_a=document.getElementById('previous-image');
    p_image_a.setAttribute('href','/#'+previous_image.id);
    var p_image_c=document.getElementById('previous-comments');
    p_image_c.setAttribute('href','/#'+previous_image.id);
    var p_image_html=p_image_a.getElementsByTagName('img').item(0);
    p_image_html.name=previous_image.id;
    p_image_html.width=150;
    p_image_html.src=previous_image.src;
    document.getElementById('your-choice').innerHTML=previous_image.choice.replace(/&/g,'&amp;').replace(/</g,'&lt;');
    var presults=document.getElementById('previous-results');
    for(var i=0;i<previous_image.results.length;i++){
      var pr=previous_image.results[i]
      Result_to_html(pr, presults);
      if(pr.choice==previous_image.choice){
	document.getElementById('your-points').innerHTML=format_points(pr.score);
	show_points();
      }
    }
    stumble_image(document.getElementById('previous_stumble'), previous_image);
    show('voted');

    if(previous_image.poster){
      var pip=document.getElementById('previous-image-poster');
      User_to_html(previous_image.poster,pip);
      show('previous-image-poster');
    }else{
      hide('previous-image-poster');
    }
  }else{
    hide('voted');
  }

  var images=response.getElementsByTagName('image');
  if(images.length===0){
    set_current_hash('');
    finished();
    return;
  }else{
    hide('finished');
    show('view');
    show('comments');
  }

  current_image=Image_from_xml(images.item(0));

  document.title=base_page_title+' '+image_title(current_image);

  var image_a=document.getElementById('current-image');
  //image_a.setAttribute('href', '/#'+current_image.id);
  var image_html=image_a.getElementsByTagName('img').item(0);
  image_html.name=current_image.id;
  image_html.src=current_image.src;
  //image_html.width=current_image.width;
  //image_html.height=current_image.height;
  set_current_hash(current_image.id);
  if(current_image.poster){
    var cip=document.getElementById('current-image-poster');
    User_to_html(current_image.poster,cip);
    show('current-image-poster');
  }else{
    hide('current-image-poster');
  }
  stumble_image(document.getElementById('current_stumble'), current_image);

  if(subdomain_prompts[current_image.domain]){
    document.getElementById('prompt').innerHTML=subdomain_prompts[current_image.domain];
  }else{
    document.getElementById('prompt').innerHTML=root_prompt;
  }

  var nc=document.getElementById('new-choice');
  nc.value='';
  nc.disabled=false;

  if(current_image.choices.length){
    show('vote');
    show('flag');
    var choices_html=document.getElementById('choices');
    for (var j=0;j<current_image.choices.length;j++){
      var b=document.createElement('button');
      b.innerHTML=current_image.choices[j];

      choices_html.appendChild(b);
      choices_html.appendChild(document.createTextNode(' '));

      b.onclick=button_click_scope_factory(current_image.choices[j]);
    }
    document.getElementById('show-other').disabled=false;
  }else if(current_image.choices.length===0 && !current_image.voted){
    if(current_image.is_owner){
      hide('vote');
      hide('flag');
    }else{
      show('vote');
      show('flag');
      show_other();
    }
  }else{
    hide('vote');
    hide('flag');
  }

  if(current_image.voted || current_image.is_owner){
    show('results');
    if(current_image.results.length){
      var cresults=document.getElementById('current-results');
      for(var k=0;k<current_image.results.length;k++){
	Result_to_html(current_image.results[k], cresults);
      }
      hide('no-results-blurb');
      show('results-blurb');
    }else{
      hide('results-blurb');
      show('no-results-blurb');
    }
    hide('comments-notvoted');
    if(get_cookie('userid')){
      hide('comments-unauthenticated');
      show('show-post');
    }else{
      hide('show-post');
      show('comments-unauthenticated');
    }
    XMLDoc('POST','/comments/'+current_image.id,'',load_comments);
  }else{
    document.getElementById('skip').href='javascript:skip();';
    document.getElementById('flag').href='javascript:flag();';
    hide('results');
    hide('show-post');
    show('comments-notvoted');
  }

}

function show_loading_spinner(){
  var image_a=document.getElementById('current-image');
  var image_html=image_a.getElementsByTagName('img').item(0);
  image_html.src='/themes/white/loading.gif';
}

function next(){
  show_loading_spinner();
  XMLDoc('POST','/view','',image_load);
}

function finished(){
  if(subdomain){
    document.getElementById('finished-subdomain-label').innerHTML=subdomain_labels[subdomain]+' ';
    show('finished-subdomain-item');
    var fsl=document.getElementById('finished-subdomain-list');
    fsl.innerHTML='';
    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));
	fsl.appendChild(a);
	if(i>subdomains.length-2){
	}else{
	  fsl.appendChild(document.createTextNode(', '));
	}
      }
    }
    fsl.appendChild(document.createTextNode(' and '));
    var a=document.createElement('a');
    a.setAttribute('href','http://'+domain+(port?':'+port:'')+'/');
    a.appendChild(document.createTextNode('anything'));
    fsl.appendChild(a);
    fsl.appendChild(document.createTextNode('.'));
  }else{
    hide('finished-subdomain-item');
  }
  show_detail('finished');
  hide('vote');
  hide('results');
  hide('view');
  hide('current-image-poster');
  hide('comments');
}

function load_location(l){
  current_location=l;
  var i=l.indexOf('#');
  var controller='view'; ///
  if(i!=-1){
    var image_id=l.slice(i+1);
    if(image_id===parseInt(image_id).toString()){
      controller+='/'+image_id;
    }
  }
  show_loading_spinner();
  XMLDoc('POST',controller,'',image_load);
}
