﻿/// <reference path="jquery/jquery-1.3.2.min.js">

	function moveSectionUp(element,sectionName) {
		var section = GetSection(element);
		if ( section  != null ) {
			var section2 = GetPreviousSection(section);
			if (section2 != null) {
				
				$.ajax({
					type: "GET",
					url: "/spaAjax.aspx",
					data: "action=u&section=" + sectionName + "&r=" + Math.random(),
					cache: false,
					success: function(msg) { }
				});

				
				moveBefore(section, section2);
				fixButtons(section, section2);
				
				return false;
			}
		}
		return true;
	}
	
	function moveSectionDown(element,sectionName) {
		var section = GetSection(element);
		if ( section  != null ) {
			var section2 = GetNextSection(section);
			if (section2 != null) {

				 $.ajax({
				 	type: "GET",
				 	url: "/spaAjax.aspx",
				 	data: "action=d&section=" + sectionName + "&r=" + Math.random(),
				 	cache: false,
				 	success: function(msg) { }
				 });

				moveAfter(section, section2);
				fixButtons(section, section2);
				
				return false;
			}
		}
		return true;
	}
	
	function closeSection(element,sectionName) {
		var section = GetSection(element);
		if ( section  != null ) {
			var parent = section.parentNode;
		
			if (childNodesCount(parent) > 1) {

				 $.ajax({
				 	type: "GET",
				 	url: "/spaAjax.aspx",
				 	data: "action=c&section=" + sectionName + "&r=" + Math.random(),
				 	cache: false,
				 	success: function(msg) { }
				 });

				var p = previousSibling(section);
				var n = nextSibling(section);
				
				parent.removeChild(section);
				
				if ( n != null && p != null)
					return false;
					
					
				if (n==null && p!=null) {
					if(previousSibling(p) != null)
						fixButtons(section,p);
					else
						p.className = "section bff";
				}
				else {
					if (nextSibling(n) != null)
						fixButtons(section,n);
					else
						n.className = "section bff";
				}		
			}
			
			return false;
		}
		
		return true;
	}
	
	function fixButtons(element1, element2) {
		var a = element1.className;
		element1.className = element2.className;
		element2.className = a;
	}
	
	function GetNavButtons(element) {
		var c = element.getElementsByTagName('div');
		for(var x=0; x<c.length; x++) {
			if ( (' ' +c[x].className + ' ').indexOf(' navbut ') != -1)
				return c[x];
		}
		return null
	}
	
	function moveBefore(item1, item2) {
		var parent = item1.parentNode;
		parent.removeChild(item1);
		parent.insertBefore(item1, item2);
	}
	
	function moveAfter(item1, item2) {
		var parent = item1.parentNode;
		parent.removeChild(item1);
		parent.insertBefore(item1, item2 ? item2.nextSibling : null);
	}
	
	function GetSection(element) {
		var s = element;
		while (s != null && (' ' + s.className + ' ').indexOf(' section ') == -1 )
			s = s.parentNode;
		return s
	}
	
	function GetPreviousSection(section) {
		var s = previousSibling(section);
		while (s != null && (' ' + s.className + ' ').indexOf(' section ') == -1 )
			s = previousSibling(s);
		return s		
	}
	
	function GetNextSection(section) {
		var s = nextSibling(section);
		while (s != null && (' ' + s.className + ' ').indexOf(' section ') == -1 )
			s = nextSibling(s);
		return s		
	}
	
	
	// firefox fixes - wraps text in a nodeType 3 node
	function nextSibling(obj) {
		var vSibling;
	
		vSibling = obj.nextSibling;
		while (vSibling != null && (vSibling.nodeType==3 || (' ' + vSibling.className + ' ').indexOf(' fixed ') != -1)) {
			vSibling = vSibling.nextSibling;
		}
		
		return vSibling
	}

	function previousSibling(obj) {
		var vSibling;
		vSibling = obj.previousSibling;
		while (vSibling != null && (vSibling.nodeType==3 || (' ' + vSibling.className + ' ').indexOf(' fixed ') != -1)) {
			vSibling = vSibling.previousSibling;
		}
		return vSibling
	}
	
	function childNodesCount(obj) {
		var count = 0;
		var cn = obj.childNodes;
		
		for (var x=0; x<cn.length; x++) {
			if (cn[x].nodeType!=3 && (' ' + cn[x].className + ' ').indexOf(' fixed ') == -1)
				count += 1;
		}
		
		return count;
	}

