// true if IE
var IE = /*@cc_on!@*/false;

$$('html')[0].className += ' json';



var pingalaCMS = {
		
	load : function() {
		pingalaCMS.openWindow();
		pingalaCMS.printWindow();
		pingalaCMS.formStyle();
		pingalaCMS.selectStyle();
		pingalaCMS.confirmBox();
		StyleSheetSwitcher.initialize();
		if($("case")){pingalaCMS.liLinks("case");}
		if($("partners")){pingalaCMS.liLinks("partners");}
	},
		
	openWindow : function(){
		
		allNodes = $$('.openWindow');
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			pageTracker._trackEvent('Outbound Links', this.href);
			window.open(this.href); return false;
		};
		});
	},
	
	printWindow : function(){
		
		allNodes = $$('.printWindow');
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {window.print()};
		});

	},
	
	selectStyle : function(){
		
		allNodes = $$('.styleSelect'); 
		 
			allNodes.each(function(node, i) {
				
				val = $F(node).split('|')[1]; if (val.length > 23){ val = val.substring(0, 22); }
				$(node).insert({ 'after' : '<span id="span_'+i+'">'+val+'</span>' })
				
				allNodes[i].onchange = function() {	
					val = $F(node).split('|')[1]; if (val.length > 23){ val = val.substring(0, 22); }
					$('span_'+i).innerHTML = val
				 };
				 
			});
		
	},
	
	confirmBox : function() {
	
		allNodes = $$('.confirm'); 
		allNodes.each(function(node, i) {
							   
			allNodes[i].onclick = function() {return confirm('Are you sure you want to '+this.title+'?')}; 
			
		});
	},
	
	formStyle : function(){
		
		allNodes = $$('.textform');
		allNodes.each(function(node, i) {
			
			Event.observe(allNodes[i], 'focus', 
				function(e){ 
					if(Event.element(e).nodeName != 'SELECT'){
						Event.element(e).className="textformfocus";
						if (Event.element(e).value.charAt(0) == '-') { Event.element(e).value='' }
					}
					}
				);

			Event.observe(allNodes[i], 'blur', 
				function(e){ 
					Event.element(e).className="textform";
					}
				);
		});
	},
	
	liLinks : function(liList) {
		
		linkNode = $A($(liList).childNodes);
		linkNode.each(function(node, i) {
		
			if (node.nodeName=="LI") {
				node.className += ' hand';
				node.onclick=function() { window.location = node.getElementsByTagName('A')[0].href }	
			}
		
		})
	}
	
}


var pingalaWeb = {
	
	selectMulti : function(type) {
	
		allNodes = $A($("select_"+type).childNodes)
			
			allNodes.each(function(node, i) {
				if (node.nodeName=="LI") {
					node.onclick = function() {
							
						if(this.childNodes[0].className=='selected'){			
							this.childNodes[0].className='';
							$("advanced_"+type).value = $("advanced_"+type).value.replace(this.childNodes[0].href.split('#')[1] +',','') 
						}else{
							Element.addClassName(this.childNodes[0], "selected");	
							$("advanced_"+type).value += this.childNodes[0].href.split('#')[1] +',';
						}	
						
					};
				}
	
			});	
	},
	
	selectAll : function(alink,type) {
		
		allNodes = $A($("select_"+type).childNodes)
		
		if(alink.innerHTML == 'Deselect'){	
		
			alink.innerHTML = 'Select All';
				
			allNodes.each(function(node, i) {
				if (node.nodeName=="LI") {
					node.childNodes[0].className=''; $("advanced_"+type).value = '';
				}
			});	
		
		}else{
			
			alink.innerHTML = 'Deselect'; $("advanced_"+type).value = '';
			
			allNodes.each(function(node, i) {
				if (node.nodeName=="LI") {
					Element.addClassName(node.childNodes[0], "selected");	
					$("advanced_"+type).value += node.childNodes[0].href.split('#')[1] +','; 
				}
			});	
		}	
	},
	
	showCal : function() {
		
		
			if ($('graduate_employed').value=='y'||$('graduate_employed').value=='1') { $('showcal').style.display='block';  } ;
			$('graduate_employed').onchange = function() { if ($('graduate_employed').value=='y'||$('graduate_employed').value=='1') { $('showcal').style.display='block';  } else { $('showcal').style.display='none';  } } 
	
	}
	
	
}


var pingalaAjax = {
			
	makeRequest : function(url,getID) { 
			
		new Ajax.Request(url, {
			method: 'post',
			//parameters: 'ajax=2',
			onSuccess: function(originalRequest){$(getID).innerHTML = originalRequest.responseText;}
		});
	
	},
	
	submitForm : function(pageUrl) {
		
		$('waiting').innerHTML = '<img src="/_img/layup/loading.gif" />'
		$('submitButton').type == 'button'; 
		$('submitButton').disabled = 'disabled'; 
			
		if(pageUrl) {pageLocation = pageUrl} else {pageLocation = window.location.href}
		
		waitPadd = (Element.getDimensions($('formArea')).height - 48)  /2
		$('waiting').style.paddingTop =  waitPadd + "px"
		$('waiting').style.paddingBottom =  waitPadd + "px"
		
		 new Effect.Morph('formArea', { 
		  style:  'opacity: 0;', 
		  duration: 0.2,
		  afterFinish: function(){
			  	Element.show('waiting'); 
				new Ajax.Request(pageLocation, {
					method: 'post',
					parameters: 'ajax=1&' + Form.serialize("cmsForm"),
					onSuccess: pingalaAjax.showResponse
				});
			
			 }
		 }); 

			
	},

	showResponse : function(originalRequest) {
			
			if(originalRequest.responseText.substring(0,4) == 'http') {
				window.location = originalRequest.responseText; 
			  } else {
			
				$('formArea').innerHTML = originalRequest.responseText; 
				
					Effect.Fade('waiting'); 
					new Effect.Morph('formArea', { 
					  style:  'opacity: 1;', 
					  duration: 0.9,
					  afterFinish: function(){
							//new Effect.Shake('submitButton');
							pingalaCMS.formStyle();
							if($('graduate_employed')){ pingalaWeb.showCal(); }
							Effect.ScrollTo('contents');
							
							if(originalRequest.responseText.substring(0,6) == 'paypal') { $('payform').submit(); } 
							
						 }
					 }); 
				
  			}

	}
	
}




var StyleSheetSwitcher = {
	
	initialize: function() {
		this.setActive(Cookies.read("style") || this.getPreferred());
		Event.observe(window, 'unload', this.unloadHandler.bindAsEventListener(this));
		
		allNodes = $A(document.getElementsByClassName("cssSwitch")); 
			allNodes.each(function(node, i) {
								   
				if(allNodes[i].innerHTML==Cookies.read("style")){allNodes[i].className+=' on';}
			
				allNodes[i].onclick = function() {
		
					StyleSheetSwitcher.setActive(this.innerHTML);
					allNodes.each(function(node, i) {allNodes[i].className ='cssSwitch'});
					this.className+=' on';
					
				};
		});
		
		
	},

	unloadHandler: function() {
		Cookies.create("style", this.getActive(), 365);
	},

	setActive: function(title) {
		$$("link").each(function(link) {
			if (link && link.getAttribute("rel").indexOf("style") != -1 && link.getAttribute("title")) {
				link.disabled = true;
				if (link.getAttribute("title") == title) link.disabled = false;
			}
		});
	},

	getActive: function() {
		var element = $$("link").detect(function(link) {
			return (
				link.getAttribute("rel").indexOf("style") != -1 &&
				link.getAttribute("title") && !link.disabled);
		});
		return element? element.getAttribute("title") : "";
	},

	getPreferred: function() {
		var element = $$("link").detect(function(link) {
			return (
				link.getAttribute("rel").indexOf("style") != -1 && 
				link.getAttribute("rel").indexOf("alt") == -1 &&
				link.getAttribute("title"));
		});
		return element? element.getAttribute("title") : "";
	}
};



var Cookies = {
	create: function(name, value, days) {
		var string = name + "=" + value;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			string += "; expires=" + date.toGMTString();
		}
		document.cookie = string + "; path=/";
	},

	read: function(name) {
		var nameEQ = name + "=";
		return (document.cookie.split(/;\s*/).detect(function(cookie) {
			return (cookie.indexOf(nameEQ) == 0);
		}) || "").substr(nameEQ.length);
	},

	exists: function(name) {
		return this.read(name) !== "";
	}
}




Event.observe(window, 'load', pingalaCMS.load, false);
