$$('html')[0].className += ' json';

var timer

var pingalaCMS = {
		
	load : function() {
		pingalaCMS.openWindow();
		pingalaCMS.formStyle(); 
		pingalaCMS.showSection();
		pingalaWeb.showProcess();
		pingalaWeb.showExtras('services');
		pingalaWeb.showExtras('about');
		if($("slideimages")){ pingalaWeb.fadeInOut(1); timer = new PeriodicalExecuter(pingalaWeb.fadeInOut, 4); }
	},
	
	openWindow : function(){
		
		$$('.openWindow').each(function(node) {
			node.onclick = function() {
			pageTracker._trackEvent('Outbound Links', this.href); //google track out links
			window.open(this.href); return false;
			};
		});
	},
	
	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";
					}
				);
		});
	},
	
	showSection: function(){
	
		allNodes = $$('.openLink'); 
		allNodes.each(function(node, i) {
			allNodes[i].getElementsByTagName('H2')[0].onclick = function() {
				
				thisDiv = node.getElementsByTagName('DIV')[0]
				pingalaWeb.closeProcess('0.3');
				
					allDivs = $A($$('.openLink>div'));
					allDivs.each(function(node, i) { 

						new Effect.Morph(node, { style:  'height: 0',  duration: 0.3,
						  afterFinish: function(){ 
								
								node.className = ''; 
								thisDiv.className = 'on'
								new Effect.Morph(thisDiv, { style: 'height: 360px;',  duration: 1, transition: Effect.Transitions.spring });
								
							}
						 });
					
				 })
					
			};	
				
		});
		
	}
	
}



var pingalaWeb = {
	
	
	fadeInOut : function(run) { 
		
		if(run==1){
			lis = $$('#slideimages>li');
			lis.each(function(node, i) {if (node.className==''){node.className='block'; node.hide()}; });
		}
			lis = $$('#slideimages>li');
			new Effect.Appear(lis[1], { duration: 2, afterFinish: function(obj) {
				lisFade = lis[0].remove();
				lisFade.hide();
				$('slideimages').appendChild(lisFade);
			}});
	},
		
	
	
	showPort : function(id,limit,direction,jump) { 
		
		if($("slideimages")){timer.stop();}
		
		getID = 'portfolio'
		thisVal = parseInt($('pos').value)
		Element.show('back'); Element.show('next');
		
		if(jump){
			if(jump>=thisVal) {direction='1'} else {direction= '0'}
			$('pos').value = parseInt(jump)
		}else{
			if(direction== '1') { $('pos').value = thisVal+1} else { $('pos').value = thisVal- 1}
		}
		
		if(direction=='1'){ setMove = -913; setLeft = '913px';} else {setMove = 913; setLeft = '-913px';}
			
		new Effect.Move(getID, {
				x: setMove, y: 0, mode: 'relative',
				transition: Effect.Transitions.sinoidal,
				duration: 0.3,
				
				afterFinish: function(){
					$(getID).style.left = setLeft;
					thisPos = $('pos').value
					
					$$('#jump>li').each(function(no) {no.className=''; });
					$('j'+thisPos).className='on';
					
					if(thisPos=='1') {Element.hide('back');}
					if(thisPos==limit) {Element.hide('next');}
					
					new Ajax.Updater(getID,'/content/portfolio.asp', {
						evalScripts : 'true',
						method: 'get',
						parameters: 'ajax=1&id='+id+'&page='+$('pos').value,
						onSuccess: function(){
								new Effect.Move(getID, { x: setMove, y: 0, mode: 'relative', transition: Effect.Transitions.sinoidal, duration: 0.3 })	
								
						}
					});
					
					
				}
							
			});
			
	},
	
	
	showProcess: function() {
		
		linkNode = $A($$('#process>li'));
		linkNode.each(function(node, i) {
			node.onclick=function() { 
			
				$('processtext').innerHTML = '';
				$('processtext').className = this.className+'on'
				$('processtext').style.width = '271px';
				thisClass = this.className
				
				switch(thisClass){
					case 'p1':
					  $('processtext').style.marginLeft = '0'; break;
					case 'p2':
					  $('processtext').style.marginLeft = '296px'; break;
					case 'p3':
					  $('processtext').style.marginLeft = '592px'; break;
					}
				
				
				new Effect.Opacity('processtext', { from: 0, to: 1, duration: 0.5,
						afterFinish: function(){ 
								new Effect.Morph('processtext', { style: 'width: 863px; margin-left: 0px;',  duration: 0.5,
								
									afterFinish: function(){ 
											
										new Ajax.Updater('processtext','/content/process.asp', {
											evalScripts : 'true',
											method: 'get',
											parameters: 'ajax=1&id='+thisClass
										});
										
										
									}
								
								 });
							}
				 });
			
				
			}	
		})
	
	},
	
	closeProcess: function(speed) {
		
		if($('processtext').className !='') {
			
			new Effect.Opacity('processtext', { from: 1, to: 0, duration: speed,
				afterFinish: function(){ 
						$('processtext').className = '';
						$('processtext').style.width = '0';
					}
			 });
			
		}
		
	},
	
	showExtras: function(whatArea) {
				
		$A($$('#'+whatArea+'>li')).each(function(node) {
		node.onclick=function() {
					
			$$('#'+whatArea+'>li').each(function(no) {no.className=''; });
			node.className = 'on';
			thislink = node.getElementsByTagName('A')[0].href.split("#")[1]
				
				 new Effect.Morph(whatArea+'text', { 
				  style:  'opacity: 0;', duration: 0.5,
				  afterFinish: function(){
						new Ajax.Updater(whatArea+'text','/content/'+whatArea+'.asp', {
							evalScripts : 'true',
							method: 'get',
							parameters: 'ajax=1&id='+thislink,
							onSuccess: function(){
								new Effect.Opacity(whatArea+'text', { from: 0, to: 1, duration: 0.5})
								
							}
							
						});
					
					 }
				 }); 
				
			}
		})
		
		
	}
	
	
}


var pingalaAjax = {
			
	
	
	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($('cmsForm')).height - 48)  /2
		$('waiting').style.paddingTop =  waitPadd + "px"
		$('waiting').style.paddingBottom =  waitPadd + "px"
		
		 new Effect.Morph('cmsForm', { 
		  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 {
			
				$('cmsForm').innerHTML = originalRequest.responseText; 
					Effect.Fade('waiting'); 
					new Effect.Morph('cmsForm', { 
					  style:  'opacity: 1;', 
					  duration: 0.9,
					  afterFinish: function(){
							if($('submitButton')){new Effect.Shake('submitButton')}
							pingalaCMS.formStyle(); 
						 }
					 }); 
				
  			}

	}
	
}


Event.observe(window, 'load', pingalaCMS.load, false);
