jQuery(function(){
	
	aka.home.phone();
	aka.home.popups();
	aka.home.slideSignupIn();
	aka.home.slideSignupOut();
    aka.global.notificationFlash();
    aka.global.sendMessage();

});


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('#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() {
	
	// hide youtube iframe (z-fighting)
	$('.col_center iframe').css('display','none');
		
	var options = {
		type:	'POST',
		target: '#password_popup',
		success: function(data) {
			$('#password_popup').replaceWith(data);
			$('#popup_inner .cancel').click(function(){
				jQuery.modal.close();
				
				// redisplay youtube iframe (z-fighting)
				$('.col_center iframe').css('display','block');
			});
			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');
	}
        
	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;
	});

	// lfh: select default text in popup textareas

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

};
