/******************************************

	Javascript library for Fox Flash
	Author: Digitaria Inc.
	Created: 06.28.2006
	Modified: 06.29.2006
	addLoadEvent functionality by Simon Willison - http://simon.incutio.com

******************************************/
/***** XMLHTTPRequest object section  *****/
// Create XMLHttpRequest object
var xmlhttp;
var xmlhttpready = true;
xmlhttp = false;
// native XMLHttpRequest object like firefox and safari
if(window.XMLHttpRequest)
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch(e)
	{
		xmlhttp = false;
	}
// IE/Windows ActiveX version (why do people still use this browser?)
}
else if(window.ActiveXObject)
{
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
}
if(!xmlhttp)
	alert("Error loading XMLHTTP object");
/****** End XMLHTTPRequest object creation  *****/

// write new page to specified element
function showPage(id,pageName)
{
	if(!pageName)	// in case no page gets passed or is passed incorrectly
		return false;
	xmlhttpready = false;
	var doNtChacheAdd='donotcacheparameter='+(Math.random()*10000);
	var regExp = RegExp = /\?/i;
	if (pageName.match(regExp) != null) {
		pageName+='&'+doNtChacheAdd;
	} else {
		pageName+='?'+doNtChacheAdd;
	}
    regExp = null;
	//Math.random( )*10000);
	//pageName

	if (document.getElementById('time_wait_pictute')!=null) {
		document.getElementById('time_wait_pictute').src="media/spinner.gif";
	}
	xmlhttp.open("GET", pageName,true);	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4)
		{
			if(xmlhttp.status == 200)
			{
					if (document.getElementById('time_wait_pictute')!=null) {
						document.getElementById('time_wait_pictute').src="media/1pxl.gif";
					}
				var target = document.getElementById(id);
				if (target !=null) {
					target.innerHTML = xmlhttp.responseText;
		            fadeIn(id,0);
	                treeNavInit();
					//alertnw(target.innerHTML)
				}
    		}
			else
			{
				alert("Data failed to load because the file, " + pageName + ", wasn't found on the server.");
			}
		xmlhttpready = true;
		}
	 }
	 xmlhttp.send("");
}

function ShowPageSync(id,querypath)
{
	//alert(xmlhttp.readyState);
	if (xmlhttp.readyState!=0 && xmlhttp.readyState!=4) {
		window.setTimeout("ShowPageSync('"+id+"','"+querypath+"')", 50);
	} else {
		showPage(id,querypath);
	}
}


// onload function written by Simon Willison - http://simon.incutio.com
// use this function instead of window.onload - usage addLoadEvent(functionName)
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
// end addLoadEvent()

function alertnw(message) {
	var newwin = window.open('','_blank', 'width=500,height=300,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,');
	newwin.document.write(message);
	return true;
}

// custom function to get elements by their class name
// function returns an array of the element objects that match the class name
document.getElementsByClassName = function(className, debug)
{
	var children = document.getElementsByTagName("*") || document.all;		// get all the elements on the page and load into an array
	var elements = [];
	for(i = 0; i < children.length; i++)
	{
		if(children[i].className == className)
		{
			elements.push(children[i]);
		}
	}
	return elements;
}
/*---------------------*/



function setupExternalVar(id, value)
{
	var el;
   if (el = document.getElementById(id)) {
		el.value =  value;
   }
}


// simulates a tree navigation
function treeNavInit()
{
	var actuators = document.getElementsByClassName("actuator");	// expand/contract link
	var subMenus = document.getElementsByClassName("subMenu");		// the corresponding menus

	for(var i = 0; i < actuators.length; i++)
	{
		actuators[i].className = " expandIcon";		// write the class that puts the expand icon on the link
		actuators[i].onclick = function()			// setup the onclick events for each actuator
		{
			var child = this.nextSibling;
			while(child)
			{
				if(child.nodeName == "UL" && child.className == "subMenu")
				{
					display = child.style.display;
					if(display == "block")
					{
						display = "none";
						this.className = this.className.replace("contractIcon"," expandIcon");
					}
					else
					{
						display = "block";
						this.className = this.className.replace("expandIcon","contractIcon");
					}
					child.style.display = display;
					break;
				}
				else
				{
					child = child.nextSibling;
				}
			}
			return false;
		}
		if (actuators[i].id) {
		actuators[i].style.clear='both';
		actuators[i].onclick();
		};
	}
	//alert(actuators[0].className);

	setupExternalVar('archived', isLiveTabSelected());
}
/*---------------------*/

// modified 08.09.2006
var dragTbls;
function loadDragArray(id,name)
{
	var dragBox = document.getElementById(id);
	dragTbls = document.getElementsByClassName(name);
	for(var i = 0; i < dragTbls.length; i++)
	{
		var dragLinks = dragTbls[i].getElementsByTagName("a");
		for(var j = 0; j < dragLinks.length; j++)
		{
			//alert(dragLinks[j].getAttribute("rel"));
			if(rel = dragLinks[j].getAttribute("rel"))
			{
				dragLinks[j].onclick = function() 	// custom onclick event for drag handle links
				{
					rel = this.getAttribute("rel")		// get the attribute
					relSplit = new Array();
					relSplit = rel.split(".");			// split it up
					if(relSplit[0] == "move")
						moveElement(relSplit[1], relSplit[2]);
					orderCheck();
					return false;
				};
			}
		} // end for
	} // end for
} // end loadDragArray()
/*---------------------*/

// function to move a draggable elment up and down with click events
// (direction to move, containing element)
function moveElement(direction, pEl)
{
	var upError = "Sorry, the item is at the top so you can't move it up anymore";
	var downError = "Sorry, the item is at the bottom so you can't move it down anymore";
	var element = dragTbls[pEl-1];

	if(direction == "up")
	{
		var prevSib = element.previousSibling;
		if(prevSib == null)	// this stops an error from being thrown if there is no previous sibling
		{
			userError(upError);
			return false;
		}

		while(prevSib != null)
		{
			var current = prevSib;
			prevSib = prevSib.previousSibling;
			/*if(prevSib == null)	// this stops an error from being thrown if there is no previous sibling
			{
				userError(upError);
				return false;
			}*/
		}
		//element.className = element.className.replace(element.className, "moved");
		helpers.moveBefore(element,current);		// use a function from ToolMan library to move items
	}
	else if(direction == "down")
	{
		var nextSib = element.nextSibling;
		if(nextSib == null)	// this stops an error from being thrown if there is no previous sibling
		{
			userError(downError);
			return false;
		}
		while(nextSib.nodeType == 3)
		{
			var current = nextSib;
			nextSib = nextSib.nextSibling;
			/*if(nextSib == null)	// this stops an error from being thrown if there is no previous sibling
			{
				userError(downError);
				return false;
			}*/
		}
		helpers.moveAfter(element,current);		// use a function from ToolMan library to move items
	}
	//moveColor(element, pEl);
}
/*---------------------*/

// modified 08.09.2006
/*function moveElement(direction, index)
{
	alert(dragTbls.length);
}*/
/*---------------------*/

function moveColor(element, pEl)
{
	var child = element.firstChild;
	if(child != null)
	{
		while(child.nodeType == 3 || child.nodeName == "TBODY")
		{
			child = child.nextSibling;		// this will give us tbody instead of tr
			if(child.nodeName == "TBODY")
				child = child.firstChild;	// going down another level to grab the tr
		}
	}
	//child.className = child.className.replace(child.className, "moved");
	child.style.background = "rgb(153,204,102) url()";
	fadeColor(child, child.style.backgroundColor, pEl);
}

function fadeColor(element,color, pEl)
{
	var color = element.style.backgroundColor;
	color = color.substring(4,13);
	alert(color);
	//if((pEl-1)%2)
		//alert("even");

}

// function to alert a user to an error or other important messages
function userError(message)
{
	alert(message);
}
/*--------------*/

// function to switch tabs to their on state
var oldTab = null
function switchTabs(id)
{
	if(oldTab == null)
		oldTab = document.getElementById("tabLive");
	if(oldTab.id == id)
		return false;
	//alert(id);
	var tab = document.getElementById(id);
    if (document.getElementById("treeMenu")) setOpacity(document.getElementById("treeMenu"),0);
	if (document.getElementById("treeMenuCorporate")) setOpacity(document.getElementById("treeMenuCorporate"),0);
	var treemenupath = "/actions/buildshowstree.php?aID="+document.getElementById('aID').value+"&RootID="+document.getElementById('RootID').value;
	if(id == "tabLive")
	{
		var sib = tab.parentNode.nextSibling;
		var dir = "next";
		//showPage("treeMenu",treemenupath+"&type=0");
		showPage("treeMenu",treemenupath+"&type=0");
		//setTimeout("treeNavInit()",50);
		ShowPageSync("treeMenuCorporate",treemenupath+"&type=0&corporate=1");
		//alert('e');
		//setTimeout("treeNavInit()",50);
		if (ShowsAutoComp!='undefined') {ShowsAutoComp.hideDiv(); ShowsAutoComp = null;}
		document.getElementById("treeSearchInput").value = '';
		document.getElementById("treeSearchInput").focus();
		ShowsAutoComp = new AutoComp(document.getElementById("treeSearchInput"), "treeSearch", "/siteadmin/includes/phpbin/templates/cmt_shows/autocomplete_navtree.php", 'archive=0', 'goToShow');
		//showPage("treeMenuCorporate",treemenupath+"&type=0");
	}
	else
	{
		var sib = tab.parentNode.previousSibling;
		var dir = "prev";
		//showPage("treeMenu",treemenupath+"&type=1");
		showPage("treeMenu",treemenupath+"&type=1");
		//setTimeout("treeNavInit()",50);
		ShowPageSync("treeMenuCorporate",treemenupath+"&type=1&corporate=1");

		//showPage("treeMenuCorporate",treemenupath+"&type=1");

		if (ShowsAutoComp!='undefined') {ShowsAutoComp.hideDiv();}
		document.getElementById("treeSearchInput").value = '';
		document.getElementById("treeSearchInput").focus();
		ShowsAutoComp = new AutoComp(document.getElementById("treeSearchInput"), "treeSearch", "/siteadmin/includes/phpbin/templates/cmt_shows/autocomplete_navtree.php", 'archive=1', 'goToShow');

	}

		oldTab.parentNode.className = oldTab.parentNode.className.replace("selected", "")
		tab.parentNode.className = "selected";

	oldTab = tab;
	//alert(isLiveTabSelected());
}
/*--------------*/

function isLiveTabSelected()
{
	if (!oldTab || oldTab.id == 'tabLive') {
		return 0;
	}
	return 1;
}


function setOpacity(obj, opacity)
{

	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";

	// Safari < 1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity,speed)
{
	if (document.getElementById)
	{
		obj = document.getElementById(objId);
		if (opacity <= 100)
		{
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+","+speed+")", 50);
		}
	}
}

//modified 08.09.2006
function DragBox(id,objName)
{
	this.id = id;
	this.elObj = document.getElementById(this.id);
	var thisObj = this;
	this.linkArray = new Array();

	this.loadArray = function()
	{
		this.dragElements = this.getElementsByClassName("dragTable");
		this.clickLinks = this.elObj.getElementsByTagName("a");
		this.i = 0;

		for(var j = 0; j < this.clickLinks.length; j++)
		{
			if(rel = this.clickLinks[j].getAttribute("rel"))
			{
				this.linkArray.push(this.clickLinks[j]);
				this.i = this.linkArray.length;
				alert(this.linkArray[this.i]);
				this.clickLinks[j].onclick = function() 	// custom onclick event for drag handle links
				{
					rel = this.getAttribute("rel")		// get the attribute
					relSplit = new Array();
					relSplit = rel.split(".");			// split it up
					if(relSplit[0] == "move")
					thisObj.moveElement();
					return false;
				};
			}
		}
	};

	this.moveElement = function(index)
	{
		alert(index);
	};

	this.getElementsByClassName = function(className)
	{
		var children = this.elObj.getElementsByTagName("*") || document.all;		// get all the elements on the page and load into an array
		var elements = [];
		for(i = 0; i < children.length; i++)
		{
			if(children[i].className == className)
			{
				elements.push(children[i]);
			}
		}
		return elements;
	};
}



