/*************************************************************
 * Magnetic AJAX - Version 3.0
 *
 * This is like the magnetic poetry things. If you use this
 * code, please give me credit and a link to my site would be nice.
 * http://www.broken-notebook.com.
 *
 * Copyright (c) 2005-2007, Garrison Locke
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *   * Neither the name of the http://www.broken-notebook.com nor the names of its
 *     contributors may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 *************************************************************/


var currentlyHeldTile = "";
var interval, s=7;
var indexLevel = 1;

var draggableOptions = {
    container: 'board',

    onStart:function()
    {
        currentlyHeldTile = this.handle.id;
        this.handle.setOpacity(.5);
        indexLevel++;
        if (indexLevel >= 65535) {
            indexLevel = 1;
        }
        this.handle.style.zIndex = indexLevel;
    },

    onComplete:function()
    {
        currentlyHeldTile = "";
        this.handle.setOpacity(1);
        var query = Object.toQueryString({f: 'save', id: this.handle.id, x: this.handle.getLeft(), y: this.handle.getTop(), z: this.handle.style.zIndex});
        new Ajax('magneticAjax.php', {
        	method: 'post',
        	//onComplete: saveCallback,
	        postBody: query}).request();
    }
};

function saveCallback(request)
{
    alert(request);
}


window.onload=function()
{

    for (i=1; i<=numWords; i++) {
        $('w_' + i).makeDraggable(draggableOptions);
    }

    startTimer();
};


function repaint()
{
    var query = 'f=repaint';
    new Ajax('magneticAjax.php', {
    	method: 'post',
    	onComplete: repaintCallback,
        postBody: query}).request();
};

function repaintCallback(response)
{
    lines = response.split("|");
	for(i=0; i<numWords; i++){
		lines[i] = lines[i].split(":");
		wordId = i+1;
		if("w_" + wordId != currentlyHeldTile){
			if (parseInt($("w_" + wordId).style.top) != lines[i][0] ||
			    parseInt($("w_" + wordId).style.left) != lines[i][1] ||
			    $("w_" + wordId).style.zIndex != lines[i][2]) {
				currX = parseInt($("w_" + wordId).style.left);
				currY = parseInt($("w_" + wordId).style.top);
				$("w_" + wordId).style.zIndex = lines[i][2];
				var myFx = new Fx.Style('w_' + wordId, 'left').start(currX, lines[i][1]);
				var myFx = new Fx.Style('w_' + wordId, 'top').start(currY, lines[i][0]);
			}
		}
	}
};


function countdown()
{
	if (s==0) {
		repaint();
		s = 7;
		return;
	}
	s-=1;
};


function startTimer()
{
	interval=setInterval('countdown();',1000);
};