// JQuery Extensions:
jQuery.extend(jQuery.easing, {
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  }
});
 
// All page initialization goes inside here:
 
var dataValidator;
var lightbox;
 
 
// Lightbox
function Lightbox ()
{
	this.settings = {
		height: 220,
		width: 265,
		defaults: {
			height: 220,
			width: 265
		},
		hasOverlay: false,
		lbID: "lightbox",
		lbContainerID: "lightbox-container",
		lbContentID: "lightbox-content",
		lbOverlayID: "lightbox-overlay",
		overlayHTML: "\
		<div id=\"lightbox-overlay\"></div> \
		",
		html: "\
		<div id=\"lightbox\"> \
			<div id=\"lightbox-container\"> \
				<div id=\"lightbox-content\"> </div> \
			</div> \
		</div> \
		",
		initialized: false,
		movementAmount: 100,
		easing: "easeOutCubic"
	};
	this.prevDimensions = {};
	this.current = {};
	this._instance = null;
}
 
Lightbox.prototype._create = function (text, options)
{
	this._init();
	$("#"+this.settings.lbContentID).html(text);
	this.open(options);
};
Lightbox.prototype.load = function (url, options)
{
	var mylb = this;
	
	if (typeof this.settings.loading != "undefined")
	{
		this.settings.loading(url, options);
	}
	
	$.get(url, function(data) {
    	mylb._create(data, options);
    });
    
};
Lightbox.prototype._init = function ()
{
	$("#lightbox").stop();
		
	if (!this.settings.initialized)
	{
		this.initialize();
	}
};
Lightbox.prototype.initialize = function ()
{
	if (this.settings.hasOverlay) {
		$("body").append(this.settings.overlayHTML);	
	} 
 
	$("body").append(this.settings.html);
 
	// Set all the default settings.
	$("#lightbox").css({
		display: "none",
		position: "absolute",
		left: 0,
		top: 0,
		width: (this.settings.height) ? this.settings.height : this.settings.defaults.height,
		height: (this.settings.width) ? this.settings.width : this.settings.defaults.height
	});
	
	this.center();
	this.onComplete();
	if (this.settings.hasOverlay) {
		$(window).resize(this.centerAll);
		$(window).scroll(this.centerAll);
	} else {
		$(window).resize(this.center);
		$(window).scroll(this.center);
	}
	
	this.settings.initialized = true;
};
 
Lightbox.prototype.visible = function ()
{
	return !this._hidden();
};
Lightbox.prototype._hidden = function ()
{
	return $("#lightbox").css("display") == "none" || $("#lightbox").css("opacity") == 0;
};
Lightbox.prototype.open = function (options)
{
	this._init();
	if (options)
		options = this._parseOptions(options);
	else if (this.settings.width && this.settings.height) 
		options = this._parseOptions({width: this.settings.width, height: this.settings.height});
	else 
		options = this._parseOptions({width: this.settings.defaults.width, height: this.settings.defaults.height});
	
	var movedOptions = {};
	// If the lightbox isn't visible, reset to the appropriate size.
	if (!this.visible()) 
	{
		movedOptions = {
			width: options.width - this.settings.movementAmount,
			height: options.height - this.settings.movementAmount
		};
		
		$("#lightbox").css({
			width: movedOptions.width+"px",
			height: movedOptions.height+"px"
		});
		
		this.center();
		
		$("#lightbox .filler").css({opacity:0});
	}
	
	this.show(options);
};
Lightbox.prototype.show = function(options)
{
	options = this._parseOptions(options);
	options.opacity = 1;
	this.reposition(options);
	if (this.settings.hasOverlay)
		$("#lightbox-overlay").show();
};
Lightbox.prototype.hide = function(options)
{
	options = this._parseOptions(options);
	options.opacity = 0;
	this.reposition(options);
	
	if (this.settings.hasOverlay)
		$("#lightbox-overlay").hide();
};
Lightbox.prototype.close = function ()
{
	this._init();
	
	this.prevDimensions = {
		width: this.current.width,
		height: this.current.height
	};
	
	this.hide({
		width: this.current.width - this.settings.movementAmount,
		height: this.current.height - this.settings.movementAmount
	});
	
};
Lightbox.prototype.onComplete = function()
{
	/*
	if (typeof this.prevDimensions != "undefined")
	{
		$("#lightbox").css({
			width: this.prevDimensions.width+"px",
			height: this.prevDimensions.height+"px"
		});
		this.center();
		this.prevDimensions = undefined;
	}
	*/
	this.current = {
		width: $("#lightbox").width(),
		height: $("#lightbox").height()
	};
	
	if ($("#lightbox").css("display") == "block" && $("#lightbox").css("opacity") == 0)
	{
		$("#lightbox").css("display", "none");
	}
	
	// Nudge IE's memory...
	if ($.browser.msie)
	{
		$("#lightbox"+" .filler").css("opacity", "hide");
		$("#lightbox"+" .filler").css("opacity", "show");
	}
};
 
Lightbox.prototype.reposition = function(options, force)
{
	$("#lightbox").stop();
	
	if (this.settings.hasOverlay) {
		lightbox.centerOverlay();
	}
	
	// should not use lightbox variable
	var center = lightbox.centerOptions(options);
	
	var newOptions = {
		"top": center.top+"px",
		"left": center.left+"px",
		width: options.width+"px",
		height: options.height+"px"
	};
	
	var mylb = this;
	
	if (options.opacity >= 0)
		newOptions.opacity = options.opacity;
		
	if (force)
	{
		$("#lightbox").css(newOptions);
		this.onComplete();
	}
	else 
	{
		if (this.visible())
		{
			if (newOptions.opacity == 0)
			{
				$("#lightbox .filler").animate({opacity: 0}, 150, mylb.settings.easing, function()
				{
					$("#lightbox").animate(newOptions, 200, mylb.settings.easing, mylb.onComplete);
				
				});
			}
			else
			{
				$("#lightbox").animate(newOptions, 200, mylb.settings.easing, mylb.onComplete);
				
			}
		}
		else
		{
			$("#lightbox").animate(newOptions, 200, mylb.settings.easing, function()
			{
				$("#lightbox .filler").animate({opacity: 1}, 150, mylb.settings.easing, mylb.onComplete);
			
			});
		}
	}
		
};
Lightbox.prototype.centerAll = function () 
{
	lightbox.center();
	lightbox.centerOverlay();
}
Lightbox.prototype.centerOverlay = function()
{
	var opts = lightbox.centerOverlayOptions();
	$("#lightbox-overlay").css({
		"top":opts.top+"px",
		"left":"0px"
	});
};
Lightbox.prototype.centerOverlayOptions = function()
{
	var opts = {};
	//var width = $("lightbox-overlay").width();
	//var height = $("lightbox-overlay").height();
	
	opts.top = $(window).scrollTop();
	opts.left = 0;
	opts.width = $(window).width();
	opts.height = $(window).height();
	
	return opts;
};
Lightbox.prototype.center = function (options, instance)
{
	// should not use lightbox variable
	var opts = lightbox.centerOptions(options);
	$("#lightbox").css({
	 	'top':opts.top+"px",
		"left":opts.left+"px"
	});
}
Lightbox.prototype.centerOptions = function (options)
{
	var opts = {};
	var width = $("#lightbox").width();
	var height = $("#lightbox").height();
	
	if (options && options.width && options.height)
	{
		width = options.width;
		height = options.height;
	}
	
	opts.top = Math.floor($(window).height()/2 - height/2 + $(window).scrollTop());
	opts.left = Math.floor($(window).width()/2 - width/2);
	
	return opts;
};
Lightbox.prototype._parseOptions = function (options)
{
	if (options == {} || options == undefined)
	{
		options = {};
		options.width = this.current.width;
		options.height = this.current.height;
	}
	else 
	{
		if (options.width == "same" || options.width == undefined) 
			options.width = this.current.width;
			
		if (options.height == "same" || options.height == undefined)
			options.height = this.current.height;
			
		if (options.width < 0)
			options.width = 0;
			
		if (options.height < 0)
			options.height = 0;
	}
	
	return options;
};
// Tab Navigation Functionality
var TabNavigator = {
	init : function(viewStackID) 
	{
		TabNavigator.bindViewStack();
		TabNavigator.setInitialView(viewStackID);
		// Show the first tab/content in the list
		return true;
	},
	bindViewStack : function()
	{
		$("a.tabNav").click(
			function ()
			{
				// Hide the visible
				$("div.tabContent").hide();
				$("div#"+$(this).attr("rel")).css("opacity",0);
				$("a.tabNav").attr("class","tabNav");
				
				// Show the selected one
				$("div#"+$(this).attr("rel")).show();
				$("div#"+$(this).attr("rel")).animate({ opacity: 1 }, 300, 'easeOutCubic');
				$(this).attr("class","tabNav selected");
			}
		);
	},
	setInitialView : function(viewStackID)
	{
		$("a[rel='"+viewStackID+"']").attr("class","tabNav selected");
		$("div#"+viewStackID).show();
	}
};
 
// Form Submission Functionality
var FormSubmitter = {
	speed : 300,
	isValid : false,
	errorClass : "error",
	formID : "",
	init : function()
	{
		FormSubmitter.activateSubmitButton();
		FormSubmitter.setValidation();
	},
	validateText : function(id)
	{
		if (dataValidator.isString($("#"+id).val())) {
			$("#"+id).removeClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeOut(FormSubmitter.speed);
		} else {
			$("#"+id).addClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeIn(FormSubmitter.speed);
		}
	},
	validateEmail : function(id)
	{
		if (dataValidator.isEmail($("#"+id).val())) {
			$("#"+id).removeClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeOut(FormSubmitter.speed);
		} else {
			$("#"+id).addClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeIn(FormSubmitter.speed);
		}
	},
	validatePhone : function(id)
	{
		if (dataValidator.isPhone($("#"+id).val())) {
			$("#"+id).removeClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeOut(FormSubmitter.speed);
		} else {
			$("#"+id).addClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeIn(FormSubmitter.speed);
		}
	},
	validateURL : function(id)
	{
		if(dataValidator.isURL($("#"+id).val())) {
			$("#"+id).removeClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeOut(FormSubmitter.speed);
		} else {
			$("#"+id).addClass(FormSubmitter.errorClass);
			$("label[for='"+id+"'] > span").fadeIn(FormSubmitter.speed);
			if(!dataValidator.isString($("#"+id).val())) {
				$("#"+id).val('http://');
			}
		}
	},
	setValidation : function()
	{
		var formID;
		
		$("input[class*='text']").blur(function() {
			FormSubmitter.validateText($(this).attr("id"));
			formID = "#"+$(this).closest("form").attr("id");
			FormSubmitter.checkForm(formID);
		}
		);
		$("textarea").blur(function() {
			FormSubmitter.validateText($(this).attr("id"));
			formID = "#"+$(this).closest("form").attr("id");
			FormSubmitter.checkForm(formID);
		}
		);
		$("input[class*='email']").blur(function() {
			FormSubmitter.validateEmail($(this).attr("id"));
			formID = "#"+$(this).closest("form").attr("id");
			FormSubmitter.checkForm(formID);
		}
		);
		$("input[class*='phone']").blur(function() {
			FormSubmitter.validatePhone($(this).attr("id"));
			formID = "#"+$(this).closest("form").attr("id");
			FormSubmitter.checkForm(formID);
		}
		);
		$("input[class*='url']").blur(function() {
			FormSubmitter.validateURL($(this).attr("id"));
			formID = "#"+$(this).closest("form").attr("id");
			FormSubmitter.checkForm(formID);
		}
		);
		$("input, textarea").keyup(function(event)
		{
			formID = "#"+$(this).closest("form").attr("id");
			//alert("test");
			FormSubmitter.checkForm(formID);
		});
	},
	checkForm : function (form)
	{
		if (FormSubmitter.isFromValid(form))
		{
			$(form + " button").removeClass("disabled");
			$(form + " button").removeAttr("disabled");
		}
		else 
		{
			$(form + " button").addClass("disabled");
			$(form + " button").attr("disabled","disabled");
		}
	},
	isFromValid : function (form)
	{
		var isValid = true;
		
		$("#"+form+" input, #"+form+" textarea").each(function(){
			$this = $(this);
			
			if ($this.hasClass("text"))
			{
				if (!dataValidator.isString($this.val()))
					isValid = false;
			}
			if ($this.hasClass("email"))
			{
				if (!dataValidator.isEmail($this.val()))
					isValid = false;
			}
			if ($this.hasClass("phone"))
			{
				if (!dataValidator.isPhone($this.val()))
					isValid = false;
			}
			if ($this.hasClass("url"))
			{
				if (!dataValidator.isURL($this.val()))
					isValid = false;
			}
		});
		
		return isValid;
	},
	sendData: function () 
	{
		// @NOTE: UPDATE THE POINTER TO THIS PHP FILE IF NECESSARY
		$.get("/video/php/emailer.php",$("#"+FormSubmitter.formID).serializeArray(),function(data)
		{	
			$("input").val("");
			$("textarea").val("");
			
			// @NOTE: UPDATE THE POINTER TO THE THANK YOU HTML FILE IF NECESSARY
			lightbox.load('/video/email-this-page/thankyou.html', { width: 420, height: 150 });
		});
	},
	activateSubmitButton : function() 
	{
		// @NOTE: THIS ONLY GETS TRIGGERED IN THE EMAIL FORM BUTTON.
		$("#email-form").submit(function()
			{
				FormSubmitter.formID = $(this).attr("id");
				FormSubmitter.sendData();
				return false;
			}
		);
			
		$("button.formSubmitButton").hover(
			function() {
				$(this).addClass("mouseOver");
			},
			function() {
				$(this).removeClass("mouseOver");
				$(this).removeClass("mouseDown");
			}
		);
		
		$("button.formSubmitButton").bind("mousedown",function()
			{
				$(this).addClass("mouseDown");
			}
		);
		$("button.formSubmitButton").bind("mouseup",function()
			{
				$(this).removeClass("mouseDown");
				$(this).addClass("mouseOver");
			}
		);
		
	}
}
 
// DataValidator object: 
function DataValidator ()
{
	this.pattern = new Pattern();
}
DataValidator.prototype.isEmail = function (value) {
	return String(value).search(this.pattern.email) != -1;
};
DataValidator.prototype.isInteger = function (value) {
	return String(value).search(this.pattern.integer) != -1;
};
DataValidator.prototype.isPhone = function (value) {
	return String(value).search(this.pattern.phone) != -1;
}
DataValidator.prototype.isURL = function(value) {
	return String(value).search(this.pattern.url) != -1;
}
DataValidator.prototype.isString = function (value) {
	return String(value).search(this.pattern.string) != -1;
}
DataValidator.prototype.trim = function (value) {
	return String(value).replace(/^\s+|\s+$/g, '');
}
 
// Lightbox object:
 
 
// Input field patterns:
function Pattern ()
{
	this.email = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	this.string = /\S/;
	this.integer = /^\s*(\+|-)?\d+\s*$/;
	this.phone = /^((\+\d{1,3}(-| )?\(?\d\)?(.|-| )?\d{1,5})|(\(?\d{2,6}\)?))(.|-| )?(\d{3,4})(.|-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
	this.url = /(http|https):\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&amp;?\/.=]+$/;
}



// DOC READY
$(document).ready(function()  
{  
	dataValidator = new DataValidator();
	lightbox = new Lightbox();
	lightbox._instance = lightbox;
	FormSubmitter.init();
	if ($('body').hasClass('collapsed')) {
		$('#next-step').click(function(){
			$('#form-group').slideDown(500);
		});
	} else {
		$('#next-step').click(function(){
			$('html,body').animate({
				scrollTop: ($('#next-step').offset().top)+'px'
				}, 500, 'easeOutCubic');
		});
	}
	// wire up the rel="popup" link
	$('a[rel=popup-form]').click(function() {
		
		// @NOTE: UPDATE THIS ADDRESS TO POINT TO THE FILE ON YOUR DIRECTORY.
		lightbox.load('/video/email-this-page/index.html', { width: 420, height: 540 });
	
		return false;
	});
	$("a").focus( function () { $(this).blur() });
	$("object").append('<param name="wmode" value="transparent">');
});
