var float_img = document.getElementById('img_float');
var position = 1;
var float_arr = new Array();
var float_art_arr = new Array();
float_arr['loading'] =  new Image;
float_arr['loading'].src = 'images/card_loading.png';
var rate_orig = new Array();
var work_q = new Array();
var work_q_locked = 0;

function process_q() {
   var job;
   if (work_q_locked == 1) return;
   while (job = work_q.pop()) {
      switch (job[0]) {
         case 'open_float':
            _close_float(job[1],job[3]);
            _open_float(job[1],job[2],job[3]);
            break;
         case 'close_float':
            _close_float(job[1],job[2]);
            break;
      }
   }
   work_q_locked == 0;
}

function open_float(id, image, thumb_img) {
   var job = new Array('open_float',id,image,thumb_img);
   work_q.push(job);
   process_q();
}

function close_float(id, thumb_img) {
   var job = new Array('close_float',id,thumb_img);
   work_q.push(job);
   process_q();
}

function _open_float(id, image, thumb_img) {
   if (!float_arr[id]) {
      float_arr[id] = new Image;
      float_arr[id].src = 'images/cards/' + image;
   }
   if (thumb_img != undefined && thumb_img != null 
         && thumb_img.src != undefined && thumb_img.src != null) {
      if (!float_art_arr[id]) {
         float_art_arr[id] = new Image;
         var art_img = image.replace(/\.jpg/, '.png');
         float_art_arr[id].src = 'images/art_sml/art_sml_' + art_img;
      }
      thumb_img.src = float_art_arr[id].src;
   }
   float_img.src = float_arr[id].src;

   if (float_img.className == 'img_float_hidden') {
      float_img.className = 'img_float_visible';
   }
   // float_img.visibility = 'visible';
   float_img.style.top = (document.all)?(document.documentElement.scrollTop + 50):(window.pageYOffset + 25) + 'px';
}


function _close_float(id, thumb_img) {
   if (float_img.className == 'img_float_visible') {
      float_img.className = 'img_float_hidden';
   }
   //float_img.visibility = 'hidden';
   float_img.src = float_arr['loading'].src;
   if (thumb_img != undefined && thumb_img != null 
         && thumb_img.src != undefined && thumb_img.src != null
         && float_arr[id] != undefined && float_arr[id] != null) {
      thumb_img.src = float_arr[id].src;
   }
}


function move_float() {
   if (position == 1) {
      float_img.style.left = 'auto';
      float_img.style.right = '25px';
      position = 2;
   } else {
      float_img.style.right = 'auto';
      float_img.style.left = '25px';
      position = 1;
   }
}


function rate_over(rating) {
   var i;
   if (rate_orig[1] == undefined) {
      for (i=1; i <= 5; i++) {
         var img = document.getElementById('rate_' + i);
         rate_orig[i] = img.src;
      }
   }
   for (i=1; i <= rating; i++) {
      var img = document.getElementById('rate_' + i);
      img.src = "images/star_gold.png";
   }
   for (; i <= 5; i++) {
      var img = document.getElementById('rate_' + i);
      img.src = "images/star_grey.png";
   }
}


function rate_out(img) {
   var i;
   if (rate_orig[1] != undefined) {
      for (i=1; i <= 5; i++) {
         var img = document.getElementById('rate_' + i);
         img.src = rate_orig[i];
      }
   }
}


function rate(deck_id, user_id, rating) {

   if (rating == undefined || !isFinite(rating)
      || user_id == undefined || !isFinite(user_id) 
      || deck_id == undefined || !isFinite(deck_id)) {
      alert('Invalid something. ' + rating + ' ' + deck_id + ' ' + user_id);
      return false;
   }
   if (rate.xhr_busy == true) {
      return;
   }
   rate_orig = new Array();
   rate_over(rating);

   if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      rate.xhr = new XMLHttpRequest();
   } else if (window.ActiveXObject) { // IE
      rate.xhr = new ActiveXObject("Microsoft.XMLHTTP");
   }
   rate.xhr_busy = true;
   rate.xhr.onreadystatechange = rate_process_ajax_response;
   rate.xhr.open('POST', "deck_builder_ajax.php", true); 
   rate.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   rate.xhr.send("id=" + deck_id + "&mode=rate&rating=" + rating);
}


function rate_process_ajax_response() { 
   // State 4 is "complete"
   if(rate.xhr.readyState == 4) {
      // Status 200 is a normal http response
      if(rate.xhr.status && rate.xhr.status != 0 && rate.xhr.status == 200) {
         if (rate.xhr.responseText.substr(0,7) != "SUCCESS") {
            alert("Error " + rate.xhr.status + " occured while saving.  Your changes were probably not saved.\n\n" 
               + rate.xhr.responseText );
         }
      }  
      rate.xhr_busy = false;
      if (window.location != undefined) window.location.reload();
      else if (history != undefined) history.go(0);
      else if (window.location.href != undefined) window.location.href=window.location.href;
   }
} 


