$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        if( window.location.href.indexOf('#') != -1 ) {
            var hashes = window.location.href.slice(window.location.href.indexOf('?')+1, window.location.href.indexOf('#')).split('&');
        } else {
            var hashes = window.location.href.slice(window.location.href.indexOf('?')+1).split('&');
        }
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    }
});


var newsletter = {
    blockOptions: {
        overlayCSS:{ backgroundColor: '#fff'},
        message: '<img src="/_img/ajax-loader.gif" class="ajaxLoader" />',
        css: {
            border: '0',
            backgroundColor: 'transparent'
        }
    },
    lastEmail: null,
    conteiner: $('div.newsletter'),
    html_subscribe_form:   '<p>Chcesz być na bieżąco? Wpisz swój adres email a ja poinformuję Ciebie o najnowszych wydarzeniach</p><input type="text" name="email" /><a href="#" class="submit js_ajax" rel="/newsletter/add?mode=subscribe">Zapisz mnie</a>',        
    html_subscribe_true:   '<p>Dziękujemy za subskrypcję!<br>W celu potwierdzenia autentyczności adresu e-mail, na podany adres został wysłan kod aktywujący. Postępuj zgodnie ze wskazówkami zawartymi w e-mailu.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_subscribe_false:  '<p>Podano niepoprawny adres e-mail.<br>Upewnij się, że adres jest poprawny i spróbuj jeszcze raz.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_subscribe_exists_active: '<p>Twój adres e-mail, znajduje się w bazie danych. Czy chcesz go usunąć?</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.unsubscribe_form">TAK</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#" class="js_goto" rel="newsletter.subscribe_form">NIE</a></p>',
    html_subscribe_exists_inactive: '<p>Twój adres e-mail, znajduje się w bazie danych lecz nie jest aktywowany.<br>Czy chcesz wysłać kod aktywacyjny ponownie?</p><p style="text-align:center;padding-top:5px;"><input type="hidden" name="email" /><a href="#" class="js_ajax" rel="/newsletter/resend?mode=resend">TAK</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#" class="js_goto" rel="newsletter.subscribe_form">NIE</a></p>',

    html_resend_true: '<p>Na podany adres został wysłan kod aktywujący. Postępuj zgodnie ze wskazówkami zawartymi w e-mailu.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_resend_false: '<p>Wystąpił nieoczekiwany błąd, proszę spęróbować później.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    // @todo: html_resend_true, html_resend_false, html_spam_limit
    
    html_activate_form:   '<p>Aby dokonczyć rejestrację, wpisz poniżej kod aktywacyjny, następnie kliknij na "Aktywuj!"</p><input type="text" name="js_code" /><a href="#" class="submit js_ajax" rel="/newsletter/activate?mode=activate">Aktywuj!</a>',
    html_activate_true:   '<p style="text-align:center;">Aktywacja zakończona sukcesem!<br>Od tej chwili będziesz otrzymywać biuletyn od Prezydenta.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_activate_false:  '<p style="text-align:center;">Kod aktywujący jest niepoprawny lub stracił ważność.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    
    html_unsubscribe_form:'<p>Jeżeli chcesz się wypisać z subskrypcji, wpisz poniżej swój adres e-mail, następnie kliknij na "Wypisz mnie!"</p><input type="text" name="email" /><a href="#" class="submit  js_ajax" rel="/newsletter/delete?mode=unsubscribe">Wypisz mnie!</a>',
    html_unsubscribe_true:'<p>W celu potwierdzenia tożsamości, na Twój adres e-mail został wysłany specjalny kod. Postępuj zgodnie ze wskazówkami zawartymi w e-mailu.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_unsubscribe_false:'<p>Podany adres e-mial nie został odnaleziony w naszej bazie danych.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.unsubscribe_form">&laquo; wróć</a></p>',
    
    html_deactivate_form:   '<p>Aby potwierdzić tożsamość, wpisz poniżej specjalny kod, następnie kliknij na "Wypisz mnie!"</p><input type="text" name="code" /><a href="#" class="submit js_ajax" rel="/newsletter/deactivate?mode=deactivate&email='+$.getUrlVar('email') +'">Wypisz mnie!</a>',
    html_deactivate_true:   '<p>Twój adres e-mail został usunięty z naszej bazy. Od tej chwili nie będziesz już otrzymywać aktualności od Prezydenta</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    html_deactivate_false:  '<p style="text-align:center;">Kod deaktywujący jest niepoprawny lub stracił ważność.</p><p style="text-align:center;padding-top:5px;"><a href="#" class="js_goto" rel="newsletter.subscribe_form">&laquo; wróć</a></p>',
    
    
    init: function() {
    newsletter.detectUrlParam();
        newsletter.detectUrlParam();
        newsletter.liveEvents();
    },
    
    liveEvents: function() {
        newsletter.conteiner.find('a.js_goto').live('click', function(){
            eval( $(this).attr('rel')+'()' );
            return false;
        });
        newsletter.conteiner.find('a.js_ajax').live('click', function(){
            var input = newsletter.conteiner.find('input');
            if(input.attr('name') == 'email') {
                if( input.val() != '' ) {
                    newsletter.lastEmail = input.val();
                } else {
                    input.val(newsletter.lastEmail);
                }
            }
            newsletter.conteiner.block(newsletter.blockOptions);
            $.ajax({
                url: $(this).attr('rel'),
                dataType: 'json',
                data: input.attr('name') + '=' + input.val(),
                success: function(data) {
                    newsletter.conteiner.unblock();
                    var retObjTest = eval('typeof(newsletter.'+data.mode+'_'+data.status+')');
                    if( retObjTest == "function" ) {
                        eval('newsletter.'+data.mode+'_'+data.status+'()');
                    } else {
                        alert('blad');
                    }
                
                },
                error: function() {
                    newsletter.conteiner.unblock();
                    alert('blad');
                }
            });
            return false;
        });
    },
    
    detectUrlParam: function() {
        var urlObj = eval('typeof(newsletter.'+$.getUrlVar('newsletter') + '_form)');
        if( urlObj != "function" ) {
            newsletter.subscribe_form();
        } else {
            eval('newsletter.'+$.getUrlVar('newsletter') + '_form()');
            $(document).scrollTop( newsletter.conteiner.offset().top );
        }
    },
    
    subscribe_form: function() {newsletter.conteiner.html(newsletter.html_subscribe_form);},
    subscribe_true: function() {newsletter.conteiner.html(newsletter.html_subscribe_true);},
    subscribe_false: function() {newsletter.conteiner.html(newsletter.html_subscribe_false);},
    subscribe_exists_active: function() {newsletter.conteiner.html(newsletter.html_subscribe_exists_active);},
    subscribe_exists_inactive: function() {newsletter.conteiner.html(newsletter.html_subscribe_exists_inactive);},
    
    resend_true: function() {newsletter.conteiner.html(newsletter.html_resend_true);},
    resend_false: function() {newsletter.conteiner.html(newsletter.html_resend_false);},
    
    activate_form: function() {newsletter.conteiner.html(newsletter.html_activate_form);},
    activate_true: function() {newsletter.conteiner.html(newsletter.html_activate_true);},
    activate_false: function() {newsletter.conteiner.html(newsletter.html_activate_false);},
    
    unsubscribe_form: function() {newsletter.conteiner.html(newsletter.html_unsubscribe_form);},
    unsubscribe_true: function() {newsletter.conteiner.html(newsletter.html_unsubscribe_true);},
    unsubscribe_false: function() {newsletter.conteiner.html(newsletter.html_unsubscribe_false);},

    deactivate_form: function() {newsletter.conteiner.html(newsletter.html_deactivate_form);},
    deactivate_true: function() {newsletter.conteiner.html(newsletter.html_deactivate_true);},
    deactivate_false: function() {newsletter.conteiner.html(newsletter.html_deactivate_false);}
    
}; 

