$(document).ready(function() {
	
	$('.tipsyBtnS').tipsy({
		gravity: 's',
		opacity: '0.8',
		html: true
	});
	
	$('.tipsyBtnN').tipsy({
		gravity: 'n',
		opacity: '0.8',
		html: true
	});
	
	$('.tipsyBtnE').tipsy({
		gravity: 'e',
		opacity: '0.8',
		html: true
	});
	
	
	$('.cbImg').colorbox({
		rel: 'gallery'
	});
	
	// tabs
	$('.tabs div').click( function() {
		// removes current class
		$('.tabs .current').attr('class','button');
		// sets new current
		$(this).removeClass('button');
		$(this).addClass('current');
		// show list
		$('.tabList').find('.list').hide();
		$('.tabList').find('.'+$(this).attr('rel')).fadeIn();
	});
	
	// submit form on enter for .submitBtn
	$('.submitBtn').keydown(function(event) {
		if (event.keyCode == '13') {
			$( $(this).attr('rel') ).submit();
		}
	});
	
	// focus on input field
	$('.txtInput').focus( function() {
		$('.txtInput').removeClass('focus');
		$(this).addClass('focus');
	});
	$('.txtInput').blur( function() {
		$(this).removeClass('focus');
	});
	
	
	// clear input fields
	$('.clearInput').focus( function() {
		if( $(this).val() == $(this).attr('rel') || $(this).val() == '') {
			$(this).val('');
			$(this).parent().removeClass('error');
		}
	});
	$('.clearInput').blur( function() {
		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('rel') );
		}
	});

	// clear input fields
	$('.passwordInput .text').focus( function() {
		$(this).parent().find('.text').hide();
		$(this).parent().find('.pass').show();
		//$('#target').focus();
		$(this).parent().find('.pass').focus();
	});
	$('.passwordInput .pass').blur( function() {
		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('rel') );
			$(this).parent().find('.text').show();
			$(this).parent().find('.pass').hide();
		}
	});
	$('.txtInput').keyup( function() {
		//alert('remove error');
		$(this).parent().removeClass('error');
	});
	
	// create an account 
	$('.widget.createAnAccount').find('.txtInput').focus( function() {
		$('.widget.createAnAccount').find('.passwordInput').slideDown('fast');
	});
	
});



function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function closeLightbox() {
	$.colorbox.close();
}

function showLightbox(box) {
	//alert('box: ' + box);
	
	var aBoxHeight = $(box).height();
	
	$.colorbox({
		inline: true, 
		href: box,
		transition: 'fade',
		onComplete:function() {
			var aReduce 		= $('#cboxClose').height();
			var aHeight			= $('#cboxMiddleLeft').height();
			var aNewHeight		= aHeight-aReduce;
			$('#cboxMiddleLeft').css('height',aNewHeight);
			$('#cboxContent').css('height',aNewHeight);
			$('#cboxMiddleRight').css('height',aNewHeight);	
			var aHeightInner	= $('#cboxLoadedContent').height();
			$('#cboxLoadedContent').css('height',aHeightInner+60);	
		}
	});
}


// GENERAL GUI FUNCTIONS

function registerUser() {
	//$('.registerForm').submit();
	alert('Change JS-function');
}

function submitForm(arg) {
	$(arg).submit();
}

function showRakebackResult() {
	showLightbox('.rakebackCalcRespons');
	calculateRakeback();
}


function editDeal(arg) {
	//showLightbox('.rakebackCalcRespons');
	//calculateRakeback();
	alert('edit: '+arg);
}


function unLockSettings(arg) {
	if(arg == 'theProfileForm') {
		$('.'+arg+' .inputWrapper .email').removeAttr('disabled');
		$('.'+arg+' .inputWrapper .email').removeClass('disabled');
		$('.'+arg+' .inputWrapper .email').addClass('enabled');
		$('.'+arg+' .submitBtn').removeClass('disabled');
	} 
	else if(arg == 'thePasswordForm') {
		$('.'+arg+' .inputWrapper .txtInput').removeAttr('disabled');
		$('.'+arg+' .inputWrapper .txtInput').removeClass('disabled');
		$('.'+arg+' .inputWrapper .txtInput').addClass('enabled');
		$('.'+arg+' .inputWrapper.currentPassword').removeClass('hidden');
		$('.'+arg+' .submitBtn').removeClass('disabled');		
	} 
	else {
		alert('unlock. '+arg);
	}	
}


function lockSupportForm() {
	//alert('lock');
	$('.theSupportForm .inputWrapper .user_name').attr('disabled','disabled');
	$('.theSupportForm .inputWrapper .user_name').addClass('disabled');
	
	$('.theSupportForm .inputWrapper .email').attr('disabled','disabled');
	$('.theSupportForm .inputWrapper .email').addClass('disabled');
	
	$('.theSupportForm .inputWrapper .message').attr('disabled','disabled');
	$('.theSupportForm .inputWrapper .message').addClass('disabled');
	
	$('.theSupportForm .btn').addClass('disabled');
	$('.theSupportForm .btn').attr('onclick','').unbind('click');
}

function unLockSupportForm() {
	//alert('unlock');
	$('.theSupportForm .inputWrapper.name .user_name').removeAttr('disabled');
	$('.theSupportForm .inputWrapper.name .user_name').removeClass('disabled');
	
	$('.theSupportForm .inputWrapper .email').removeAttr('disabled');
	$('.theSupportForm .inputWrapper .email').removeClass('disabled');
	
	$('.theSupportForm .inputWrapper .message').removeAttr('disabled');
	$('.theSupportForm .inputWrapper .message').removeClass('disabled');
	
	$('.theSupportForm .btn').removeClass('disabled');
	$('.theSupportForm .btn').click(function() { sendSupportMail(); });
}


function sendSupportMail() {
	lockSupportForm();
	//alert('click');
	var has_error	= false;
	
	// values for user name
	var user_name	= $('.theSupportForm .inputWrapper .user_name').val();
	var user_name_l	= $('.theSupportForm .inputWrapper .user_name').attr('rel');
	
	// values for user email
	var email 		= $('.theSupportForm .inputWrapper .email').val();
	var email_l 	= $('.theSupportForm .inputWrapper .email').attr('rel');
	
	// values for user message
	var message 	= $('.theSupportForm .inputWrapper .message').val();
	var message_l 	= $('.theSupportForm .inputWrapper .message').attr('rel');
	
	if( !user_name || user_name == user_name_l || $('.theSupportForm .inputWrapper .user_name').val().length < 4 ) {
		has_error = 'User name';
		$('.theSupportForm .user_name').addClass('error');
	} else {
		$('.theSupportForm .user_name').removeClass('error');
	}
	
	if( !email || email == email_l || $('.theSupportForm .inputWrapper .email').val().length < 8 ) {
		has_error = 'Email';
		$('.theSupportForm .email').addClass('error');
	} else {
		$('.theSupportForm .email').removeClass('error');
	}
	
	if( !message || $('.theSupportForm .inputWrapper .message').val().length < 8 ) {
		has_error = 'Message';
		$('.theSupportForm .message').addClass('error');
	} else {
		$('.theSupportForm .message').removeClass('error');
	}
	
	if( !has_error ) {
		// send mail if has_error is true
		$.post("/ajax/send_support_mail?user_name="+user_name+"&email="+email+"&message="+message, function(data) {
			//alert('Mail sent');
			$('.support .feedback').slideDown();
		});
	} else {
		alert('has_error: '+has_error+' : '+ user_name.lenght );
		unLockSupportForm();
	}
}







