$(function() {
    //script type="text/javascript" src="http://stilbuero.de/jquery/cookie/jquery.cookie.js"
    //var postcode = $.cookie('postcode') || '';
    //var cookie_options = { path: '/', expires: 90 };
    
    $('#mpform').submit(function(event) {
        event.preventDefault();
        if($('#mpsubmit').attr('disabled')) {return;}
        var apikey = 'B3aL5xC2Jxm5EourdTFDgzt8';
        $('#mpsubmit').attr('disabled',true);
        $('#mploader').show();
        $.getJSON(
            'http://www.theyworkforyou.com/api/getMP?postcode=' + $('#pc').val() + '&key=' + apikey + '&output=js&callback=?',
            function(data) {
                $('#mploader').hide();
                if(data.error) {
                    if(data.error.match(/No parameter/)) {
                        alert('No postcode entered.');
                    } else {
                        alert(data.error + '.');
                    }
                    $('#pc').val('');
                    $('#pc').focus();
                } else {
                    //$.cookie('postcode',$('#pc').val(),cookie_options);
                    $('#mpresults').html(
                        '<img src="http://www.theyworkforyou.com/' + data.image + '" alt="'+data.full_name+'" style="float: right;" />' +
                        '<h3><a href="http://www.theyworkforyou.com/postcode/?pc=' + $('#pc').val() + '">' + data.full_name+ '</a></h3>' +
                        '<p>'+ data.party +' MP for <strong>'+ data.constituency+'</strong></p>' +
                        '<p><a href="http://www.writetothem.com/?a=WMC&pc=' + $('#pc').val() + '" id="mpwrite" class="write" target="_blank">Send a message to ' + data.full_name + '</a></p>' + 
                        '<p class="back" style="clear: both;"><a href="" class="newsearch">&laquo; Contact another MP</a></p>' +
                        '<div style="clear:both; overflow:hidden;"></div>'
                    );
                    $('#mpresults').show();
                    $('#mpquestion').hide();

                    $('#mpwrite').click(function(event) {
                        //event.preventDefault();
                        $.get(
                            '/library/cgi/mpstat.php?mp=' + encodeURIComponent(data.full_name) + '&constituency=' + encodeURIComponent(data.constituency)+'&page=' + encodeURIComponent(document.title + ''),
                            function(data) {
                                //var newWin = window.open($('#mpfinder #mpwrite').attr('href'),'contactyourmp',"location=1,scrollbars=1,status=1,toolbar=1,width=700,height=500")
                                //if(!newWin) {
                                //    document.location = $('#mpwrite').attr('href');
                                //}
                            }
                        );
                    });
                }
                $('#mpsubmit').attr('disabled',false);
            }
        );
    });
    

    $('#mpresults .newsearch').live('click', function(event) {
        event.preventDefault();
        //$.cookie('postcode','',cookie_options);
        $('#pc').val('');
        $('#mpquestion').show();
        $('#mpresults').hide();
    });

//    if($('#pc').val() !== '') {
//        $('#mpform').submit();
//    }

    //if(postcode !== '') {$('#pc').val(postcode); $('#mpform').submit();}
});

