if(typeof Prototype != "undefined"){
	document.write('<script src="http://academic.cuesta.edu/lead/date_time.asp" type="text/javascript"><\/script> <img id="testImage" src="images/windows/gray_point.png?'+new Date().getTime()+'" \/>');
	
	Element.addMethods({
		imgPreLoad: function(){ //image preloader
			var tmp;
			var cacheImg = $A(arguments);
			cacheImg = cacheImg.map(function(img){
				tmp = new Image();
				return Element.writeAttribute(tmp,"src",img);
			});
		}
	});
	
	Form.Element.Methods.processing = function(element){
		element = $(element);
		//skip buttons and HIDDEN inputs
		if(['submit','reset','hidden'].member(element.readAttribute('type'))) return;
		//Set controls for focus, blur, and change events
		var value = (element.hasAttribute('value'))?element.readAttribute('value'):"";
		function toggle(evt){
			this.writeAttribute('value',(this.hasAttribute('value'))?false:value);
			Event.stop(evt);
		}
		Event.observe(element,'focus',toggle);
		Event.observe(element,'blur',toggle);
		Event.observe(element,'change',function(){
			Event.stopObserving(element,'blur',toggle);
		});
	};
	Element.addMethods();

	Object.extend(Number.prototype, {
		rand: function(min){
			if(typeof min == "undefined") min = 0;
			return (Math.random()*this).round()+min;
		}
	});
	
	function highlightOnLoad() {
		// Get search string
		if (/s\=/.test(window.location.search)) {
			var searchString = getSearchString();
			// Starting node, parent to all nodes you want to search
			var textContainerNode = $$("body").reduce();
			// The regex is the secret, it prevents text within tag declarations to be affected
			var regex = new RegExp(">([^<]*)?("+searchString+")([^>]*)?<","ig");
			highlightTextNodes(textContainerNode, regex);
		}
	}
	
	// Pull the search string out of the URL
	function getSearchString() {
		// Return sanitized search string if it exists
		var rawSearchString = window.location.search.replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/,"$1");
		// Replace '+' with '|' for regex
		return rawSearchString.replace(/\+/g,"\|");
	}
	
	function highlightTextNodes(element, regex) {
		var tempinnerHTML = element.innerHTML;
		// Do regex replace
		Element.update(element,tempinnerHTML.replace(regex,">$1<span class='highlighted'>$2</span>$3<"));
	}
	
	function checkWidth(){
		var test = $('sizer').getWidth();
	}
	
	//to keep the page from being loaded in a frame
	if(window != top) top.location.href = location.href;
	//preload common images
	Element.imgPreLoad('images/windows/hm_logo.gif','images/windows/valid-xhtml10-blue.png');
	
	//check if images are loading
	Event.observe(window,'load',function(){
		var img = $('testImage');
		while(!img || typeof img == "undefined") return;
		if(img.getWidth() == 0) Element.writeAttribute(document.body,"id",false);
		$('left').setStyle({ marginLeft: "5px", left: "-74%" });
		$('main').setStyle({ left: "25%" });
		
		var greens = new Array('796','574','796','ad8');
		var yellows = new Array('cd0','990','ba1','bb4');
		var others = new Array('976','666','221');
		var colors = $A([greens, yellows, others]).flatten();
		$$('div#content div.borderBricks').each(function(bb){
			$A($R(1,12)).each(function(br){
				Element.insert(bb, new Element('div').setStyle({
					width: (Math.floor(100/12)).round()+"%", 
					backgroundColor: "#"+colors[(colors.size()).rand()]
				}));
			});
			Element.show(bb);
		});
	});
	
	document.observe('dom:loaded',function(){
		checkWidth();
		//set _blank target on those links with a rel of "external"
		$$('body a[rel="external"]').invoke('writeAttribute','target','_blank');
		
		//set fix for input text fields
		$$('body input[type=text]','body textarea').invoke('processing');
		
		//highlight search terms
		highlightOnLoad();
		
		//set date/semester information
		if(typeof date == "undefined" || typeof semester == "undefined") document.write('<script src="http://academic.cuesta.edu/lead/date_time.asp" type="text/javascript"><\/script>');
		if(typeof date != "undefined" && typeof semester != "undefined"){
			var left = $$('div#left ul.navigation').collect(function(ul){
				if(ul.nextSiblings().size() == 0) return ul;
				else if(ul.nextSiblings().first().inspect().include('<hr')) return ul.nextSiblings().first();
				else return ul;
			}).reduce().insert({ after: new Element('p').insert(semesterdates).setStyle({margin: "0", fontWeight: 'bold'}) }).insert({ after: new Element('h2').insert(semester+" "+year).setStyle({ fontSize: "100%" }) });
		}
		
		//surfto function for Quick Links
		var loc;
		$$('select#select1').each(function(sel){
			Event.observe(sel,'change',function(evt){
				if($F(sel) != 0) loc = $F(sel);
				$(sel).selectedIndex = 0;
				location = loc;
				Event.stop(evt); // stop event here
			});
			Element.show(sel);
		});
	});
	
	if(Prototype.Browser.IE){
		//this fixes a problem Internet Explorer has with caching background images
		try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
		
		Event.observe(window,'load',function(){
			//ensure that image-replaced elements have the hasLayout property
			//set
			$$('.replace').invoke('setStyle',{ display:"inline-block", display:"block" });
			
			//makes IMG elements on the page positioned relative
			//without any implicit alteration in location in the viewport
			//this fixes a number of IE bugs in displaying images
			$$('body img').invoke('makePositioned');
			$$('.photo img').invoke('undoPositioned');
		});
		
		document.observe('dom:loaded',function(){
			//fix for quotes around quoted material for Internet Explorer
			$$('body q', '.quoted','.single').each(function(q){
				Element.insert(q,{ top:q.hasClassName('single')?'\u2018':'\u201c' });
				Element.insert(q, { bottom:q.hasClassName('single')?'\u2019':'\u201d' });
			});
		});
	}
} else {
	//reloads the window if Nav4 resized
	function MM_reloadPage(init){
		if (init==true) with (navigator){
			if ((appName=="Netscape")&&(parseInt(appVersion)==4)){
				document.MM_pgW=innerWidth;
				document.MM_pgH=innerHeight;
				onresize=MM_reloadPage;
			}
		} else if(innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH){
			location.reload();
		}
	}
	MM_reloadPage(true);
	
	//for older browsers like Netscape 4...
	window.onerror = function (){ return true; }
}