$( document ).ready( function() {
	$( "hr" ).wrap( "<div class='hr'></div>" );
	
	$( "#content h1" ).each( function() {
		var theText = $( this ).text();
		theText = theText.replace( " of ", " <span>o</span> " );
		theText = theText.replace( " to ", " <span>y</span> " );
		$( this ).html( theText );
	} );
	
	$( "#content h1 img, #content h2 img, #content h3 img, #content h4 img, #content h5 img, #content h6 img" ).each( function() {
		var myImg = $( this );
		
		if( $( this ).parent( "a" ).length ) {
			myImg = $( this ).parent( "a" );
		}
		
		myImg.parent().before( myImg );
	} );
	
	$( ".post-content img, .sidebar-widget img" ).each( function() {
		if( !$( this ).hasClass( "noFrame" ) ) {
			var wrapClass = "";
			
			$( this ).addClass( "the_photo" );
			
			var wrapee = $( this );
			
			if( $( this ).parent( "a" ).length ) {
				wrapee = $( this ).parent( "a" );
			}
			
			if( $( this ).hasClass( "alignleft" ) ) {
				$( this ).removeClass( "alignleft" );
				wrapClass = "alignleft";
			}
			
			if( $( this ).hasClass( "alignright" ) ) {
				$( this ).removeClass( "alignright" );
				wrapClass = "alignright";
			}
			
			if( $( this ).hasClass( "aligncenter" ) ) {
				$( this ).removeClass( "aligncenter" );
				wrapClass = "aligncenter";
			}
			
			wrapee.wrap( '<div class="photo ' + wrapClass + '"><div class="photo_wrapper"></div></div>' );
			
			var currWidth = this.width;
			var currCol = wrapee.parents( ".col" );
			var maxWidth = currWidth;
			
			if( currCol.hasClass( "colspan-two" ) ) {
				if( maxWidth > 560 ) {
					maxWidth = 560;
				}
			}
			else if( currCol.hasClass( "colspan-one" ) ) {
				if( maxWidth > 240 ) {
					maxWidth = 240;
				}
			}
			
			var myPhotoWrapper = wrapee.parents( ".photo_wrapper" );
			var myPhoto = myPhotoWrapper.parents( ".photo" );
			
			myPhotoWrapper.width( maxWidth );
			myPhoto.width( maxWidth + 20 );
		}
	} );
	
	
	if( !( document.all ) ) {
		$( ".photo" ).each( function() {
			var shadowHeight = 9;
			var myPhoto = $( ".photo_wrapper", this );
			
			var myDir = $( "#directory" ).text();
			var myShadow = $( "<img />" ).attr( "src", myDir + "/lib/img/photo-small-bg.png" ).addClass( "the_shadow" );//, this );
			
			myShadow.load( function() {
				$( this ).height( this.height );
				$( this ).width( myPhoto.width() );
				myPhoto.after( myShadow );
			} );
		} );
	}
	
	$( "form label" ).each( function() {
		var labelText = $( this ).text();
		
		$( "input#" + $( this ).attr( "for" ) + ", textarea#" + $( this ).attr( "for" ) ).addClass( "waiting" ).val( labelText ).focus( function() {
			if( $( this ).hasClass( "waiting" ) ) {
				$( this ).removeClass( "waiting" ).val( "" );
			}
		} ).blur( function() {
			if( $.trim( $( this ).val() ) == "" ) {
				$( this ).addClass( "waiting" ).val( labelText );
			}
		} );
				
		$( this ).hide();
	} );
	
	$( "form#contact" ).submit( function() {
		var retVal = true;
		
		$( ".element", this ).removeClass( "error" );
		
		$( "input.waiting, textarea.waiting", this ).each( function() {
			$( this ).parent( ".element" ).addClass( "error" );
			retVal = false;
		} );
		
		if( retVal ) {
			var myDir = $( "#directory" ).text();
			var loader = $( "<p></p>" ).addClass( "sending" ).append( $( "<img></img>" ).attr( "src", myDir + "/lib/img/loader.gif" ).attr( "alt", "Sending..." ).addClass( "noFrame" ) ).append( " Sending..." );
			$( "#submit", this ).css( "opacity", 0.5 ).css( "cursor", "default" ).after( loader ).click( function() {
				return false;
			} );
			
			//<p class="sending"><img src="<?php bloginfo( 'template_directory' ); ?>/lib/img/loader.gif" alt="Sending..." class="noFrame" /> Sending...</p>
			
			var name = $( "input#name", this ).val();
			var email = $( "input#email", this ).val();
			var comment = $( "textarea#comment", this ).val();
			
			var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment;  
			//alert (dataString);return false;  
			$.ajax( {  
				type: "POST",  
				url: myDir + "/lib/bin/email.php",  
				data: dataString,  
				success: function() {
					$( "#contact-form" ).html( "<div id='message'></div>" );
					$( "#message" ).html( "<h3>Contact Form Submitted!</h3>" )
						.append( "<p>We will be in touch with you soon!</p>" )
						.hide()  
						.fadeIn( 1500, function() {  
							  
						} );  
				}  
			} );
			
			return false;
		}
		
		return retVal;
	} );
} );

Cufon.replace( '#nav a', {
	fontFamily: 'Brothers',
	fontSize: '20px',
	textShadow: '#000 1px 1px'
} );

Cufon.replace( '.price', {
	fontFamily: 'Brothers',
	fontSize: '34px'
} );

Cufon.replace( '#content h1', {
	color: '-linear-gradient( #6f5956, 0.75=#251917, #251917 )',
	fontFamily: 'Brothers',
	textShadow: '#fff 0 1px'
} );

Cufon.replace( '#content h1 span', {
	color: '-linear-gradient( #6f5956, 0.75=#251917, #251917 )',
	fontFamily: 'Brothers Word Logos',
	textShadow: '#fff 0 1px'
} );