$(function(){
    $('input[placeholder]').each(function(){
        var ph = $(this).attr("placeholder");
        var currvalue = $(this).val();
        if (ph == currvalue) {
            $(this).addClass('placeholder');
        }
        if (currvalue == "") {
            $(this).addClass('placeholder');
            $(this).val(ph);
        }
        $(this).focus(function(){
            $(this).removeClass('placeholder');
            if ($(this).val() == ph) {
                $(this).val('');
            }
        }).blur(function(){
            if ($(this).val() == '') {
                $(this).addClass('placeholder');
                $(this).val(ph);
            }
        });
    });

    hideTime('#sltArrRet', '#timeRet');
    hideTime('#sltArr', '#timeOut');
    $('.changestext').hide();
    $('.statustext').hide();
    $('.callingpoints').hide();
    $('.fares').hide();
    $('.change_link').click(function(e){
        $(e.target).closest('li').find('.changestext').toggle('slow');
        return false;
    });
    $('.status').click(function(e){
        $(e.target).closest('li').find('.statustext').toggle('slow');
        return false;
    });
    $('.calling_link').click(function(e){
        var div = $(e.target).closest('li').find('.callingpoints');
        if (div.html()) {
            div.toggle('slow');
            return false;
        }
        var href = e.target.href + ';ajax=1';
        div.load(href, '', function(e){
            div.show('slow');
        });
        return false;
    });
    $('.calling_link_inner').click(function(e){
        var tr = $(e.target).closest('tr');
        if (tr.next().hasClass('ajax')) {
            tr.next().find('div.ajax').toggle('slow');
            return false;
        }
        var href = e.target.href + ';ajax=1';
        $.get(href, function(out){
            tr.after('<tr class="ajax"><td colspan=6><div style="display:none" class="ajax">' + out + '</div></td></tr>');
            tr.next().find('div.ajax').show('slow');
        });
        return false;
    });
    if ($('.change_link').length) {
        $('.expand_all').html('<a href="#" onclick="$(\'.changestext\').show(\'slow\'); return false;">Show all changes</a>');
    }

    /* Fares links */
    $('.fares_link').click(function(e){
        var div = $(e.target).closest('li').find('.fares');
        if (div.html()) {
            div.toggle('fast');
            return false;
        }
        var href = e.target.href + ';ajax=1';
        var link = $(this);
        div.load(href, '', function(e){
            link.html(link.html().substring(0,link.html().length-1));
            div.show('slow');
        });
        link.html( link.html() + '&hellip;');
        return false;
    });

    /* Show/hide */
    $('#operatorMode').change(function(){
        var val = $(this).val();
        if (val == 'SHOW') {
            $('#operator_all').text('All train operators');
        } else {
            $('#operator_all').text('-');
        }
    });

    $('#liveboard tr').click(function(){
        window.location = $(this).find('a').attr('href');
    }).css('cursor', 'pointer').hover(function(){
        $(this).toggleClass("active");
        //$(this).find('td.d').show();
    }, function(){
        $(this).toggleClass("active");
        //$(this).find('td.d').hide();
    }); 
    //$('#liveboard td.d').append('<img src="/images/fatcow/32x32/arrow_right.png" alt="Details">').hide().find('a').hide();

    if (navigator.geolocation) {
        $('#txtFrom').after(' <span>(<a href="javascript:traintimes.geolocate.lookup(\'txtFrom\')">nearest</a>)</span>');
        $('#txtTo').after(' <span>(<a href="javascript:traintimes.geolocate.lookup(\'txtTo\')">nearest</a>)</span>');
    }

});

function hideTime(select, row) {
    $(select).change(function(){
        var val = $(select).val();
        if (val == 'FIRST' || val == 'LAST') {
            $(row).hide('fast');
        } else {
            $(row).show('fast');
        }
    });
    $(select).change();
}

var traintimes = {
    'geolocate': {
        'success': function(position, id) {
            //var s = $('#status');
            //if (s.className == 'success') return;
            //s.html("Location found, looking up nearest station...").addClass('success');
            $.getJSON('/nearest.php?ajax=1;n=5;lat=' + position.coords.latitude + ';lon=' + position.coords.longitude , function(data) {
                var out = '<select name="' + $('#'+id).attr('name') + '" id="' + id + '">';
                for (var i = 0; i < data.length; i++) {
                    var station = data[i]['station'];
                    out += '<option value="' + data[i]['code'] + '">' + station + '</option>';
                }
                out += '</select>';
                $('#'+id).replaceWith(out);
                $('#'+id).next().remove();
            });
        },
        'error': function(msg) {
            //$('#status').html("Couldn't automatically locate you, please fill in the form below").addClass('warning');
        },
        'lookup': function(id) {
            if (navigator.geolocation) {
                //$('#status').html('<img src="/map/i/loading.gif" alt="" align="middle"><br>Asking you for your location...');
                navigator.geolocation.getCurrentPosition(function(position){
                    traintimes.geolocate.success(position, id);
                }, traintimes.geolocate.error);
            }
        }
    }
};


