/*

	Fudge vs. Area 17 vs. Prototype & Scriptaculous

	written by Phil (phil at fudgestudios.com) and Mike "1312" (mike at fudgestudios.com)

	for Area 17 (www.area17.com)
	

	This javascript is the main javascript for the sites actions.
	
*/

// --------------------------------------------------------------------------------------------------------------
// set up global variables

var isSafari = false;
var isSafari3 = false;
var isIE = false;
var isIE7 = false;	
var testDataString = "";

// --------------------------------------------------------------------------------------------------------------
// what to do on DOM ready
document.observe("dom:loaded", run_on_load);

function run_on_load() {
	// stuff to happen as the dom has loaded
		
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ some simple browser testing */
	// Safari versioning
	isSafari = Prototype.Browser.WebKit;
	// are we on safari 3? add a class (it over anti aliases everything which causes padding issues)
	// http://www.hedgerwow.com/360/dhtml/detect-safari3-by-js-css.html
	if( window.devicePixelRatio && window.getMatchedCSSRules && !window.Opera){
		isSafari3 = !!window.getMatchedCSSRules(document.documentElement,'');
	}
	if (isSafari) {	
		$$('body').invoke('addClassName', 'isSafari');
		if (isSafari3) { $$('body').invoke('addClassName', 'isSafari3'); }
	}
	// (ok so theres an assumption that any Safari thats not Safari3 is Safari2 here)		
	// IE versioning
	isIE = Prototype.Browser.IE;
	if (typeof document.body.style.maxHeight != "undefined") { if (!isSafari) { isIE7 = true; } }
	if (isIE) {	
		$$('body').invoke('addClassName', 'isIE');
		if (isIE7) { $$('body').invoke('addClassName', 'isIE7'); }
	}
	/* http://gmatter.wordpress.com/2006/10/20/detecting-ie7-in-javascript/ */
	// ### ok so theres an assumption that any IE thats not IE7 is IE6 here	
	
	// are we on a Mac ?
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$$('body').invoke('addClassName', 'isMac');
	} else {
		
	}
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ set up some adding of classes to things */	
	
	setUpClasses();		
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ do some roll over events */
	
	// class="image" (for input type=image)
	if($$('.triangles ul li').length > 0) {
		$$('.triangles ul li').each(function(el,i){
			el.observe('mouseover', function(event) {
				$$('.triangles li h3')[i].style.display = "block"
				$$('.triangles li p')[i].style.display = "block"	
				el.style.backgroundPosition = "0px -160px";
				el.style.cursor = "pointer";					
			});
			el.observe('mouseout', function(event) {
				$$('.triangles li h3')[i].style.display = "none"
				$$('.triangles li p')[i].style.display = "none"			
				el.style.backgroundPosition = "0px 0px";
			});	
			el.observe('click', function(event) {
				link = el.down().down();
				document.location.href = link;
			})	
		});
	}

	if($$('.home').length > 0) {
		// draw the coloured triangles
		triangles = new triangles();
		triangles.draw('home');
	}
	if($$('.programLanding').length > 0) {
		// draw the coloured triangles
		triangles = new triangles();
		triangles.draw('program');
	}
	if($$('.professionalDevelopmentLanding').length > 0) {
		// draw the coloured triangles
		triangles = new triangles();
		triangles.draw('pdp');
	}
	if($$('.aboutUsLanding').length > 0) {
		// draw the coloured triangles
		triangles = new triangles();
		triangles.draw('about');
	}	
	if($$('.getInvolvedLanding').length > 0) {
		// draw the coloured triangles
		triangles = new triangles();
		triangles.draw('connect');
	}			
	
	

	
	// add in ol numbers
	if($$('.highlights')) {
		$$('.highlights ol li').each(function(el,i) {
			i++;
			bullet = "<span>0"+i+"</span>";
			el.insert(bullet, {position: "top"});
		})
	}
	
	
	if($$('.program').length > 0) {
	// draw the coloured triangles
	triangles = new triangles();
	//triangles.draw();
	}
	
	// copy to clip board on get involved promote page
	
	if ($$("input.copy").length != 0) {
		$$('input.copy').each(function(el,i){
			el.observe('click', function(event) {
				copy($("copyMe"));
				Event.stop(event);
			});
		});
	}
	function checkForLoop() {
		
	}	
	
	if($('feature_nav')) {
		$$('#feature_nav li').each(function(el, i) {
			el.observe('click', function(event) {
				thisMoveTo = (el.childElements()[0].innerHTML-1 )* 500;
				feature_to_show = "feature_"+el.id.split("_")[2];
				
				$$('#feature_nav li').each(function(innerel, j) {
					innerel.removeClassName('current');
				})
				// 
				// $$('.feature_div').each(function(innerel, j) {
				// 	innerel.addClassName('hide');
				// })
				
				// $(feature_to_show).removeClassName('hide');
				el.addClassName("current");
				
				// $('sliderInner').
				// new Effect.Move ('sliderInner',{ x: -60, y: 0, mode: 'absolute', duration: 0.99, transition: Effect.Transitions.sinoidal, afterFinish: checkForLoop });				
				new Effect.Morph('sliderInner', {style: 'left: -'+thisMoveTo+'px', duration: 0.5, transition: Effect.Transitions.sinoidal});
				// alert($('sliderInner'));
				Event.stop(event);
			})
		})
	}

	$$('select.jumpTo').each(function(el, i) {
		el.observe('change', function(event) {
			if(this.value != "") {
				document.location.href = this.value;
			}
		})
	})	
	
}

Event.observe(window, 'load', function() {
	// what to do when everything has loaded	
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ redoing for IE! */
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ set up some adding of classes to things */	
	if (isIE) {
		setUpClasses();	
	}
	otherFormField();
	
	jumpToAnchors();
});

// --------------------------------------------------------------------------------------------------------------
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ application wide functions - MB & PJ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (remember to test if the elements you are messing with exist) */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ( eg. if (($$('body.homepage'))!=""){ } ) */




// --------------------------------------------------------------------------------------------------------------
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ utility functions */
// this function search and replaces a string and returns the new string
// http://www.daveshuck.com/blog/index.cfm/2006/12/13/Javascript-examples--removeElement-and-replaceAll
function replaceAll( str, searchTerm, replaceWith, ignoreCase )   {
   var regex = "/"+searchTerm+"/g";
   if( ignoreCase ) regex += "i";
   return str.replace( eval(regex), replaceWith );
}

// setting up classes on things, to replace psuedo selectors
function setUpClasses() {
	$$('input[type=submit]').invoke('addClassName', 'submit');
	$$('input[type=radio]').invoke('addClassName', 'radio');
	$$('input[type=checkbox]').invoke('addClassName', 'checkBox');
	$$('input[type=image]').invoke('addClassName', 'image');
	//$$('input[type=file]').invoke('addClassName', 'file');	
	$$('tr:nth-child(even)').invoke('addClassName', 'even');
	$$('li:nth-child(even)').invoke('addClassName', 'even');	
	// $$('li:nth-child(even)').invoke('addClassName', 'even');	
	$$('ul li:first-child').invoke('addClassName', 'first');
	$$('ul li:last-child').invoke('addClassName', 'last');
	$$('ol li:first-child').invoke('addClassName', 'first');
	$$('ol li:last-child').invoke('addClassName', 'last');
	
	$$('li:nth-child(1)').invoke('addClassName', 'first');	
	$$('li:nth-child(2)').invoke('addClassName', 'second');	
	$$('li:nth-child(3)').invoke('addClassName', 'third');		
	$$('li:nth-child(4)').invoke('addClassName', 'fourth');		
	$$('li:nth-child(5)').invoke('addClassName', 'five');			
	$$('li:nth-child(6)').invoke('addClassName', 'six');				
	$$('li:nth-child(7)').invoke('addClassName', 'seven');				
	
	
}

// Phil - I've swapped this out to a js detect and run plug in from:
// http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
// (the same people that did swfObject, the one we use for embedding flash movies)
function InsertMovie() {
	document.write('<object CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" height="287" width="460" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">\n');
	document.write('<param name="src" value="temp-movie.mov" />\n');
	document.write('<param name="controller" value="true">\n');	
	document.write('</object>\n');
}

// Copy to clipboard
// http://www.jeffothy.com/weblog/clipboard-copy/
// remixed by 1312
function copy(inElement) {
  if (Prototype.Browser.IE) {	  
	  //var range = inElement.value;
	  //range.createTextRange();
	  range = inElement.createTextRange();
	  range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/images/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

// Other form field clearer and check
// get involved -> sign up
function otherFormField() {
	if($("formField-other")) {
		if ($("formField-other").value == "") {
			$("formField-other").value = 'Other';
		}
		$$("input.label").invoke('observe', 'focus', 
			function() {
				if ($("formField-other").value == 'Other') {
					$("formField-other").value = '';
				}	
			}
		);
		$$("input.label").invoke('observe', 'blur', 
			function() {
				if ($("formField-other").value == '') {
					$("formField-other").value = 'Other';
				}	
			}
		);		
	}
}

// make left links anchor to the top of the text
function jumpToAnchors() {	
	$$("div.subnav").invoke("writeAttribute","id","textContent");
	$$("div.subnav ul li a").invoke('observe', 'click', 
		function(event) {
			var el = Event.element(event);
			var url = el.href;
			var doUrlMod = url.search("#");
			if(doUrlMod == -1) {
				url = url + "#textContent";
				window.location = url;
				Event.stop(event);
			}
		}
	);
	var currentAddress = document.location;
	currentAddress = currentAddress.toString();
	var doScrollTo = currentAddress.search("textContent");
	//alert(currentAddress+" "+doScrollTo);
	if(doScrollTo != -1) {
		$("textContent").scrollTo();
	}
}