thereCanBeOnlyOne.addElement({
	prefix: 'shareBox', 
	isopen: false, 
	fnOpen: function() {
		//console.log('open sharebox');
		shareBoxOpen();
	}, 
	fnClose: function() {
		//console.log('close sharebox');
		shareBoxClose();
	}
});
function shareBoxOpen()
{
	$('#share-box-mail-msg').css('display', 'none');
	$('#share-box-mail').css('display', 'block');
	/*$('#share-box-input-name').val('');
	$('#share-box-input-email').val('');*/
	$('#share-box').slideDown('normal');
	//overlayShow();
}
function shareBoxClose()
{
	$('#share-box').slideUp('fast');
	//overlayHide();
}

thereCanBeOnlyOne.addElement({
	prefix: 'freecallBox', 
	isopen: false, 
	fnOpen: function() {
		//console.log('open freecallbox');
		freecallBoxOpen();
	}, 
	fnClose: function() {
		//console.log('close freecallbox');
		freecallBoxClose();
	}
});
function freecallBoxOpen()
{
	$('#freecall-box-msg').css('display', 'none');
	$('#freecall-box-input').css('display', 'block');
	/*$('#freecall-box-input-name').val('');
	$('#freecall-box-input-phone').val('');*/
	$('#freecall-box').slideDown('normal');
	//overlayShow();
}
function freecallBoxClose()
{
	$('#freecall-box').slideUp('fast');
	//overlayHide();
}

thereCanBeOnlyOne.addElement({
	prefix: 'newsletterBox', 
	isopen: false, 
	fnOpen: function() {
		//console.log('open newsletterbox');
		newsletterBoxOpen();
	}, 
	fnClose: function() {
		//console.log('close newsletterbox');
		newsletterBoxClose();
	}
});
function newsletterBoxOpen()
{
	$('#newsletter-box-mail-msg').css('display', 'none');
	$('#newsletter-box-mail').css('display', 'block');
	//$('#newsletter-box-input-email').val('');
	$('#newsletter-box').slideDown('normal');
	//overlayShow();
}
function newsletterBoxClose()
{
	$('#newsletter-box').slideUp('fast');
	//overlayHide();
}

$(document).ready(function(){
	function setFieldError(inputId, containerId)
	{
		if (!$('#'+containerId).hasClass('input-error')) {
			$('#'+containerId).addClass('input-error');
			$('#'+inputId).css('width', ($('#'+inputId).width() - 2));
		}
	}
	
	function setFieldNoError(inputId, containerId)
	{
		if ($('#'+containerId).hasClass('input-error')) {
			$('#'+containerId).removeClass('input-error');
			$('#'+inputId).css('width', ($('#'+inputId).width() + 2));
		}
	}
	
	function checkEmail(inputId, containerId) {
    	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
		if(reg.test($('#'+inputId).val()) === false || $('#'+inputId).val() == $('#'+inputId).attr('placeholder')) {
			setFieldError(inputId, containerId);
			$('#'+inputId).keyup(function() {
				if(reg.test($('#'+inputId).val()) && $('#'+inputId).val() != $('#'+inputId).attr('placeholder')) {
					setFieldNoError(inputId, containerId);
				}
				else { setFieldError(inputId, containerId); }
			});
		}
		else { return(true); }
	}
	
	function checkNoEmpty(inputId, containerId)
	{
		if ($('#'+inputId).val() == '' || $('#'+inputId).val() == $('#'+inputId).attr('placeholder')) {
			setFieldError(inputId, containerId);
			$('#'+inputId).keyup(function() {
				if ($(this).val() != '') {
					setFieldNoError(inputId, containerId);
				}
				else { setFieldError(inputId, containerId); }
			});
		}
		else { return(true); }
	}
	
	/*==========================================================================
				* SHARE BOX *
	==========================================================================*/
	$('#share-box-close').click(function() {
		//shareBoxClose();
		thereCanBeOnlyOne.closeElement('shareBox');
	});
	
	$('#share-box-input-email').blur(function() {
		checkEmail('share-box-input-email', 'share-box-container-email');
	});
	
	$('#share-box-input-name').blur(function() {
		checkNoEmpty('share-box-input-name', 'share-box-container-name');
	});
	
	$('#share-box-mail-send').click(function() {
		var senderName = $('#share-box-input-name').val();
		var recipientEmail = $('#share-box-input-email').val();
		if (checkNoEmpty('share-box-input-name', 'share-box-container-name') && checkEmail('share-box-input-email', 'share-box-container-email')) {
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: 'http://'+SERVER_NAME+'/sharebymail/send?language='+LANGUAGE,
				data: 'senderName='+senderName+'&recipientEmail='+recipientEmail+'&uri='+window.document.location.href+'&language='+LANGUAGE,
				success: function(result) {
					$('#share-box-mail').css('display', 'none');
					$('#share-box-mail-msg').css('display', 'block');
					$('#share-box-mail-msg').html(result.content);
				}
			});
		}
	});
	
	$('#share-box-social-button-facebook').click(function() {
		$(this).attr('href', 'http://www.facebook.com/share.php?u='+window.document.location.href.replace('#', '%23'));
	});
	/*==========================================================================
				* NEWSLETTER BOX *
	==========================================================================*/
	$('#footer-bt-newsletter').click(function() {
		thereCanBeOnlyOne.openElement('newsletterBox');
		return(false);
	});
	$('#newsletter-box-close').click(function() {
		thereCanBeOnlyOne.closeElement('newsletterBox');
	});
	
	if (DOMAIN_ANCHOR) {
		if (DOMAIN_ANCHOR.match("^newsletter.+")) {
			var arr_newsletterParams = DOMAIN_ANCHOR.split('-');
			newsletterBoxRequest(arr_newsletterParams[1], 'confirm', arr_newsletterParams[3]);
		}
	}
	
	$('#newsletter-box-input-email').blur(function() {
		checkEmail('newsletter-box-input-email', 'newsletter-box-container-email');
	});
	
	function newsletterBoxRequest(type, param1, param2)
	{
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: 'http://'+SERVER_NAME+'/newsletter/'+type+'/'+param1+'/'+param2+'?language='+LANGUAGE,
			success: function(result){
				$('#newsletter-box-input').css('display', 'none');
				$('#newsletter-box-msg').css('display', 'block');
				$('#newsletter-box-msg').html(result.content);
			}
		});
	}
	
	$('#newsletter-box-subscribe').click(function() {
		if (checkEmail('newsletter-box-input-email', 'newsletter-box-container-email')) {
			var contactEmail = $('#newsletter-box-input-email').val();
			newsletterBoxRequest('subscribe', 'request', contactEmail);
		}
	});
	
	$('#newsletter-box-unsubscribe').click(function() {
		if (checkEmail('newsletter-box-input-email', 'newsletter-box-container-email')) {
			var contactEmail = $('#newsletter-box-input-email').val();
			newsletterBoxRequest('unsubscribe', 'request', contactEmail);
		}
	});
	/*==========================================================================
				* FREECALL BOX *
	==========================================================================*/
	$('#header_bt_freecallback').click(function() {
		thereCanBeOnlyOne.openElement('freecallBox');
		return(false);
	});
	$('#freecall-box-close').click(function() {
		thereCanBeOnlyOne.closeElement('freecallBox');
	});
	
	$('#freecall-box-input-phone').blur(function() {
		checkNoEmpty('freecall-box-input-phone', 'freecall-box-container-phone');
	});
	
	$('#freecall-box-input-name').blur(function() {
		checkNoEmpty('freecall-box-input-name', 'freecall-box-container-name');
	});
	
	$('#freecall-box-send').click(function() {
		var senderName = $('#freecall-box-input-name').val();
		var senderPhone = $('#freecall-box-input-phone').val();
		if (checkNoEmpty('freecall-box-input-name', 'freecall-box-container-name') && checkNoEmpty('freecall-box-input-phone', 'freecall-box-container-phone')) {
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: 'http://'+SERVER_NAME+'/freecall/send?language='+LANGUAGE,
				data: 'senderName='+senderName+'&senderPhone='+senderPhone+'&language='+LANGUAGE,
				success: function(result){
					$('#freecall-box-input').css('display', 'none');
					$('#freecall-box-msg').css('display', 'block');
					$('#freecall-box-msg').html(result.content);
				}
			});
		}
	});
});
