﻿var page;
var activePackage;
var activeNutrition;
var activeSample;
var activeTip = null;
var activeFeedback = null;
var activePackage = null;
window.addEvent('domready', function() {
    page = window.location.toString() || "";
    page = page.substring(page.lastIndexOf("/") + 1);

    if (page == '') page = 'index.html';

    // attach home link function
    var link = $('homeLink');

    if (link != null) {
        link.addEvent('click', function() {
            window.location = 'index.html';
        });
    }

    switch (page) {
        case "packages.html": // FEES & ACKAGES
            {/*
                // attach mouse events to frequency buttons
                $$(".button_pack").each(function(item, index) {
                    item.addEvents({
                        'mouseenter': function() {
                            if (activePackage != this.id.substring(this.id.lastIndexOf("_") + 1).toInt()) {
                                this.addClass("button_pack_over");
                            }
                        },
                        'mouseleave': function() {
                            if (activePackage != item.id.substring(item.id.lastIndexOf("_") + 1).toInt()) {
                                item.removeClass("button_pack_over");
                            }
                        },
                        'click': function() {
                            var oldID = activePackage;
                            activePackage = item.id.substring(item.id.lastIndexOf("_") + 1).toInt();
                            showPackage(oldID, activePackage);
                        }
                    });
                });
                // hide/show package details
                activePackage = 1;
                showPackage(null, activePackage);
*/

                // add accordion style effect

                $$(".wrapper").each(function(item, index) {
                    // wrap the item's next sibling in a div and add a Morph animation to that div
                    //                    var next = item.getNext();
                    //                    var wrapper = new Element('div', { 'class': 'wrapper', 'styles': { /*'overflow': 'hidden' */} });
                    //                    wrapper.wraps(next);
                    item.setStyles({ 'height': '115px', 'overflow': 'hidden'/*, 'display': 'none'*/ });
                    item.set('morph', { duration: 'short', transition: 'sine:in:out', link: 'ignore' });
                    //wrapper.morph({ 'height': next.getStyle('height').toInt() });

                    item.addEvents({
                        'click': function() {
                            //item.addClass("blue");

                            var wrap = item.getNext();
                            /*item.removeClass('white');*/

                            if (activePackage == item) // user has clicked on an already open tip, so we have to close it and set activePackage to null
                            {
                                activePackage.get('morph').start({ 'height': '115px'/*, 'opacity': ['1', '0']*/ }).chain(function() {
                                    /*activeTip*//*this.getNext().setStyles({ 'display': 'none' });*/
                                } .bind(activePackage));
                                activePackage = null;
                                /*item.removeClass('white');*/
                            }
                            else if (activePackage != null) { // user has clicked on a closed tip, so we close the active one, open the new one and set activePackage to it
                                /*activePackage.removeClass('white');
                                item.addClass('white');*/
                                activePackage.get('morph').start({ 'height': '115px'/*, 'opacity': ['1', '0']*/ }).chain(function() {
                                    /*activePackage.getNext().setStyles({ 'display': 'none' });*/
                                    /*wrap.setStyles({ 'display': 'block' });*/
                                    item.get('morph').start({ 'height': '400px'/*wrap.getFirst().getSize().y*//*, 'opacity': ['0', '1']*/ }); //getDimensions(true).totalHeight()
                                    activePackage = item;
                                });
                            }
                            else { // activePackage == null so open the clicked tip, set it as active
                                /*wrap.setStyles({ 'display': 'block' });*/
                                item.get('morph').start({ 'height': '400px'/*wrap.getFirst().getSize().y*//*, 'opacity': ['0', '1']*/ });
                                activePackage = item;
                                /*item.addClass('white');*/
                            }
                        },
                        'mouseenter': function() {
                            if (item != activePackage) {
                                //item.removeClass('blue');
                                item.addClass('packageOver');

                            }
                        },
                        'mouseleave': function() {
                            if (item != activePackage)
                                item.removeClass('packageOver');
                        }
                    });
                });

                break;
            }
        case "nutrition.html": // NUTRITION PAGE
            {
                // attach mouse events to frequency buttons
                $$(".button_nut").each(function(item, index) {
                    item.addEvents({
                        'mouseenter': function() {
                            if (activeNutrition != item.id.substring(item.id.lastIndexOf("_") + 1).toInt()) {
                                item.addClass("button_nut_over");
                            }
                        },
                        'mouseleave': function() {
                            if (activeNutrition != item.id.substring(item.id.lastIndexOf("_") + 1).toInt()) {
                                item.removeClass("button_nut_over");
                            }
                        },
                        'click': function() {
                            var oldID = activeNutrition;
                            activeNutrition = item.id.substring(item.id.lastIndexOf("_") + 1).toInt();
                            showNutrition(oldID, activeNutrition);
                        }
                    });
                });
                activeNutrition = 1;
                showNutrition(null, activeNutrition);

                // attach diet tips accordion mouse events

                $$(".diet_tip").each(function(item, index) {
                    // wrap the item's next sibling in a div and add a Morph animation to that div
                    var next = item.getNext();
                    var wrapper = new Element('div', { 'class': 'wrapper', 'styles': { /*'overflow': 'hidden' */} });
                    wrapper.wraps(next);
                    wrapper.setStyles({ 'height': '1px', 'overflow': 'hidden', 'display': 'none' });
                    wrapper.set('morph', { duration: 'short', transition: 'sine:in:out', link: 'ignore' });
                    //wrapper.morph({ 'height': next.getStyle('height').toInt() });

                    item.addEvents({
                        'click': function() {
                            //item.addClass("blue");

                            var wrap = item.getNext();
                            item.removeClass('white');

                            if (activeTip == item) // user has clicked on an already open tip, so we have to close it and set activeTip to null
                            {
                                activeTip.getNext().get('morph').start({ 'height': '1px', 'opacity': ['1', '0'] }).chain(function() {
                                    /*activeTip*/this.getNext().setStyles({ 'display': 'none' });
                                } .bind(activeTip));
                                activeTip = null;
                                item.removeClass('white');
                            }
                            else if (activeTip != null) { // user has clicked on a closed tip, so we close the active one, open the new one and set activeTip to it
                                activeTip.removeClass('white');
                                item.addClass('white');
                                activeTip.getNext().get('morph').start({ 'height': '1px', 'opacity': ['1', '0'] }).chain(function() {
                                    activeTip.getNext().setStyles({ 'display': 'none' });
                                    wrap.setStyles({ 'display': 'block' });
                                    wrap.get('morph').start({ 'height': wrap.getFirst().getSize().y, 'opacity': ['0', '1'] }); //getDimensions(true).totalHeight()
                                    activeTip = item;
                                });
                            }
                            else { // activeTip = null so open the clicked tip, set it as active
                                wrap.setStyles({ 'display': 'block' });
                                wrap.get('morph').start({ 'height': wrap.getFirst().getSize().y, 'opacity': ['0', '1'] });
                                activeTip = item;
                                item.addClass('white');
                            }
                        },
                        'mouseenter': function() {
                            if (item != activeTip) {
                                //item.removeClass('blue');
                                item.addClass('white');
                            }
                        },
                        'mouseleave': function() {
                            if (item != activeTip)
                                item.removeClass('white');
                        }
                    });
                });

                break;
            }
        case "fitness.html": // FITNESS SAMPLES PAGE
            {
                // add alternating row colors to tables on this page
                $$("table.samples").each(function(table, tIndex) {
                    table.getElements("tr").each(function(row, rIndex) {
                        if (rIndex != 0) {
                            if (rIndex % 2 == 0) row.addClass("evenRow");
                            else row.addClass("oddRow");
                        }
                        else {
                            //set table header style
                            row.setStyles({ "border": "solid 1px white", "border-width": "1px 0px" });
                        }
                    });
                });

                // add link effects to level of fitness links at the top of the page
                $$("#sample_link_1", "#sample_link_2", "#sample_link_3").each(function(link, index) {

                    link.addEvents({
                        'mouseenter': function() {
                            if (activeSample - 1 != index) link.addClass("link1");
                        },
                        'mouseleave': function() {
                            if (activeSample - 1 != index) link.removeClass("link1");
                        },
                        'click': function() {
                            var oldID = activeSample;
                            activeSample = link.id.substring(link.id.lastIndexOf("_") + 1).toInt();
                            if (oldID != activeSample) {
                                showSample(oldID, activeSample);
                            }
                        }
                    });
                });
                //var id = link.id.substring(link.id.lastIndexOf('_') + 1).toInt();
                $$("#sample_1", "#sample_2", "#sample_3").each(function(sample, index) {
                    sample.setStyle("opacity", "0"); //if (index != id - 1)
                });

                activeSample = 1;
                showSample(null, activeSample)
                break;
            }
        case "contactus.html":
            {
                // add checkbox selection mouse effects

                $$(".checkList li").each(function(item, index) {
                    item.addEvents({
                        'mouseenter': function() {
                            item.addClass('checkListOver');
                        },
                        'mouseleave': function() {
                            item.removeClass('checkListOver');
                        },
                        'click': function() {
                            var isChecked = item.retrieve('checked');
                            if (isChecked != null) {
                                item.eliminate('checked');
                                item.setStyles({ "background": "url(images_new/check1.jpg) no-repeat left 3px" });
                            }
                            else {
                                item.store('checked', "true");
                                item.setStyles({ "background": "url(images_new/check2.jpg) no-repeat left 3px" });
                            }
                        }
                    });
                });

                // add send and clear events to buttons
                $("btnSend").addEvent('click', function(e) {
                    sendEmail();
                });

                $("btnClear").addEvent('click', function(e) {
                    e.stop();
                    clearFields();
                });
                break;
            }
        case "testimonials.html": // TESTIMONIALS PAGE
            {
                //send event
                $('btnSend').addEvent('click', function(e) {
                    e.stop();
                    sendFeedback();
                });

                // attach mouse events to testimonial boxes
                $$(".tip").each(function(item, index) {
                    /*if (index % 2 == 0) {
                    item.setStyle('background-color', "#204564");
                    item.setStyle('color', "#B4BFC9");
                    }*/
                    item.addEvents({
                        'mouseenter': function() {
                            item.setStyles({ 'background-color': '#204564' });
                        },
                        'mouseleave': function() {
                            item.setStyles({ 'background-color': '#163A5A' });
                        },
                        'click': function() {
                            showFeedback(this);
                        } .bind(item)
                    });
                });
                break;
            }

        case "index.html":
            {
                // attach mouse click event to quote ad on home page
                $('home_ad').addEvents({
                    'mouseenter': function() {
                        $('home_ad').addClass('adOver');
                    },
                    'mouseleave': function() {
                        $('home_ad').removeClass('adOver');
                    },
                    'click': function() {
                        window.location = "contactus.html"
                    }
                });
                break;
            }
        case "jobs.html":
            {
                // hide job description
                $('jobDetail').setStyle('visibility', 'hidden');

                // attach mouse click event to quote ad on home page
                $('btnJob').addEvents({
                    'mouseenter': function() {
                        $('btnJob').addClass('blue');
                    },
                    'mouseleave': function() {
                        $('btnJob').removeClass('blue');
                    },
                    'click': function() {
                        $('jobDetail').fade('in');
                    }
                });
                break;
            }

    }
});

function sendFeedback() {
    
    new Form.Request($('frmFeedback'), $('update'), {
        requestOptions: {
            spinnerOptions: {
                message: 'Sending...'
            }
        },
        onSend: function() { $('btnSend').set('disabled', true); },
        onFailure: function() { $('btnSend').set('disabled', false); $('update').set('html', 'Oops, sending your message failed, please try again.'); },
        onComplete: function() { $('btnSend').set('disabled', false); $('update').set('html', 'Thank You.'); },
        extraData: { 'type': 'feedback', 'message': $('message').value },
        resetForm: false
    }).send();
}

function showFeedback(container) {
    var content2 = $(container).getElement("div");

    if (activeFeedback != null) activeFeedback.destroy();
    
    var copy = container.clone(true);
    activeFeedback = copy;
    copy.setStyles({
        'height': '280px',
        'width': '360px',
        'font-size': '13px',
        'position': 'absolute',
        'overflow-y': 'auto',
        'opacity': '0'
    });

    var temp = copy.getElement('.gold');
    if (temp != null) temp.destroy();
    
    copy.addEvent('click', function() { copy.destroy(); });

    var div = new Element('div', { 'text': 'click to close', 'class': 'gold', 'styles': { 'float': 'right'} });
    copy.grab(div);
    
    $('content').grab(copy);

    new Element('br').inject(copy.getElement('.heading'), 'after');
    
    copy.position();
    copy.fade('in');
}

function getInterests() {
    var val;
    var res = "";
    $$(".checkList li").each(function(item, index) {
        val = item.retrieve('checked');
        if (val != null) res += item.get('text') + "\n";
    });
    return res;
}

function sendEmail() {
    var interests = getInterests();

    var formValidator = new Form.Validator($('emailForm'));
//    var valName = new InputValidator('validate-alpha');
//    var valEmail = new InputValidator('validate-email');
//    var valCity = new InputValidator('validate-alpha');

//    var valMessage = new Inputvalidator('text', {});
    //
    formValidator.add('name', {
        errorMsg: "You name has to be alphanumeric and can contain appostrophes and spaces",
        test: function(field) {
            return field.get('value').test("^[A-Za-z ']+$");
        }
    });

    formValidator.add('city', {
        errorMsg: "The city field has to be alpha-numeric and can contain spaces",
        test: function(field) {
            return field.get('value').test("^[A-Za-z0-9 ]+$");
        }
    });
    
    //validate form
    if (!formValidator.validate()) {
        alert("Make sure all fields are filled out and that your email address is valid.");

    }
    else {

        // submit form
        new Form.Request($('emailForm'), $('update'), {
            requestOptions: {
                spinnerOptions: {
                    message: 'Sending...'
                }
            },
            onSend: function() { $('btnSend').set('disabled', true) },
            onFailure: function() { $('btnSend').set('disabled', false); alert('Oops, sending your message failed, please try again.'); },
            onComplete: function() { $('btnSend').set('disabled', false) },
            extraData: { 'interests': interests, 'message': $('message').value },
            resetForm: false
        }).send();
    }
}

function clearFields() {
    $('name').value = "";
    $('email').value = "";
    $('city').value = "";
    $('message').value = "";
    $$(".checkList li").each(function(item, index) {
        val = item.retrieve('checked');
        if (val != null) {
            item.eliminate('checked');
            item.setStyles({ "background": "url(images_new/check1.jpg) no-repeat left 3px" });
        } 
    });
}

function showSample(oldID, sampleID) {
    if ($('sample_' + sampleID) == null) return;
    if (oldID != null) $("sample_link_" + oldID).removeClass("link1");
    $('sample_link_' + sampleID).addClass("link1");
    // fade out the old
    var fxOut = null;
    if (oldID != null) { fxOut = new Fx.Morph('sample_' + oldID, { duration: '350', transition: Fx.Transitions.Sine.easeOut }); }
    
    // fade in the new
    var fxIn = new Fx.Morph('sample_' + sampleID, { duration: '350', transition: Fx.Transitions.Sine.easeOut });
    
    if (fxOut != null) {
        fxOut.start({
            'opacity': '0',
            'top': '7'
        }).chain(function() { 
            fxIn.start({
                'opacity': '1',
                'top': [-7, 0]
            });    
        });
    }
    else {
        fxIn.start({
            'opacity': '1',
            'top': [-7, 0]
        });   
    }

    
    
    
}

function showNutrition(oldID, nutritionID) {
    if (oldID != null) $("nut_link_" + oldID).removeClass("button_nut_click");
    if (oldID != null) $("nut_link_" + oldID).removeClass("button_nut_over");
    $("nut_link_" + nutritionID).addClass("button_nut_over");
    $("nut_link_" + nutritionID).addClass("button_nut_click");

    $$("#nut_1", "#nut_2", "#nut_3", "#nut_4", "#nut_5").each(function(item, index) {
        if (index != nutritionID - 1) {
            item.setStyles({
                "display": "none"
            });
        }
        else {
            item.setStyles({ 'opacity': '0', 'display': 'block' });
            item.fade('in');
        }
    });
}

function showPackage(oldID, packageID) {
    if (oldID != null) $("package_link_" + oldID).removeClass("button_pack_click");
    if (oldID != null) $("package_link_" + oldID).removeClass("button_pack_over");
    $("package_link_" + packageID).addClass("button_pack_over");
    $("package_link_" + packageID).addClass("button_pack_click");

    $$("#package_1", "#package_2", "#package_3", "#package_4").each(function(item, index) {
        if (index != packageID - 1) {
            item.setStyles({
                "display": "none"
            });
        }
        else {
            item.setStyles({ 'opacity': '0', 'display': 'block' });
            item.fade('in');
        }
    });
    
    
    
}

