/*
 * SimpleModal modal Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2008 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: modal.js 132 2008-05-23 16:05:17Z emartin24 $
 *
 */
    
         
$(document).ready(function () {
	// preload images
	var img = ['cancel.png','form_bottom.gif','form_top.gif','form_top_ie.gif','loading.gif','send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '/Content/Images/modal/' + this;
	});
});

var modal = {
    message: null,
    open: function(dialog) {
        // add padding to the buttons in firefox/mozilla
        if ($.browser.mozilla) {
            $('#modal-container .modal-button').css({
                'padding-bottom': '2px'
            });
        }
        // input field font size
        if ($.browser.safari) {
            $('#modal-container .modal-input').css({
                'font-size': '.9em'
            });
        }

        var title = $('#modal-container .modal-title').html();
        $('#modal-container .modal-title').html('Loading...');
        dialog.overlay.fadeIn(200, function() {
            dialog.container.fadeIn(200, function() {
                dialog.data.fadeIn(200, function() {
                    $('#modal-container .modal-content').animate({
                        height: 260
                    }, function() {
                        $('#modal-container .modal-title').html(title);
                        $('#modal-container form').fadeIn(200, function() {
                            $('#modal-container #modal-name').focus();

                            // fix png's for IE 6
                            if ($.browser.msie && $.browser.version < 7) {
                                $('#modal-container .modal-button').each(function() {
                                    if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
                                        var src = RegExp.$1;
                                        $(this).css({
                                            backgroundImage: 'none',
                                            filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")'
                                        });
                                    }
                                });
                            }
                        });
                    });
                });
            });
        });
    },
    show: function(dialog) {
        $('#modal-container .modal-send').click(function(e) {
            e.preventDefault();
            // validate form
            if (modal.validate()) {
                $('#modal-container .modal-message').fadeOut(function() {
                    $('#modal-container .modal-message').removeClass('modal-error').empty();
                });
                $('#modal-container .modal-title').html('Sending...');
                $('#modal-container form').fadeOut(200);
                $('#modal-container .modal-content').animate({
                    height: '80px'
                }, function() {
                    $('#modal-container .modal-loading').fadeIn(200, function() {
                        $.ajax({
                            url: '/Content/htmlSLcontrols/loading.htm',
                            data: $('#modal-container form').serialize() + '&action=send',
                            type: 'post',
                            cache: false,
                            dataType: 'html',
                            complete: function(xhr) {
                                $('#modal-container .modal-loading').fadeOut(200, function() {
                                    $('#modal-container .modal-title').html('Thank you!');
                                    $('#modal-container .modal-message').html(xhr.responseText).fadeIn(200);
                                });
                            },
                            error: modal.error
                        });
                    });
                });
            }
            else {
                if ($('#modal-container .modal-message:visible').length > 0) {
                    var msg = $('#modal-container .modal-message div');
                    msg.fadeOut(200, function() {
                        msg.empty();
                        modal.showError();
                        msg.fadeIn(200);
                    });
                }
                else {
                    $('#modal-container .modal-message').animate({
                        height: '30px'
                    }, modal.showError);
                }

            }
        });
    },
    close: function(dialog) {
        $('#modal-container .modal-message').fadeOut();
        $('#modal-container .modal-title').html('Goodbye...');
        $('#modal-container form').fadeOut(200);
        $('#modal-container .modal-content').animate({
            height: 40
        }, function() {
            dialog.data.fadeOut(200, function() {
                dialog.container.fadeOut(200, function() {
                    dialog.overlay.fadeOut(200, function() {
                        $.modal.close();
                    });
                });
            });
        });
    },
    error: function(xhr) {
        alert(xhr.statusText);
    },
    validate: function() {
        modal.message = '';

        //no validation at this time
        return true;
        
        if (modal.message.length > 0) {
            return false;
        }
        else {
            return true;
        }
    },
    showError: function() {
        $('#modal-container .modal-message')
			.html($('<div class="modal-error">').append(modal.message))
			.fadeIn(200);
    }
};

   
