$(document).ready(function(){
	$('#fg_window').center();
	$('#FG_link').click(function(e){
		e.preventDefault();
		$('#fg_window').fadeIn("slow").draggable();
	});
	
	
	$('a.closeFG').click(function(){
		$('#fg_window').fadeOut();
	});
	
	$('#reserve').click(function(){
		var message = "";
		if ($("#fgname").val() == '') {
			message += 'please verify your name. \n';
		}
		if ($("#fglname").val() == '') {
			message += 'please verify your last name. \n';
		}
		if (!validateEmail($("#fgemail").val())) {
			message += 'please verify your email! \n';
		}
		if (message == '') {
			var tmp_name = escape($("#fgname").val());
			var tmp_lname = escape($("#fglname").val());
			var tmp_email = escape($("#fgemail").val());
			$.post("contact.php", {
				action: 'reserv',
				name: $("#fgname").val(),
				lname: $("#fglname").val(),
				email: $("#fgemail").val()
			}, function(data){
				
			});
			$.post("http://www.familygamesamerica.com/mainsite/includes/cyb.adm.usr.php?opc=new&name=" + tmp_name + "&lname=" + tmp_lname + "&email=" + tmp_email, {}, function(data){
				var textout = '';
				if (data.charAt(0) != '*') {
					datas = data.split("|");
					for (var i = 1; i < datas.length; i++) {
						var values = datas[i].split("-");
						textout += "<a href='http://www.familygamesamerica.com/mainsite/consumers/productview.php?pro_id=" + values[1] + "' title='" + values[2] + "'  class='fg_img'><img src='http://www.familygamesamerica.com/mainsite/resources/products/100x100/" + values[0] + ".png'   width='90px' height='90px' border='0' alt='" + values[2] + "'></a> \n";
					}
					$('#fg_prds').html(textout);
					$('#fg_form').hide();
					$('#fg_seemore').fadeIn('fast');
				}
				else {
					alert(data);
				}
			});
			alert("A game has been reserved on your behalf.");
		}
		else {
			alert(message);
		}
	});
});

jQuery.fn.center = function(params){
	var options = {
		vertical: true,
		horizontal: true
	}
	op = jQuery.extend(options, params);
	return this.each(function(){
		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();
		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width"));
		var borderBottom = parseInt($self.css("border-bottom-width"));
		//get the media of padding and borders
		var mediaBorder = (borderTop + borderBottom) / 2;
		var mediaPadding = (paddingTop + paddingBottom) / 2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width / 2) * (-1);
		var halfHeight = ((height / 2) * (-1)) - mediaPadding - mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};
		if (op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if (op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if (positionType == 'static') {
			$self.parent().css("position", "relative");
		}
		//aplying the css
		$self.css(cssProp);
	});
};

function validateEmail(elementValue){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue);
}
