$(document).ready(function() {
    // open links in a new window
    $('a.new-window').click(function(){
        window.open(this.href);
        return false;
    });

    // address textbox focus/blur
    $('#address').each(function(){
        this.value = $(this).attr('title');
        $(this).addClass('grey');
        $(this).focus(function(){
            if(this.value == $(this).attr('title')) {
                this.value = '';
                $(this).removeClass('grey');
            }
        });
        $(this).blur(function(){
            if(this.value == '') {
                this.value = $(this).attr('title');
                $(this).addClass('grey');
            }
        });
    });
    // youtube tutorial
    $('a[href*="youtube."]').each(function(){
        var videoID = $(this).attr('href').match(/watch\?v=(.+)+/);
        videoID = videoID[1];
        $(this).qtip({
            //<![CDATA[
            content: '<div id="youtube-embed-'+videoID+'">You need Flash player 8+ to view this video.<\/div>',
            //]]>
            position: {
                corner: {
                    tooltip: 'leftMiddle',
                    target: 'rightMiddle'
                }
            },
            show: {
                when: 'click',
                solo: true
            },
            hide: 'unfocus',
            style: {
                width: 432,
                height: 264,
                padding: 0,
                tip: true,
                name: 'light'
            },
            api: {
                onRender: function(){
                    var params = {
                        allowScriptAccess: 'always',
                        allowfullScreen: 'false'
                    };
                    var attrs = {
                        id: 'youtube-video-'+videoID
                    };

                    swfobject.embedSWF('http://www.youtube.com/v/'+videoID+'&amp;enablejsapi=1&amp;playerapiid=youtube-api-'+videoID,
                        'youtube-embed-'+videoID, '425', '264', '8', null, null, params, attrs);
                },

                onHide: function(){
                    var playerAPI = this.elements.content.find('#youtube-video-'+videoID).get(0);
                    if(playerAPI && playerAPI.pauseVideo) playerAPI.pauseVideo();
                }
            }
        }
        ).attr('href', '#');
    });

});