var designPreview = false;
var designChanges = false;

// FUNCTIONS
//--------------------------------------------------

// SET REMINDER TO SAVE WORK
function changeRemind() {
	if (designChanges == false) {
		//$( '#myForm' ).prepend( '<p>SAVE YOUR WORK!!!</p>' );
		designChanges = true;
		
		//$( '.submit-top input,.submit-btm input' ).attr("src","/admin/images/btn_save_ani.gif");
	}
}

// RADIO BUTTON FUNCTION
jQuery.fn.extend({
	check: function() {
		return this.each(function() { this.checked = true; });
	},
	uncheck: function() {
		return this.each(function() { this.checked = false; });
	}
});

// PRELOAD IMAGES
$.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		$("<img>").attr("src", arguments[i]);
	}
}

// jQUERY Onready
//--------------------------------------------------
$(document).ready (function() {
		
	// ADD BUSY BAR TO FORMS
	$( '.submit,.submit-btm,.submit-upload' ).before( '<p class="progress"><img src="/images/loadingAnimation.gif" /></p>' );
	$( '.submit-top' ).before( '<p class="progress-top"><img src="/images/loadingAnimation.gif" /></p>' );
	$( '.progress,.progress-top' ).css( 'display','none' );
	
	$( '#myForm' ).submit(
		function() {
			
			$( '.submit,.submit-top,.submit-btm,.submit-upload' ).css( 'display','none' );
			$( '.progress,.progress-top' ).css( 'display','block' );
			
		}
	);
	
	// ADD BUSY CIRCLE
	$( '.submit-upload,.submit-add' ).before( '<img src="/admin/images/ajax-loader_upload.gif" class="uploadingAnimation" />' );
	$( '.uploadingAnimation' ).css( 'display','none' );
	$( '#myTinyForm' ).submit(
		function() {
			
			$( '.submit-upload,.submit-add' ).css( 'display','none' );
			$( '.uploadingAnimation' ).css( 'display','block' );
			
		}
	);
	
	// ADD BUSY BAR DELETE AND STATUS BUTTONS
	$( '.makemebusy' ).before( '<span class="busy"><img src="/admin/images/ajax-loader.gif" /></span>' );
	$( '.busy' ).css( 'display','none' );
	
	$( '.myForm' ).submit(
		function() {
			
			$( '.makemebusy',this ).css( 'display','none' );
			$( '.busy',this ).css( 'display','inline' );
			
		}
	);
	
	// INPUT FOCUS
	$( '.text' ).focus ( function() { $(this).addClass("focus"); } )
	$( '.text' ).blur ( function() { $(this).removeClass("focus"); } )
	
	// TEXTAREA FOCUS
	$( '.textarea' ).focus ( function() { $(this).addClass("focus"); } )
	$( '.textarea' ).blur ( function() { $(this).removeClass("focus"); } )
	
	// SUBMIT BUTTONS HOVER
	$( '.submit-top input,.submit-btm input' ).hover(
		function(){ $(this).attr("src","/admin/images/btn_save_over.png"); },
		function(){ $(this).attr("src","/admin/images/btn_save.png"); }
	);
	$( 'input.submit-upload' ).hover(
		function(){ $(this).attr("src","/admin/images/btn_upload_over.png"); },
		function(){ $(this).attr("src","/admin/images/btn_upload.png"); }
	);
	$( '.submit-add' ).hover(
		function(){ $(this).attr("src","/admin/images/btn_add_page_over.png"); },
		function(){ $(this).attr("src","/admin/images/btn_add_page.png"); }
	);
	
	// CANCEL BUTTON CONFIRM SUBMIT
	$( '.submit-top a,.submit-btm a' ).click(
		function() {

			var agree = confirm( 'Are you sure? Your changes will not be saved!' );
			if (agree) {
				return true;
			} else {
				return false;
			}

		}
	);
	
	// CONFIRM CLICK IF DESIGN CHANGES HAVE BEEN MADE
	$( '#sub-navigation a,#site-navigation a,#site-branding a' ).click(
		function() {

			if (designChanges == true) {
				var agree = confirm( 'Are you sure? Your changes will not be saved!' );
				if (agree) {
					return true;
				} else {
					return false;
				}
			}
		}
	);
	
	// SIGNIN BUTTON HOVER
	$( '.signin input' ).hover(
		function(){ $(this).attr("src","/admin/images/btn_signin_over.png"); },
		function(){ $(this).attr("src","/admin/images/btn_signin.png"); }
	);
	
	// PASSWORD BUTTON HOVER
	$( '.password input' ).hover(
		function(){ $(this).attr("src","/admin/images/btn_password_over.png"); },
		function(){ $(this).attr("src","/admin/images/btn_password.png"); }
	);
	
	// CONFIRMATION MESSAGE FADE OUT
	$( '.success,.form_error' ).animate({opacity: 1.0}, 5000);
	$( '.success,.form_error' ).fadeOut('slow', function() {
		$(this).remove();
	});
	
	// DELETE BUTTON HOVER
	$( '.btn_delete input' ).hover(
		function(){ $(this).attr("src","images/btn_delete_over.gif"); },
		function(){ $(this).attr("src","images/btn_delete.gif"); }
	);
	
	// RE-ORDER BUTTON HOVER
	$( '.btn_order_dn input' ).hover(
		function(){ $(this).attr("src","images/btn_order_dn_over.gif"); },
		function(){ $(this).attr("src","images/btn_order_dn.gif"); }
	);
	$( '.btn_order_up input' ).hover(
		function(){ $(this).attr("src","images/btn_order_up_over.gif"); },
		function(){ $(this).attr("src","images/btn_order_up.gif"); }
	);
	
	// STATUS BUTTON HOVER
	$( '.btn_status_0 input' ).hover(
		function(){ $(this).attr("src","images/btn_status_0_over.gif"); },
		function(){ $(this).attr("src","images/btn_status_0.gif"); }
	);
	$( '.btn_status_1 input' ).hover(
		function(){ $(this).attr("src","images/btn_status_1_over.gif"); },
		function(){ $(this).attr("src","images/btn_status_1.gif"); }
	);
	
	// HOME BUTTON HOVER
	$( '.btn_home input' ).hover(
		function(){ $(this).attr("src","images/house_on.gif"); },
		function(){ $(this).attr("src","images/house_off.gif"); }
	);
	
	// REQUIRED FIELDS
	$( '.required .text' ).after( '<img src="/admin/images/required.gif" />' );
	
	// HIGHLIGHT RECENTLY ADDED ROW
	$( 'tr.highlightThis' ).highlightFade( {color:'yellow',speed:2000} );
	
	// PREVIEW CONTENT
	$( '.myPreview' ).parent().after( '<p class="tools-textarea"><a class="trigger" href="#">Show Live Preview</a></p>' );
	$( '.myPreview' ).parent().before( '<div id="previewcomment">&nbsp;</div>' );
	$( '.trigger' ).parent().css({ color: "#ccc" });
	
	$( '#previewcomment' ).hide();
	$( 'a.trigger' ).toggle ( 
		function() {
			$( '.trigger' ).html( 'Hide Live Preview' );
			$( '#previewcomment' ).fadeIn("fast");
			reloadPreviewDiv( $('.myPreview').attr('id') );
		},
		function() {
			$( '.trigger' ).html( 'Show Live Preview' );
			$( '#previewcomment' ).fadeOut("fast");
		}
	);
	
	$( '.myPreview' ).keyup( function() { reloadPreviewDiv( $(this).attr('id') ); } );
	
	// FORMATTING GUIDE
	$( '.trigger' ).after( ' | <a class="formatting" href="#">Show Formatting Guide</a>' );
	$( '.trigger' ).parent().after( '<div id="formatting_guide"><table cellspacing="0"><thead><tr><th width="20%">To do this&hellip;</th><th width="30%">Type this&hellip;</th><th width="20%">To do this&hellip;</th><th width="30%">Type this&hellip;</th></tr></thead><tbody><tr><td><h3>Subheading</h3></td><td>h3. Subheading</td><td><ul><li>Bullet List</li></ul></td><td>* Bullet List</td></tr><tr><td><strong>Bold text</strong></td><td>*Bold text*</td><td><ol><li>Number List</li></ol></td><td># Number List</td></tr><tr><td><em>Italic text</em></td><td>_Italic text_</td><td><a href="http://www.yoursite.com">Link</a></td><td>"Link":http://www.yoursite.com</td></tr><tr><td colspan="4"><a href="_inc/formatting_reference.php?keepThis=true&TB_iframe=true&height=500&width=500" title="Formatting Reference" class="thickbox">Formatting Reference</a></td></tr></tbody></table></div>' );
	
	$( '#formatting_guide' ).hide();
	$( 'a.formatting' ).toggle ( 
		function() {
			$( '.formatting' ).html( 'Hide Formatting Guide' );
			$( '#formatting_guide' ).fadeIn("fast");
		},
		function() {
			$( '.formatting' ).html( 'Show Formatting Guide' );
			$( '#formatting_guide' ).fadeOut("fast");
		}
	);
	
	// RESIZABLE TEXT AREA
	$( '.trigger' ).before( '<a class="trigger-resizable" href="#">More Typing Area</a>' );
	$( 'a.trigger-resizable' ).toggle ( 
		function() {
			$( this ).html( 'Less Typing Area' );
			$( '.resizable' ).css({ height: "650px" });
		},
		function() {
			$( this ).html( 'More Typing Area' );
			$( '.resizable' ).css({ height: "350px" });
		}
	);
	
	
	// EXPAND / CONTRACT LIVE PREVIEW
	$( '#site-preview' ).after( '<p id="trigger-previewExpand"><a href="#">more</a></p>' );
	
	$( '#trigger-previewExpand a' ).toggle ( 
		function() {
			$( this ).html( 'less' );
			$( '#site-preview' ).css( 'height','350px' );
			$( '#preview-browser' ).css( 'min-height','350px' );
		},
		function() {
			$( this ).html( 'more' );
			$( '#site-preview' ).css( 'height','250px' );
			$( '#preview-browser' ).css( 'min-height','250px' );
		}
	);
	
	// OPEN / CLOSE LIVE PREVIEW
	if (designPreview == true) {
		$( '#content-heading' ).append( '<a class="trigger-preview" href="#">Show Live Preview</a>' );
		$( '#site-preview' ).hide();
		$( '#trigger-previewExpand' ).hide();
		
		$( 'a.trigger-preview' ).toggle ( 
			function() {
				$( this ).html( 'Hide Live Preview' );

				$( '#site-preview' ).slideDown();
				if (!$.browser.msie) { $( '#trigger-previewExpand' ).show(); }
			},
			function() {
				$( this ).html( 'Show Live Preview' );
				
				$( '#trigger-previewExpand' ).hide();
				$( '#site-preview' ).slideUp();
			}
		);
	}
	
	// GRAPHIC RADIO BUTTONS FOR SITE POSITION
	$( '#site_position_left' ).parent().addClass("graphic_radio");
	$( '#site_position_center' ).parent().addClass("graphic_radio");
	
	$( '#site_position_left' ).parent().before( '<a href="#" id="check_position_left" title="Set position of the page within the browser to left."></a>' );
	$( '#site_position_center' ).parent().before( '<a href="#" id="check_position_center" title="Set position of the page within the browser to center."></a>' );
	
	$( 'input[@name="site_position"]:checked' ).val() == 'left' ? $( '#check_position_left' ).addClass( 'selected_left' ) : $( '#check_position_center' ).addClass( 'selected_center' );

	$( '#check_position_left' ).click (
		function() {
			$( '#site_position_left' ).check();
			$( '#check_position_left' ).toggleClass( 'selected_left' );
			$( '#check_position_center' ).toggleClass( 'selected_center' );
			$( '#preview-wrapper' ).css( 'margin','0 0 0 0' );
			
			changeRemind();
			return false;
		}
	);
	$( '#check_position_center' ).click (
		function() {
			$( '#site_position_center' ).check();
			$( '#check_position_center' ).toggleClass( 'selected_center' );
			$( '#check_position_left' ).toggleClass( 'selected_left' );
			$( '#preview-wrapper' ).css( 'margin','0 auto' );
			
			changeRemind();
			return false;
		}
	);
	
	// GRAPHIC RADIO BUTTONS FOR MENU POSITION
	$( '.menu_position' ).parent().addClass("graphic_radio");
	
	$( '#menu_position_left' ).parent().before( '<a href="#" id="check_menu_left" title="Set position of the menu to left."></a>' );
	$( '#menu_position_top' ).parent().before( '<a href="#" id="check_menu_top" title="Set position of the menu to top."></a>' );
	$( '#menu_position_right' ).parent().before( '<a href="#" id="check_menu_right" title="Set position of the menu to right."></a>' );
	$( '#menu_position_none' ).parent().before( '<a href="#" id="check_menu_none" title="Hide the menu."></a>' );
	
	if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' ) {
		$( '#check_menu_left' ).addClass( 'menu_selected_left' );
	} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
		$( '#check_menu_top' ).addClass( 'menu_selected_top' );
		$( '#menu_options_width' ).hide();
	} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
		$( '#check_menu_right' ).addClass( 'menu_selected_right' );
	} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'none' ) {
		$( '#check_menu_none' ).addClass( 'menu_selected_none' );
		$( '.menu_options' ).hide();
	}
	
	$( '#check_menu_left' ).click (
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' ) {
				$( '#check_menu_left' ).toggleClass( 'menu_selected_left' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				$( '#check_menu_top' ).toggleClass( 'menu_selected_top' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				$( '#check_menu_right' ).toggleClass( 'menu_selected_right' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'none' ) {
				$( '#check_menu_none' ).toggleClass( 'menu_selected_none' );
			}
			
			// CONTENT WIDTH
			var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
			$( '#preview-content' ).css( 'width',contentWidth );
			
			// MENU WIDTH
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
			var cssContentWidth = siteWidth - menuWidth - 2;
			
			var adjMenuWidth = menuWidth - menuPadR - menuPadL;
			var adjButtonWidth = adjMenuWidth - buttonPadR - buttonPadL;
			
			$( '#dimension-content .dimension-value' ).html( cssContentWidth + 'px' );
			$( '#dimension-menu .dimension-value' ).html( menuWidth + 'px' );
			$( '#dimension-button .dimension-value' ).html( adjMenuWidth + 'px' );

			$( '#preview-nav' ).css( 'width',adjMenuWidth );
			$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			
			
			$( '#menu_position_left' ).check();
			$( '#check_menu_left' ).toggleClass( 'menu_selected_left' );
			
			$( '.menu_options' ).fadeIn("fast");
			$( '#menu_options_width' ).fadeIn("fast");
			
			$( '#preview-nav' ).show();
			$( '#preview-nav' ).css( 'float','left' );
			$( '#preview-nav li' ).css( 'float','left' );
			$( '#preview-nav li a' ).css( 'float','none' );
			$( '#preview-content' ).css( 'float','right' );
			
			changeRemind();
			return false;
		}
	);
	
	$( '#check_menu_top' ).click (
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' ) {
				$( '#check_menu_left' ).toggleClass( 'menu_selected_left' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				$( '#check_menu_top' ).toggleClass( 'menu_selected_top' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				$( '#check_menu_right' ).toggleClass( 'menu_selected_right' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'none' ) {
				$( '#check_menu_none' ).toggleClass( 'menu_selected_none' );
			}
			
			// CONTENT WIDTH
			var contentWidth = siteWidth - contentPadR - contentPadL;
			$( '#preview-content' ).css( 'width',contentWidth );
			
			// MENU WIDTH
			var menuWidth = siteWidth - menuPadR - menuPadL;
			$( '#preview-nav' ).css( 'width',menuWidth );
			
			// BUTTON WIDTH
			var adjButtonWidth = Math.ceil((menuWidth)/navigationPreview - buttonPadR - buttonPadL);
			var cssButtonWidth = Math.ceil((menuWidth)/navigationPreview);
			$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			
			var lastButtonWidth = (menuWidth - ((Math.ceil((menuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
			$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
			
			$( '#menu_position_top' ).check();
			$( '#check_menu_top' ).toggleClass( 'menu_selected_top' );
			
			$( '.menu_options' ).fadeIn("fast");
			$( '#menu_options_width' ).fadeOut("fast");
			
			$( '#dimension-content .dimension-value' ).html( siteWidth + 'px' );
			$( '#dimension-menu .dimension-value' ).html( siteWidth + 'px' );
			$( '#dimension-button .dimension-value' ).html( cssButtonWidth + 'px' );
			
			$( '#preview-nav' ).show();
			$( '#preview-nav' ).css( 'float','none' );
			$( '#preview-nav' ).css( 'overflow','hidden' );
			$( '#preview-content' ).css( 'float','none' );
			$( '#preview-nav li' ).css( 'float','left' );
			$( '#preview-nav li' ).css( 'width','auto' );
			$( '#preview-nav li a' ).css( 'float','left' );
			
			changeRemind();
			return false;
		}
	);
	
	$( '#check_menu_right' ).click (
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' ) {
				$( '#check_menu_left' ).toggleClass( 'menu_selected_left' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				$( '#check_menu_top' ).toggleClass( 'menu_selected_top' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				$( '#check_menu_right' ).toggleClass( 'menu_selected_right' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'none' ) {
				$( '#check_menu_none' ).toggleClass( 'menu_selected_none' );
			}
			
			// CONTENT WIDTH
			var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
			var cssContentWidth = siteWidth - menuWidth - 2;
			$( '#preview-content' ).css( 'width',contentWidth );
			
			// MENU WIDTH
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			var adjMenuWidth = menuWidth - menuPadR - menuPadL;
			var adjButtonWidth = adjMenuWidth - buttonPadR - buttonPadL;
			
			$( '#dimension-content .dimension-value' ).html( cssContentWidth + 'px' );
			$( '#dimension-menu .dimension-value' ).html( menuWidth + 'px' );
			$( '#dimension-button .dimension-value' ).html( adjMenuWidth + 'px' );
			
			$( '#preview-nav' ).css( 'width',adjMenuWidth );
			$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			
			
			$( '#menu_position_right' ).check();
			$( '#check_menu_right' ).toggleClass( 'menu_selected_right' );
			
			$( '.menu_options' ).fadeIn("fast");
			$( '#menu_options_width' ).fadeIn("fast");
			
			$( '#preview-nav' ).show();
			$( '#preview-nav' ).css( 'float','right' );
			$( '#preview-nav li' ).css( 'float','right' );
			$( '#preview-nav li a' ).css( 'float','none' );
			$( '#preview-content' ).css( 'float','left' );
			
			changeRemind();
			return false;
		}
	);
	
	$( '#check_menu_none' ).click (
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' ) {
				$( '#check_menu_left' ).toggleClass( 'menu_selected_left' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				$( '#check_menu_top' ).toggleClass( 'menu_selected_top' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				$( '#check_menu_right' ).toggleClass( 'menu_selected_right' );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'none' ) {
				$( '#check_menu_none' ).toggleClass( 'menu_selected_none' );
			}
			
			$( '#dimension-content .dimension-value' ).html( siteWidth + 'px' );
			$( '#dimension-menu .dimension-value' ).html( 'n/a' );
			$( '#dimension-button .dimension-value' ).html( 'n/a' );
			
			$( '#menu_position_none' ).check();
			$( '#check_menu_none' ).toggleClass( 'menu_selected_none' );
			
			$( '.menu_options' ).fadeOut("fast");
			
			var contentWidth = siteWidth - contentPadR - contentPadL;
			$( '#preview-content' ).css( 'width',contentWidth );
			$( '#preview-nav' ).hide();
			
			changeRemind();
			return false;
		}
	);
	
	// OPEN / CLOSE BODY BACKGROUND IMAGE PROPERTIES
	$( '#body_img_options' ).hide();
	$( '#body_img' ).change( function() { $( '#body_img_options' ).fadeIn("fast"); } );
	
	// OPEN / CLOSE MENU BACKGROUND IMAGE PROPERTIES
	$( '#menu_img_options' ).hide();
	$( '#menu_img' ).change( function() { $( '#menu_img_options' ).fadeIn("fast"); } );
	
	// OPEN / CLOSE HEADER IMAGE PROPERTIES
	$( '#branding_img' ).change( function() { $( '#branding_img_options' ).fadeOut("fast"); } );
	
	// OPEN / CLOSE MAIN BACKGROUND IMAGE PROPERTIES
	$( '#main_img_options' ).hide();
	$( '#main_img' ).change( function() { $( '#main_img_options' ).fadeIn("fast"); } );
	
	// OPEN / CLOSE FOOTER BACKGROUND IMAGE PROPERTIES
	$( '#info_img_options' ).hide();
	$( '#info_img' ).change( function() { $( '#info_img_options' ).fadeIn("fast"); } );
	
	// OPEN / CLOSE CONTENT BACKGROUND IMAGE PROPERTIES
	$( '#content_img_options' ).hide();
	$( '#content_img' ).change( function() { $( '#content_img_options' ).fadeIn("fast"); } );
	
	// OPEN / CLOSE CSS MODE
	$( '#css_file_options' ).hide();
	$( '#css_file' ).change( function() { $( '#css_file_options' ).fadeIn("fast"); } );
	
	// OPEN CLOSE HIDE-ABLE OPTIONS
	$( '.design-options' ).before( '<p><a class="design-options-trigger" href="#">Show Options</a></p>' );
	$( '.design-options' ).hide();
	
	$( 'a.design-options-trigger' ).toggle ( 
		function() {
			$( this ).html('Hide Options');
			$( this.parentNode ).next(".design-options").fadeIn('fast');
		},
		function() {
			$( '.design-options-trigger' ).html( 'Show Options' );
			$( this.parentNode ).next(".design-options").fadeOut("fast");
		}
	);
	
	// SET NUMERIC ONLY TEXT INPUTS
	$( 'input.numeric' ).numeric();
	
	// SHOW / HIDE FILE BIN DETAILS
	$( '.file-infolink' ).append( '<a class="info_trigger" href="#">Show Info</a>' );
	$( '.file-info' ).hide();
	$( 'a.info_trigger' ).toggle ( 
		function() {
			$( this ).html( 'Hide Info' );
			$( this.parentNode.parentNode ).next('.file-info').fadeIn('fast');
		},
		function() {
			$( this ).html( 'Show Info' );
			$( this.parentNode.parentNode ).next('.file-info').fadeOut('fast');
		}
	);
	
	// OPEN / CLOSE FILE UPLOAD PROPERTIES
	$( '.imageonly,.imageswfonly,.cssonly,.jsonly' ).after( '<span class="imagenotice"></span>' );
	$( '.imagenotice' ).hide();
	$( '.imageonly' ).change(
		function() {
			
			var myString = $(this).attr( 'value' );
			var myArray = myString.split('/');
			
			var myFile = myArray[myArray.length-1];
			var myExt = myFile.substr( myFile.lastIndexOf('.')+1,myFile.length );
			
			if (myExt.toLowerCase() != 'gif' && myExt.toLowerCase() != 'jpg' && myExt.toLowerCase() != 'jpeg') {
				$(this).next( '.imagenotice' ).html( '<strong>' + myFile + '</strong><br />Files with the extension \'' + myExt + '\' are not valid.' );
				$(this).attr( 'value','');
				
				$(this).next( '.imagenotice' ).fadeIn( 'fast' );
			} else {
				$(this).next( '.imagenotice' ).hide();
			}

		}
	);
	$( '.imageswfonly' ).change(
		function() {
			
			var myString = $(this).attr( 'value' );
			var myArray = myString.split('/');
			
			var myFile = myArray[myArray.length-1];
			var myExt = myFile.substr( myFile.lastIndexOf('.')+1,myFile.length );
			
			if (myExt.toLowerCase() != 'gif' && myExt.toLowerCase() != 'jpg' && myExt.toLowerCase() != 'jpeg' && myExt.toLowerCase() != 'swf') {
				$(this).next( '.imagenotice' ).html( '<strong>' + myFile + '</strong><br />Files with the extension \'' + myExt + '\' are not valid.' );
				$(this).attr( 'value','');
				//$(this).next( '.imagenotice' ).highlightFade( {color:'#a00',speed:1000} );
				
				$(this).next( '.imagenotice' ).fadeIn( 'fast' );
			} else {
				$(this).next( '.imagenotice' ).hide();
			}

		}
	);
	$( '.cssonly' ).change(
		function() {
			
			var myString = $(this).attr( 'value' );
			var myArray = myString.split('/');
			
			var myFile = myArray[myArray.length-1];
			var myExt = myFile.substr( myFile.lastIndexOf('.')+1,myFile.length );
			
			if (myExt.toLowerCase() != 'css') {
				$(this).next( '.imagenotice' ).html( '<strong>' + myFile + '</strong><br />Files with the extension \'' + myExt + '\' are not valid. Please choose a CSS file.' );
				$(this).attr( 'value','');
				//$(this).next( '.imagenotice' ).highlightFade( {color:'#a00',speed:1000} );
				
				$(this).next( '.imagenotice' ).fadeIn( 'fast' );
				$( '#css_file_options' ).hide();
			} else {
				$(this).next( '.imagenotice' ).hide();
			}

		}
	);
	$( '.jsonly' ).change(
		function() {
			
			var myString = $(this).attr( 'value' );
			var myArray = myString.split('/');
			
			var myFile = myArray[myArray.length-1];
			var myExt = myFile.substr( myFile.lastIndexOf('.')+1,myFile.length );
			
			if (myExt.toLowerCase() != 'js') {
				$(this).next( '.imagenotice' ).html( '<strong>' + myFile + '</strong><br />Files with the extension \'' + myExt + '\' are not valid.' );
				$(this).attr( 'value','');
				//$(this).next( '.imagenotice' ).highlightFade( {color:'#a00',speed:1000} );
				
				$(this).next( '.imagenotice' ).fadeIn( 'fast' );
			} else {
				$(this).next( '.imagenotice' ).hide();
			}

		}
	);
	
	// LIVE PREVIEW : SITE WIDTH
	$( '#site_width' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			var infoPadR = parseInt( $( '#info_pad_r' ).attr( 'value' ) );
			var infoPadL = parseInt( $( '#info_pad_l' ).attr( 'value' ) );
			
			if ( $( '#menu_position' ).attr( 'value' ) == "left" || $( '#menu_position' ).attr( 'value' ) == "right" ) {
				var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
				var cssContentWidth = siteWidth - menuWidth - 2;
			} else {
				var contentWidth = siteWidth - contentPadR - contentPadL;
				var cssContentWidth = siteWidth;
				
				
				var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
				var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
				var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
				var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
				
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
				var adjButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview) - buttonPadR - buttonPadL;
				
				var cssButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview);
				
				$( '#preview-nav' ).css( 'width',adjMenuWidth );
				$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
				
				var lastButtonWidth = (adjMenuWidth - ((Math.ceil((adjMenuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
				$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
				
				$( '#dimension-button .dimension-value' ).html( cssButtonWidth + 'px' );
				
			}
			
			var infoWidth = siteWidth - infoPadR - infoPadL;
			
			$( '#dimension-page .dimension-value' ).html( siteWidth + 'px' );
			$( '#dimension-content .dimension-value' ).html( cssContentWidth + 'px' );
			
			$( '#preview-wrapper' ).css( 'width',siteWidth );
			$( '#preview-branding' ).css( 'width',siteWidth );
			$( '#preview-main' ).css( 'width',siteWidth );
			$( '#preview-content' ).css( 'width',contentWidth );
			$( '#preview-info' ).css( 'width',infoWidth );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : CONTENT WIDTH (PADDING-RIGHT)
	$( '#content_pad_r' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			
			if ( $( '#menu_position' ).attr( 'value' ) == "left" || $( '#menu_position' ).attr( 'value' ) == "right" ) {
				var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
			} else {
				var contentWidth = siteWidth - contentPadR - contentPadL;
			}
			
			$( '#preview-content' ).css( 'width',contentWidth );
			$( '#preview-content' ).css( 'padding-right',contentPadR );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : CONTENT WIDTH (PADDING-LEFT)
	$( '#content_pad_l' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			
			if ( $( '#menu_position' ).attr( 'value' ) == "left" || $( '#menu_position' ).attr( 'value' ) == "right" ) {
				var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
			} else {
				var contentWidth = siteWidth - contentPadR - contentPadL;
			}
			
			$( '#preview-content' ).css( 'width',contentWidth );
			$( '#preview-content' ).css( 'padding-left',contentPadL );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : MENU WIDTH
	$( '#menu_width' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var contentPadR = parseInt( $( '#content_pad_r' ).attr( 'value' ) );
			var contentPadL = parseInt( $( '#content_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' || $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				var contentWidth = siteWidth - menuWidth - contentPadR - contentPadL - 2;
				var adjMenuWidth = menuWidth - menuPadR - menuPadL;
				var adjButtonWidth = adjMenuWidth - buttonPadR - buttonPadL;
				
				var cssContentWidth = siteWidth - menuWidth - 2;
				var cssMenuWidth = menuWidth;
				var cssButtonWidth = menuWidth - menuPadR - menuPadL;
			} else {
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
			}
			
			$( '#dimension-content .dimension-value' ).html( cssContentWidth + 'px' );
			$( '#dimension-menu .dimension-value' ).html( cssMenuWidth + 'px' );
			$( '#dimension-button .dimension-value' ).html( cssButtonWidth + 'px' );
			
			$( '#preview-content' ).css( 'width',contentWidth );
			$( '#preview-nav' ).css( 'width',adjMenuWidth );
			$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : MENU WIDTH (PADDING-RIGHT)
	$( '#menu_pad_r' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' || $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				var adjMenuWidth = menuWidth - menuPadR - menuPadL;
				var adjButtonWidth = adjMenuWidth - buttonPadR - buttonPadL;
				
				var cssButtonWidth = menuWidth - menuPadR - menuPadL;
				
				$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
				var adjButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview) - buttonPadR - buttonPadL;
				
				var cssButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview);
				
				$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
				
				var lastButtonWidth = (adjMenuWidth - ((Math.ceil((adjMenuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
				$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
			}
			
			$( '#dimension-button .dimension-value' ).html( cssButtonWidth + 'px' );
			
			$( '#preview-nav' ).css( 'width',adjMenuWidth );
			$( '#preview-nav' ).css( 'padding-right',menuPadR );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : MENU WIDTH (PADDING-LEFT)
	$( '#menu_pad_l' ).keyup(
		function() {
		
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' || $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				var adjMenuWidth = menuWidth - menuPadR - menuPadL;
				var adjButtonWidth = adjMenuWidth - buttonPadR - buttonPadL;
				
				var cssButtonWidth = menuWidth - menuPadR - menuPadL;
				
				$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
			} else if ( $( 'input[@name="menu_position"]:checked' ).val() == 'top' ) {
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
				var adjButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview) - buttonPadR - buttonPadL;
				
				var cssButtonWidth = Math.ceil((adjMenuWidth)/navigationPreview);
				
				$( '#preview-nav li a' ).css( 'width',adjButtonWidth );
				
				var lastButtonWidth = (adjMenuWidth - ((Math.ceil((adjMenuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
				$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
			}
			
			$( '#dimension-button .dimension-value' ).html( cssButtonWidth + 'px' );
			
			$( '#preview-nav' ).css( 'width',adjMenuWidth );
			$( '#preview-nav' ).css( 'padding-left',menuPadL );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : BUTTON WIDTH (PADDING-RIGHT)
	$( '#button_pad_r' ).keyup(
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' || $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				var buttonWidth = menuWidth - menuPadR - menuPadL - buttonPadR - buttonPadL;
				
				$( '#preview-nav li a' ).css( 'width',buttonWidth );
			} else {
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
				var buttonWidth = Math.ceil((adjMenuWidth)/navigationPreview) - buttonPadR - buttonPadL;
				
				$( '#preview-nav li a' ).css( 'width',buttonWidth );
				
				var lastButtonWidth = (adjMenuWidth - ((Math.ceil((adjMenuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
				$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
			}
			
			$( '#preview-nav li a' ).css( 'padding-right',buttonPadR );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : BUTTON WIDTH (PADDING-LEFT)
	$( '#button_pad_l' ).keyup(
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var menuWidth = parseInt( $( '#menu_width' ).attr( 'value' ) );
			var menuPadL = parseInt( $( '#menu_pad_l' ).attr( 'value' ) );
			var menuPadR = parseInt( $( '#menu_pad_r' ).attr( 'value' ) );
			var buttonPadR = parseInt( $( '#button_pad_r' ).attr( 'value' ) );
			var buttonPadL = parseInt( $( '#button_pad_l' ).attr( 'value' ) );
			
			if ( $( 'input[@name="menu_position"]:checked' ).val() == 'left' || $( 'input[@name="menu_position"]:checked' ).val() == 'right' ) {
				var buttonWidth = menuWidth - menuPadR - menuPadL - buttonPadR - buttonPadL;
				
				$( '#preview-nav li a' ).css( 'width',buttonWidth );
			} else {
				var adjMenuWidth = siteWidth - menuPadR - menuPadL;
				var buttonWidth = Math.ceil((adjMenuWidth)/navigationPreview) - buttonPadR - buttonPadL;
				
				$( '#preview-nav li a' ).css( 'width',buttonWidth );
				
				var lastButtonWidth = (adjMenuWidth - ((Math.ceil((adjMenuWidth)/navigationPreview)) * (navigationPreview-1))) - buttonPadR - buttonPadL;
				$("#preview-nav li:last-child a").css( 'width',lastButtonWidth );
			}
			
			$( '#preview-nav li a' ).css( 'padding-left',buttonPadL );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : FOOTER WIDTH (PADDING-RIGHT)
	$( '#info_pad_r' ).keyup(
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var infoPadR = parseInt( $( '#info_pad_r' ).attr( 'value' ) );
			var infoPadL = parseInt( $( '#info_pad_l' ).attr( 'value' ) );
			
			var infoWidth = siteWidth - infoPadR - infoPadL;
			
			$( '#preview-info' ).css( 'padding-right',infoPadR );
			$( '#preview-info' ).css( 'width',infoWidth );
			
			changeRemind();
			
		}
	);
	
	// LIVE PREVIEW : FOOTER WIDTH (PADDING-LEFT)
	$( '#info_pad_l' ).keyup(
		function() {
			
			var siteWidth = parseInt( $( '#site_width' ).attr( 'value' ) );
			var infoPadR = parseInt( $( '#info_pad_r' ).attr( 'value' ) );
			var infoPadL = parseInt( $( '#info_pad_l' ).attr( 'value' ) );
			
			var infoWidth = siteWidth - infoPadR - infoPadL;
			
			$( '#preview-info' ).css( 'padding-left',infoPadL );
			$( '#preview-info' ).css( 'width',infoWidth );
			
			changeRemind();
			
		}
	);
		
});

// CONFIRM SUBMIT
//--------------------------------------------------
function confirmSubmit(message) {
	var agree = confirm(message);
	if (agree)
		return true;
	else
		return false;
}

// TEXTILE EMULATOR
//--------------------------------------------------
function superTextile(s) {
    var r = s;
    // quick tags first
    qtags = [['\\*', 'strong'],
             ['\\?\\?', 'cite'],
             ['\\+', 'ins'],  //fixed
             ['~', 'sub'],   
             ['\\^', 'sup'], // me
             ['@', 'code']];
    for (var i=0;i<qtags.length;i++) {
        ttag = qtags[i][0]; htag = qtags[i][1];
        re = new RegExp(ttag+'\\b(.+?)\\b'+ttag,'g');
        r = r.replace(re,'<'+htag+'>'+'$1'+'</'+htag+'>');
    }
    // underscores count as part of a word, so do them separately
    re = new RegExp('\\b_(.+?)_\\b','g');
    r = r.replace(re,'<em>$1</em>');
	
	//jeff: so do dashes
    re = new RegExp('[\s\n]-(.+?)-[\s\n]','g');
    r = r.replace(re,'<del>$1</del>');

    // links
    re = new RegExp('"\\b(.+?)\\(\\b(.+?)\\b\\)":([^\\s]+)','g');
    r = r.replace(re,'<a href="$3" title="$2">$1</a>');
    re = new RegExp('"\\b(.+?)\\b":([^\\s]+)','g');
    r = r.replace(re,'<a href="$2">$1</a>');

    // images
    re = new RegExp('!\\b(.+?)\\(\\b(.+?)\\b\\)!','g');
    r = r.replace(re,'<img src="$1" alt="$2">');
    re = new RegExp('!\\b(.+?)\\b!','g');
    r = r.replace(re,'<img src="$1">');
    
    // block level formatting
	
		// Jeff's hack to show single line breaks as they should.
		// insert breaks - but you get some....stupid ones
	    re = new RegExp('(.*)\n([^#\*\n].*)','g');
	    r = r.replace(re,'$1<br />$2');
		// remove the stupid breaks.
	    re = new RegExp('\n<br />','g');
	    r = r.replace(re,'\n');
	
    lines = r.split('\n');
    nr = '';
    for (var i=0;i<lines.length;i++) {
        line = lines[i].replace(/\s*$/,'');
        changed = 0;
        if (line.search(/^\s*bq\.\s+/) != -1) { line = line.replace(/^\s*bq\.\s+/,'\t<blockquote>')+'</blockquote>'; changed = 1; }
		
		// jeff adds h#.
        if (line.search(/^\s*h[1-6]\.\s+/) != -1) { 
	    	re = new RegExp('h([1-6])\.(.+)','g');
	    	line = line.replace(re,'<h$1>$2</h$1>');
			changed = 1; 
		}
		
		if (line.search(/^\s*\*\s+/) != -1) { line = line.replace(/^\s*\*\s+/,'\t<liu>') + '</liu>'; changed = 1; } // * for bullet list; make up an liu tag to be fixed later
        if (line.search(/^\s*#\s+/) != -1) { line = line.replace(/^\s*#\s+/,'\t<lio>') + '</lio>'; changed = 1; } // # for numeric list; make up an lio tag to be fixed later
        if (!changed && (line.replace(/\s/g,'').length > 0)) line = '<p>'+line+'</p>';
        lines[i] = line + '\n';
    }
	
    // Second pass to do lists
    inlist = 0; 
	listtype = '';
    for (var i=0;i<lines.length;i++) {
        line = lines[i];
        if (inlist && listtype == 'ul' && !line.match(/^\t<liu/)) { line = '</ul>\n' + line; inlist = 0; }
        if (inlist && listtype == 'ol' && !line.match(/^\t<lio/)) { line = '</ol>\n' + line; inlist = 0; }
        if (!inlist && line.match(/^\t<liu/)) { line = '<ul>' + line; inlist = 1; listtype = 'ul'; }
        if (!inlist && line.match(/^\t<lio/)) { line = '<ol>' + line; inlist = 1; listtype = 'ol'; }
        lines[i] = line;
    }

    r = lines.join('\n');
	// jeff added : will correctly replace <li(o|u)> AND </li(o|u)>
    r = r.replace(/li[o|u]>/g,'li>');

    return r;
}

function reloadPreviewDiv( textareaID ) { 
    var commentString = document.getElementById( textareaID ).value;
	var con = superTextile(commentString);
    document.getElementById('previewcomment').innerHTML = con;
}


// jQuery Plugin highlightFade (jquery.offput.ca/highlightFade)
//--------------------------------------------------
jQuery.fn.highlightFade = function(settings) {
	var o = (settings && settings.constructor == String) ? {start: settings} : settings || {};
	var d = jQuery.highlightFade.defaults;
	var i = o['interval'] || d['interval'];
	var a = o['attr'] || d['attr'];
	var ts = {
		'linear': function(s,e,t,c) { return parseInt(s+(c/t)*(e-s)); },
		'sinusoidal': function(s,e,t,c) { return parseInt(s+Math.sin(((c/t)*90)*(Math.PI/180))*(e-s)); },
		'exponential': function(s,e,t,c) { return parseInt(s+(Math.pow(c/t,2))*(e-s)); }
	};
	var t = (o['iterator'] && o['iterator'].constructor == Function) ? o['iterator'] : ts[o['iterator']] || ts[d['iterator']] || ts['linear'];
	if (d['iterator'] && d['iterator'].constructor == Function) t = d['iterator'];
	return this.each(function() {
		if (!this.highlighting) this.highlighting = {};
		var e = (this.highlighting[a]) ? this.highlighting[a].end : jQuery.highlightFade.getBaseValue(this,a) || [255,255,255];
		var c = jQuery.highlightFade.getRGB(o['start'] || o['colour'] || o['color'] || d['start'] || [255,255,128]);
		var s = jQuery.speed(o['speed'] || d['speed']);
		var r = o['final'] || (this.highlighting[a] && this.highlighting[a].orig) ? this.highlighting[a].orig : jQuery.curCSS(this,a);
		if (o['end'] || d['end']) r = jQuery.highlightFade.asRGBString(e = jQuery.highlightFade.getRGB(o['end'] || d['end']));
		if (typeof o['final'] != 'undefined') r = o['final'];
		if (this.highlighting[a] && this.highlighting[a].timer) window.clearInterval(this.highlighting[a].timer);
		this.highlighting[a] = { steps: ((s.duration) / i), interval: i, currentStep: 0, start: c, end: e, orig: r, attr: a };
		jQuery.highlightFade(this,a,o['complete'],t);
	});
};

jQuery.highlightFade = function(e,a,o,t) {
	e.highlighting[a].timer = window.setInterval(function() { 
		var newR = t(e.highlighting[a].start[0],e.highlighting[a].end[0],e.highlighting[a].steps,e.highlighting[a].currentStep);
		var newG = t(e.highlighting[a].start[1],e.highlighting[a].end[1],e.highlighting[a].steps,e.highlighting[a].currentStep);
		var newB = t(e.highlighting[a].start[2],e.highlighting[a].end[2],e.highlighting[a].steps,e.highlighting[a].currentStep);
		jQuery(e).css(a,jQuery.highlightFade.asRGBString([newR,newG,newB]));
		if (e.highlighting[a].currentStep++ >= e.highlighting[a].steps) {
			jQuery(e).css(a,e.highlighting[a].orig || '');
			window.clearInterval(e.highlighting[a].timer);
			e.highlighting[a] = null;
			if (o && o.constructor == Function) o.call(e);
		}
	},e.highlighting[a].interval);
};

jQuery.highlightFade.defaults = {
	start: [255,255,128],
	interval: 50,
	speed: 400,
	attr: 'backgroundColor'
};

jQuery.highlightFade.getRGB = function(c,d) {
	var result;
	if (c && c.constructor == Array && c.length == 3) return c;
	if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(c))
		return [parseInt(result[1]),parseInt(result[2]),parseInt(result[3])];
	else if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(c))
		return [parseFloat(result[1])*2.55,parseFloat(result[2])*2.55,parseFloat(result[3])*2.55];
	else if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(c))
		return [parseInt("0x" + result[1]),parseInt("0x" + result[2]),parseInt("0x" + result[3])];
	else if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(c))
		return [parseInt("0x"+ result[1] + result[1]),parseInt("0x" + result[2] + result[2]),parseInt("0x" + result[3] + result[3])];
	else
		return jQuery.highlightFade.checkColorName(c) || d || null;
};

jQuery.highlightFade.asRGBString = function(a) {
	return "rgb(" + a.join(",") + ")";
};

jQuery.highlightFade.getBaseValue = function(e,a,b) {
	var s, t;
	b = b || false;
	t = a = a || jQuery.highlightFade.defaults['attr'];
	do {
		s = jQuery(e).css(t || 'backgroundColor');
		if ((s  != '' && s != 'transparent') || (e.tagName.toLowerCase() == "body") || (!b && e.highlighting && e.highlighting[a] && e.highlighting[a].end)) break; 
		t = false;
	} while (e = e.parentNode);
	if (!b && e.highlighting && e.highlighting[a] && e.highlighting[a].end) s = e.highlighting[a].end;
	if (s == undefined || s == '' || s == 'transparent') s = [255,255,255];
	return jQuery.highlightFade.getRGB(s);
};

jQuery.highlightFade.checkColorName = function(c) {
	if (!c) return null;
	switch(c.replace(/^\s*|\s*$/g,'').toLowerCase()) {
		case 'aqua': return [0,255,255];
		case 'black': return [0,0,0];
		case 'blue': return [0,0,255];
		case 'fuchsia': return [255,0,255];
		case 'gray': return [128,128,128];
		case 'green': return [0,128,0];
		case 'lime': return [0,255,0];
		case 'maroon': return [128,0,0];
		case 'navy': return [0,0,128];
		case 'olive': return [128,128,0];
		case 'purple': return [128,0,128];
		case 'red': return [255,0,0];
		case 'silver': return [192,192,192];
		case 'teal': return [0,128,128];
		case 'white': return [255,255,255];
		case 'yellow': return [255,255,0];
	}
};

// NUMBERS ONLY
//--------------------------------------------------
jQuery.fn.numeric = function(decimal, callback) {
	decimal = decimal || ".";
	callback = typeof callback == "function" ? callback : function(){};
	this.keypress(
		function(e)
		{
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if(key == 13 && this.nodeName.toLowerCase() == "input")
			{
				return true;
			}
			else if(key == 13)
			{
				return false;
			}
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
			// allow or deny Ctrl+V (paste), Shift+Ins
			if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
			|| (e.shiftKey && key == 45)) return true;
			// if a number was not pressed
			if(key < 48 || key > 57)
			{
				/* '-' only allowed at start */
				if(key == 45 && this.value.length == 0) return true;
				/* only one decimal separator allowed */
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
				{
					allow = false;
				}
				// check for other keys that have special purposes
				if(
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				)
				{
					allow = false;
				}
				else
				{
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if(typeof e.charCode != "undefined")
					{
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if(e.keyCode == e.which && e.which != 0)
						{
							allow = true;
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
						{
							allow = true;
						}
					}
				}
				// if key pressed is the decimal and it is not already in the field
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) == -1)
				{
					allow = true;
				}
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	)
	.blur(
		function()
		{
			var val = jQuery(this).val();
			if(val != "")
			{
				var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
				if(!re.exec(val))
				{
					callback.apply(this);
				}
			}
		}
	);
	return this;
}

// jQuery Plugin jTip
//--------------------------------------------------
$(document).ready(JT_init);

function JT_init(){
	$("a.jTip")
	.hover(function(){JT_show(this.href,this.id,this.name)},function(){$('#JT').remove()})
	.click(function(){return false});	   
}

function JT_show(url,linkId,title){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery( queryString );
	if(params['width'] === undefined){params['width'] = 250};
	if(params['link'] !== undefined){
	$('#' + linkId).bind('click',function(){window.location = params['link']});
	$('#' + linkId).css('cursor','pointer');
	}
	
	if(hasArea>((params['width']*1)+75)){
		$("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//right side
		var arrowOffset = getElementWidth(linkId) + 11;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{
		$("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_right' style='left:"+((params['width']*1)+1)+"px'></div><div id='JT_close_right'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//left side
		var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 15); //set x position
	}
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$('#JT_copy').load(url);

}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
	if(evt.target){
		evt.preventDefault();
	}else{
		evt.returnValue = false;
	}
}