var tip = [];
var tips = [];
var targetM;
var accumAns = [];
var firstT = [];
var finStr = [];

var   myoptions = {revert:false, helper: "original", containment:"document", start: function(ev, ui)
{
this.style.zIndex = 7;
},  stop: function(ev, ui)

{
this.style.zIndex = 5;
}






};

function getViewportSizeM()
{
  var size = [0,0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth !== 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

function getScrollingPositionM()
{
  //array for X and Y scroll position
  var position = [0, 0];

  //if the window.pageYOffset property is supported
  if(typeof window.pageYOffset != 'undefined')
  {
    //store position values
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
  }

  //if the documentElement.scrollTop property is supported
  //and the value is greater than zero
  if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
  {
    //store position values
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
  }

  //if the body.scrollTop property is supported
  else if(typeof document.body.scrollTop != 'undefined')
  {
    //store position values
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
  }

  //return the array
  return position;
}

function getElementsByAttributeM(attribute, attributeValue)
{
  var elementArray = [];
  var matchedArray = [];

  if (document.all)
  {
    elementArray = document.all;
  }
  else
  {
    elementArray = document.getElementsByTagName("*");
  }

  for (var i = 0; i < elementArray.length; i++)
  {
    if (attribute == "class")
    {
      var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

      if (elementArray[i].className.match(pattern))
      {
        matchedArray[matchedArray.length] = elementArray[i];
      }
    }
    else if (attribute == "for")
    {
      if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
      {
        if (elementArray[i].htmlFor == attributeValue)
        {
          matchedArray[matchedArray.length] = elementArray[i];
        }
      }
    }
    else if (elementArray[i].getAttribute(attribute) == attributeValue)
    {
      matchedArray[matchedArray.length] = elementArray[i];
    }
  }

  return matchedArray;
}


window.unload = (function() {


$("*",document.body).draggableDestroy().droppableDestroy().unbind();

});

function allhidden()
{

for (var i = 0; i < tips.length; i++)
  
  {
    

	for(y=0;y<DroAlength; y++)
	{
	


	
	tip[i][y].style.visibility = "hidden";


	}
  }
  }

window.onload = (function initTooltipsM()
{
  tips = getElementsByAttributeM("class", "hastooltip");
  var d;
  var y;
  

  for (var i = 0; i < tips.length; i++)
  
  
  {
    tip[i] = [];

	for(y=0;y<DroAlength; y++)
	{
	
	firstT[y] = true;
	tip[i][y] = document.createElement("div");


	tip[i][y].appendChild(document.createTextNode(contents[y][i]));
	tip[i][y].className = "Mtooltip"; // + i + y;
	tip[i][y].style.visibility = "hidden";
	tip[i][y].setAttribute("id", i + y + "tooltip");
	
	}
  }
  
  // other onload stuff can be done here
  

  
 $(".hastooltip").draggable(myoptions);
 

  
 $(".Mdroppable").droppable({ accept: ".hastooltip", tolerance: "intersect", drop: function(ev, ui) // use this as a callback and use the object properties
{
   
    targetM = ui.draggable; 
    
    
   


  var thenum = parseInt((targetM.attr("id")).substr(1),10); // this is a number, otherwise get key portion
 

  var thenum2 = parseInt(($(this).attr("id")).substr(1),10);
  
  
  
  
  
  var thistip = tip[thenum][thenum2];
  
  
  
 (ui.draggable).bind("dblclick", allhidden);

 



  var scrollingPosition = getScrollingPositionM();
  var cursorPosition = [0, 0];

  if (typeof ev.pageX != "undefined" && typeof ev.x != "undefined")
  {
    cursorPosition[0] = ev.pageX;
    cursorPosition[1] = ev.pageY;
  }
  else
  {
    cursorPosition[0] = ev.clientX + scrollingPosition[0];
    cursorPosition[1] = ev.clientY + scrollingPosition[1];
  }

  thistip.style.position = "absolute";
  thistip.style.left = cursorPosition[0] + 10 + "px";
  thistip.style.top = cursorPosition[1] + 10 + "px";
  thistip.style.visibility = "hidden";
 

  document.getElementsByTagName("body")[0].appendChild(thistip);

  var viewportSize = getViewportSizeM();

  if (cursorPosition[0] - scrollingPosition[0] + 10 + thistip.offsetWidth > viewportSize[0] - 25)
  {
    thistip.style.left = scrollingPosition[0] + viewportSize[0] - 25 - thistip.offsetWidth + "px";
  }
  else
  {
    thistip.style.left = cursorPosition[0] + 10 + "px";
  }

  if (cursorPosition[1] - scrollingPosition[1] + 10 + thistip.offsetHeight > viewportSize[1] - 25)
  {
    if (ev.clientX > (viewportSize[0] - 25 - thistip.offsetWidth))
    {
      thistip.style.top = cursorPosition[1] - thetip.offsetHeight - 10 + "px";
    }
    else
    {
      thistip.style.top = scrollingPosition[1] + viewportSize[1] - 25 - thistip.offsetHeight + "px";
    }
  }
  else
  {
    thistip.style.top = cursorPosition[1] + 10 + "px";
  }
for(var q=0;q< DroAlength; q++)
{
if(!(accumAns[q]))
{
accumAns[q] =  [];
}
}
var tempArray = [];
tempArray = ansArray[thenum2];

for(d=0;d<tempArray.length;d++)
{


if(tempArray[d] == ("D" + thenum))
{

if(tempArray.length > 1)
{

if((finStr[thenum2]) && (firstT[thenum2] == false))
{
alert(finStr[thenum2]);
break;
}

if(((checkArray[thenum2]) === true) && (d > 0 ) && (d != (accumAns[thenum2].length)) && firstT[thenum2] == true)
{
alert("Correct later, but not now.  ORDER MATTERS HERE.  Please move this drag object off the target.");
break;

}

}

alert(contents[thenum2][thenum]);

if((jQuery.inArray(tempArray[d],accumAns[thenum2])) == -1)
{
accumAns[thenum2][((accumAns[thenum2]).length)] = tempArray[d];
if(((accumAns[thenum2]).length) == 1) 
{
finStr[thenum2] = "";
finStr[thenum2] += "The answers were: ";
}

finStr[thenum2] += "*" +(document.getElementById(tempArray[d])).lastChild.lastChild.nodeValue + "* ";

}

if((tempArray.length > 1) && ((accumAns[thenum2]).length == tempArray.length) && firstT[thenum2] == true)
{
alert("You now got them all, congratulations!!");
if((checkArray[thenum2]) === true)
{
finStr[thenum2] += " (in that order)";
}
alert(finStr[thenum2]);
firstT[thenum2] = false;

}


 break;
}

if(d == (tempArray.length - 1))
{
  thistip.style.visibility = "visible";
  
  
  }
  
 } 
  return true;
}, out: function(ev, ui)
{

for (var i = 0; i < tips.length; i++)
  
  {
    

	for(y=0;y<DroAlength; y++)
	{
	


	
	tip[i][y].style.visibility = "hidden";


	}
  }
  }
});


  return true;
});





