$(function(){
    
    SS.setLabelText();
    SS.setContactLink();  
    SS.setContactForm(); 
    SS.setListForm();   

    if ($('#toggler').length) {
        $('#toggler').toggle(
            function () {
                //$('#content').css('opacity', 0.85);
                $('#content').css('background', 'rgba(0, 0, 0, 0.9)');
                return false;
            },
            function () {
                //$('#content').css('opacity', 0.75);
                $('#content').css('background', 'rgba(0, 0, 0, 0.8)');
                return false;
            },
            function () {
                //$('#content').css('opacity', 0.65);
                $('#content').css('background', 'rgba(0, 0, 0, 0.7)');
                return false;
            },
            function () {
                //$('#content').css('opacity', 1);
                $('#content').css('background', 'rgba(0, 0, 0, 0.6)');
                return false;
            },
            function () {
                //$('#content').css('opacity', 1);
                $('#content').css('background', 'rgba(0, 0, 0, 0.5)');
                return false;
            },
            function () {
                //$('#content').css('opacity', 1);
                $('#content').css('background', 'rgba(0, 0, 0, 1)');
                return false;
            }          
        );
    }

});

var SS = {
    
    form: null,
    
    lform: null,
    
    formDirty: false,
    
    form2Dirty: false,
    
    overflow: false,
    
    setListForm: function(){
        SS.lform = $('#lf').html();
        SS.initListForm();
    },
    
    setContactLink: function(){
        $('#contact-link').toggle(function(){
            var height = $('#contact-pane').height();
            var width = $('#contact-pane').width();
            if ((height + (height * 0.5)  > $(window).height()) || (width + (width * 0.5)  > $(window).width())) {
                $('body').css('overflow', 'auto');
                SS.overflow = true;
            }
            $('#contact-pane').fadeIn(300, function(){
                $('#contact-pane .content').fadeIn();
                $('#contact-pane .close').click(function(){
                    $('#contact-link').trigger('click');
                });
            });
            return false;
        },
        function(){
            $('#contact-pane .content').hide();
            $('#contact-pane').hide();
            $('#contact-pane .close').unbind('click');
            if (!SS.formDirty) {
                $('#cmessage').show();
                $('#cform').show();
                SS.initContactForm();   
            }
            $('body').css('overflow', 'hidden');
            SS.overflow = false;
            return false;
        });
    },
    
    setContactForm: function(){
        SS.form = $('#cform').html();
        SS.initContactForm();
    },
    
    /**
     * contact form handler
     */
    initContactForm: function(){
        $('#contactForm').submit(function() { 
            $(this).ajaxSubmit({
                beforeSubmit: function(){
                    $('#cform').hide();
                    $('#cmessage').html("<div id='loading'></div>");
                    SS.formDirty = true;
                },
                success: function(responseText, statusText){
                    var parts = responseText.split("|");
                    $('#cmessage').html(parts[0]);
                    if (parts[1]) {
                        $('#cform').html(parts[1]);
                        $('#cform').show();
                        $('#contactForm').show();
                        SS.initContactForm();
                    } else {
                        $('#cform').html(SS.form);
                        SS.formDirty = false;
                    }
                }
            }); 
            return false; 
        }); 
    },
    
    
    /**
     * toggle 'your email' text
     */
    setLabelText: function(){
        var labelText = 'Your Email';
        $('#email-address').val(labelText);
        $('#label-email-address').text(" ");
        $('#email-address').focus(function(){
            $(this).css('color', '#333');
            if (this.value == 'Your Email') {
                this.value = "";
            }
        });
        $('#email-address').blur(function(){
            $(this).css('color', '#999');
            if (!this.value) {
                this.value = 'Your Email';
            }
        });
    },
    
    initListForm: function(){
        $('#listForm').submit(function() { 
            $(this).ajaxSubmit({
                beforeSubmit: function(){
                    $('#list-pane').fadeIn(300, function(){
                        $('#list-pane .content').fadeIn();
                        $('#list-pane .close').click(function(){
                            $('#listForm').remove();
                            $('#lf').html(SS.lform);
                            SS.initListForm();
                            $('#list-pane').hide();
                            $('#list-pane .close').unbind('click');
                        });
                    });
                    $('#message2').html("<div id='loading'></div>");
                    SS.form2Dirty = true;
                    $('#listForm').remove();
                },
                success: function(responseText, statusText){
                    var parts = responseText.split("|");
                    $('#message2').html(parts[0]);
                    if (parts[1]) {
                        $('#form2').html(parts[1]);
                        $('#form2').show();
                        $('#listForm').show();
                        SS.initListForm();
                    } else {
                        SS.form2Dirty = false;
                    }
                }
            }); 
            return false; 
        }); 
    }
    
};
