function d() { if ( window.console !== undefined && window.console !== null ) console.debug( arguments ); }
function ddd(){ return d.apply( this, arguments ); }


jQuery( function($) {
    
    var gallery_cache = {};
    function draw_gallery( idx ) {
        $( '#temp_loader' ).remove();
        gallery_cache[ idx ].find( 'a:first' ).click();
    }   
    
    $.fn.extend( {
        content_load: function() {
            return $( this ).each( function() {
                
                // link to gallery
                $( 'a[rel=gallery],a.wpGallery', this ).click( function() {
                    var href = $( this ).attr( 'href' );
                    if ( !href.match( /\/$/ ) ) {
                        href += '/';
                        $( this ).attr( 'href', href );
                    }
                    
                    if ( gallery_cache[ href ] !== undefined )
                        draw_gallery( href )
                    else
                        $.get( href, function( html ) {
                            var gallery = $( '#gallery a', html ).attr( 'rel', 'load_gallery['+ ( new Date() ).getTime()+ "]");
                            var cache = $( '<div class="hidden-gallery" style="display:none"></div>' ).appendTo( 'body' );
                            cache.append( gallery );
                            gallery.prettyPhoto();
                            gallery_cache[ href ] = cache;
                            draw_gallery( href );
                        } );
                    return false;
                } );
                
                // replacement for the inline gallery
                $( 'a.wp-caption-dd', this ).each( function() {
                    var a = $( this );
                    var img = a.find( 'img' );
                    d( "READ", a.attr( 'href' ), img.attr( 'src' ) );
                    $.get( a.attr( 'href' ), function( html ) {
                        
                        // get parent and build replacement for the "body"
                        var p = a.parent(), r = $( '<div class="jcarousel-outer"></div>' ).css( {
                            background: 'url('+ img.attr( 'src' )+ ') 0 0 no-repeat',
                            width: img.width() + 'px',
                            height: img.height() + 'px'
                        } );
                        
                        // extract all a-links (gallery) from retreived html
                        var e = $( '#content .gallery-icon a', html );
                        var c = $(
                            '<div class="jcarousel-container jcarousel-container-horizontal" style="display: block;">'+
                                '<div class="jcarousel-prev jcarousel-prev-horizontal"/>'+
                                '<div class="jcarousel-next jcarousel-next-horizontal"/>'+
                                '<div class="jcarousel-clip jcarousel-clip-horizontal">'+
                                '</div>'+
                            '</div>'
                        );
                        
                        // generate the ul and append all a-links in lis
                        var u = $( '<ul class="jcarousel-list jcarousel-list-horizontal"></ul>' );
                        e.each( function() { u.append( $( '<li></li>' ).append( this ) ); } );
                        c.find( '.jcarousel-clip' ).append( u );
                        
                        // append the diplay out
                        var maxw = img.width(), maxh = img.height() - 42;
                        var out = $( '<div class="jcarousel-out"></div>' ).css( {
                            width: maxw + 'px',
                            height: maxh + 'px'
                        } );
                        
                        // set and append the image
                        var set_image = function( img, whlt ) {
                            var old = out.find( 'img' );
                            var show = function() {
                                out.empty().append( $( img ).css( {
                                    width: whlt[0]+ 'px',
                                    height: whlt[1]+ 'px',
                                    marginLeft: whlt[2]+ 'px',
                                    marginTop: whlt[3]+ 'px',
                                    visibility: 'visible',
                                    opacity: '0.0',
                                    position: 'static'
                                } ) );
                                img.animate( { opacity: '1.0' } );
                            };
                            if ( old.length > 0 )
                                old.fadeOut( show );
                            else
                                show();
                        };
                        
                        // load the image
                        var load_image = function( img ) {
                            img = $( img );
                            
                            // either has been loaded already
                            var whlt = img.data( 'whlt' );
                            if ( whlt !== undefined && whlt !== null )
                                set_image( img, whlt );
                            
                            // or ot
                            else
                                img.load( function() {
                                    var $this = $( this )
                                        .css( { visibility: 'invisible', position: 'absolute', zIndex: -1 } )
                                        .appendTo( 'body' );
                                    
                                    setTimeout( function() {
                                        var h = $this.height(), w = $this.width();
                                        
                                        // determine h
                                        if ( h > maxh ) {
                                            var q = maxh / h;
                                            h = maxh;
                                            w = q * w;
                                        }
                                        
                                        // determine w
                                        if ( w > maxw ) {
                                            var q = maxw / w;
                                            w = maxw;
                                            h = q * h;
                                        }
                                        
                                        // calc left and top
                                        var l = ( maxw - w ) / 2, t = ( maxh - h ) / 2;
                                        
                                        // cache width, height, left and top
                                        whlt = [ w, h, l, t ];
                                        $this.data( 'whlt', whlt );
                                        
                                        // output image
                                        set_image( $this, whlt );
                                    }, 150 );
                                } );
                        };
                        
                        // append the new content with the gallery
                        p.empty().append( r.append( out ).append( c ) );
                        c.find( 'a' ).click( function() {
                            var img = new Image();
                            img.src = $( this ).attr( 'href' );
                            load_image( img );
                            return false;
                        } );
                        c.jcarousel();
                        
                        // load first
                        var first = new Image();
                        first.src = c.find( 'a:first' ).attr( 'href' )
                        load_image( first );
                        
                        // remove the focus thing in mozilla
                        $( 'a', c ).focus( function() { $( this ).blur(); } );
                    } );
                } );
                
                
                
                // link all youtubes
                $( 'a[href*=youtube.com]', this ).each( function() {
                        
                    // get the link for transformation
                    var href = $( this ).attr( 'href' );
                    
                    // determine url, widht and height depending on quality
                    var url = href, width = 640, height = 385, version = 8;
                    if ( href.match( /\/v\/([^&\?]+)/ ) )
                        url = 'http://www.youtube.com/watch?v='+ RegExp.$1;
                    else if ( href.match( /\bwatch?v=([^&\?]+)/ ) )
                        url = 'http://www.youtube.com/watch?v='+ RegExp.$1;
                    url += '&hl=en&fs=1&rel=0';
                    
                    // better quali
                    if ( href.match( /fmt=(\d+)/ ) ) {
                        var type = RegExp.$1;
                        url += '&ap=%2526fmt%3D'+ type;
                    }
                    url += '&width='+ width+ '&height='+ height;
                    //d( "YOUTUBE "+ url );
                    // link with 
                    $( this ).attr( 'href', url );
                } ).attr( 'rel', 'prettyPhoto[flash]' ).prettyPhoto();
                
                
                
                // link all vimeos
                $( 'a[href*=vimeo]', this ).each( function() {
                    var h = $( this ).attr( 'href' );
                    var a = [];
                    if ( ! h.match( /\bwidth=\b/ ) )
                        a.push( 'width=600' );
                    if ( ! h.match( /\bheight=\b/ ) )
                        a.push( 'height=338' );
                    if ( ! h.match( /\bautoplay=\b/ ) )
                        a.push( 'fullscreen=1&autoplay=1' );
                    if ( a.length > 0 ) {
                        h += h.match( /\?/ ) ? '&' : '?';
                        h += a.join( '&' );
                    }
                    $( this ).attr( 'href', h );
                    //http://vimeo.com/moogaloop.swf?clip_id=6081640&server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=ff9933&fullscreen=1
                } ).attr( 'rel', 'prettyPhoto[vimeo]' ).prettyPhoto();
                
                
                // load all contents AJAXian
                $( '#topnav a,#imagesite a', this )
                    .not( 'a[rel=gallery],a.wpGallery,a.wp-caption-dd,a[href*=youtube],a[href*=.mp3],.external' )
                    .click( function() {
                    var content_old = $( '#content' ); 
                    var off = content_old.offset();
                    var holder = $( '<div id="holder"></div>' ).css( {
                        width: content_old.width(),
                        height: content_old.height(),
                        postion: 'absolute',
                        top: off.top + 'px',
                        left: off.left + 'px',
                        opacity: 0,
                        background: '#fff'
                    } );
                    content_old.after( holder ).animate( { opacity: '0.0' } );
                    holder.animate( { opacity: '1.0' } );
                    
                    $.get( $( this ).attr( 'href' ), function( html ) {
                        
                        // get new contnet, remove old contnet
                        var new_content = $( '#content', html ).css( 'opacity', 0 );
                        content_old.remove();
                        
                        // insert and show content
                        holder.after( new_content ).remove();
                        new_content.animate( { opacity: '1.0' } ).content_load();
                        
                        // go to top
                        jQuery.scrollTo( 0, 'fast' );
                        
                        // check and set title
                        var new_title   = document.title;
                        if ( html.match( /<title>([^>]+?)</ ) )
                            new_title = RegExp.$1;
                        try {
                            var temp = $( '<span display="none"></span>' )
                                .appendTo( 'body' ).html( new_title );
                            document.title = temp.text();
                            temp.remove();
                        }catch(e){}
                        
                    } );
                    return false;
                } );
                
                
                // hover for those in-site snippets
                $( 'img[src*=_normal.jpg]', this ).each( function() {
                    var orig = $( this ), orig_src = $( this ).attr( 'src' );
                    var match = orig.attr( 'src' ).match( /^(.*?)_normal\.jpg$/ );
                    var hover = $( '<img style="display:none" src="'+ match[1]+ '_hover.jpg" />' ).appendTo( 'body' );
                    orig.mouseover( function() {
                        orig.attr( 'src', hover.attr( 'src' ) );
                    } );
                    orig.mouseout( function() {
                        orig.attr( 'src', orig_src );
                    } );
                } );
                
                
                
                // remove the focus thing in mozilla
                $( 'a', this ).focus( function() { $( this ).blur(); } );
                
            } );
        }
    } );
    
    $( 'body' ).content_load();
    $( '#header_player' ).empty().flash( {
        src: '/wp-content/themes/prinzpi/img/player/player.swf',
        width: 965,
        height: 27
    } );
    $( '#header_twitter' ).empty().flash( {
        src: '/wp-content/themes/prinzpi/img/twitter.swf',
        width: 965,
        height: 52
    } );
} );
