// JavaScript Document

var results = new Array();
var order;
$(function() {
	$("#sortable").sortable({
		//axis: 'y',
		containment: 'parent',
		scroll: false,
		update: function(event, ui) { 
			updateLabels();
		}
	});;

	$("#sortableDeleted").sortable({
		axis: 'y',
		connectWith: '#sortable',
		update: function(event, ui) { 
			//updateLabels();
		}
	});;
	processKeyPresses();
});

function processKeyPresses() {
	$('#sortable input').keypress(function (e) {
		if ((e.which == 8) || (e.which == 46)) { return true; } // backspace or delete - allow it
		if (e.which != 13 && !(e.which >= 49 && e.which <= 56)) { //if its not a number between 1 and 8, bail
			return false;
		}

		if (e.which == 13) { //if they hit enter, process it
			query = ($('#sortableInputs').serialize()) + '&' + $("#sortable").sortable('serialize');

			$.ajax({
				type: "POST",
				url: "updateSortable.php",
				data: query,
				success: function(msg){
					$('#sortable').html(msg);
					updateLabels();
					processKeyPresses();
				}
			});
		}
	});
}

//swap the current li node with the one above it
function up(button_node) {
	var current_node = button_node.parentNode;
	var list_children = document.getElementById('sortable').childNodes;
	var list_node = document.getElementById('sortable');
	var temp_node;
	var previous_node;
  
	for(var i=0;i<list_children.length;i++) {
		if(current_node.id == list_children[i].id && previous_node) {
			temp_node = list_children[i].cloneNode(true);
			list_node.removeChild(list_children[i]);
			list_node.insertBefore(temp_node,previous_node);
			var position = $(temp_node).position();
			newheight = position.top+52+'px';
			$(temp_node).css("zIndex",i*20);
			$(temp_node).animate({"top": "+=52px"},1);
			$(temp_node).animate({opacity: .2},1);
			$(temp_node).animate({"top": "-=52px",opacity: 1});
	}
		if(list_children[i].tagName=='LI') previous_node=list_children[i];
	}
}

//swap the chosen li node with the one below it
function down(button_node) {
	var current_node = button_node.parentNode;
	var list_children = document.getElementById('sortable').childNodes;
	var list_node = document.getElementById('sortable');
	var temp_node;
	var previous_node = "";
  
	for(var i=0;i<list_children.length;i++) {
		if(current_node.id == list_children[i].id) {
			previous_node=list_children[i];  
		} else if(previous_node && list_children[i].tagName=='LI') {
			temp_node = list_children[i].cloneNode(true);
			list_node.removeChild(list_children[i]);

			list_node.insertBefore(temp_node,previous_node);
			var position = $(temp_node).position();
			newheight = position.top+52+'px';
			$(temp_node).css("zIndex",i*20);
			$(temp_node).animate({"top": "+=52px"},1);
			$(temp_node).animate({opacity: .2},1);
			$(temp_node).animate({"top": "-=52px",opacity: 1});
			previous_node = "";

		}
	}
}


function updateLabels() {
	results = $('#sortable').sortable('toArray');
	for (i=0;i<results.length;i++) {
		theitem = '#'+results[i]+' input'; 
		$(theitem).val(i+1);
	}	

	results = $('#sortableDeleted').sortable('toArray');
	for (i=0;i<results.length;i++) {
		theitem = '#'+results[i]+' span'; 
		$(theitem).html('0');
	}	

}

function moveUp(whichOne) {
	//$(whichOne.parentNode).css("top",'-25px');
	up(whichOne);
	$("#sortable").sortable('refreshPositions');
	//up(whichOne);
	updateLabels();
	return false;
}

function moveDown(whichOne) {
	down(whichOne);
	updateLabels();
	return false;
}

//swap the current li node with the one above it
function removeElement(button_node) {
	var current_node = button_node.parentNode;
	var list_children = document.getElementById('sortable').childNodes;
	var list_node = document.getElementById('sortable');
	var new_children = document.getElementById('sortableDeleted').childNodes;
	var new_node = document.getElementById('sortableDeleted');
	var temp_node;
	var previous_node;

	if (current_node.parentNode.id == 'sortable') {
		var numberOfItems = 0;
		for(var i=0;i<list_children.length;i++) {
			if (list_children[i].tagName == 'LI') numberOfItems++; 
		}		
		if (numberOfItems <= 3) { 
			//tb_show('Please keep at least 3 business goals','#TB_inline?height=0&width=200&inlineId=lessThanThree', '')
			alert('Please keep at least 3 business goals','#TB_inline?height=0&width=200&inlineId=lessThanThree');
		}
		else {
			for(var i=0;i<list_children.length;i++) {
				if(current_node.id == list_children[i].id) {
					temp_node = list_children[i].cloneNode(true);
					list_node.removeChild(list_children[i]);
					new_node.appendChild(temp_node);
				}
			}
		}
	} else {
		for(var i=0;i<new_children.length;i++) {
			if(current_node.id == new_children[i].id) {
				temp_node = new_children[i].cloneNode(true);
				new_node.removeChild(new_children[i]);
				list_node.appendChild(temp_node);
			}
		}
	}		
	updateLabels();
	return false;
}

function gotoStep2() {
	query = $("#industrySelectForm").serialize() + '&' + $("#sortable").sortable('serialize');
	$.ajax({
		type: "POST",
		url: "process-step1.php",
		data: query,
		success: function(msg){
			if (msg == 1) { location.href = 'step2.php'; }
			else {
				$('#error').html(msg);
			}
		}
	});

	return false;
}