/*##############################################################################
Global functions
##############################################################################*/

// Special jquery blur.
// Credits: http://stackoverflow.com/questions/1199293/simulating-focus-and-blur-in-jquery-live-method
 (function(){var special=jQuery.event.special,uid1='D'+(+new Date()),uid2='D'+(+new Date()+1);jQuery.event.special.focus={setup:function(){var _self=this,handler=function(e){e=jQuery.event.fix(e);e.type='focus';if(_self===document){jQuery.event.handle.call(_self,e);}};jQuery(this).data(uid1,handler);if(_self===document){if(_self.addEventListener){_self.addEventListener('focus',handler,true);}else{_self.attachEvent('onfocusin',handler);}}else{return false;}},teardown:function(){var handler=jQuery(this).data(uid1);if(this===document){if(this.removeEventListener){this.removeEventListener('focus',handler,true);}else{this.detachEvent('onfocusin',handler);}}}};jQuery.event.special.blur={setup:function(){var _self=this,handler=function(e){e=jQuery.event.fix(e);e.type='blur';if(_self===document){jQuery.event.handle.call(_self,e);}};jQuery(this).data(uid2,handler);if(_self===document){if(_self.addEventListener){_self.addEventListener('blur',handler,true);}else{_self.attachEvent('onfocusout',handler);}}else{return false;}},teardown:function(){var handler=jQuery(this).data(uid2);if(this===document){if(this.removeEventListener){this.removeEventListener('blur',handler,true);}else{this.detachEvent('onfocusout',handler);}}}};})();

function toggleDetailsText() {
	if ($('#seeDetails span').html() == 'Vezi detalii')
		$('#seeDetails span').html('Ascunde detalii');
	else if ($('#seeDetails span').html() == 'Ascunde detalii')
		$('#seeDetails span').html('Vezi detalii');
	else if ($('#seeDetails span').html() == 'View details')
		$('#seeDetails span').html('Hide details');
	else if ($('#seeDetails span').html() == 'Hide details')
		$('#seeDetails span').html('View details');
}

function toggleOrder() {
	$('#foodsOrdered').slideToggle('fast', function() {
		toggleDetailsText();
	});
}

function removeOrder(i, id) {
	if (typeof i != 'number') {
		return false;
	}
	$('#orderCart').load(
		BASE+'php-pieces/addToCart.php?lang='+LANG,
		{removeFromCart: 'true', orderRemove: i, orderId: id},
		function() {
			toggleDetailsText();
		}
	);
}

// proper login
function doLogin() {
	var checkEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = $('#loginEmail').val();
	var pass = $('#loginPass').val();
	var error = false;

	if (!checkEmail.test(email) || email == 'email') {
		$('#loginEmail').addClass('wrong');
		error = true;
	}
	if (pass == '' || pass.length < 4) {
		$('#loginPass').addClass('wrong');
		error = true;
	}

	if (!error) {
		$('#loginForm').slideUp('fast', function() {
			$('#loginForm').load(BASE+'php-pieces/doLogin.php?lang='+LANG, {doLogin: 'true', loginEmail: email, loginPass: pass});
		});
	} else {
		var errorText = LANG == 'ro' ? 'date incorecte' : 'wrong credentials';
		$('#wrongLogin').html(errorText);
	}
}

// logout
function doLogout() {
	$('#loginForm').slideUp('fast', function() {
		$('#loginForm').load(BASE+'php-pieces/doLogin.php?lang='+LANG, {doLogout: 'true'});
	});
}

// forgot password
function forgotPassword() {
	var email = $('#loginEmail').val();
	var link = BASE+LANG+'/';
	link += LANG == 'ro' ? 'am-uitat-parola.html' : 'forgot-password.html';
	if (email.length)
		link += '&email='+email;
	window.location = link;
}

// un/subscribe from/to newsletter
function newsletter(type) {
	var checkEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var error = false;

	if (!checkEmail.test($('#newsletterEmail').val())) {
		$('#newsletterEmail').addClass('wrong');
		error = true;
	}
	if (!error) {
		$('#newsletterFormAweber').submit();
		// AM TRECUT LA FORMULAR AWEBER 13 MAI 2010
		/*$('#newsletterForm').slideUp('fast', function() {
			if (type == 'abonare') {
				$('#newsletterForm').load(BASE+'php-pieces/newsletter.php?lang='+LANG, {abonare: 'true', newsletterEmail: $('#newsletterEmail').val()});
			} else if (type == 'dezabonare') {
				$('#newsletterForm').load(BASE+'php-pieces/newsletter.php?lang='+LANG, {dezabonare: 'true', newsletterEmail: $('#newsletterEmail').val()});
			}
		});*/
	} else {
		var errorText = LANG == 'ro' ? 'Adresa de email este incorecta' : 'Wrong email address';
		$('#wrongNewsletter').html(errorText);
	}
}

$('document').ready(function() {

	// load the cart
	$('#orderCart').load(BASE+'php-pieces/addToCart.php?lang='+LANG);

	// check user
	$('#loginForm').load(BASE+'php-pieces/doLogin.php?lang='+LANG);

	// get newsletter
	$('#newsletterForm').load(BASE+'php-pieces/newsletter.php?lang='+LANG);

	// fancy login
	$('#loginEmail, #loginPass, #newsletterEmail').live('focus', function() {
		if ($(this).val() == 'Adresa de email' || $(this).val() == 'Email address')
			$(this).val('');
		$(this).removeClass('wrong');
		if (($(this).val() == 'Parola' || $(this).val() == 'Password')/* && $(this).attr('type') == 'text'*/) {
			/*var pass = document.getElementById('loginPass');
			pass.type = 'password';*/
			$(this).val('');
		}
	});
	$('#newsletterName').live('focus', function() {
		if ($(this).val() == 'Nume' || $(this).val() == 'Name') {
			$(this).val('');
		}
	});
	$('#newsletterName').live('blur', function() {
		if ($(this).val() == '') {
			var newsName = LANG == 'ro' ? 'Nume' : 'Name';
			$(this).val(newsName);
		}
	});
	$('#loginEmail, #newsletterEmail').live('blur', function() {
		var emailText = LANG == 'ro' ? 'Adresa de email' : 'Email address';
		if ($(this).val() == '')
			$(this).val(emailText);
	});
	$('#loginPass').live('keypress', function(e) {
		if (e.keyCode == 13) {
			doLogin();
		}
	});
	$('#loginPassText').live('focus', function() {
		$(this).hide();
		$('#loginPass').show().focus();
	});
	$('#loginPass').live('blur', function() {
		if ($(this).val() == '') {
			$(this).hide();
			$('#loginPassText').show();
		}
	});
	// custom pass field
	/*$('#loginPass').live('blur', function() {
		if ($(this).attr('type') == 'password' && $(this).val() == '') {
			var pass = document.getElementById('loginPass');
			pass.type = 'text';
			var passText = LANG == 'ro' ? 'Parola' : 'Password';
			$(this).val(passText);
		}
	});*/

	// fancy newsletter
	$('#newsletterSubscribe').live('click', function() {
		newsletter('abonare');
	});
	$('#newsletterEmail').live('keypress', function(e) {
		if (e.keyCode == 13) {
			newsletter('abonare');
		}
	});
	$('#newsletterUnsubscribe').live('click', function() {
		newsletter('dezabonare');
	});

});