jQuery(function(){
	
	aka.home.phone();
	aka.home.popups();
	aka.home.slideSignupIn();
	aka.home.slideSignupOut();
        aka.global.notificationFlash(); //JUST FOR BUG-REPORT
        aka.global.sendMessage(); //JUST FOR BUG-REPORT
        
});


aka = {};
aka.home = {};
aka.global = {};


aka.home.phone = function() {

	jQuery('#ProfilePhoneId').hide().attr('name', '');
	jQuery('#ProfileVendorId').show().unbind('change').bind('change', function(){
		jQuery('.signupPhones').hide().attr('name', '');
		jQuery('#ProfilePhoneId' + jQuery('#ProfileVendorId').val()).attr('name', 'data[Profile][phone_id]').show();
	}).trigger('change');	
	jQuery('.signupPhones option[value=' + jQuery('#ProfilePhoneId').val() + ']').attr('selected', 'selected');	
};

/**
 * handles the opening of popup
 *
 * @return false
 * @author Holger Höttecke
 **/
aka.home.popups = function() {

	jQuery('.popupLink').livequery('click', function(){
// 		jQuery.get(this.href, null, function(response, status, request){
//  			jQuery('#popup_inner').html(response);

 		jQuery('#popup_inner').load(this.href, null, function(response, status, request) {

			if( 'success' == status ) {
				jQuery('#popup').modal({'closeClass': 'cancel'});
				jQuery('#popup_inner').modal({'closeClass': 'cancel'});
				jQuery('#popup_agb_ok').unbind('click').click(function() {
					jQuery.modal.close();
				});
				
				jQuery('#popup_privacy_ok').unbind('click').click(function() {
					jQuery.modal.close();
				});

				aka.home.forgotAjax();
				aka.home.moreAjax();

			} else {
				alert('sorry, an error occured...');
			}

		});

		return false;
	});
};


/**
 * handles ajax-support for pw-vergessen
 * for password-forgotten-action in users_controller
 * @return false
 * @author ledentist
 **/
aka.home.forgotAjax = function() {

	var options = {
		type:	'POST',
		target: '#password_popup',
		success: function(data) {
			$('#password_popup').replaceWith(data);
			$('#popup_inner .cancel').click(function(){jQuery.modal.close()});
			aka.home.forgotAjax();
		}
	}
	
	jQuery('#forgot').unbind('submit').submit(function() {

			$("#forgot").ajaxSubmit(options);
			return false;
	});
};

/**
 * handles ajax-support for tos and privacy links
 * for actions in more-controller
 * @return false
 * @author ledentist
 **/
aka.home.moreAjax = function() {
	var options = {
		type:	'POST',
		target: '#dualLayoutContent',
		success: function(data) {
			$('#dualLayoutContent').replaceWith(data);
			$('#popup_inner .cancel').click(function(){jQuery.modal.close()});
			aka.home.moreAjax();
		}
	}
};


/**
 * slide in/out signup include 
 * in beta2go signup landing page
 * @return false
 * @author ledentist
 **/

aka.home.slideSignupIn = function() {
	$(document).ready(function() {
		$('.slideSignupIn').click(function() {
			$('.slideSignupIn').addClass('hideButton');
// 			$('.col_marginal_replacer').addClass('col_marginal');
			$('#signupContent').slideDown(1000);
		});
	});
};

aka.home.slideSignupOut = function() {
	$(document).ready(function() {
		$('.slideSignupOut').click(function() {
			$('#signupContent').slideUp(1000,function() {
				$('.slideSignupIn').removeClass('hideButton');
			});
		});
	});
};


/**
 * flashes the notification-moose
 *
 * @return false
 * @author ledentist
 **/
aka.global.notificationFlash = function() {
	jQuery('#notificationSuccessInner').ready(
		function() {
			jQuery('#notificationSuccessInner').
			animate( { backgroundColor: "#f4fff4" },200).
			animate( { backgroundColor: "#b6ffb6" },500).
			animate( { backgroundColor: "#f4fff4" },200);
		}
	);
	jQuery('#notificationErrorInner').ready(
		function() {
			jQuery('#notificationErrorInner').
			animate( { backgroundColor: "#fff3f8" },200).
			animate( { backgroundColor: "#ffb6e9" },500).
			animate( { backgroundColor: "#fff3f8" },200);
		}
	);
}


/**
 * sends the message-form with ajax and closes the popup
 *
 * @return false
 * @author Holger Höttecke
 **/
aka.global.sendMessage = function() {

	if( !$scope ) {
		var $scope = jQuery('body');
	}

	//aka.global.charcounters(jQuery('body'));
	//                     aka.global.selectRange(document.getElementById("form-message"),0,0);


	
        
	jQuery('#message_form').livequery('submit',function($scope) {
		var form=jQuery(this);
                jQuery.post(form.attr("action"), form.serialize(), function(data){
			jQuery.modal.close();
			$popupOpen=false;
			jQuery('#notificationSuccess, #notificationError, #separator').remove();
			jQuery('#container_content').before(data);
		});
		return false;
	});

	/*jQuery('#message_form').unbind('submit').submit(function($scope) {

	aka.global.enableSector();

		var options = {
			target: '#message_form',
			type:	'POST',
			success: function (data) {

					if( -1 != data.indexOf('messageWrite') ) {
						jQuery('#popup_inner').empty().append(data);
						aka.global.disableSector();
						aka.global.sendMessage();
					} else {
						$popupOpen=false;
						jQuery.modal.close();

						// redirect if href found
						jQuery('#popup_inner').empty().append(data);
						if(jQuery('#popup_inner .redirectAfterAjax').hasClass('redirectAfterAjax')) {
							location.href= jQuery('#popup_inner .redirectAfterAjax').attr('href');
							return;
						}

						jQuery('#notificationSuccess, #notificationError, #separator').remove();
						jQuery('#container_content').before(data);
					}

					jQuery('#popup_inner .cancel').unbind('click').bind('click', function(){
						$popupOpen=false;
						jQuery.modal.close();

					});
				}
			};
		jQuery('#message_form').ajaxSubmit(options);
		return false;
	});
	*/


	// lfh: select default text in popup textareas

	defaultText=jQuery('#popup_inner').find('.defaultText')[0];
	if (defaultText) {
		defaultText.focus();
		defaultText.select();
	}

};