// true if IE
var IE = /*@cc_on!@*/false;

$$('html')[0].className += ' json';



var pingalaCMS = {
		
	load : function() {
		pingalaCMS.openWindow();
		pingalaCMS.printWindow();
		pingalaCMS.formStyle(); 
		pingalaCMS.confirmBox();
		if($("qlink")) {pingalaWeb.boxLink();}
		if($("imgholder")) {pingalaWeb.demoShow(1);}
	},
	
	openWindow : function(){
		
		allNodes = $$('.openWindow');
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			pageTracker._trackEvent('Outbound Links', this.href); //google track out links
			window.open(this.href); return false;
		};
		});
	},
	
	printWindow : function(){
		
		allNodes = $$('.printWindow'); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {window.print()};
		});

	},
	
	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";
					}
				);
		});
	},
	
	
	showDocs : function() {
		
		
		allAnswers = $$('#dataGrid>li>div');
		allAnswers.each(function(node, i) {node.hide(); node.className='block'; });
		
		allNodes = $$('#dataGrid>li');
		allNodes.each(function(node, i) {

				allNodes[i].onclick = function() {
						
					if(this.className==''){
						
							allAnswers = $("dataGrid").select('.on'); 
							allAnswers.each(function(node, i) {
								node.className = '';	
								Effect.BlindUp(node.getElementsByTagName('DIV')[0], { duration: 0.2 });
							});
						
						this.className="on";
						Effect.BlindDown(this.getElementsByTagName('DIV')[0], { duration: 0.5, afterFinish: function(){pingalaWeb.sortHeight(); } });
						
						
					}
					
						
				};

		});
	}
	
	
	
}


var pingalaWeb = {
	
	boxLink : function() {
			$("qlink").onclick=function() { window.location = $("qlink").getElementsByTagName('A')[0].href }	
	},
	
	
	demoShow : function(loadme) {
		
		lis = $$('#imgholder>li');
		if(loadme==1){ lis.each(function(node, i) {if (node.className==''){node.className='block'; node.hide()}; }); };
		
		new Effect.Appear(lis[1], { duration: 0.5, afterFinish: function(obj) {
			lisFade = lis[0].remove();
			lisFade.hide();
			$('imgholder').appendChild(lisFade);
			
		}});
		
	},
	
	
	sortHeight : function(){
		if($('leftside')){
			leftH = $('leftside').getHeight();
			middH = $('middle').getHeight();
			if(leftH>middH){$$('.maintext')[0].style.minHeight =(leftH-42)+'px' };
			if(middH>leftH){$('leftside').style.minHeight =(middH-55)+'px' };
		}
	},
	
	
	liveSearch : function(){
	
	 new Form.Observer('searchform', 1, 
      function(element, value) {new Ajax.Updater(
        'results', '/secure/ajax.asp',
         {asynchronous:true, evalScripts:true, 
           onComplete:function(request) { if (request.responseText!=''){Element.show('results')}else{Element.hide('results')} pingalaWeb.sortHeight();}, 
           method:'get', parameters:'admin=search&' + value
		})})
	  
	 },
	 
	 
	 selectMulti : function() {
	
		allNodes = $A($("selectusers").childNodes)
			
			allNodes.each(function(node, i) {
				if (node.nodeName=="A") {
					node.onclick = function() {
						
						if(this.className!='confirmed'){
									
							$("changed_users").value = '1';
							if(this.className=='selected'){			
								this.className='';
								$("event_users").value = $("event_users").value.replace(this.href.split('#')[1] +',','') 
							}else{
								Element.addClassName(this, "selected");	
								$("event_users").value += this.href.split('#')[1]+',';
							}
						}
							
						
					};
				}
	
			});	
	},
	
	
	addCart : function() {
		
		allNodes = $$('.addCart');
			allNodes.each(function(node, i) { 
				allNodes[i].onclick = function() {
					
					
					
					new Ajax.Request('/secure/ajax.asp', {
						method: 'get',
						parameters: 'admin=addcart&id='+this.href.split('/')[5],
						onSuccess: function(originalRequest){
							$('cartStatus').innerHTML = originalRequest.responseText; 
							
								new Effect.Morph('basketText', {
									style: { color: '#fffea4' }, 
									duration: 0.2,
									afterFinish: function(){ new Effect.Morph('basketText', {style: { color: '#ffffff' } }); node.hide(); }
								});
						}
					});
					
					return false;
							
				}; 
				
			});
	
	}

	
	
}


var pingalaAjax = {
			
	makeRequest : function(url,getID) { 
			
		new Ajax.Updater(getID,url, {
			evalScripts : 'true',
			method: 'post',
			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($('details')){$('details').value = CKEDITOR.instances['details'].getData();}
			
		if(pageUrl) {pageLocation = pageUrl} else {pageLocation = window.location.href}
		
		waitPadd = (Element.getDimensions($('cmsForm')).height - 100)  /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.Updater('cmsForm',pageLocation, {
					evalScripts : 'true',
					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 {
				  
				if($('details')){ CKEDITOR.instances['details'].destroy();}
				$('cmsForm').innerHTML = originalRequest.responseText;
				 
				 pingalaWeb.sortHeight();
					Effect.Fade('waiting'); 
					new Effect.Morph('cmsForm', { 
					  style:  'opacity: 1;', 
					  duration: 0.9,
					  afterFinish: function(){
							if($('submitButton')){ new Effect.Shake('submitButton'); }
							pingalaCMS.load(); 
							Effect.ScrollTo('contents');
						 }
					 }); 
				
  			}

	}
	
}


Event.observe(window, 'load', pingalaCMS.load, false);
