Ext.onReady( function() 
{ 
	
	
//email	
	Ext.select(".contactTrigger").on("click", function (e,t) {
		Ext.fly("contact").setStyle("visibility","visible");
		Ext.fly("contactThankYou").setStyle("visibility","hidden");
		Ext.fly("contactLoading").setStyle("visibility","hidden");
		Ext.fly("contact").fadeIn();
	});
	Ext.select(".sendEmailTrigger").on("click", function (e,t) {
		//Ext.fly("contactForm").fadeOut({duration:.3});
		Ext.fly("contactLoading").setStyle("visibility","visible");
		Ext.fly("contactLoading").fadeIn({duration:.3});
		
		message = "FROM: " + Ext.fly("yourEmailInput").dom.value;
		message += "\n\nMESSAGE: " + Ext.fly("yourMessageInput").dom.value;		
		message += "\n\nPAGE: " + location.href;
		
		var x = navigator;
		browserInfo = "\n\tJava Status: " + x.javaEnabled();
		browserInfo += "\n\tScreen Resolution: " + screen.width + "x" + screen.height;
		browserInfo += "\n\tScreen Available Width: " + screen.availWidth + "x" + screen.availHeight;
		browserInfo += "\n\tBrowser: " + x.appName;
		browserInfo += "\n\tScreen Depth: " + window.screen.colorDepth;
		browserInfo += "\n\tFont Smoothing? " + window.screen.fontSmoothingEnabled;
		browserInfo += "\n\tCurrent Document: " + document.location;
		browserInfo += "\n\tCooies Status: " + x.cookieEnabled;
		browserInfo += "\n\tCPU Class: " + x.cpuClass;
		browserInfo += "\n\tPlatform: " + x.platform;
		browserInfo += "\n\tLanguage: " + x.userLanguage;
		
		message += "\n\nBROWSER INFO: " + browserInfo;		
		
		new Ext.util.DelayedTask(function(){
			// The web service call
			var req  = 'http://www.octopikids.com/contact/?yourEmail=octopi@octopikids.com&message=' + encodeURIComponent(message) + '&page=' + encodeURIComponent(document.location); 
			bObj = new JSONscriptRequest(req); 
			bObj.buildScriptTag(); 
			bObj.addScriptTag();
			
			
			/*Ext.Ajax.request({
			    url: 'http://www.octopikids.com/contact/'
			   ,method: "POST"
			   ,params: {
			   		 "yourEmail" : "octopi@octopikids.com"
			   		,"message" : message
			   	}			   
			   ,success: function(response, opts) {	
			   		//Ext.fly("contactForm").fadeIn({duration:.3});
					Ext.fly("contactLoading").setStyle("opacity", 0 );	
					Ext.fly("contactThankYou").setStyle("visibility","visible");
					Ext.fly("contactThankYou").setStyle("opacity", 100 );
					new Ext.util.DelayedTask(function(){
						Ext.fly("contact").fadeOut();
						Ext.fly("contactThankYou").fadeOut();
						Ext.fly("yourEmailInput").dom.value = "";
						Ext.fly("yourMessageInput").dom.value = "";//response.responseText;					
					}).delay(2000);			
			   }
			   ,failure: function(response, opts) {
			   }
			});*/
			
		}).delay(1000);	
	});
	
	
	
	Ext.select(".cancelEmailTrigger").on("click", function (e,t) {
		Ext.fly("contact").fadeOut();
		Ext.fly("yourEmailInput").dom.value = "";
		Ext.fly("yourMessageInput").dom.value = "";
	});

//shareemail
	 if ( Ext.fly("shareEmail_submit") ) {
	 	Ext.fly("shareEmail_submit").on("click", function (e,t) {
			yourEmail = Ext.fly("shareEmail_yourEmail").dom.value;
			friendEmail = Ext.fly("shareEmail_friendsEmail").dom.value;
			page = location.href;//Ext.fly("shareEmail_page").dom.value;
			message = yourEmail + " sent you a link and a message about OctopiKids:\n\n" + page + "\n\n" + Ext.fly("shareEmail_message").dom.value;
			
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var validYourEmail = reg.test(yourEmail);
			
			if ( !validYourEmail ) {
				alert("your email not valid");
				return;
			}
			var validFriendEmail = reg.test(friendEmail);
			if ( !validFriendEmail ) {
				alert("friend email not valid");
				return;
			}
			
			Ext.fly("shareEmailLoading").setStyle("visibility","visible");
			
			var req  = 'http://www.octopikids.com/contact/share/?'; 
			req += 'yourEmail=' + encodeURIComponent( yourEmail );
			req += '&friendEmail=' + encodeURIComponent( friendEmail );
			req += '&page=' + encodeURIComponent(  page );
			req += '&message=' + encodeURIComponent(  message );
			
			bObj = new JSONscriptRequest(req); 
			bObj.buildScriptTag(); 
			bObj.addScriptTag();
			
			/*Ext.Ajax.request({
			    url: 'http://www.octopikids.com/contact/share/'
			   ,method: "POST"
			   ,params: {
			   		 "yourEmail" : yourEmail
			   		,"friendEmail" : friendEmail
			   		,"message" : message
			   		,"page" : page
			   	}			   
			   ,success: function(response, opts) {		
			   		Ext.fly("shareEmailLoading").setStyle("visibility","hidden");
			   		Ext.fly("shareEmailSuccess").setStyle("visibility","visible");
			   		
					new Ext.util.DelayedTask(function(){
						Ext.fly("shareEmailSuccess").setStyle("visibility","hidden");
						Ext.fly('shareBarEmail').setStyle("visibility","hidden");
						Ext.fly("shareEmail_yourEmail").dom.value = "";
						Ext.fly("shareEmail_friendsEmail").dom.value = "";
						Ext.fly("shareEmail_message").dom.value = "";
						
						Ext.fly('shareBar').shift({ height: 0 });
					}).delay(2000);		   		
			   }
			   ,failure: function(response, opts) {
			   }
			});*/
			
	 	});
	 	Ext.fly("shareEmail_cancel").on("click", function (e,t) {
			Ext.fly("shareEmail_yourEmail").dom.value = "";
			Ext.fly("shareEmail_friendsEmail").dom.value = "";
			Ext.fly("shareEmail_message").dom.value = "";
			
			new Ext.util.DelayedTask(function(){
				Ext.fly('shareBarEmail').setStyle("visibility","hidden");
				Ext.fly('shareBar').shift({ height: 0 });
			}).delay(200);
	 	});
	 }
 	
//Newsletter
	Ext.fly("newsletterForm").on('submit', function(e, t) {
		var email = Ext.fly("newsletterEmail").dom.value;
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var validEmail = reg.test(email);
		
		if ( !validEmail ) {
			Ext.fly("newsletterEmail").fadeOut({duration:.3});
			Ext.fly("newsletterInvalidEmail").fadeIn({duration:.3});	
			Ext.fly("newsletterSubmit").fadeOut();
			new Ext.util.DelayedTask(function(){
				Ext.fly("newsletterEmail").fadeIn();
				Ext.fly("newsletterInvalidEmail").fadeOut();
				Ext.fly("newsletterSubmit").fadeIn();
			}).delay(2500);				
			return false;
		}
				
		Ext.fly("newsletterSubmit").fadeOut();
		Ext.fly("newsletterLoader").fadeIn();
		Ext.fly("newsletterEmail").fadeOut();
		
		var req  = 'http://www.octopikids.com/subscribe/TfkpP0UX5b2mIiN/?email=' + encodeURIComponent(email); 
		bObj = new JSONscriptRequest(req); 
		bObj.buildScriptTag(); 
		bObj.addScriptTag();
		
		/*Ext.Ajax.request({
		    url: 'http://www.octopikids.com/subscribe/TfkpP0UX5b2mIiN/?email=' + email
		   ,form: 'newsletterForm'
		   ,success: function(response, opts) {							
				Ext.fly("newsletterLoader").fadeOut();							
				//if ( response.responseText == "success" ) {
					Ext.fly("newsletterEmail").dom.value = "";
					Ext.fly("newsletterThankyou").fadeIn({duration:.3});
					Ext.fly("newsletterSubmit").fadeOut();
					Ext.fly("newsletterEmail").fadeIn();
					new Ext.util.DelayedTask(function(){
						Ext.fly("newsletterThankyou").fadeOut();
						Ext.fly("newsletterSubmit").fadeIn();
					}).delay(3000); 
				//}
		   }
		   ,failure: function(response, opts) {
		      	Ext.fly("newsletterSubmit").fadeIn();
				Ext.fly("newsletterLoader").fadeOut();				
		      	alert('failure please use contact us!');
		   }
		});	*/		
	});	
	
	
				
});

function shareEmailCallBack ( result ) {
	
	Ext.fly("shareEmailLoading").setStyle("visibility","hidden");
	Ext.fly("shareEmailSuccess").setStyle("visibility","visible");
	
	new Ext.util.DelayedTask(function(){
		Ext.fly("shareEmailSuccess").setStyle("visibility","hidden");
		Ext.fly('shareBarEmail').setStyle("visibility","hidden");
		Ext.fly("shareEmail_yourEmail").dom.value = "";
		Ext.fly("shareEmail_friendsEmail").dom.value = "";
		Ext.fly("shareEmail_message").dom.value = "";
		
		Ext.fly('shareBar').shift({ height: 0 });
	}).delay(2000);	
	
}


function contactCallBack( result ) {
	//Ext.fly("contactForm").fadeIn({duration:.3});
	Ext.fly("contactLoading").setStyle("opacity", 0 );	
	Ext.fly("contactThankYou").setStyle("visibility","visible");
	Ext.fly("contactThankYou").setStyle("opacity", 100 );
	new Ext.util.DelayedTask(function(){
		Ext.fly("contact").fadeOut();
		Ext.fly("contactThankYou").fadeOut();
		Ext.fly("yourEmailInput").dom.value = "";
		Ext.fly("yourMessageInput").dom.value = "";//response.responseText;					
	}).delay(2000);	
}

function subscribeCallBack(result) {
	Ext.fly("newsletterLoader").fadeOut();							
	//if ( response.responseText == "success" ) {
		Ext.fly("newsletterEmail").dom.value = "";
		Ext.fly("newsletterThankyou").fadeIn({duration:.3});
		Ext.fly("newsletterSubmit").fadeOut();
		Ext.fly("newsletterEmail").fadeIn();
		new Ext.util.DelayedTask(function(){
			Ext.fly("newsletterThankyou").fadeOut();
			Ext.fly("newsletterSubmit").fadeIn();
		}).delay(3000); 
	//}	
	
}