
/**
 * Course related functions
 */

var course_page = {

    // --- ATTRIBUTES ---

    /**
     * The current course id
     */
    course_id: null,

    /**
     * The current section
     */
    section: null,

    /**
     * The id of "blind down" panel currently being displayed
     */
    current_panel: null,

    // --- COMMON ---

    /**
     * Constructor
     */
    initialize: function(course_id, nav_page)
    {
        // Nothing
        this.course_id = course_id;
        this.nav_page = nav_page;
    },

    /**
     * Display something
     */
    display: function ()
    {
        // establish what is to be displayed in the search results navigation
        this.saveCoursesParams({cid:this.course_id});

        this.courses_params = this.getCoursesParams();
        this.section = this.courses_params.section;

        var page = this.estCurrPage(this.courses_params.section);

        if (this.nav_page) // the page that the user is currently on is being changed
        {
            this.courses_params[this.section+'_page'] = this.nav_page;
            var params = {};
            params[this.section+'_page'] = this.nav_page;
            this.saveCoursesParams(params);              
        }
        else
        {
		        this.courses_params.page = page;
        }        

        this.refrCoursesNav(this.courses_params);
        this.refrCourseTools();
        this.refrForms();

        if ($('staf-form'))
        {
					  this.setVForms('staf-form', [
						    { field:'form_staf_email', v:'ve', fm:"Please enter a valid recipient email address." },
						    { field:'form_staf_message', v:'s', fm:"Please enter a message." }
					  ]);
        }
        if ($('contact-form'))
        {
						this.setVForms('contact-form', [
								{ field:'form_contact_name', v:'s',fm:"Please enter a contact name." },
								{ field:'form_contact_phone', v:'s',fm:"Please enter a phone number." },
								{ field:'form_contact_email', v:'ve',fm:"Please enter a valid email address." },
								{ field:'form_contact_enquiry', v:'s',fm:"Please enter your enquiry." }
						]);
        }
    },

    /**
     * Refresh the tools bar
     */
    refrCourseTools: function ()
    {
        if ($('course-tools-print')) Event.observe($('course-tools-print'), 'click', this.actCoursePrint.bind(this,'course-tools-print'));

        if ($('course-tools-staf'))
        {
            // send to a friend
            var a = $('course-tools-staf').select('a')[0];
            a.href = "javascript:void(0)";
            Event.observe(a, 'click', this.actCourseStaf.bind(this,'course-tools-staf'));
        }

        if ($('course-tools-contact'))
        {
            // contact us
            var a = $('course-tools-contact').select('a')[0];
            a.href = "javascript:void(0)";
            Event.observe(a, 'click', this.actCourseContact.bind(this,'course-tools-contact'));
        }
    },

    /**
     * Set up the forms in the page
     */
    refrForms: function ()
    {
        if ($('staf-form'))
        {
            // Send to a friend form
            $('staf-form').action = "javascript:void(0);";
            if ($('staf-form-submit')) Event.observe('staf-form-submit', 'click', this.actStafSubmit.bind(this));
        }
        if ($('contact-form'))
        {
            // Contact us form
            $('contact-form').action = "javascript:void(0)";
            if ($('contact-form-submit')) Event.observe('contact-form-submit', 'click', this.actContactSubmit.bind(this));
        }
    },

    /**
     * Refresh the message on the status pagel
     */
    refrStatusPanel: function (data)
    {
        if (!data || !$('status-message')) return false;
        $('status-message').className = "stat-"+data.msg_type;
        $('status-message').innerHTML = data.msg_text;
    },

      // --- LOADS ---

    /**
     * "Contact us" form submit load
     */
    _load_contact_submit: function ()
    {
        var loc = window.location.href;
        var parts = loc.split("?");
        // TODO: Implement fallback url
        return [parts[0], {action:'submit_contact',inline:1}, 'post'];
    },

    /**
     * "Contact us" form submit loaded
     */
    _loaded_contact_submit: function (r)
    {
        var resp = r.responseText;
        var data = eval(resp);

        if (!data) return false;

        // TODO: Not very intuitive, calling this here
        this.clearLoading('contact-form');

        var do_display = function (msg)
        {
            this.refrStatusPanel(msg);
            this.current_panel = {elem:'status-message'};
            Effect.BlindDown('status-message', { duration : 0.5 });
        }.bind(this, data[0]);

        // close any open panel
        if (this.current_panel)
        {
            Effect.BlindUp(this.current_panel.elem, { duration : 0.3 });
            if (this.current_panel.ctrl)
            {
                $(this.current_panel.ctrl).removeClassName('panel-down');
                $(this.current_panel.ctrl).addClassName('panel-up');
            }
            window.setTimeout(do_display.bind(this),300);
        }
        else
        {
            do_display();
        }
    },

    /**
     * "Send to a friend" form submit load
     */
    _load_staf_submit: function ()
    {
        var loc = window.location.href;
        var parts = loc.split("?");
        // TODO: Implement fallback url
        return [parts[0], {action:'staf_submit',inline:1}, 'post'];
    },

    /**
     * "Send to a friend" form submit loaded
     */
    _loaded_staf_submit: function (r)
    {
        var resp = r.responseText;
        var data = eval(resp);
        if (!data) return false;

        // TODO: Not very intuitive, calling this here
        this.clearLoading('staf-form');

        var display = function (msg)
        {
            this.refrStatusPanel(msg);
            this.current_panel = {elem:'status-message'};
            Effect.BlindDown('status-message', { duration : 0.5 });
        }.bind(this, data[0]);

        // close any open panel
        if (this.current_panel)
        {
            Effect.BlindUp(this.current_panel.elem, { duration : 0.3 });
            if (this.current_panel.ctrl)
            {
                $(this.current_panel.ctrl).removeClassName('panel-down');
                $(this.current_panel.ctrl).addClassName('panel-up');
            }
            window.setTimeout(display.bind(this),300);
        }
        else
        {
            display();
        }
    },

    // --- ACTIONS ---

    /**
     * Course results pagination previous
     */
    actNavPrevious: function (page)
    {
        var new_params = {};
        new_params[this.section+'_page'] = page;
        var page_params = this.getResultsParams(new_params);
        this.load('courses_nav_sub',page_params);
    },

    /**
     * Course results pagination next
     */
    actNavNext: function (page)
    {
        var new_params = {};
        new_params[this.section+'_page'] = page;
        var page_params = this.getResultsParams(new_params);
        this.load('courses_nav_sub',page_params);
    },

    /**
     * Go to page dropdown click
     */
    actPageDrop: function (elem_id)
    {
        if (!$(elem_id)) return false;
        var page = $(elem_id).options[$(elem_id).selectedIndex].value;
        var new_params = {};
        new_params[this.section+'_page'] = page;
        var page_params = this.getResultsParams(new_params);
        this.load('courses_nav_sub',page_params);
    },

    /**
     * Filter courses by subject dropdown click
     */
    actSubjectDrop: function (elem_id)
    {
        if (!$(elem_id)) return false;
        var val = $(elem_id).options[$(elem_id).selectedIndex].value;
        var page_params = this.courses_params;
        page_params.fb_subject = val;
        page_params[this.section+'_page'] = 1;
        this.load('courses_nav_sub',page_params);
    },

    /**
     * Sort courses dropdown click
     */
    actSortDrop: function (elem_id)
    {
        if (!$(elem_id)) return false;
        var val = $(elem_id).options[$(elem_id).selectedIndex].value;
        var params = this.getResultsParams({sort:val,page:1});
        this.load('courses_nav_sub',params);
    },

    /**
     * Print course button click
     */
    actCoursePrint: function (elem_id)
    {
        if (!$(elem_id)) return false;
        window.print();
    },

    /**
     * 'Contact us' course button click
     */
    actCourseContact: function (elem_id)
    {
        if (!$(elem_id) || !$('contact-form-cont')) return false;
        if (this.current_panel && this.current_panel.elem == 'contact-form-cont')
        {
            // close the form
            this.current_panel = null;
            Effect.BlindUp('contact-form-cont', { duration : 0.5 });
            $(elem_id).removeClassName('panel-down');
            $(elem_id).addClassName('panel-up');
        }
        else
        {
            var openNew = function ()
            {
                // open the form
                this.current_panel = {elem:'contact-form-cont',ctrl:elem_id};
                Effect.BlindDown('contact-form-cont', { duration : 0.5 });
                $(elem_id).removeClassName('panel-up');
                $(elem_id).addClassName('panel-down');
            }.bind(this);

            // close any open panel
            if (this.current_panel)
            {
                Effect.BlindUp(this.current_panel.elem, { duration : 0.3 });
                if (this.current_panel.ctrl)
                {
                    $(this.current_panel.ctrl).removeClassName('panel-down');
                    $(this.current_panel.ctrl).addClassName('panel-up');
                }
                window.setTimeout(openNew.bind(this),300);
            }
            else
            {
                openNew();
            }
        }
    },

    /**
     * 'Send to a friend' course button click
     */
    actCourseStaf: function (elem_id)
    {
        if (!$(elem_id) || !$('staf-form-cont')) return false;
        if (this.current_panel && this.current_panel.elem == 'staf-form-cont')
        {
            // close the form
            this.current_panel = null;
            Effect.BlindUp('staf-form-cont', { duration : 0.5 });
            $(elem_id).removeClassName('panel-down');
            $(elem_id).addClassName('panel-up');
        }
        else
        {
            var openNew = function ()
            {
                // open the form
                this.current_panel = {elem:'staf-form-cont'};
                Effect.BlindDown('staf-form-cont', { duration : 0.5 });
                $(elem_id).removeClassName('panel-up');
                $(elem_id).addClassName('panel-down');
            }.bind(this);

            // close any open panel
            if (this.current_panel)
            {
                Effect.BlindUp(this.current_panel.elem, { duration : 0.3 });
                if (this.current_panel.ctrl)
                {
                    $(this.current_panel.ctrl).removeClassName('panel-down');
                    $(this.current_panel.ctrl).addClassName('panel-up');
                }
                window.setTimeout(openNew.bind(this),300);
            }
            else
            {
                openNew();
            }
        }
    },

    /**
     * Submit of a 'send to a friend' form
     */
    actStafSubmit: function ()
    {
        var valid = this.formValidate($('staf-form'));
        if (valid)
        {
            this.setLoading('staf-form','Processing ...','stat-button');
            this.load('staf_submit', $('staf-form').serialize(true));
        }
    },

    /**
     * Submit of a contact form
     */
    actContactSubmit: function ()
    {
        var valid = this.formValidate($('contact-form'));
        if (valid)
        {
            this.setLoading('contact-form','Processing ...','stat-button');
            this.load('contact_submit', $('contact-form').serialize(true));
        }
    }

};

s1JS.classes.Learning.Corporate.course = Class.create(s1JS.classes.Learning.Corporate.page, course_page);
