var Layout =  $.Class.create({
    initialize: function() {
        if($('#sitelinks').length != 0) {
            this.initSitelinks;
        }

        // Placeholder browser-compatibility. Webkit browsers have their own implementation.
        if(!jQuery.browser.webkit) {
            $('input[placeholder]').each(function() {
                // Don't do anything if the element is prefilled or the placeholder is empty
                if($(this).val() != '' || $(this).attr('placeholder') == '') return;

                // Apply placeholder
                $(this).val($(this).attr('placeholder'))
                       .css('color', '#a9a9a9');

                $(this).focus(function() {
                    if($(this).val() == $(this).attr('placeholder')) {
                        $(this).val('')
                               .css('color', '#000000')
                    }
                });
                $(this).blur(function() {
                    if($(this).val() == '') {
                        $(this).val($(this).attr('placeholder'))
                               .css('color', '#a9a9a9');
                    }
                });

                var element = this;
                // Try to prevent the placeholder to be submitted.
                // $(this).parent('form') won't work.
                $(this).closest('form').submit(function() {
                    if($(element).val() == $(element).attr('placeholder')) {
                        $(element).val('');
                    }
                });
            });
        }
    },
    initSitelinks: function() {
        printPage = $('<li></li>').click(this.printPage)
                                  .html($('<a></a>').attr('href', '#')
                                                    .html($('<span></span>').attr('title', 'Afdrukken')
                                                                            .attr('alt',   'Afdrukken')
                                                                            .addClass('icon')
                                                                            .addClass('icon-print')));

        favorite = $('<li></li>').click(this.addToFavorites)
                                 .html($('<a></a>').attr('href', '#')
                                                   .html($('<span></span>').attr('title', 'Toevoegen aan favorieten')
                                                                           .attr('alt',   'Toevoegen aan favorieten')
                                                                           .addClass('icon')
                                                                           .addClass('icon-addtofavourites')));

        $('#sitelinks').append(printPage);
        if ($.browser.msie) {
            $('#sitelinks').append(favorite);
        }
    },
    printPage: function() {
        if (window.print) {
            window.print();
        }
    },
    addToFavorites: function() {
        if ($.browser.msie) {
            window.external.AddFavorite(
                window.location,
                document.title);
        } else {
            window.alert('Deze functie wordt niet ondersteund door uw browser.');
        }
    }
});

$(function() {
    new Layout();
});
