var dsq = {

    currpage : '',
    pending : [],
    state : 'init',
    messages : [],

    init : function () {

	// wait until document is ready
	$(document).ready( function () {

	    // load data
	    //setTimeout( dsq.load_dsq_data, 100 );

	    $('#outerbox')
		.delegate( '.collapsing','click',function() { $(this).next().slideToggle( 'fast' ) } )
		.delegate( '.cued','focus',function() { $(this).val('').removeClass('cued') } );

	    /* links */
	    //$('#outerbox').delegate( 'a', 'click', dsq.click_link );
	    //$('#menu a').click( dsq.click_link );

	    /* initial panel fade-ins */
	    dsq.init_faded_panels();

	});  // document.ready
    },

    init_faded_panels : function () {

	// show the underlying photo, and attach hover behavior
	$('.faded').fadeTo( 0, 0.8 ).hover( function () { $(this).fadeTo( 0, 1 ) },
					    function () { $(this).fadeTo( 0, 0.8 ) } );

	// after a pause, fade in the content overlay, and bind camera icon
	setTimeout( function () {
	    //$('.faded').fadeTo( 2000, 0.8 ).each( show_camera_icon );
	    $('.faded').each( show_camera_icon );
	}, 1500 );

	function show_camera_icon () {
	    var faded = this;
	    $('<img src="img/icon/camera.png" class="camera" title="show/hide background photos" />' )
		.prependTo( $(faded).parent() )
		.click( dsq.toggle_faded_panels );
	}
    },

    see_faded_panels : 0.8,  // state flag toggled by camera icon (hackaroo)

    toggle_faded_panels : function () {

	var see = 0.8 - dsq.see_faded_panels;  // switch it

	$('.faded').fadeTo( 200, see );

	dsq.see_faded_panels = see;
    },

    load_dsq_data : function () {
	var d = new Date;
	$.getJSON( ['data','info.json'].join( '/' ) + '?cb=' + d.getMilliseconds(), {}, info_response );

	function info_response (data,status) {
	    dsq.msg( 'got dsq data - status=' + status );
	    dsq.data = data;
	    dsq.init_complete();
	}

    },

    init_complete : function () {
	dsq.state = 'ready';
	$.each( dsq.pending, function (i,pending_fn) { pending_fn() } );
	//if (!dsq.currpage) dsq.load_page( 'welcome' );
	dsq.cycle_photos( dsq.data.banner.photos, $('#main_cycler') );
    },

    cycle_photos : function (photos,box,delaysecs) {

	setTimeout( change_photo, (delaysecs || 5) * 1000 );

	var active_photos = photos;  //$.grep( photos, function (photo) { return photo.status == 'active' } );
	$('.cycler-credit',box).fadeOut( 10 );

	var counter = 0;

	function change_photo () {
	    counter = (counter + 1) % active_photos.length;
	    var photo = active_photos[counter];
	    var photo_url = photo.src;
	    dsq.msg( photo.src );
	    $('.cycler-lower',box).attr( 'src', photo.src );
	    $('.cycler-credit',box).fadeOut( 'slow' );
	    $('.cycler-quote',box).fadeOut( 'slow' );
	    $('.cycler-upper',box)
		.fadeOut( 1500, function () {
		    $(this)
			.attr( 'src', photo.src )
			.fadeIn( 'slow', function () {
			    setTimeout( change_photo, 1000 * (delaysecs || 5) );
			    if (photo.credit)
				$('.cycler-credit',box).html( 'photo credit: ' + photo.credit ).fadeIn( 1500 );
			    if (photo.quote)
				$('.cycler-quote',box).html( '<div class="pqtxt">"' +
							     photo.quote.txt +
							     '"</div>' +
							     '<div class="pqsrc">' +
							     photo.quote.src +
							     '</div>' ).fadeTo( 2500, 0.7 );
			});
		});
	}
    },

    encpw : 'dinkirlydmyl',

    init_schedule : function ( next_stamp ) {  /* events.schedule panel behavior */

	// behavior
	function hint_show () { $(this).addClass( 'expandable' ) }
	function hint_hide () { $(this).removeClass( 'expandable' ) }
	$('.collapsing','#events_schedule').hover(hint_show,hint_hide).each( function () { $(this).next().hide() } );

	// initial exposure of next up
	var nextup_event = $('#event-' + next_stamp); //.css('background-color','red');
	var nextup_month = nextup_event.parent().closest( 'dd' ); //.css('background-color','pink');
	var nextup_year = nextup_month.parent().closest( 'dd' ); //.css('background-color','orange');

	// ripple open the next up event
	setTimeout( function () { nextup_year.slideDown( 'slow' ) }, 1000 );
	setTimeout( function () { nextup_month.slideDown( 'slow' ) }, 2000 );
	setTimeout( function () { nextup_event.slideDown( 'slow' ) }, 3000 );
    },

    delayed : function (o,j,m) {
	setTimeout( function () { $(o).html(m.join(j)) }, 1000 );
    },

    msg : function (m) {
    }

};


