﻿
//variables

var isMoving = false;
var tmpInd;

/* ------------------------ */
/*	Template Reordering		*/
/* ------------------------ */

addEvent(window,'load',reorderingWatch);

function reorderingWatch() {
    if($('projectList')) addEvent($('projectList'),'selectstart',function() {return false;},false);
	jQuery('.moveHandle').bind("mousedown", function(e) {
		//reorderStart(this, e);
		if(isMoving) return;
		jQuery(this)
			.css({width:'100px',height:'100px',zIndex:'100',top:'-43px',right:'-42px',position:'absolute',zIndex:'2000'})
			.attr('id','movingBoxHandle')
			.parents('li').eq(0).attr('id','movingBox').css({zIndex:'10000'})
		tmpX1 =  (e.clientX + getScrollXY()[0])//(e.clientX - findPos(box)[0] + getScrollXY()[0]) + findPos(box)[0] - box.offsetLeft + 15;
		tmpY1 =  (e.clientY + getScrollXY()[1])//(e.clientY - findPos(box)[1] + getScrollXY()[1]) + findPos(box)[1] - box.offsetTop;
		tmpX2 =  $('movingBox').offsetLeft;
		tmpY2 =  $('movingBox').offsetTop;
		tmpInd = getInd($('movingBox'));
		isMoving = true;
		jQuery(this).parent().css({MozUserSelect:"none"});
	});
	jQuery(document).bind("mousemove", function(e) {reorderMove(e);});
	jQuery(document).bind("mouseup", function(e) {reorderEnd(e);});
}

function reorderShift(e) {
    if(typeof isMoving == 'undefined') return;
    if(!isMoving) return;
	alert('xx');
    var box;
	if(e.childNodes)
		box = e;
	else if(this.childNodes)
		box = this;
	else if(e.srcElement.childNodes)
		box = e.srcElement;
	if($('blankBox')) {
		$('blankBox').parentNode.removeChild($('blankBox'));
		box.parentNode.insertBefore($('blankBox'),box);
	}
}

function getInd(box) {
	var i = Math.round(Math.round(box.offsetTop / 210) * 3) + Math.round(Math.max(Math.min(Math.max(box.offsetLeft / 160, 0), 2), 0));
	i = Math.max(0,i);
	var max = box.parentNode.childNodes.length;
	i = Math.min(max-3,i);
	return i;
}

function reorderMove(e) {
    if(typeof isMoving == 'undefined') return;
    if(!isMoving) return;
    var box;
    box = $('movingBox');
	var x = ( e.clientX + getScrollXY()[0] ) - tmpX1;
	var y = ( e.clientY + getScrollXY()[1] ) - tmpY1;

    box.style.left = x + 'px';
    box.style.top = y + 'px';
    newInd = getInd(box);

    if(tmpInd != newInd) {
		x = box.offsetLeft; y = box.offsetTop;
		var p = box.parentNode;
		p.removeChild(box);
		var li = p.childNodes[newInd];
		p.insertBefore(box, li);

		for(i=0; i<p.childNodes.length; i++)
			p.childNodes[i].className = p.childNodes[i].className.replace("clear ","").replace("project ",(i%3 ==0 ? "project clear " : "project "));
		
		tmpX1 += (box.offsetLeft - x);
		tmpY1 += (box.offsetTop - y);
		var x = ( e.clientX + getScrollXY()[0] ) - tmpX1;
		var y = ( e.clientY + getScrollXY()[1] ) - tmpY1;

		box.style.left = x + 'px';
		box.style.top = y + 'px';

		tmpInd = newInd;
	}
	return false;
}

function reorderEnd(e) {
    if(typeof isMoving == 'undefined') return;
    if(!isMoving) return;
    var box;
    box = $('movingBox');
	isMoving = false;
	setTimeout(reorderSnapBack, 5);
	if($('blankBox')) $('blankBox').parentNode.removeChild($('blankBox'));
	var tempId = box.className.substring(box.className.indexOf('-')+1);
	jQuery.ajax({
		type: "POST",
		url: getAccUrl()+'partial/reordering/'+tempId,
		data: 'action=reorderTemplate&request=partial&templateid='+tempId+'&templateorder='+(tmpInd+1),
		success: function(data){}
	});
	box = $('movingBoxHandle');
	box.style.width = '15px';
	box.style.height = '15px';
	box.style.zIndex = '';
	box.style.top = '';
	box.style.right = '';
	box.style.position = 'relative';
	box.style.zIndex ='';
	box.id = '';
}

function reorderSnapBack() {
    box = $('movingBox');
    x = box.style.left.substring(0,box.style.left.length-2) /2;
    y = box.style.top.substring(0,box.style.top.length-2) /2;
    //alert(x);
	box.style.left = x+'px';
	box.style.top = y+'px';

	if(Math.abs(x)>5 || Math.abs(y)>5)
		setTimeout(reorderSnapBack, 1);
	else {
		box.style.left = '';
		box.style.top = '';
		box.id = '';
		box.style.position = '';
		box.style.zIndex = '';
	}
}
