/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Treefingers 2.0 Custom Javascript
// Wish everyone couldn't see this.  Our php is much nicer!
/////////////////////////////////////////////////////////////////////////////////////////////////////////

// Ajax Updater to stop showing the loading image and replace it with the grid
function showGrid() {
	new Ajax.Updater('show_grid_box', 'showgrid.php',
	 { 	method: 'get', 
	  	parameters: pars //+ '&search=' + srch 
	  }
	  );
}

// Reloads the grid periodically!
function reGrow(){
	// Another Global for the periodical updater
	 grid = new Ajax.PeriodicalUpdater('show_grid_box', 'showgrid.php', 
	 { 	method: 'get', 
		frequency: 20, 
		decay: 1.5 , 
		parameters: pars }
		);
}

// Reloads the plant preview plant each keypress
function showLiveUpdate() {
var com = document.getElementById('post_comment');
	new Ajax.Updater('plant_livepreview', 'plant_thought_ajax.php',
	 { 	method: 'get', 
	  	parameters: 'preview=1&comment='+ com.value
	  	}
	  );
}

// Are you accepting our cookies
function cookieCheck() {
	new Ajax.Updater('leftnav', 'cookie_check.php');
}

// Does the shortcut stuff
function shortCuts(wut) {

	if (wut == "enable") {
	
		// add a keyboard shortcut for showing plant box
		shortcut.add("Shift",function() {
		location.href='#new'; 
		showLB('new');
				},{
		'type':'keydown',
		'propagate':true,
		'target':document
				});
	
			// LEFT / RIGHT SHORTCUTS
		shortcut.add("left",function() {
		var prevlink = document.getElementById('biggie_id_left').className;
		showBiggie(prevlink);
					},{
		'type':'keydown',
		'propagate':true,
		'target':document
				});
				
				
		shortcut.add("right",function() {
		
		var nextlink = document.getElementById('biggie_id_right').className;
		showBiggie(nextlink);
					},{
		'type':'keydown',
		'propagate':true,
		'target':document
				});
				
				
				
				
		// add a keyboard shortcut for adminbox
		shortcut.add("Ctrl+Alt",function() {
		location.href='#adminbox'; 
		showLB('adminbox');
				},{
		'type':'keydown',
		'propagate':true,
		'target':document
				});
	
		shortcut.remove("esc");	
	
	}
		
	if (wut == "disable") {
		// remove shortucts
		shortcut.remove("Shift");	
		shortcut.remove("Ctrl+Alt");	
	}
		
}
			
// Changes the footer classes on hover
function hoverFoot(name) {
	var x = document.getElementById(name).className;

	if (x.indexOf("_hover") == "-1") {
		document.getElementById(name).className = "footerbox footerbox_hover";
	}
	else {
		document.getElementById(name).className = "footerbox";
	}
	
}

// Changes the plant classes on hover
function hoverPlant(name) {
	var x = document.getElementById(name).className;
	
	if (x.indexOf("plantn6") != "-1" || x.indexOf("plantn5") != "-1") {
		if (x.indexOf("_hover") == "-1") {
			document.getElementById(name).className = x + "_hover";
			document.getElementById(name).style.cursor = "pointer";
		}
		else {
			document.getElementById(name).className = x.substring(0, x.length-6);
		}
		return;
	}		
	
	
	if (x.indexOf("_hover") == "-1") {
		document.getElementById(name).className = "plant plant_hover" + x.substring(5,x.length);
		document.getElementById(name).style.cursor = "pointer";
	}		
	else {
		document.getElementById(name).className = "plant" + x.substring(17, x.length);
	}
}

// Parses the url and loads a panel if it needs to
function permalink() {
	var pound = document.URL.indexOf('#');
	var bigpound = document.URL.indexOf('#plant_');
	var blogpound = document.URL.indexOf('#blog_');

	if (pound != "-1") {
		var string = document.URL.substring(pound+1, document.URL.length);
	}
	
	if (bigpound != "-1") {
		var bigstring = document.URL.substring(bigpound+7, document.URL.length);
	}

	if (blogpound != "-1") {
		var blogstring = document.URL.substring(blogpound+6, document.URL.length);
	}
	
	if (string != null && string != "") {
		if( bigstring != null && bigstring !="") {
			// show a biggie plant if that's what we're looking for
			showBiggie(bigstring);
		}
		else if (blogstring != null && blogstring != "") {
			// show corresponding blog post
			showBlog(blogstring);
		}
		else{
			// otherwise show a normal lightbox
			showLB(string);
		}
	}
}

// Launches biggie panel
function showBiggie(cma) {
	var old = document.getElementById("big").innerHTML = "<div class='big big_loading' style='min-height: 305px; text-align: center;'><img style='margin-top: 100px;' src='gui/ajax-loader.gif' /></div>";
	new  Ajax.Updater('big', 'biggie_ajax.php' , { method: 'get', parameters: 'cma='+cma});

	var old2 = document.getElementById("toolbar").innerHTML = "";
	new  Ajax.Updater('toolbar', 'biggie_ajax.php' , { method: 'get', parameters: 'tbar=true&cma='+cma});
	
	showLB("biggie");
}

// Launches blog panel
function showBlog(cma) {
	var old = document.getElementById("blog_p").innerHTML = "<div id='blog_p'><div class='b' style='min-height: 305px; text-align: center;'><img style='margin-top: 100px;' src='gui/ajax-loader.gif' /></div></div>";
	new  Ajax.Updater('blog_p', 'blog_post.php' , { method: 'get', parameters: 'cma='+cma});
	showLB("blog");
}

var scroll = 0;	
// Show a lightbox
function showLB(id) {
	lb = new lightbox(); lb.initCallable(id); lb.activate();

	// focuses on the textbox if you're gonna plant something
	if (id == "new") {
		document.Add.comment.focus();
	}
	
	// add a keyboard shortcut for closing the lightbox
	shortcut.add("esc",function() {
	closeLB(id);
	location.href='#'; 
			},{
	'type':'keydown',
	'propagate':true,
	'target':document
			});

	shortCuts('disable');
	//scroll = document.body.scrollTop;
	scroll = window.pageYOffset;
}
	
// Close that lightbox
function closeLB(id) {
	lb = new lightbox(); 
	lb.initCallable(id);
	lb.deactivate();	
	shortCuts('enable');

//	document.body.scrollTop = scroll;	
	window.scrollBy(0,scroll);
}
	
// Stolen preload images function
var arImages=new Array();
function preload() {
 var temp = preload.arguments; 
 for(x=0; x < temp.length; x++) {
  arImages[x]=new Image();
  arImages[x].src=preload.arguments[x];
 }
}

// Global Var: Get User's Screen Resolution
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
	pars = 'resolution=1200';
}
else {
	pars = 'resolution=' + screen.width;
}	


/*
// Keep the timeout consistent with the frequency in reGrow, for consistency sake	
function restartUpdate(){
	setTimeout("resumeUpdate()", 20000);
	}
*/	

function checkPlantResponse(){
	alert(this.transport.responseText);
}


// Makes the customize hovers cross browser compatible
function deSelectAll(except, radio){
	document.getElementById('newest_div').className= "options";
	document.getElementById('watered_div').className= "options";
	document.getElementById('watered_month_div').className= "options";
	document.getElementById('weeded_div').className= "options";
	document.getElementById('contro_div').className= "options";
	document.getElementById('long_div').className= "options";
	document.getElementById('short_div').className= "options";
	document.getElementById('random_div').className= "options";
	document.getElementById(except).className = "options_selected";
	document.getElementById(radio).checked = true;
}

/*
function isThereaFiya(){
var div = document.getElementById('plant_liveinfo');

if(div.innerHTML !=""){
	showLB('new');
	}
}
*/

// Stolen function to see if a cookie exists in javascript
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return "yes";
    } 
  }
return "no";
}

// Shows the welcome panel
function welcome() {
	// makes sure a different lightbox isnt gonna be opened
	var pound = document.URL.indexOf('#');
	if (pound == "-1") {
		if (getCookie('t_welcome2') == "no") {
			showLB("welcome");	
		}	
	}
}

/*
function loadPanel(name){
	new  Ajax.Updater('panel_div', 'panel.php' , { asynchronous: false, method: 'get', parameters: 'loadpanel='+name});
		// showLB(name);
}
*/

//////////////////////////////////////////////////////////////////////
// Floating Menu script -  by Roy Whittle (http://www.javascript-fx.com/)
// Heavily Modified by Mathew.

function divFloat() {
	var startX = 591;
	var startY = 0;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;

	function ml(id)	{
		var el = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.div[id];
		if(d.div)el.style = el;
		el.sP = function(x,y) { 
			// "px" must be added to make it work on an xhtml transitional doctype
			this.style.left= x + "px" ;
			this.style.top= y + "px" ; 
		};
		el.y = startY;
		el.x = ns ? pageXOffset + innerWidth : document.body.scrollLeft + document.body.clientWidth;
		el.x -= startX;
		return el;
	}
	window.stayTopRight=function() {
		var pX = ns ? pageXOffset + innerWidth : document.body.scrollLeft + document.body.clientWidth;
		ftlObj.x += (pX - startX - ftlObj.x)/8;
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopRight()", 10);
	}
	
	ftlObj = ml("divStayTopRight");
	stayTopRight();
}

// function: viewReplace
// Put 'title' descriptions in the view_message box onMouseOver, resets on MouseOut!

function viewReplace(msg){
	document.getElementById('view_message').innerHTML = msg;	
}

// makes sure the plant a thought form can only be submitted once.
var submitted = false;
function submitOnce() {
	if(submitted == true) { return; }
	document.getElementById('Add').submit();
	document.getElementById('submitplant').value = 'Planting...';
	//document.getElementById('submitplant').disabled = true;
	submitted = true;
}

function toggleIphoneForm() {
	if (document.getElementById('iphoneplant').style.display == 'none') {
		document.getElementById('iphoneplant').style.display = 'block'; 
		return;
	}
	if (document.getElementById('iphoneplant').style.display == 'block') {
		document.getElementById('iphoneplant').style.display = 'none'; 
		return;
	}
}