//
//	MISC
//

var cx;
var cy;

function init() {
  if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}

function getXY(e) {
  cx = (window.Event) ? e.pageX : event.clientX;
  cy = (window.Event) ? e.pageY : event.clientY;
}

function showHide() { 
	for(i=0;i<arguments.length;i++) {
		element = document.getElementById(arguments[i]);
		element.style.display = (element.style.display?'':'none');
	}
}

function hide() {
	for(i=0;i<arguments.length;i++) {
		element = document.getElementById(arguments[i]);
		element.style.display = 'none';
	}
}

function show() {
	for(i=0;i<arguments.length;i++) {
		element = document.getElementById(arguments[i]);
		element.style.display = '';
	}
}

//
//	SELECTS
//

var openTracker = 0;

function switchLinks(type,onOff) {
	
	if(type=='top') {	
		document.getElementById('topLink').style.color=(onOff<=0?'black':'');
	} else {
		document.getElementById('allLink').innerHTML=(onOff<=0?'CLOSE ALL':'OPEN ALL');
		//document.getElementById('allLink').style.color=(onOff<=0?'black':'');
	}
}

function doAll() {
	
	isOpen=0;
	for(i=0;i<arguments.length;i++) {
		state	= document.getElementById('project'+arguments[i]).style.display;
		if(state=='') isOpen	+= 1;
	}
	
	if(isOpen) {
		for(i=0;i<arguments.length;i++) {
			document.getElementById('project'+arguments[i]).style.display='none';
			document.getElementById('projectLink_'+arguments[i]).style.color='';
			openTracker=0;
		}
	} else {
		for(i=0;i<arguments.length;i++) {
			getProject(arguments[i]);
		}
	}
	
	switchLinks('all',isOpen);
}

function topSelects() {
	
	for(i=0;i<arguments.length;i++) {
		getProject(arguments[i]);
	}
	
	switchLinks('all',0);
}

function closeAll() {

	for(i=0;i<arguments.length;i++) {
		document.getElementById('project'+arguments[i]).style.display='none';
		document.getElementById('projectLink_'+arguments[i]).style.color='';
		openTracker=0;
	}
}

var fetchedProjects	= new Array();

function getProject(id) {
	
	projectDiv					= document.getElementById('project'+id)
	state						= projectDiv.style.display;
	projectDiv.style.display	= (state=='none'?'':'none');
	
	if(state=='none'&&fetchedProjects[id]!=true) {
		ajaxGet('project'+id,'/index.php?ajax=project&id='+id,'','<i>Fetching media...</i>');
		fetchedProjects[id]=true;
	}
	
	if(state=='none') {
		document.getElementById('projectLink_'+id).style.color='black';
		openTracker+=1;
	} else {
		document.getElementById('projectLink_'+id).style.color='';
		openTracker=(openTracker-1);
	}
	
	switchLinks('all',(openTracker?0:1));
}

//
//	NAV
//
var fetchedContent	= new Array();

function openClose(id) {
	
	nav					= document.getElementById('nav'+id);
	page				= document.getElementById('page'+id)
	state				= page.style.display;
	nav.src				= (state=='none'?'/images/close.jpg':'/images/open.jpg');
	page.style.display	= (state=='none'?'':'none');

	
	if(state=='none'&&fetchedContent[id]!=true) {
	//if(page.style.display=='') {
		ajaxGet('content'+id,'/index.php?ajax=content&id='+id,'','<i>Fetching content...</i>');
		fetchedContent[id]=true;
	} else {
		
		stopQuicktime();
		
		//document.getElementById('content'+id).innerHTML = '';
	}
}

function stopQuicktime() {
	//
	//	STOP ANY QUCIK TIME OBJECTS
	//
	if(document.embeds.length) {
		for (i=0;i<document.embeds.length;i++) {
			//document.embeds[i].Stop()
		}
	}
}

//
//	PLAY DISPLAY
//
function playDisplay(media,move,width,height) {
	
	stopQuicktime();
	
	ajaxGet('player','/index.php?ajax=playview&id='+media+'&move='+move,'','<i>Fetching media...</i>')

	show('player');
	if(move) movePlayer(width,height);
}

function movePlayer(width,height) {
	
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0; 
	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	
	var x = window.innerWidth || document.body.clientWidth || 0;
	var y = window.innerHeight || document.body.clientHeight || 0;
	
	document.getElementById('player').style.left	= ((x/2)+scrollLeft)-(width/2)+"px";
	document.getElementById('player').style.top		= ((y/2)+scrollTop)-(height/2)+"px";
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

//
//	AJAX
//
function ajaxInteraction(divID,url,callback) {

	var req = init();
	req.onreadystatechange = processRequest;

	function init() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	function processRequest() {
		if(req.readyState==4) {
			if(req.status==200) {
				if(callback) {
					callback(divID,req.responseText,req.responseXML);
				} else {
					if(divID) document.getElementById(divID).innerHTML = req.responseText;
				}
			}
		}
	}

	this.doGet = function() {
		req.open("GET",url,true);
		req.send(null);
	}

	this.doPost = function(body) {
		req.open("POST",url,true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(body);
	}
}

function ajaxGet(divID,url,callback,loading) {
	var ajax = new ajaxInteraction(divID,url,callback);
	ajax.doGet();
	if(loading) setLoading(loading,divID);
}

function ajaxPost(divID,url,callback,loading) {
	var ajax = new ajaxInteraction(divID,url,callback);
	ajax.doPost();
	if(loading) setLoading(loading,divID);
}

function setLoading(loading,divID) {
	element				= document.getElementById(divID);
	element.innerHTML	= (loading>0?'Loading...':loading);
}


//
//	DRAG
//

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Clear the drag element global.

  dragObj.elNode = null;

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}


