$(document).ready( function() {

	$( '.toplure' ).click( function() {
		$( '#lureSize' ).val( $( this ).attr( 'id' ) );
		$( '#lureSize' ).trigger( 'change' );
	});

	$( '.hooks' ).live( 'change', function() {
		// What we want to do now is
		// Update the paypal name since the hook is changing.
		var id = $( this ).attr( 'id' );
		var info =  $( '#paypal_' + id + '_name' ).val();
		
		info = info.split( ' / ' );
		info[ 1 ] = $( this ).val();
		info[ 1 ] = info[ 1 ].replace( ' / ', '' );
		
		var output = '';
		var i;
		for( i = 0; i < info.length; i++ ) {
			output = output + info[ i ];
			if( i != info.length - 1 )
				output = output + ' / ';
		}
		
		$( '#paypal_' + id + '_name' ).val( output );
		
	});
		
	$( '.features' ).live( 'change', function() {
		// What we want to do now is
		// Update the paypal name since the hook is changing.
		var id = $( this ).attr( 'id' );
		id = id.split( '_' );
		
		var info =  $( '#paypal_' + id[ 0 ] + '_name' ).val();
		info = info.split( ' / ' );
		info[ 2 ] = $( this ).val();
		info[ 2 ] = info[ 2 ].replace( ' / ', '' );
		
		var color = $( '#' + id[ 0 ] + '_price' ).css( 'color' );
		
		if( info[ 2 ] == 'Special Feature: Glow' ) {
			// Add $1.00 to price.
			$( '#paypal_' + id[ 0 ] + '_amount' ).val(  id[ 2 ]  );
			$( '#' + id[ 0 ] + '_price' ).fadeOut( 'fast' );
			$( '#' + id[ 0 ] + '_price' ).css( 'color', 'green' );
			$( '#' + id[ 0 ] + '_price' ).fadeIn( 'fast' );
			$( '#' + id[ 0 ] + '_price' ).fadeOut( 'fast' );
			$( '#' + id[ 0 ] + '_price' ).fadeIn( 'fast' );
			$( '#' + id[ 0 ] + '_price' ).fadeOut( 'fast', function() { $( '#' + id[ 0 ] + '_price' ).css( 'color', color ) } );
			$( '#' + id[ 0 ] + '_price' ).fadeIn( 'fast' );
			$( '#' + id[ 0 ] + '_price' ).text(  id[ 2 ]  );
		} else {
			// Selected not a glow.
			// Check what current price is, versus the original price.
			if( id[ 1 ] != $( '#paypal_' + id[ 0 ] + '_amount' ).val() ) {
				// The current price does not match the original price.
				// This means it was probably increased from glow earlier. lets decrease it.
				$( '#' + id[ 0 ] + '_price' ).text( id[ 1 ] );
				$( '#paypal_' + id[ 0 ] + '_amount' ).val(  id[ 2 ]  );
			}
		}
		
		var output = '';
		var i;
		for( i = 0; i < info.length; i++ ) {
			output = output + info[ i ];
			if( i != info.length - 1 )
				output = output + ' / ';
		}
		
		$( '#paypal_' + id[ 0 ] + '_name' ).val( output );
		
	});

	$( '.size' ).change( function() {
		var id = $( this ).attr( 'id' );
		var parent = $(this).parent();
		var info = $(this).val();
		info = info.split( '_' );
		
		$( '#' + id + '_price' ).text( info[ 0 ] );
		$( this ).parent().find( '.favorite' ).attr( 'name', $( this ).attr( 'id' ) + '_' + $( ".size :selected" ).attr( 'name' ) );
		
		if( info[ 0 ] != info[ 2 ] ) {
			$( '#' + id + '_price' ).css( 'color', '#ff0000' );
			$( '#' + id + '_symbol' ).css( 'color', '#ff0000' );
		} else {
			$( '#' + id + '_price' ).css( 'color', '#433014' );
			$( '#' + id + '_symbol' ).css( 'color', '#433014' );
		}
		
		$( '#paypal_' + id + '_amount' ).val( info[ 0 ] );

		// Update
		$( '#paypal_' + id + '_name' ).val( $(this).attr( 'name' ) + ' Size ' + $( ".size :selected" ).attr( 'name' ) + info[ 1 ] );
		
		$.post( "fav_item.php", {
			type: 'checkfav',
			info: id + '_' + $( ".size :selected" ).attr( 'name' )
		}, function( response ) {
			if( response == 'favorited' ) {
				// This size is already favorited.
				// Change .favorite to .favorited
				parent.find( '.favorite' ).attr( 'class', 'favorited' );
				parent.find( '.favorited' ).attr( 'disabled', true );
			} else {
				// Not yet favorited
				// Check if the button is 'favorited' right now, and change it back.
				parent.find( '.favorited' ).attr( 'class', 'favorite' );
				parent.find( '.favorite' ).attr( 'disabled', false );
				// Set the value
				parent.find( '.favorite' ).attr( 'name', id + '_' + $( ".size :selected" ).attr( 'name' ) );
			}
		});
		
	});
		
	$( '.quantity' ).live( 'keyup', function( e ) {
		var id = $( this ).attr( 'id' );
		this.value = this.value.replace(/[^0-9\.]/g,'');
		if( parseInt( $(this).val() ) < 1 || $(this).val() == '' )
			$(this).val( 1 );
		$( '#paypal_' + id + '_quantity').val( $(this).val() );
	});
	
	$( '.quantity' ).live( 'focus' , function() {
		this.value = this.value.replace(/[^0-9\.]/g,'');
	});
	
	$( '.quantity' ).live( 'blur', function() {
		if( $( this ).val() == '' )
			this.value = '1';
	});
	
	function bounce() {
		$("#view_fave_btn").css( 'position', 'relative' );
		$("#view_fave_btn").animate( { 'top': -10 }, 250 );
		$("#view_fave_btn").animate( { 'top': 0 }, 250 );
		bounce();
	}
	
	$( '.favorite' ).live( 'click', function() {
		var preserve = $(this);
		preserve.attr( 'class', 'saving' );
		preserve.attr( 'disabled', true );
		$.post("fav_item.php", {
			type: 'favorite',
			id: $(this).attr( 'name' )
		}, function(response) {
			if( response == 'saved' ) {
				preserve.attr( 'class', 'favorited' );
				preserve.attr( 'disabled', true );
				bounce();
			} else {
				// Couldn't favorite. Revert button
				preserve.attr( 'class', 'favorite' );
				preserve.attr( 'disabled', false );
				alert( response );
			}
		});
	});
	
	$( '#clear_favorites' ).click( function() {
		$.post( "fav_item.php", {
			type: 'clear_favorites'
		}, function( response ) {
			if( response == 'success' ) {
				location.reload();
			} else {
				alert( 'Could not clear favorites. Please try again.' );
			}
		});
	} );
	
	$( '.remove_favorite' ).click( function() {
		var id = $(this).attr( 'id' );
		$.post( "fav_item.php", {
			type: 'remove_favorite',
			pid: id
		}, function( response ) {
			if( response == 'success' ) {
				location.reload();
			} else {
				alert( response );
			}
		});	
	});
	
	$( '.viewLarge' ).colorbox();
	
	$( '#lureSize' ).change( function() {
	
		// Update the View Cart & Check out Buttons
		$( '#cart_return_checkout' ).val( 'http://lymanlures.com/store.php?category=99&sec=' + $( this ).val() );
		$( '#cart_return_viewcart' ).val( 'http://lymanlures.com/store.php?category=99&sec=' + $( this ).val() );
		
		var id = $( this ).val();
		if( id == '' ) {
			$( '.items_mid' ).html( '<div class="left_item">Please select a Lure size above to view the lures for sale.</div>' );
		} else {
			$( '.items_mid' ).html( '<div class="left_item" style="padding-top: 30px; text-align: center"><img src="images/store/loading.gif" style="padding-right: 5px" />Loading...</div>' );
			$.post( "lures_ajax.php", {
				type: 'get_lures',
				size: id
			}, function( response ) {
				if( response == 'nolures' ) {
					$( '.items_mid' ).html( '<div style="padding-left: 15px">It appears we do not have any lures in our system of this size!</div>' );
				} else {
					$( '.items_mid' ).html( response );
					$( '.viewLarge' ).colorbox();
				}
			});
		}
	});
	
	if( $( '#lureSize' ).val() != '' ) {
		// Initial lure size is set.
		$( '#lureSize' ).trigger( 'change' );
	}
	
});
