﻿/**
 * static object that handles page logic for static pagess
 * @class
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var StaticMain = function($){
    
    /**
     * @namespace Private methods and variables
     */
    var priv = {
    
        /**
         * Set up the javascript functionality for the brochure request form
         * @private
         */
        setupBrochureForm: function () {
            $("#mag-street").before("<select id=\"dd-mag-street\"></select>");

            var formValidator = new Validator.Wrappers.FormValidation($('form#order-brochure'), ['brochure']);
			$("#mag-agree").bind("click", function(){
				priv.toggleEmailRequired(formValidator);
			});
        },

        /**
         * Set up the javascript functionality for the cancellation form
         * @private
         */
        setupCancellationForm: function () {
            $("#vraagallereizigers").hide();
            $("#antwoordallereizigers").hide();
            
            $("#sendCancellationContent table input[name=allereizigers]").bind("click", function () {
                Cancellation.toggleTravelers(this);
            });
            
            $("#sendCancellationContent table input[type=button]").bind("click", function () {
                Cancellation.send();
            });
        },
        
        /**
         * Set up the javascript functionality for the company info ('about sunweb') page
         * @private
         */
        setupCompanyInfo: function () {
            $('#sendEmailLink').bind('click', function(){
                Utils.newWindow(this, 800, 640);
                return false;
            });
        },
        
        /**
         * Setup javascript functionality for the group travel form
         * @private
         */
        setupGroupTravelForm: function () {
            $(".form-list input[type=button]").bind("click", function () {
                GroupTravel.submitForm();
            });
            
            $("form[name=formulier] input").bind('change', function () {
                return GroupTravel.Check(this);
            });
            
            $("form[name=formulier] select").bind('change', function () {
                if ($(this).attr("name") == "depdatum_maand" || $(this).attr("name") == "depdatum_jaar") {
                    return GroupTravel.Check($("form[name=formulier] select[name=depdatum_dag]").get(0));
                }

                return GroupTravel.Check(this);
            });
        },
        
        /**
         * Set up the javascript functionality for the prebooking form
         * @private
         */
        setupPrebookingForm: function () {
            $("#sendprebookContent input[type=button]").bind("click", function () {
                Prebooking.send();
            });
            $("#prebook-nrpersons").bind("change", function () {
                Prebooking.showCompanions($(this).val());
            });
        },
		
		/**
		 * Toggle whether the email input field is required
		 * @param {FormValidation} formValidator The form validator object that can be used to toggle a certain field to required
		 * @private
		 */
		toggleEmailRequired: function (formValidator) {
            if ($('#mag-agree').attr("checked")) {
                formValidator.setRequired("email");
                $('#mag-emailmandatorysymbol').show();
            } else {
                formValidator.setNotRequired("email");
                $('#mag-emailmandatorysymbol').hide();
                var msg = $("#mag-email").next(".errorText").text();
                if ($(".form-close .errorText").text() == msg) {
                    $(".form-close .errorText").hide();
                }
            }
		}
    };
    
    /**
     * Map pages to their respective setup methods
     */
    var config = {
        'html-bedrijfsinfo': priv.setupCompanyInfo,
        'html-brochure': priv.setupBrochureForm,
        'static-prebooking': priv.setupPrebookingForm,
        'static-grouptravel': priv.setupGroupTravelForm,
        'static-cancellationform': priv.setupCancellationForm
    };

    /** @scope StaticMain */
    return {
    
        /**
         * Initializes the logic for the current page
         * to be called on $(document).ready
         */
        OnReady: function(){
            // Execute the correct functions for this page, defined in the config variable, using the body class
            var pageFunc = config[$('body').attr('class')];
            if (typeof(pageFunc) != "undefined" && pageFunc != null) {
                pageFunc();
            }
        },
        
        openWindow: function(url,w,h) {
	        window.open(url,'info', 'width='+w+',height='+h+',scrollbars=yes');
        }
    };
}(jQuery);

