var currentUserEmail;

$(document).ready(function()
{
	$(".tabs").tabs();
	$(".accordion").accordion({
			autoHeight: false,
			navigation: true
		});
	
	$(".phone_no").keypress(function(event){
		return isValidPhoneNumberKey(event);
	});
	$(".float").keypress(function(event){
		return isFloatNumberKey(event);
	});
	$(".lowercase").blur(function(event){
		$(this).val($(this).val().toLowerCase());
	});
	$(".propercase").blur(function(event){
		$(this).val(toProperCase($(this).val()));
	});
	
	var emailMessage = $("<div id='emailMessage'></div>");
	$(".email_check").after(emailMessage);
	$(".email_check").bind("blur.emailcheck", function(){
		if (!$(this).val().length || (currentUserEmail && $(this).val()==currentUserEmail))
		{
			emailMessage.html("");
			return;
		}
		emailMessage.html("<img src=\"images/working.gif\"><p>checking...</p>");
		$.ajax({url: "checkemail.php", 
						dataType: "json",
						data: {email:$(this).val()}, 
						success: function(data){
							if (data.result=="1")
								emailMessage.html("<img src=\"images/ok.png\"><p>OK</p>");
							else
								emailMessage.html("<img src=\"images/error.png\"><p>IN USE</p>");
						},
						async: false});
	});
	
	$(".terms").click(function(event){
		event.preventDefault();
		setupOverlay("termsofsale.html");
	});
	
	$(".privacy").click(function(event){
		event.preventDefault();
		setupOverlay("privacypolicy.html");
	});
	
	$(".social_link").click(function(event){
		_gaq.push(['_trackEvent', "Social Networking", "Page Visited", $(this).attr("id")]);
	});
});

function setupOverlay(_page)
{
	var overlay = $("<div id=\"overlay\"></div>").fadeOut(0);
	$("body").append(overlay);
	overlay.expose({
		color: "#111",
		onBeforeLoad: function()
			{
				var width = Math.min($(window).width()*0.8, 960*0.8);
				var height = $(window).height()*0.8;
				var left = ($(window).width()-width)/2;
				var top = ($(window).height()-height)/2;
				var closeButton = $("<div id=\"overlay_close\"></div>").click(function(){$.mask.close();});
				$("#overlay").css("left", left).css("top", top).css("width", width).css("height", height).append(closeButton)
				$("<div id=\"overlay_content\"></div>").load(_page, function(){
					$("#overlay").fadeIn("fast");
				}).appendTo($("#overlay"));
			},
		onBeforeClose: function()
			{
				$("#overlay").remove();
			}
	});	
}

function getElementsByClassName(oElm, strTagName, strClassName)
{
  var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  strClassName = strClassName.replace(/\-/g, "\\-");
  var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  var oElement;
  for(var i=0; i<arrElements.length; i++){
    oElement = arrElements[i];
    if(oRegExp.test(oElement.className)){
      arrReturnElements.push(oElement);
    }
  }
  return (arrReturnElements)
}

function windowSize()
{
  this.width=0;
  this.height=0;
}

function getWindowSize(_size)
{
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    _size.width = window.innerWidth;
    _size.height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    _size.width = document.documentElement.clientWidth;
    _size.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    _size.width = document.body.clientWidth;
    _size.height = document.body.clientHeight;
  }
}

function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)|'(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

function isValidPhoneNumberKey(evt)
{
  var charCode = (evt.which) ? evt.which : evt.keyCode
  if ((charCode > 32 && (charCode < 48 || charCode > 57)) && charCode!=43/*+ key*/)
    return false;

  if (!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement
    evt.target=evt.srcElement //extend obj with custom e.target prop

  if (charCode==43)
  {
    //only one (+) international code identifier allowed
    if (evt.target.value.indexOf(String.fromCharCode(charCode))>-1)
      return false;
  }

  return true;
}

function isFloatNumberKey(evt)
{
  var charCode = (evt.which) ? evt.which : evt.keyCode
  if ((charCode > 32 && (charCode < 48 || charCode > 57)) && charCode!=46)
    return false;

  if (!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement
    evt.target=evt.srcElement //extend obj with custom e.target prop

  if (charCode==46)
  {
    //only one decimal point allowed
    if (evt.target.value.indexOf(String.fromCharCode(charCode))>-1)
      return false;
  }

  return true;
}

function showInfoBox(info)
{
	var left = ($(window).width() - $("#infoBox").width())/2;
	$("#infoBox").html(info).css("left", left+"px").show().animate({height:'+=30'}, 
		function(){
			$(this).delay(4000).animate({height:"0px"}, function(){$(this).hide()});
		});
}
