// JavaScript Document
var d_intVal = null;
jQuery(document).ready(function()
{

	
	// password box
	var $password = jQuery('#password'),
			$parent = $password.parent(),
			$pp = false,
			$username = jQuery('#username');
			
	/*$password.hide();
	$pp = jQuery('<INPUT />').attr({
		'id':'ppassword',
		'type':'text',
		'value':'Password',
		'class':'text-input'
	}).appendTo($parent);
	
	$pp.focus(function()
	{
		$pp.hide();
		$password.show().focus();
		if($username.val() == 'E-Mail Address')
		{
			$username.val('');
		}
	})
	
	$password.blur(function()
	{
		if($password.val() == '' && $username.val() == 'E-Mail Address')
		{
			$password.hide();
			$pp.show();
		}
	});
	
	$username.focus(function()
	{
		if($username.val() == 'E-Mail Address')
		{
			$username.val('')
			$pp.hide();
			$password.show();
		}
	}).blur(function()
	{
		if($username.val() == '')
		{
			$username.val('E-Mail Address');
			$password.val('').hide();
			$pp.show();
		}
	});*/
	$username.focus(function(event){
		//console.log(event);
		d_intVal = 	setInterval( function(){
						myCustomEvent({type:"keyup",target:event.target});
					}, 500 );
	}).blur(function(){
		clearInterval( d_intVal );
	}).keyup(function(event) {
		//if(event.keyCode == '13'){
		//	doFormSubmit();
		//	return false;
		//}
		if($username.val() != ''){
			//$('.holder').fadeOut('normal');
			$('#ghostSpanEmailAddress').hide();
			myCustomEvent({type:"keyup",target:$password});
			//clearInterval( d_intVal );
		}else{
			//$('.holder').fadeIn('normal');
			$('#ghostSpanEmailAddress').show();
		}
	});
	$password.keyup(function(event) {
		//if(event.keyCode == '13'){
		//	doFormSubmit();
		//	return false;
		//}
		if($password.val() != ''){
			$('#ghostSpanPassword').hide();
		}else{
			$('#ghostSpanPassword').show();
		}
	});
	myCustomEvent({type:"keyup",target:$username});
	//myCustomEvent({type:"keyup",target:$password});
	counter();
	
});
function myCustomEvent(event){
	$(event.target).trigger(event);
}
/*function showHideGhostLayer(event){
	if($(event.target).val() != ''){
		//$('.holder').fadeOut('normal');
		$(event.target.nextElementSibling).hide();
	}else{
		//$('.holder').fadeIn('normal');
		$(event.target.nextElementSibling).show();
	}
}*/
function addLoc(json)
{
	var $obj = jQuery('#testimony-box .sig'),
			html = $obj.html();
			
	try
	{
		$obj.html(html+', '+json['City']);
	}
	catch(e){}
}

jQuery(document).click(function(e)
{
	var $this = jQuery(e.target);
	if($this.hasClass('video-player-button'))
	{
		activateVideo();
	}
	
	switch($this.attr('id'))
	{
		case 'login-submit':
			if(!isEmailAddressValid())
			{
				notifyEmail();
				return false;
			}
			if(jQuery('#password').val() == ''){
				alert('Please enter your webstarts account password into the Password field.');
				jQuery('#password').focus();
				return false;
			}
			break;
		case 'ghostSpanEmailAddress':
				jQuery('#username').focus();
				break;
		case 'ghostSpanPassword':
				jQuery('#password').focus();
				break;
		/*case 'forgot-password':
			if(!isEmailAddressValid())
			{
				notifyEmail();
			}
			else
			{
				window.location = 'http://www.webstarts.com/functions/sendPasswordEmail.php?email='+jQuery('#username').val();
			}
			break;*/
		case 'bookmark-this':
			bookmarksite('www.WebStarts.com | The Free Website Builder', 'http://www.WebStarts.com?lts=fav');
			break;
	}
});

function bookmarksite(title, url)
{
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}


function notifyEmail()
{
	alert('Please enter your Email address into the E-Mail Address field.');
	jQuery('#username').focus();
}

function activateVideo()
{
	var html = '<object height="318" width="400">'
							+'<param value="http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/flvplayer.swf" name="movie">'
							+'<param value="high" name="quality"><param value="thumb=http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/FirstFrame.jpg&amp;containerwidth=400&amp;containerheight=318&amp;autostart=true&amp;content=http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/WebStarts-Homepage-Demo.flv" name="flashVars">'
							+'<param value="true" name="allowFullScreen">'
							+'<param value="showall" name="scale">'
							+'<param value="always" name="allowScriptAccess">'
							+'<param value="http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/" name="base">'
							+'<embed height="318" width="400" scale="showall" base="http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/" allowfullscreen="true" flashvars="thumb=http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/FirstFrame.jpg&amp;containerwidth=400&amp;autostart=true&amp;containerheight=318&amp;content=http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/WebStarts-Homepage-Demo.flv" allowscriptaccess="always" type="application/x-shockwave-flash" quality="high" src="http://content.screencast.com/users/webstarts/folders/Default/media/3a4fa4e9-98c9-4162-8b07-c3f690a5cbc1/flvplayer.swf"></object>';
	
	jQuery('#video-player').html(html);
}


function isEmailAddressValid()
{
	var regex = /^[^@]+@[^@.]+.*\.[a-zA-Z]{2,}$/i;
			
	return regex.test(jQuery('#username').val());
}

function counter() {

	jQuery.get('http://' + window.location.host + '/customer-counter.php',function(data)
	{
		var ar = data.split("|");
		//jQuery('#counter').html(ar[1]); 
		var rawstring = ar[1];
		var cleanstring = rawstring.replace(/[^\d\.\-\ ]/g, '');
		jQuery("#countdown").flipCounter({
			number:0,
			formatNumberOptions: {
		    format:"#,###,###",
		    locale:"us"
		},
		});
		jQuery("#countdown").flipCounter("startAnimation",
			{
				number: parseInt(cleanstring),
				duration:2000
			});		
	},'text');
	
}






