jQuery( document ).ready( dpr_ready );

function dpr_ready() {
	dpr_logo();
	dpr_case_studies();
	dpr_get_bitly_clicks();
}

// Swap the logo out with a Flash version
function dpr_logo() {
	var so = new SWFObject( dpr.flash_logo, 'mpl', '815', '140', '9' );
	so.addParam( 'wmode', 'transparent' );
	so.write( 'headerimg' );
}

function dpr_case_studies() {
	// Initial inspiration from http://www.designbyadmiral.com/#portfolio
	if ( typeof( dpr.case_studies_filter ) == 'undefined' )
		return;
	// Add in the filter controls by JS as they're useless to people without JS
	jQuery( '#case-studies' ).before( html_entity_decode( dpr.case_studies_filter ) );
	// Ensure we only show the controls we need
	jQuery( '#filter-ctrls input' ).each( function() {
		var sel = '#case-studies .' + jQuery( this ).attr( 'name' );
		if ( ! jQuery( sel ).length ) {
			jQuery( this ).parent().remove();
		}
	} );
	// Note the current item width for convenience
	var item_width = jQuery( '#case-studies .case-study' ).width();
	// Toggle and animate the display/hide of industries
	jQuery( '#filter-ctrls label' ).click( function() {
		jQuery( this ).toggleClass( 'checked' );
		var check = jQuery( this ).find( 'input' );
		if ( check.is( ':checked' ) ) {
			check.removeAttr( 'checked', 'checked' );
			// We're unchecking so we're going to hide some stuff, but keep the checked ones
			var sel = '';
			jQuery( '#filter-ctrls input:checked' ).each( function() { sel += '.' + jQuery( this ).attr( 'name' ) + ', '; } );
			sel += ' .nothing';
			jQuery( '#case-studies li:not(.hidden)' ).not( sel ).animate( { width: "0", opacity: "0" }, 250, function(){ jQuery( this ).hide().addClass( 'hidden' ); } );
		} else {
			check.attr( 'checked', 'checked' );
			// We're checking so we're going to reveal anything which is checked
			sel = '';
			jQuery( '#filter-ctrls input:checked' ).each( function() { sel += ' .' + jQuery( this ).attr( 'name' ) + ', '; } );
			sel += ' .nothing';
			jQuery( sel ).filter( '.hidden' ).show().animate( { width: item_width + "px", opacity:"1" }, 250, function() { jQuery( this ).removeClass( 'hidden' ); } );
		}
		return false;
	} );
}

function dpr_get_bitly_clicks() {
	var ids = [];
	var id;
	jQuery( 'div.retweet .stat' ).each( function() {
		id = jQuery( this ).attr( 'id' );
		ids.push( id );
	} );
	var data = { 
		action: 'bitly_clicks',
		ids: ids.join( ',' ),
		1:1
	};
	jQuery.post( dpr.ajax_url, data, dpr_write_bitly_clicks, 'json' );
}

function dpr_write_bitly_clicks( data ) {
	var clicks;
	for ( id in data.clicks ) {
		clicks = Number( data.clicks[ id ] );
		if ( clicks ) {
			jQuery( '#retweet_stat_' + id ).text( clicks ).addClass( 'stat_active' );
		}
	}
}

function html_entity_decode( str ) {
	var val = false;
	try {
		var tarea = document.createElement( 'textarea' );
		tarea.innerHTML = str;
		val = tarea.value;
		tarea.parentNode.removeChild( tarea );
	} catch( e ) {
		//for IE add <div id="htmlconverter" style="display:none;"></div> to the page
		document.getElementById( "htmlconverter" ).innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
		val = document.getElementById( "innerConverter" ).value;
		document.getElementById( "htmlconverter" ).innerHTML = "";
	}
	return val;
}
