// greenhouse public CUSTOM JavaScript configuration - unique to site
// to use full prototype 'lib/prototype.js'  
	var IE6;
	var sFullPath = String(document.location);
	
	var SITEBASE = sFullPath.substr(0,sFullPath.lastIndexOf('/')+1 );   
	
//var aCustomBaseIncludes = ['x_core','x_event'];
	//Loader.load(aCustomBaseIncludes, 'includes/client/');

// 'countDown'  'sifr', 'sifr-config',

var aCustomIncludes = ['mootools-1.2-core-yc','mootools-1.2-more','uiScripts','branding','ps2','form_contact','nav']; //
	Loader.load(aCustomIncludes, 'custom/public/includes/client/');

function _init(id) {
	IE6 = (Browser.Engine.trident && Browser.Engine.version < 5)? true : false;
	if (!IE6) {
		if (id != 81) setupNav();
		if(typeof presetProjectSpotlights == "function") presetProjectSpotlights();
	}
	if(typeof presetProjects == "function") presetProjects();
	if(typeof presetContactForm == "function") presetContactForm();
	
	if(typeof presetBranding == "function") presetBranding();
	if(typeof setContent == "function") setContent();
	//do_sIFR();
}

var currentNode;
var currentNodeID = 0;


function gotoProject(id) {
	var pId = id;
	var myFx = new Fx.Scroll(window,{
		onComplete: function(){
			if ($('projects').getSize().y != 0) {
				gotoDetail(pId);
			} else {
				var end = $('projects').getFirst().getSize().y
				var hTween = new Fx.Tween('projects', {
					onComplete: function(){
						$('projects').setStyle('height','auto');
						gotoDetail(pId);
					}
				});
				hTween.start('height', end);
			}
		}
	}).toElement('projects');

}
function gotoDetail(id) {
	var button = $('project_'+id).getChildren()[1];
	var myFx = new Fx.Scroll(window,{
		onComplete: function(){
			openDetail(button, id);
		}
	}).toElement('project_'+id);
}

function gotoTop(){
	var myFx = new Fx.Scroll(window).toTop();
}

function openDetail(obj, id) {
	if (currentNodeID!=0 && currentNodeID!=id) {
		if ($('node_'+currentNodeID).getSize().y != 0) {
			openNode(currentNode, currentNodeID, true);
		}
	}
	openNode(obj, id, true);	
	currentNode = obj;
	currentNodeID = id;
	return false;
}

function openNode(obj, id, callback) {
	outerDiv = $('node_'+id);
	innerDiv = outerDiv.getFirst();
  button = $(obj);
	var nodeH = innerDiv.getSize().y;
	var curH = outerDiv.getSize().y;
	//
	//button.getParent().toggleClass('selected');
	(curH != 0)? button.getParent().removeClass('selected') : button.getParent().addClass('selected');
	var start = (curH != 0)? nodeH : 0;
  var end = (curH != 0)? 0 : nodeH;

	//outerDiv.tween('height', start, end);
	
	var hTween = new Fx.Tween(outerDiv, {
		onComplete: function(){
			if (callback) {
				if (curH != 0) { 
					$('node_'+id).getFirst().getFirst().destroy();
				} else {
					if (!IE6) {
						var myFx = new Fx.Scroll(window).toElement('project_'+id);
					} else {
						
						var projectsY = $('projects').getPosition().y;// + $('project_'+id).getPosition('projects').y;
						var projectY = $('project_'+id).getPosition('container').y;
						var endScroll = Number(projectsY + projectY - 150);
						var myFx = new Fx.Scroll(window).start(0,endScroll);
					}
					callForData(outerDiv, id);
				}
			}
		}
	});
	hTween.start('height', start, end);	

}

function callForData(el, s){

	$(outerDiv).getFirst().set('html','<div class="loading">loading</div>');

	//var myHTMLRequest = new Request.HTML([options]);
	//responseTree, responseElements, responseHTML, responseJavaScript//update:$('toolbar_content').getFirst(),
	var req = new Request.HTML({url:'custom/processors/proc_getProjectDetail.asp', data: 'myType='+s,  
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var e = $(outerDiv).getFirst();

				//e.set('text', '');//Clear the text currently inside the results div.

				e.set('html',responseHTML);
				$clear(myTimer);
				presetBranding();
				//e.innerHTML = html;
				//if(s == 'professional') setAlphaSearchContent();
			//openToolbar(true, el);

		},
		onFailure: function() {
			$(outerDiv).getFirst().set('html','<div>Unable to load.</div>');
			//openToolbar(true, el);
		}
	});

	req.send();

}

function loadContact(){
	
	if ($('contactholder').innerHTML == '') {

		var req = new Request.HTML({url:'custom/processors/proc_getContact.asp', data: 'myType=contact',  
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var e = $('contactholder');
				e.set('html','<div>'+responseHTML+'</div>');
				openContact();
				//do_sIFR();
			},
			onFailure: function() {
				$('contactholder').set('html','<div>Unable to load.</div>');
				openContact();
			}
		});

		req.send();
		
	} else {
		openContact();
	}

}
function loadContactForm(){
	

		var req = new Request.HTML({url:'custom/processors/proc_getContactForm.asp', data: 'myType=contact',  
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var e = $('contactform');
				e.set('html','<div>'+responseHTML+'</div>');
			},
			onFailure: function() {
				$('contactform').set('html','<div>Unable to load.</div>');
			}
		});

		req.send();

}


function initFeeds() {
  var feed = new google.feeds.Feed("http://www.carbonhouseblog.com/feed/");
	feed.setNumEntries(2);
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feedControl");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
				var sDate = new Date(entry.publishedDate);
				sDate = (sDate.getMonth() + 1) + '-' + sDate.getDate() + '-' + sDate.getFullYear();
        var li = document.createElement("li");
				var date = li.appendChild(document.createElement("div"));
				date.className = "date";
				date.appendChild(document.createTextNode(sDate));
        var link = li.appendChild(document.createElement("a"));
				link.appendChild(document.createTextNode(entry.title));
				link.href = entry.link;
				link.target = "_blank";
				
				// Have to pull data and insert as HTML so it will translate the entities
				// linkURL.innerHTML = entry.title;
		
				var tagline = li.appendChild(document.createElement("p"));
				tagline.className = "tag";
				tagline.innerHTML = entry.contentSnippet;
								
				// OLD var tagline = li.appendChild(document.createTextNode(entry.contentSnippet));
								
        container.appendChild(li);
      }
    }
  });
}

