var PW = {

    init:function () {
        Cufon.replace('h2', { fontFamily:'Museo Sans' });
        Cufon.replace('#nav-main', { fontFamily:'Museo Sans' });

        PW.quoteColorSetup();
        PW.colorChange('color-change');
        PW.colorChange('color-match');


        $('a[rel*=facebox]').facebox();

        $(document).bind('reveal.facebox', PW.setupFacebox);
    },

    setupFacebox:function () {


        var submitSendEmail = function () {
            var formData = $('#facebox #sendEmail').serialize();
            $('.popup .content').html('Sending...');

            $.ajax({
                       type:"POST",
                       url:"/quote/email",
                       data:formData,
                       success:function (data) {
                           $('.popup .content').html(data);
                       },
                       fail:function (data) {
                           $('.popup .content').html('Email could not be sent');
                       }
                   });

            return false;
        };

        $("#facebox #sendEmail").RSV({
                                         displayType:"display-html",
                                         errorTargetElementId:'facebox #rsvErrors',
                                         onCompleteHandler:submitSendEmail,
                                         rules:[
                                             "required,email,Please enter an email addrees",
                                             "valid_email,email,The email address you entered is invalid."
                                         ]
                                     });

        var submitSendOrderEmail = function () {
            var formData = $('#facebox #sendOrderEmail').serialize();
            $('.popup .content').html('Sending...');

            $.ajax({
                       type:"POST",
                       url:"/quote/orderemail",
                       data:formData,
                       success:function (data) {
                           $('.popup .content').html(data);
                       },
                       fail:function (data) {
                           $('.popup .content').html('Email could not be sent');
                       }
                   });

            return false;
        };

        $("#facebox #sendOrderEmail").RSV({
                                              displayType:"display-html",
                                              errorTargetElementId:'facebox #rsvErrors',
                                              onCompleteHandler:submitSendOrderEmail,
                                              rules:[
                                                  "required,email,Please enter an email addrees",
                                                  "valid_email,email,The email address you entered is invalid.",
                                                  "required,name,Please enter your name",
                                              ]
                                          });


        var submitGetQuote = function () {
            var id = $("#facebox #quoteId").val();
            window.location.href = '/quote/v/' + id;
            return false;
        };

        $("#facebox #getQuote").RSV({
                                        displayType:"display-html",
                                        errorTargetElementId:'facebox #rsvErrors',
                                        onCompleteHandler:submitGetQuote,
                                        rules:[
                                            "required,quoteId,Please enter a quote ID"
                                        ]
                                    });

    },

    colorChange:function (divId) {
        var f = function () {
            if ($(this).is(':checked'))
                $('#' + divId + ' select').removeAttr('disabled');
            else
                $('#' + divId + ' select').attr('disabled', true);
        };

        $('#' + divId + ' input').each(f);
        $('#' + divId + ' input').click(f);
    },

    quoteColorSetup:function () {
        if ($("#colors-number").length != 0) {
            $('#colors-number').change(function () {
                numColors = $('#colors-number').val();
                for (x = 1; x <= 6; x++) {
                    if (numColors >= x) {
                        $('#id_colorSelect' + x).show();
                        $('#label_colorSelect' + x).show();
                        color = $('#id_colorSelect' + x).val();
                        swatch = $('#id_colorSelect' + x).next();
                        swatch.css('backgroundColor', pmsColor[color]);
                        swatch.show();
                    } else {
                        $('#id_colorSelect' + x).hide();
                        $('#label_colorSelect' + x).hide();
                        $('#id_colorSelect' + x).next().hide();

                    }
                }
            });
            $('#colors-number').trigger('change');
        }
        $('div.colorSelect select').change(function () {
            if (this.options[this.selectedIndex].value == '') {
                this.nextSibling.style.backgroundColor = pmsColor['WHITE'];
            } else {
                this.nextSibling.style.backgroundColor = pmsColor[this.options[this.selectedIndex].value];
            }
        });
    }

}

$(document).ready(PW.init);


/* Helpers */

jQuery.fn.extend({

                     showMe:function () {
                         if (this.isHidden()) this.removeClass('hidden');

                         return this;
                     },

                     hideMe:function () {
                         if (!this.isHidden()) this.addClass('hidden');

                         return this
                     },

                     toggleMe:function () {
                         return this.toggleClass('hidden');
                     },

                     isHidden:function () {
                         if (this.hasClass('hidden'))
                             return true;
                         else
                             return false;
                     }

                 });
