/* List-Zilla.com
	
	Public Area Javascript Functions

	Copyright (c) Vendetta Apps, Inc. All Rights Reserved.
	
	Written By: Tai Sheppard
		
	-------------------------------------------------------------- 
*/

/* MAIN DOMAIN VARIABLE USED IN ALL AJAX CALLS */

var site = "http://www.list-zilla.com";
var sitesecure = "https://www.list-zilla.com";
	
	
/* 	DOMREADY FUNCTIONS
	--------------------------------------
	Notes:
		All functions within the pageVariables functions
		are implemented onDomReady within MooTools.
		Any functions required onLoad should be placed
		inside the roundedCorners function below.
	-------------------------------------------------------------------------- */

function pageVariables () {
	
	/* Browser Detection */
	// Internet Explorer
	if (Browser.Engine.trident) {
		var version = Browser.Engine.version;
		if (version < 5) {
			Sexy.error ("<h1>ERROR!</h1><p>Your browser, Internet Explorer 6 or older cannot be used to view List-Zilla.com.</p><p>Please download the latest version of Internet Explorer 8, or visit us with another standards compliant browser.</p><p>Thank you.</p>");	
		}
	}
	
	/* Registration Step One */
	if ($('stepone')) {
		var StepOneValidate = new Validate('stepone', {
			errorClass: 'red'
		});
	}
	
	/* Transaction Validation on Return From AP */
	if ($('sales-loader')) {
		checkSaleIPN.periodical (5000);
		CancelTimer.delay (60000);
	}
	
	function checkSaleIPN () {
		var ajax = new Request.HTML ({
			url: site + "/order/checksale/"+ orderid,
			onSuccess: function (tree, ele, html, js) {
				if (html.contains ("complete")) {
					document.location.href = site + "/thank-you/complete";
				}
			}
		}).get ();
	}
	
	if ($('offer-loader')) {
		setupLists.delay (8000);
	}
	
	function setupLists () {
		var ajax = new Request.HTML ({
			url: site + "/thank-you/finalize/"+ memberid,
			onSuccess: function (tree, ele, html, js) {
				if (html.contains ("complete")) {
					document.location.href = site + "/thank-you/enrollcomplete";
				}
			}
		}).get ();
	}
	
	if ($('offer-return-loader')) {
		checkOfferIPN.periodical (5000);
		CancelTimer.delay (60000);
	}
	
	function checkOfferIPN () {
		var ajax = new Request.HTML ({
			url: site + "/offer/checksale/"+ orderid,
			onSuccess: function (tree, ele, html, js) {
				if (html.contains ("complete")) {
					document.location.href = site + "/thank-you/enrollcomplete";
				}
			}
		}).get ();
	}
	
	function CancelTimer () {
		document.location.href = site + "/order/error";	
	}
	
	if ($('video_reg')) {
		var so = new SWFObject (site + "/helpers/player.swf", "mpl", "640", "350", "9");
		so.addParam		("allowscriptaccess", "always");
		so.addParam		("allowfullscreen", "false");
		so.addParam		("bgcolor", "000000");
		so.addVariable 	("autostart", "true");
		so.addVariable 	("streamer", "rtmp://cdn.social-servers.net/listzilla&file=enroll-video.mp4&type=mp4");
		so.addVariable 	("controlbar", "none");
		so.write		("video_reg");
	}
	
	if ($('video-player')) {
		var so = new SWFObject (site + "/helpers/player.swf", "mpl", "854", "480", "9");
		so.addParam		("allowscriptaccess", "always");
		so.addParam		("allowfullscreen", "false");
		so.addParam		("bgcolor", "000000");
		so.addVariable 	("autostart", "true");
		so.addVariable 	("streamer", "rtmp://cdn.social-servers.net/listzilla&file=new-front.mp4&type=mp4");
		so.addVariable 	("logo", site + "/images/watermark.png");
		so.addVariable 	("image", site + "/images/v3/8.png");
		so.addVariable 	("controlbar", "none");
		so.write		("video-player");
	}
	
	if ($('video-player-test')) {
		var so = new SWFObject (site + "/helpers/player.swf", "mpl", "854", "480", "9");
		so.addParam		("allowscriptaccess", "always");
		so.addParam		("allowfullscreen", "false");
		so.addParam		("bgcolor", "FFFFFF");
		so.addVariable 	("autostart", "true");
		so.addVariable 	("streamer", "rtmp://cdn.social-servers.net/listzilla&file=new-front.mp4&type=mp4");
		so.addVariable 	("logo", site + "/images/watermark.png");
		so.addVariable 	("image", site + "/images/v3/8.png");
		so.addVariable 	("controlbar", "none");
		so.write		("video-player-test");
	}
	
	var slider;
	if ($('cart-panel')) {
		$('button-panel').fade ('hide');
		slider = new Fx.Slide('cart-panel').hide();
		//showcart.delay (807000);
		//showbuttons.delay (807500);
		showcart.delay (920000);
		showbuttons.delay (920500);
	}
	function showcart () {
		slider.slideIn ();
	}
	function showbuttons () {
		$('button-panel').fade ('in');	
	}
	
	// TEST
	var slidertest;
	if ($('cart-panel-test')) {
		$('button-panel').fade ('hide');
		slidertest = new Fx.Slide('cart-panel-test').hide();
		showcarttest.delay (920000);
		showbuttonstest.delay (920500);
	}
	function showcarttest () {
		slidertest.slideIn ();
	}
	function showbuttonstest () {
		$('button-panel').fade ('in');	
	}
	
	if ($('pre-launch-form')) {
		var el = $('pre-launch-form');
		el.addEvent ('submit', function (e) {
			e.stop ();
			var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
			if (el["fname"].value == "" || el["fname"].value == "First Name") {
				Sexy.error ("<h1>ERROR!</h1><p>Come on now, we need your First Name in order to know who we're sending this email to.</p>");
			} else if (el["email"].value == "" || el["email"].value == "Email Address") {
				Sexy.error ("<h1>ERROR!</h1><p>Kinda hard to send you an email without an address .... let's go, you know the drill.</p>");
			} else if (!el["email"].value.toUpperCase().match(regEmail) ) {
				Sexy.error ("<h1>ERROR!</h1><p>Whoops .... looks like you got an error in your email address there ... let's try that again shall we?</p>");
			} else if (el["username"].value == "" || el["username"].value == "Username") {
				Sexy.error ("<h1>ERROR!</h1><p>We need you to select a username in order to refer others.</p>");
			} else if (el["username"].value != "") {
				new Request.HTML ({
					url: site + "/index.php",
					onSuccess: function (tree, ele, html, js) {
						if (html.contains ("complete")) {
							new Request.HTML ({
								url: site + "/index.php",
								onSuccess: function (tree, ele, html, js) {
									if (html.contains ("Flynn")) {
										Sexy.info (html);
									} else {
										Sexy.error (html);
									}
									ClearForm.delay (3000);
								}
							}).post (el);
							function ClearForm () {
								var elements = $$('input[type=text]');
								elements.each (function (el) {
									el.value = el.defaultValue;						 
								});
							}
						} else {
							Sexy.error (html);
						}
					}
				}).get ({'go': 'checkusername', 'username': el["username"].value});
			}
		});
		// Clearing function
		var elements = $$('input[type=text]');
		elements.each(function (el) {
			el.addEvent('focus', function (e) {
				if (el.defaultValue == el.value) el.value = "";						   
			});
			el.addEvent ('blur', function (e) {
				if (el.value == "") el.value = el.defaultValue;
			});
		});
	}
	
	if ($('user-setup')) {
		var el = $('user-setup');
		var button = $('submit-button');
		button.addEvent ('click', function (e) {
			e.stop ();
			// first check username
			var username = el["username"].value;
			if (username == "") {
				Sexy.error ("<h1>ERROR!</h1><p>Please select a username.</p>");
			} else {
				new Request.HTML ({
					url: site + "/pre-launch.php",
					onSuccess: function (tree, ele, html, js) {
						if (html.contains ("ERROR")) {
							Sexy.error (html);
						} else {
							new Request.HTML ({
								url: site + "/pre-launch.php",
								onSuccess: function (tre, elem, htm, j) {
									if (htm.contains ("Cool Beans")) {
										Sexy.plain (htm);
										Redirect.delay (3000);
										
									} else {
										Sexy.error (htm);	
									}
								}
							}).post (el);	
						}
					}
				}).get ({'go': 'checkuser', 'username': username});
			}
		});
		function Redirect() {
			document.location.href = site + "/pre/complete";
		}
	}
}

/* ============================================================== */

/* 	ONLOAD FUNCTIONS 
	---------------------------------
	Notes: 
		All functions included inside the loadVariables function
		are implemented onLoad().  Any functions or variables
		needing domReady() should be placed into pageVariables ().
	------------------------------------------------------------------------------------------- */
	
function loadVariables () {
	
	
}

/* ============================================================== */

/* 	SUPPLEMENTAL FUNCTIONS
	----------------------------------------------
	Notes:
		All functions listed below are not implemented via the window dom
		and must be called from elsewhere as valid js functions.
	----------------------------------------------------------------------------------------------------- */

/* ============================================================== */

window.addEvent('domready', pageVariables);
window.addEvent('load', loadVariables);