﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

$.fn.contactform = function(options) {
    //global
    var mx, my, name, mail, message, found;
    var aspeed = 400;
    var curl = '/Kontakt.aspx';
    var ajaxcont = '#contactForm';
    var containercss = "ajaxContactContainer";
    var container = $('<div></div>');
    var loadimg = $('<img src="/Image/ajax_loader.gif" alt="Laddar innehåll, var god vänta!" />');
    //init
    container.css({ 'display': 'none' });
    //trigger
    var csscheck = parseInt($('#logotype a').css('text-indent').replace('px', ''));
    if (csscheck < 0) {
        $(this).click(function(event) {
            createOverlay();
            createContainer(event);
            return false;
        });
    }
    else {
        return false;
    }


    //HELPER FUNCTIONS

    //Display & animations --->
    function createOverlay() {
        //displaying a overlay and setting a click event to it
        $('<div>&nbsp;</div>').attr('id', 'contactFormOverlay').css({
            'position': 'absolute',
            'top': '0',
            'left': '0',
            'width': $('body').outerWidth() + 'px',
            'height': $('body').outerHeight() + 'px',
            'z-index': '1000'
        }).appendTo('body').click(function(e) {
            if (e.target.id == $(this).attr('id')) {
                $(this).remove();
                animateAndHide(container);
            }
        });
    }
    function createContainer(e) {
        //displaing a container and loads content to it with ajax
        mx = e.pageX + 'px';
        my = e.pageY + 'px';
        container.attr('id', containercss).appendTo('body');
        container.css({
            'top': my,
            'left': mx,
            'width': '32px',
            'height': '32px',
            'z-index': '1001'
        });
        //showing container and triggering ajax loading function
        loadimg.hide().appendTo(container);
        container.show(aspeed, function() {
            loadimg.css({
                'position': 'relative',
                'top': '0px',
                'left': '0px'
            }).fadeIn(aspeed, function() {
                loadContactForm($(this));
            });
        });
    }
    function displayContactForm(d) {
        //loading loaded data in to a jQuery object
        d = $(d).find(ajaxcont);
        //appending data to container
        d.hide().appendTo(container);
        //animating container and displying data
        loadimg.fadeOut(aspeed, function() {
            container.animate({
                'width': d.outerWidth() + 'px',
                'height': d.outerHeight() + 'px'
            }, aspeed, function() {
                d.fadeIn(aspeed);
                d.find('input:first').focus();
            });
        });
        //listening to send button event
        d.find('input[type="submit"]').click(function() { sendContactForm(d); });
    }
    function animateAndHide(o) {
        o.hide(aspeed, function() {
            o.html(" ");
            o.remove();
        });
    }

    //Ajax --->
    function loadContactForm(o) {
        //loading contact form from page set in 'curl'
        $.ajax({
            url: curl,
            error: function() { aError(container); },
            success: function(data) { displayContactForm(data); }
        });
    }
    function sendContactForm(d) {
        //sending form to page set in 'curl'
        name = d.find('.contactName').val();
        mail = d.find('.contactMail').val();
        message = d.find('.contactMessage').val().replace(/\n/g, '$br$');

        if (name != "" && mail != "" && message != "") {
            $.ajax({
                url: curl,
                data: { fa: 'true', n: name, m: mail, msg: message },
                error: function() { aError(container); },
                beforeSend: function() {
                    container.find(ajaxcont).fadeOut(aspeed, function() {
                        loadimg.css({
                            'top': ((container.height() / 2) - (loadimg.height() / 2)) + 'px',
                            'left': ((container.width() / 2) - (loadimg.width() / 2)) + 'px'
                        }).fadeIn(aspeed);
                    });
                },
                success: function(data) {
                    found = $(data).find(ajaxcont);
                    found.attr('id', '').hide().appendTo(container);
                    loadimg.fadeOut(aspeed, function() {
                        container.animate({
                            'height': found.outerHeight() + 'px'
                        }, aspeed, function() {
                            container.html(found);
                            found.fadeIn(aspeed);
                        });
                    });
                }
            });
        }
        else {
            alert('Du måste fylla i namn, e-post och meddelande för att kunna skicka!\nTack!');
        }
    }

    //Ajax error handling -->
    function aError(o) {
        o.css({
            'width': '160px',
            'height': 'auto'
        }).html('<div class="message">Någonting gick fel, var god försök senare eller klicka <a title="Kontaktsidan på linuspersson.net" href="' + curl + '">här</a></div>');
    }

    return this;
}