$(document).ready(function(){
	$('#navigation a')
		.click(function(){
			var id = '#' + $( this ).attr('title').toLowerCase() + '-frame';
			
			$( '#navigation a' ).removeClass('active');
			$( '.frame ').hide();
			
			$('.repeated_home').css('height','65px');
			
			$( id ).css('opacity',0);
			$( id ).show()
			$( id ).animate({opacity: 1}, {duration: 300, queue: false});

			$( this ).addClass('active');
			return false;
		});
		
	$('.seemore')
		.click(function(){
				var p=$(this).parent();
				
				$('.repeated_home').css('height','200px');
				$('#' + p.attr('id') + '-big' ).show();
				p.hide();
		});
	
	
	$('.blink')
		.focus( function(){
			if( $( this ).attr('value') == $(this).attr('title') ) {
				$( this ).attr({ 'value': '' });
				$( this ).addClass( 'field-writing' );
			}
		})
		.blur( function(){
			if( $( this ).attr('value') == '' ) {
				$( this ).attr({ 'value': $(this).attr('title') });
				$( this ).removeClass( 'field-writing' );
			}
				
		});
	
	$('.passwordhint')
		.focus( function(){
			$( this ).removeClass( 'passwordhint' );
		})
		.blur( function(){
			if($(this).attr('value')=="")
				$( this ).addClass( 'passwordhint' );
		});
		
	$('.sign-in-link')
		.click( function(){
			$( this ).parent().parent().hide();
			$('#sign-up').hide();
			$('#login-div').show(400);
			$('#product-add-id').attr('value',"");
			return false;
		})
		
	$('.sign-up-link')
		.click(function(e){
			var s = $('#sign-up');
			s.show();
			s.css('left',e.pageX-parseInt(s.css('width')));
			s.css('top',e.pageY);
			s.css('zIndex',10000);
			$('#product-add-id').attr('value',"");
			return false;
		})
		
	$('.sign-up-link-float')
		.click(function(e){
			var si = $('#sign-in-float');
			si.hide();
			var s = $('#sign-up');
			s.show();
			s.css('left',si.css('left'));
			s.css('top',si.css('top'));
			s.css('zIndex',10000);
			return false;
		})
	
	$('#sign-up .close')
		.click(function(){
			$('#sign-up').hide();
			return false;
		});
	
	$('.close-thanks')
		.click(function(){
			$('#thanks').hide(300);
			return false;
		});
	
	$('#login-div .close')
		.click(function(){
			$('#not-logged-div').show();
			$('#login-div').hide();
			return false;
		});
	
	$('#sign-in-float .close')
		.click(function(){
			$('#sign-in-float').hide();
			return false;
		});
		
	$('#SignUp-captcha')
		.keyup(function(e) {
			if(e.keyCode == 13) {
				ProcessSignUp();
			}	
  		}); 
		
	$('#sign-up-submit')
		.click(function(){
			ProcessSignUp();
			return false;
		});
		
	$('#sign-up-fb')
	.click(function() {
			FB_RequireFeatures(["XFBML"], function()
			{
				FB.Connect.requireSession(ProcessFacebookSignUp);
			});
			
		});
	
	$('#sign-in-fb')
		.click(function() {
			FB_RequireFeatures(["XFBML"], function()
			{
				FB.Connect.requireSession(ProcessFacebookSignIn);
			});
			
		});
	
	$('#sign-in-fb2')
		.click(function() {
			FB_RequireFeatures(["XFBML"], function()
			{
				FB.Connect.requireSession(ProcessFacebookSignIn);
			});
			
		});
		
	$('.later')
		.click(function(){
			$('#info-form').hide();
			$('#info-saved').show();
			return false;
		});
	
	$('#edit-info-link')
		.click(function(){
			$('#info-form').show();
			$('#info-saved').hide();
			return false;
		});
	
	$('#submit-sign-in')
		.click(function(){
			ProcessSignIn(
				0,
				$('#SignIn-email').attr('value'),
				$('#SignIn-password').attr('value'),
				false);
			return false;
		});
		
	$('#SignIn-password')
		.keyup(function(e) {
			if(e.keyCode == 13) {
				ProcessSignIn(
					0,
					$('#SignIn-email').attr('value'),
					$('#SignIn-password').attr('value'),
					false);
			}	
  		});  
	
	$('#submit-signin-float')
		.click(function(){
			ProcessSignIn(
				$('#product-add-id').attr('value'),
				$('#SignIn-email-float').attr('value'),
				$('#SignIn-password-float').attr('value'),
				true);
			return false;
		});
		
	$('#newsletter-submit')
		.click(function(){
			//send ajax request
			var html = $.ajax(
				{url: 'index.php?route=controls/newslettersignup', 
				async: false, 
				type:"POST", 
				data:"Newsletter[email]="+$('#newsletter-email').attr('value')
				}).responseText;
			
			if (html=="OK")
			{
				$('#newsletter-title').html('Thank you!');
				$('#newsletter-email').attr('value','');
				
			} else
				$('#newsletter-title').html('Please enter a valid email.');
			
			return false;
		});
	
	var temp;
	if((temp=$("#results")).length>0)
	{
		var url = "index.php?route=controls/results&t="+encodeURIComponent($("#t").attr('value'))+"&s="+encodeURIComponent($("#s").attr('value'))+"&param="+encodeURIComponent($("#param").attr('value'));
		
		$("#results").load(url);
	}
	if((temp=$("#item-navigation")).length>0)
		temp.load("index.php?route=controls/featuredproducts&i=0&sku="+$("#item-navigation").attr("title"));
	
	if((temp=$("#item-sku")).length>0)
		temp.load("index.php?route=controls/skuproducts&i=0&sku="+$("#item-sku").attr("title"));
	
	if((temp=$("#item-shoebox")).length>0)
		temp.load("index.php?route=controls/shoeboxresults&i=0&u="+$("#item-shoebox").attr('value'));
	
	
});

function SignupErrorMsg(html)
{
	//JSON stringify 
	eval("var error="+html);
	
	if((!error)||(!error.field))
		return false;
	
	var msgblock=$('#sign-up-error');
	
	switch(error.field)
	{
		case "email":
			switch(error.rule)
			{
				case "req":
				case "email":
					msgblock.html("Please enter a valid email address.");
					break;
				case "unique":
					msgblock.html("This email address is taken.");
					break;
				default:
					alert(html);
					break;
			}
			break;
		case "password":
			switch(error.rule)
			{
				case "req":
				case "minlength":
					msgblock.html("Please enter a valid password.");
					break;
			}
			break;
		case "captcha":
			switch(error.rule)
			{
				case "captcha":
					msgblock.html("Please re-enter the image text.");
					break;
			}
			break;
	}
	msgblock.show();
}
function SigninErrorMsg(isFloat)
{
	if(isFloat)
	{
		$('#sign-in-error-float').show();
		$('#sign-in-title-float').hide();
	}
	else
	{
		$('#sign-in-error').show();
		$('#sign-in-title').hide();
	}
}

function FacebookLoginShoebox(u)
{
	window.location='index.php?route=share/facebookconnect&t=shoebox&u='+u;	
}

function ProcessSignIn(pid,email,pwd,isFloat)
{
	var html = $.ajax(
		{url: 'index.php?route=controls/postsignin', 
		async: false, 
		type:"POST", 
		data:"Products[id]="+pid+"&SignIn[email]="+email+"&SignIn[password]="+pwd}).responseText;
	
	if (ProcessJsonResult(html))
		return false;
	
	SigninErrorMsg(isFloat);
}
function ProcessSignUp()
{
	var html = $.ajax(
		{url: 'index.php?route=controls/register', 
		async: false, 
		type:"POST", 
		data:"Products[id]="+$('#product-add-id').attr('value')+"&Users[email]="+$('#SignUp-email').attr('value')+"&Users[password]="+$('#SignUp-password').attr('value')+"&captcha="+$('#SignUp-captcha').attr('value')}).responseText;
	
	if (ProcessJsonResult(html))
		return false;
		
	SignupErrorMsg(html);
}
function ProcessFacebookSignIn()
{
	var fb_id=FB.Facebook.apiClient.get_session().uid;
	
	var html = $.ajax(
		{url: 'index.php?route=controls/registerfb', 
		async: false, 
		type:"POST", 
		data:"Products[id]="+$('#product-add-id').attr('value')+"&Users[facebook_id]="+fb_id}).responseText;
	
	if (ProcessJsonResult(html))
		return false;
		
	SignupErrorMsg(html);
}
function ProcessFacebookSignUp()
{
	
	
	//FB_RequireFeatures(["XFBML"], function()
	//{
	//	FB.Facebook.init("<?php echo FACEBOOK_APIKEY; ?>", "<?php echo FACEBOOK_XD; ?>");
	//	
	//});
	
	var fb_id=FB.Facebook.apiClient.get_session().uid;
	
	var html = $.ajax(
		{url: 'index.php?route=controls/registerfb', 
		async: false, 
		type:"POST", 
		data:"Products[id]="+$('#product-add-id').attr('value')+"&Users[facebook_id]="+fb_id}).responseText;
	
	if (ProcessJsonResult(html))
		return false;
		
	SignupErrorMsg(html);
}
function ProcessJsonResult(html)
{
	eval("var j="+html);
	if(j.status=="OK")
	{
		if(j.product>0)
			location.href = location.href+"&ty=1";
		else
			location.href = location.href;
	
		return true;
	}
	else
	{
		/*switch(j.field)
		{
			case "email":
				$('#sign-up-error').html();
				break;
			case "":
				$('#sign-up-error').html();
				break;
			case "":
				$('#sign-up-error').html();
				break;
			case "":
				$('#sign-up-error').html();
				break;
				
		}*/
	}
	
	return false;
}