//check if the next sibling node is an element node
function cleanWhitespace() {
  element = document;
  var cur = element.firstChild;

  while ( cur != null ) {
	  if ( cur.nodeType == 3 && ! /\S/.test(cur.nodeValue) ) {
	    element.removeChild( cur );
      } else if ( cur.nodeType == 1 ) {
	    cleanWhitespace( cur );
	  }

	  cur = cur.nextSibling;
    }
}

function stopBubbling(e) {
	//If an event object is provided, then this is a non-IE browser
	if ( e && e.stopPropagation )
	 	e.stopPropagation();
	else
	 	window.event.cancelBubble = true;
}

function bringToFront(bring) {
  var d = document.getElementById('dialogue').style.zIndex;
  var s = document.getElementById('studio').style.zIndex;
  var p = document.getElementById('project').style.zIndex;
  var last; 

  if (d == 14) { last = 'dialogue'; }
  else if (s == 14) { last = 'studio'; }
  else { last = 'project'; }
  if (last == bring) {
    if (d == 12) { last = 'dialogue'; }
    else if (s == 12) { last = 'studio'; }
    else { last = 'project'; }
  }

  document.getElementById('dialogue').style.zIndex=11;
  document.getElementById('studio').style.zIndex=11;
  document.getElementById('project').style.zIndex=11;
  document.getElementById(last).style.zIndex=12;
  document.getElementById(bring).style.zIndex=14;
}

function position(config, howfast) {
  	var d = document.getElementById('dialogue');
	var p = document.getElementById('project');
	var s = document.getElementById('studio');
	var b = document.getElementById('tinamanis-body');
	var thisconfig=("config-" + config);

	
  if (config == 1) {
	new Effect.Move(p, { x: 0, y: 100, duration: howfast, mode: 'absolute' });
	new Effect.Move(s, { x: 560, y: 167, duration: howfast, mode: 'absolute' });
	new Effect.Move(d, { x: 460, y: 233, duration: howfast, mode: 'absolute' });
  } else if (config == 2) {
	new Effect.Move(p, { x: 150, y: 100, duration: howfast, mode: 'absolute' });
	new Effect.Move(s, { x: 0, y: 225, duration: howfast, mode: 'absolute' });
	new Effect.Move(d, { x: 615, y: 75, duration: howfast, mode: 'absolute' });
  } else if (config == 3) {
	new Effect.Move(p, { x: 518, y: 100, duration: howfast, mode: 'absolute' });
	new Effect.Move(s, { x: 355, y: 300, duration: howfast, mode: 'absolute' });
	new Effect.Move(d, { x: 20, y: 175, duration: howfast, mode: 'absolute' });
  }

	Element.removeClassName(b, 'config-1');
	Element.removeClassName(b, 'config-2');
	Element.removeClassName(b, 'config-3');
	Element.addClassName(b, thisconfig);

}

function dropIt(what, howfast){
  if (what == 'dialogue'){
	  var d = document.getElementById('dialogue');
	var dx = parseInt(d.getStyle('left'));
	if (dx < 240) {
	  position(3, howfast);	
	} else if (dx > 500) {
	  position(2, howfast);
	} else {
	  position(1, howfast);
	}
  } else if (what == 'project') {
	  var p = document.getElementById('project');
	var px = parseInt(p.getStyle('left'));
	if (px < 150) {
	  position(1, howfast);
	} else if (px > 324) {
	  position(3, howfast);
	} else {
	  position(2, howfast);
	}
  } else if (what == 'studio') {
	  var s = document.getElementById('studio');
	var sx = parseInt(s.getStyle('left'));
	if (sx < 180) {
	  position(2, howfast);
	} else if (sx > 457) {
	  position(1, howfast);
	} else {
	  position(3, howfast);
	}
  }
} 

function dragIt(what, how){
	elem = document.getElementById(what);
	new Draggable(what, { constraint: false, 
								endeffect: false, 
								starteffect: false //, commented this out because it sticks in ie
								//onEnd:function(){ 
								  //  dropIt(what, how);	 
								//} 
							  });
	//add an event handler here, such as: addEvent( what, "onMouseUp", function(){ dropIt(what, how); }:
	if (elem.addEventListener) {
    elem.addEventListener("mouseup", function() { dropIt( what, how ); }, false);
  } else {
    elem.attachEvent("onmouseup", function() { dropIt( what, how ); });
  }
  
}

function justDrag(what){
	elem = document.getElementById(what);
	new Draggable(what, { constraint: false, 
								endeffect: false, 
								starteffect: false //, commented this out because it sticks in ie
								//onEnd:function(){ 
								  //  dropIt(what, how);	 
								//} 
							  });
	
}

function projectView(how, newProject){
	var dialogue = document.getElementById('dialogue');
	var studio = document.getElementById('studio');
	var imagesBlock = document.getElementById('images-block');
	var topNav = document.getElementById('top-nav');
	var genericHeading = document.getElementById('just-projects-section-head-word');
	var projectTitle = document.getElementById('project-section-head-title');
	
	Effect.Fade(dialogue, { duration: how });
  Effect.Fade(studio, { duration: how });
	Effect.Fade(genericHeading, { duration: 0 }); 
	Effect.Appear(topNav.id, { duration: how }); //does this work now in ie7?
	Effect.Appear(projectTitle.id, { duration: 0.17, queue: 'end' }); //does this work now in ie7?

	if (newProject != 'true'){
	  Effect.Appear('images-block', { duration: how });
		Effect.Appear('magnify', { duration: how });
	  Effect.Appear('project-image-caption', { duration: how });
	}
}

function centerView(what, howfast){
	var dialogue = document.getElementById('dialogue');
	var studio = document.getElementById('studio');
	var topNav = document.getElementById('top-nav');
	var imagesBlock = document.getElementById('images-block');
	var genericHeading = document.getElementById('just-projects-section-head-word');
	var projectTitle = document.getElementById('project-section-head-title');

	Effect.Appear(dialogue, { duration: howfast });
	Effect.Appear(studio, { duration: howfast });
	Effect.Fade(topNav, { duration: howfast });
  Effect.Fade(imagesBlock, { duration: howfast });
  Effect.Fade('project-image-caption', { duration: howfast });
  Effect.Fade('magnify', { duration: howfast });
  Effect.Fade('project-section-head-title', { duration: 0 });
	Effect.Appear(genericHeading.id, { duration: 0, queue: 'end' });
	
  if (what == 'dialogue') {
  	position(1, howfast);
	} else if (what == 'studio') {
  	position(3, howfast);
  }
	bringToFront(what);
}

function showTopNav(){
	var topNav = document.getElementById('top-nav');
	Effect.Appear(topNav, { duration: 0.1 } );
}

function downScroll(speed){
	var factoredSpeed = speed * 1000;
	var distance = 10;
	var intInterval = 0;
	var dialogue_box = $('dialogue-content-inner');
	var dh = parseInt(dialogue_box.getStyle('height'));
	
	Event.observe('dialogue-down', 'mouseup', function(event) {
	    intInterval=window.clearInterval(intInterval);
	});
	
	function moveMyScrollDivDownBaby() {
		dialogue_top = parseInt(dialogue_box.style.top);
		new Effect.Move(dialogue_box,{x: 0, y: distance * -1, duration: .001, mode:'relative'});
		if ((dh - 355) < (dialogue_top * -1)){
			intInterval=window.clearInterval(intInterval);
		}
	}
	
	if (parseInt(dialogue_box.style.top)){
	    var dialogue_top = parseInt(dialogue_box.style.top);
	} else {
		dialogue_top = 0
	}
	
	//$('address').innerHTML = ' ' + dialogue_top; //testing variables
	
	if ((dh - 355) > (dialogue_top * -1)){
		intInterval=window.setInterval(moveMyScrollDivDownBaby, 1);
	//	setInterval(moveMyScrollDivDownBaby, 0.1);
	//	clearInterval(intervalID);
	//	new Effect.Move(dialogue_box,{x: 0, y: -165, duration: speed, mode:'relative'})
	//	Event.stop(event);
	}
}

function upScroll(speed){
	var factoredSpeed = speed * 1000;
	var distance = 10;
	var intInterval = 0;
	var dialogue_box = $('dialogue-content-inner');
	var height = parseInt(dialogue_box.getStyle('height'));
	var dialogue_top = parseInt(dialogue_box.getStyle('top'));

	Event.observe('dialogue-up', 'mouseup', function(event) {
	    intInterval=window.clearInterval(intInterval);
	});
	
	function moveMyScrollDivUpBaby() {
		dialogue_top = parseInt(dialogue_box.style.top);
		if (dialogue_top > distance * -2){
			intInterval=window.clearInterval(intInterval);
			new Effect.Move(dialogue_box,{x: 0, y: 0, duration: .001, mode:'absolute'})		
		} else {
			new Effect.Move(dialogue_box,{x: 0, y: distance, duration: 0, mode:'relative'});
		}
	}

	if (dialogue_top < distance * -1){
		intInterval=window.setInterval(moveMyScrollDivUpBaby, 1);
	//	Event.stop(event);
	} else if (dialogue_top < 0){
		new Effect.Move(dialogue_box,{x: 0, y: 0, duration: speed, mode:'absolute'})		
		
	//	new Effect.Move(dialogue_box,{x: 0, y: 0, duration: speed, mode:'absolute'})
	//	Event.stop(event);	
	}
}
//refer to this for internet explorer addClassName variation and event attaching:
//
//if (window.attachEvent) {
//	b.className=b.className.replace(new RegExp(" config-3\\b"), "");
//	b.className+=thisconfig;
//} else {
//	b.removeClassName('config-3'); 
//	b.addClassName('config-' + config); 	
//}
//
function adminMenuAccordion(id, id2){
	var e = document.getElementById(id);
	var rows = document.getElementById("admin-menu").getElementsByTagName("div");
	for (var i=0; i<rows.length; i++) {
		if (Element.hasClassName(rows[i], 'pdf-active')) {
			if (rows[i].id != id){				
				Element.removeClassName(rows[i], 'pdf-active');
				Effect.BlindUp(rows[i].id, { duration: 0.4 });
			}
		}
	}
	Effect.toggle(id, 'blind', { duration: 0.4 });
	if (Element.hasClassName(e, 'pdf-active')) {
		Element.removeClassName(e, 'pdf-active')
	} else {
		Element.addClassName(e, 'pdf-active')
	}
	
}


sfMenuHover = function() {
	var dialogue = document.getElementById("dialogue-heading");
	var studio = document.getElementById("studio-heading");
	var project = document.getElementById("project-heading");

	var rows = document.getElementById("wrapper").getElementsByTagName("div");
	for (var i=0; i<rows.length; i++) {
		if (Element.hasClassName(rows[i], 'section-heading')) {
			rows[i].onmouseover=function() {
				Element.addClassName(this, 'over')
			}
			rows[i].onmouseout=function() {
				Element.removeClassName(this, 'over')
			}
			
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfMenuHover);

