/**
 * Bicon Company Inc. 2008
 */

/* color picker function for left column */
function pick_color(x, y) {
  h = 33.0;
  w = 217.0;
  org = $('colorpicker').viewportOffset();
  x -= org[0];
  y -= org[1];
  y = h - y;
  if (x < 9 || y > 23 || x > 225) {
    return false;
  }
  if (x<0) x = 0;
  if (x>233) x = 233;
  if (y<0) y = 0;
  if (y>33) y = 33;
  r = parseInt(255.0-(y/h)*255.0*(1.0+Math.sin(6.3*x/w))/2.0);
  g = parseInt(255.0-(y/h)*255.0*(1.0+Math.cos(6.3*x/w))/2.0);
  b  = parseInt(255.0-(y/h)*255.0*(1.0-Math.sin(6.3*x/w))/2.0);
  color = b + 256 * g + 65536 * r;
  rcolor = (255-b) + 256*(255-g) + 65536*(255-r);
  var paramcolor = "color=" + encodeURIComponent(color.toString(16));
  new Ajax.Request('/main/setcolor', {
    asynchronous: true,
    evalScripts: true,
    onComplete: function() {
      document.getElementById('left_column').style.backgroundColor = "#"+color.toString(16);
    },
    parameters: paramcolor
  });
}
var clickFlag = true;
var dragFlag = false;
var optDraggable = {
  onStart: function(e) {
    clickFlag = false;
    UnTip();
  },	
  starteffect: function(drag) {
    drag.parentNode.style.zIndex = 1;
    drag.setStyle({ opacity: 0.5 });
    dragFlag = true;
    if (drag.id.charAt(0) == 's') {
    }		},
  endeffect: function(drag) {
    drag.parentNode.style.zIndex = 0;
    drag.setStyle({ opacity: 1.0 })
    .setStyle({ left: 0, top: 0 });
    dragFlag = false;
    if (drag.id.charAt(0) == 's') {
    }
  }
}
	
function isDragging() {
  return dragFlag;
}
	
function clickToNavigate(url) {
  if (clickFlag)
    window.location.replace( url );
  else {
    //alert("canceled "+url);
    clickFlag = true;
  }
}

function onStartDrag(sidx) {
  clickFlag = false;
  $('slot_'+sidx).setStyle({zindex: 200});
  alert('slot_'+sidx+': '+$('slot_'+sidx).getStyle('z-index'));
}
function onEndDrag(sidx) {
  $('slot_'+sidx).setStyle({zindex: 0});
}
function onDropTrash(drag, drop){
  for (var i = 0; i < 16; i++) {
    var paramstr = 'bicon='+encodeURIComponent(drag.id);
    $('slot_'+i).setOpacity(0.3);
  }
  $('spinner').show();
  new Ajax.Request('/main/trash', {
    asynchronous: true,
    evalScripts: true,
    onComplete: function(r) {
      $('spinner').hide(); 
      for (var i = 0; i < 16; i++) {
        $('slot_'+i).setOpacity(1.0);
      }
    },
    parameters: paramstr
  });
}
function onDropSlot(drag, drop) {
  var dragid = drag.id.toString();
  var dropid = drop.id.toString();
  var from;
  var to;
  var bicon;
  var arr = [];
  var paramstr;

  drag.parentNode.style.zIndex = 0;
  drag.setStyle({ left: 0, top: 0 });	

  dragid.scan(/\d+/, function(num){ arr[arr.length] = num; });
  dropid.scan(/\d+/, function(num){ to = parseInt(num); });			
	
  if (dragid.search("slot")>=0) {
    from = parseInt(arr[0]);
    bicon = parseInt(arr[1]);
    if (from<0||from>15||to<0||to>15||bicon<0) {
      alert("Invalid drop of bicon:"+bicon+" from:"+from+" to:"+to);
      return;
    }
    paramstr = 'from='+encodeURIComponent(from)+
      '&to='+encodeURIComponent(to)+
      '&bicon='+encodeURIComponent(bicon);
    if (from!=to) {
      $('slot_'+from).style.top = 0; // if tremble, return to normal position			
    }
    else
      return;	
  }
  else {
    bicon = parseInt(arr[0]);
    if (bicon<0||from<0||from>15) {
      alert("Invalid drop of bicon:"+bicon+" to:"+to);
      return;
    }
    paramstr = '&to='+encodeURIComponent(to) +
      '&bicon='+encodeURIComponent(bicon);				
  }
	
  //$('dashboard').setOpacity(0.5);
  for (var i = 0; i < 16; i++) {
    $('slot_'+i).setOpacity(0.3);
  }
  $('spinner').show();
  new Ajax.Request('/main/drop', {
    asynchronous: true,
    evalScripts: true,
    onComplete: function(r) {
      $('spinner').hide(); 
      //$('dashboard').setOpacity(1.0);
      for (var i = 0; i < 16; i++) {
        $('slot_'+i).setOpacity(1.0);
      }
    },
    parameters: paramstr
  });
}
	
function is_ie()
{
  return document.all? 1 : 0;
}
	
var steps = [-1,1,3,4,5,5,5,4,3,1,-1];
var tremble_pos = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
function tremble_enable(idx, val)
{
  tremble_pos[idx] = val;
}
	
function tremble_disable(idx)
{
  tremble_pos[idx] = -1;		
}
function tremble_do()
{
  var i = 0;			
  for(i=0; i<16; i++) {
    if (tremble_pos[i]<0)
      continue;
    var slot = document.getElementById("slot_"+i);
    if (slot) {
      slot.style.top = -steps[tremble_pos[i]] + 'px';
    }
    tremble_pos[i] += 1;
    if (tremble_pos[i]>=steps.length)
      tremble_pos[i] = 0;
  }
}
setInterval("tremble_do();", 60);