function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function whichSibling( $child, $parent ) {
    if ( typeof( $parent ) == 'undefined' ) $parent = $child.parent();
    
    $children = $parent.children();
    
    for ( var i = 0; i < $children.length; i++ ) {
        if ( $( $children[i] )[0] == $child[0] ) return i;
    }
    
    return $children;
}

function changeLayout( whichLayout ) {
    $bodyWrap.fadeOut(500, function() {
        $bodyWrap.removeClass();
        
        switch ( whichLayout ) {
            case 0:
            break;
            
            case 1:
                $bodyWrap.addClass('layout1');
            break;
            
            case 2:
                $bodyWrap.addClass('layout2');
            break;
        }
        
        $bodyWrap.fadeIn(500);
    });
    
    document.cookie = 'glayout=' + whichLayout;
}

function changePic( ev ) {
    ev.preventDefault();
        
    $target = $( ev.target );
    if ( $target.children('IMG').length ) $target = $target.children('IMG');
    
    var sibNum = whichSibling( $target.parent() );
    var thisSrc = '/img/photos/' + imgArr[ sibNum ];
    var inactivePic = activePic ? 0 : 1;
    
    
    
    if ( typeof( loadedImgs[ sibNum ] ) == 'undefined' ) {
       gallery.loadInactive( $picWrappers[ 2 ], 300 );
        
       gallery.unloadActive( $picWrappers[ activePic ] );
       
       $mainPics[ inactivePic ].attr('src', thisSrc );
       
       var img = new Image();
       
       $(img).load( function() {                
            $picWrappers[ 2 ] . fadeOut( 300 ) . removeClass('active');
            gallery.loadInactive( $picWrappers[ inactivePic ], 500 );
        }).attr('src', thisSrc );
        
        loadedImgs[ sibNum ] = thisSrc;
    }
    else {            
        $mainPics[ inactivePic ].attr('src', thisSrc);

        gallery.unloadActive( $picWrappers[ activePic ] );
        gallery.loadInactive( $picWrappers[ inactivePic ], 500 );
    }
    
    var cleanName = imgArr[ sibNum ].substr( 0, imgArr[ sibNum ].length - 4 );
    
    //cleanName = cleanName.replace(/(\_|\-)/ , ' ');
    
    window.location.hash = cleanName;
    
    activePic = inactivePic;
}

var gallery = {

    unloadActive : function( thePanel, speed ) {
        if ( typeof( speed ) == 'undefined' ) speed = 500;
        
        thePanel.fadeOut( speed, function() {
            thePanel.children('img').attr('src','/img/blank.gif');
        }).removeClass('active');
    },
    
    loadInactive : function( thePanel, speed ) {        
        thePanel.fadeIn( speed ).addClass('active');
    }
};

var $bodyWrap;
var $mainPicCol;
var $thumbsWrap;

var loadedImgs = [];

$(function() {
    $bodyWrap = $('#bodyContainer');
    $mainPicCol = $('#mainPicColumn');
    $thumbsWrap = $('#thumbsColumn');
    
    $thumbs = $('A', $thumbsWrap);
    $mainPics = [];
    $picWrappers = $('.picWrapper', $mainPicCol);
    
    for ( var i=0; i < 3; i++) {
        $mainPics[i] = $('<img />').appendTo( $picWrappers[i] );
        $picWrappers[i] = $( $picWrappers[i] );
    }
    
    $mainPics[2].attr('src', '/img/loading.gif').addClass('loading');
    
    activePic = 0;
    
    $thumbs.click( function(ev) {
        changePic(ev);
        
    });
        
    $( $thumbs[0] ).trigger('click');
    
    
    $layoutPickWrap = $('#layoutPick');
    $layoutPicks = $('A', $layoutPickWrap);
    
    $layoutPicks.click( function(ev) {
        $target= $( ev.target );
    
        var whichSib = whichSibling( $target, $layoutPickWrap );
        
        changeLayout( whichSib );
    });
});