/*
 * contactable 1.2 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2009-09-24 $
 *
 */
 
//extend the plugin
(function ($) {

    //define the new for the plugin ans how to call it	
    $.fn.contactable = function (options) {
        //set default options  
        var defaults = {
            name: 'Imię',
            email: 'Email',
            message: 'Wiadomość',
            recipient: 'test@test.co.uk',
            subject: 'Tytuł',
            recievedMsg: 'Dziękujemy, Twoja wiadomość została wysłana poprawnie',
            notRecievedMsg: 'Twoja wiadomość nie została wysłana, spróbuj ponownie za chwilę',
            disclaimer: 'Please feel free to get in touch, we value your feedback',
            another: 'Wyślij kolejną sugestię',
            IzzyHelp_ID: '21'
        };

        //call in the default otions
        var options = $.extend(defaults, options);
        //act upon the element that is passed into the design    
        return this.each(function (options) {
            //construct the form
        $(this).html('<div id="contactable"></div><scr' + 'ipt type="text/javascript" src="http://www.izzyhelp.com/help.php?id=' + defaults.IzzyHelp_ID + '&mode=contactable"></scr' + 'ipt><form id="contactForm" method="" action=""><a id="contactableContact" href="javascript:void(null)">Napisz do nas</a><div id="loading"></div><div id="callback"></div><div id="callbackSend">' + defaults.another + '</div><div class="holder"><input type="hidden" id="recipient" name="recipient" value="' + defaults.recipient + '" /><input type="hidden" id="subject" name="subject" value="' + defaults.subject + '" /><p><label for="name">Imię i nazwisko <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="email">E-Mail <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="comment">Twoja wiadomość <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><b>Formularz służy przekazywaniu nam wszelkich uwag</b> na temat naszych produktów, funkcjonalności serwisu internetowego jak i działalności całej firmy. Masz propozycję, sugestię, radę? Napisze do nas. Każda opinia jest dla nas cenna.</p><p><b>Pytania, na które oczekujecie Państwo odpowiedzi</b> prosimy zadawać w <a href="/Kontakt.aspx#formularz">formularzu kontaktowym</a>, bądź bezpośrednio na adres <a href="mailto:drzwi@drzwivasco.pl">drzwi@drzwivasco.pl</a>.</p><p><input class="submit" type="submit" value="Wyślij"/></p></div></form>');
            //show / hide function
            $("#contactableIzzyHelp").click(function () {
                $("#IzzyHelp").slideDown();
                $(".holder").slideUp();
            })
            /*$("#contactableContact").click(function(){
            $("#IzzyHelp").slideUp();
            $(".holder").slideDown();*=
            })*/

            $("#callbackSend").click(function () {
                $("#callback,#callbackSend").hide();
                $("#callback").html("");
                $(".holder").show();
                $("#name,#email,#comment").val("");
            });

            $('div#contactable').toggle(function () {
                $('#overlay').css({ display: 'block' });
                $(this).animate({ "marginLeft": "-=5px" }, "fast");
                $('#contactForm').animate({ "marginLeft": "-=0px" }, "fast");
                $(this).animate({ "marginLeft": "+=387px" }, "slow");
                $('#contactForm').animate({ "marginLeft": "+=390px" }, "slow");

                $("#callback,#callbackSend").hide();
                $("#callback").html("");
                $(".holder").show();
                $("#name,#email,#comment").val("");
            },
			function () {
			    $('#contactForm').animate({ "marginLeft": "-=390px" }, "slow");
			    $(this).animate({ "marginLeft": "-=387px" }, "slow").animate({ "marginLeft": "+=5px" }, "fast");
			    $('#overlay').css({ display: 'none' });
			});

            //validate the form 
            $("#contactForm").validate({
                //set the rules for the fild names
                rules: {
                    name: {
                        required: true,
                        minlength: 2
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    comment: {
                        required: true
                    }
                },
                //set messages to appear inline
                messages: {
                    name: "",
                    email: "",
                    comment: ""
                },
                submitHandler: function () {
                    $('.holder').hide();
                    $('#loading').show();
                    /*
                    $.get('Mail.ashx',{recipient:$('#recipient').val(), subject:$('#subject').val(), name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val()},
                    function(data){
                    $('#loading').css({display:'none'}); 
                    if( data == 'success') {
                    $('#callback').show().append(defaults.recievedMsg);
                    } else {
                    $('#callback').show().append(defaults.notRecievedMsg);
                    }
                    });
                    */

                    $.ajax({
                        type: "POST",
                        url: "mail.ashx",
                        data: { recipient: $('#recipient').val(), subject: $('#subject').val(), name: $('#name').val(), email: $('#email').val(), comment: $('#comment').val() },
                        success: function (msg) {
                            $('#callback').show().append('<div id="callbackOk">' + defaults.recievedMsg + '</div>');
                            //$('#callback').show().append(msg);
                            $('#loading').hide();
                            $('#callbackSend').show();
                        },
                        error: function (msg) {
                            $('#callback').show().append('<div id="callbackError">' + defaults.notRecievedMsg + '</div>');
                            $('#loading').hide();
                            $('#callbackSend').show();
                        }
                    });

                    /*
                    $.ajax({
                    type: "POST",
                    cache: true,
                    async: false,
                    url: "WebMethods.aspx.cs/sendConfirm",
                    data: "{name:'" + $("#name").val() + "',email:'" + $("#email").val() + "',comment:'" + $("#comment").val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    beforeSend: function() { },
                    success: function(msg) {
                    $("#info").html(msg.d[0].statement);
                    if (msg.d[0].status === true) { }
                    },
                    error: function(msg) {  }
                    });
                    */
                }
            });
        });
    };
    //end the plugin call 
})(jQuery);


