﻿$(document).ready(function(){
	$('input.watermark').focus(function(){
		$(this).filter(function(){
			return $(this).val() == '' || $(this).val() == $(this).attr('defaultTxt')
		}).removeClass('empty').val('');
	});
	$('input.watermark').blur(function(){
		$(this).filter(function(){
			return $(this).val() == ''
		}).addClass('empty').val($(this).attr('defaultTxt'));
	});
	//eliminateOrphans();
});

//**** slider control ****
var imgCurrentPosition = 0;
var contCurrentPosition = 0;
var numberOfImgSlides = 0;
var numberOfContSlides = 0;
var slideWidth = 685;
var slidePadding = 17;
var timer = 0;
var startTimer = 0;

function slideshowSetup()
{
	var imgSlides = $('.imgSlide');
	var contSlides = $('.contSlide');
	numberOfImgSlides = imgSlides.length;
	numberOfContSlides = contSlides.length;

	// Remove scrollbar in JS
	$('#imgSlidesContainer').css('overflow', 'hidden');
	$('#contSlidesContainer').css('overflow', 'hidden');

	// Wrap all .slides with .slideInner div
	// Float left to display horizontally, readjust .slides width
	imgSlides
	.wrapAll('<div class="slideInner"></div>').css({
		'float' : 'left',
		'width' : slideWidth
	});
	contSlides
	.wrapAll('<div class="slideInner"></div>').css({
		'float' : 'left',
		'width' : slideWidth-slidePadding
	});

	// Set .slideInner width equal to total width of all slides
	$('#imgSlideshow .slideInner').css('width', slideWidth * numberOfImgSlides);
	$('#contSlideshow .slideInner').css('width', slideWidth * numberOfContSlides);

	// Insert left and right arrow controls in the DOM
	$('#imgSlideshow')
		.prepend('<span class="control leftControl"><div class="arrow-left"></div></span>')
		.append('<span class="control rightControl"><div class="arrow-right"></div></span>');
	
	if(contSlides.length > 2)
	{
		$('#contSlideshow')
			.prepend('<span class="control leftControl"><div class="arrow-left"></div></span>')
			.append('<span class="control rightControl"><div class="arrow-right"></div></span>');
	}

	// Hide left arrow control on first load
	manageImgControls(imgCurrentPosition);
	manageContControls(contCurrentPosition);
	
	$('#imgSlidesContainer').bind('mouseenter',function(){clearInterval(timer);clearTimeout(startTimer);}).bind('mouseleave',initInterval);
	
	startTimer = setTimeout(function(){initInterval();}, 5000);
	
	// Create event listeners for .controls clicks
	$('#imgSlideshow .control').bind('click', slideShowClick);
	
	$('#contSlideshow .control').bind('click', function(){
		if($(this).hasClass('noSlides')!= true)
		{
			// Determine new position
			contCurrentPosition = ($(this).hasClass('rightControl')) ? contCurrentPosition+1 : contCurrentPosition-1;

			// Hide / show controls
			manageContControls(contCurrentPosition);
			// Move slideInner using margin-left

			$('#contSlideshow .slideInner').animate({
				'marginLeft' : slideWidth*(-contCurrentPosition)
			},700);
		}
	});
	
	$('#imgSlideshow .imgSlide > img').bind('click', function(){
		showEnlargedImage(this);
	});
}

// manageControls: Hides and shows controls depending on currentPosition
function manageImgControls(position)
{
	// Hide left arrow if position is first slide
	if(position==0)
	{
		//remove click function
		$('#imgSlideshow .leftControl').unbind('click', slideShowClick);
		
		$('#imgSlideshow .leftControl').animate({width: 0}, 750, function(){$('#imgSlideshow .leftControl').hide();});
	} else {
		//rebind click function
		$('#imgSlideshow .leftControl').bind('click', slideShowClick);
		
		$('#imgSlideshow .leftControl').show()
		$('#imgSlideshow .leftControl').animate({width: 26}, 750);
	}

	// Hide right arrow if position is last slide
	if(position==numberOfImgSlides-1)
	{
		$('#imgSlideshow .rightControl').unbind('click', slideShowClick);
	
		$('#imgSlideshow .rightControl').animate({width: 0}, 750, function(){$('#imgSlideshow .rightControl').hide();});
	} else {
		$('#imgSlideshow .rightControl').bind('click', slideShowClick);
	
		$('#imgSlideshow .rightControl').show();
		$('#imgSlideshow .rightControl').animate({width: 26}, 750);
	}
}

function manageContControls(position)
{
	// Hide left arrow if position is first slide
	if(position==0)
	{
		$('#contSlideshow .leftControl').addClass('noSlides')
	} else {
		$('#contSlideshow .leftControl').removeClass('noSlides')
	}

	// Hide right arrow if position is last slide
	if(position==numberOfContSlides-1)
	{
		$('#contSlideshow .rightControl').addClass('noSlides')
	} else {
		$('#contSlideshow .rightControl').removeClass('noSlides')
	}
}

function cycleImages()
{
	imgCurrentPosition += 1;
	
	if (imgCurrentPosition == numberOfImgSlides)
		imgCurrentPosition = 0;
		
	manageImgControls(imgCurrentPosition);
	// Move slideInner using margin-left
	$('#imgSlideshow .slideInner').animate({
		'marginLeft' : slideWidth*(-imgCurrentPosition)
	},700);
}

function initInterval()
{
	timer = setInterval(function(){cycleImages();}, 6000);
}

function slideShowClick(){
	// Determine new position
	imgCurrentPosition = ($(this).hasClass('rightControl')) ? imgCurrentPosition+1 : imgCurrentPosition-1;

	// Hide / show controls
	manageImgControls(imgCurrentPosition);
	// Move slideInner using margin-left
	$('#imgSlideshow .slideInner').animate({
		'marginLeft' : slideWidth*(-imgCurrentPosition)
	},700);
}

function showEnlargedImage(img)
{
	var src = $(img).attr('src');
	var fpath = src.substring(0,src.lastIndexOf('/')+1);
	var file = src.substring(src.lastIndexOf('/')+1);
	
	$('#imgSlidesContainer').unbind('mouseenter').unbind('mouseleave');
	clearInterval(timer);
	clearTimeout(startTimer);
	
	$('.videoOverlay').show();
	$('.videoOverlay').stop(true);
	$('.videoOverlay').animate({opacity:0.95},300,function(){$('.galleryContainer').show();});
	
	$('.galleryContainer .galleryShift').html('<div class="closeIcon">&nbsp;</div><img src="'+fpath+'lrg-'+file+'" alt="Image" />');
}

function redrawStage()
{
	var tPos = $(window).height()/2 - ($('.galleryContainer').outerHeight()/2);
	var lGPos = $(window).width()/2 - ($('.galleryContainer').outerWidth()/2);
	var lPos = $(window).width()/2 - ($('.videoContainer').outerWidth()/2);
	$('.videoContainer').css({'left': lPos, 'top': 172});
	$('.galleryContainer').css({'left': lGPos+27, 'top': tPos+27});
}

function showVideo(src, prvw)
{
	$('.videoOverlay').show();
	$('.videoOverlay').stop(true);
	$('.videoOverlay').animate({opacity: 0.95}, 300, function(){$('.videoContainer').show();switchFlashVideo(src, prvw);});
}

function hideVideo()
{
	$('.videoContainer').hide();
	$('.videoOverlay').stop(true);
	$('.videoOverlay').animate({opacity: 0}, 300, function(){$('.videoOverlay').hide();});
	stopVideo();
}

function hideScreen()
{
	$('.videoContainer').hide();
	$('.galleryContainer').hide();
	initInterval();
	$('#imgSlidesContainer').unbind('mouseenter').unbind('mouseleave');
	$('#imgSlidesContainer').bind('mouseenter',function(){clearInterval(timer);clearTimeout(startTimer);}).bind('mouseleave',initInterval);
	$('.videoOverlay').stop(true);
	$('.videoOverlay').animate({opacity: 0}, 300, function(){$('.videoOverlay').hide();});
}


// **** expander control ****
function expanderSetup()
{	
	$('.profileItem').each(function(i){
		this.id = 'profileItem'+i;
	});
	
	$('#productProfiles .profileItem').click(function(){
		switchProfile(this.id);
	});

    $('#productStyles .expanderWrapper').each(function(i){
		this.id = 'styleItem'+i;
    });
	
    $('#productStyles .imageExpander').click(function(){
		switchStyle(this.parentNode.id);
    });
	
	$('#productProfiles .profileItem img').each(function(){
		var src = $(this).attr('src');
		var fpath = src.substring(0,src.lastIndexOf('/')+1);
		var file = src.substring(src.lastIndexOf('/')+1);
	});
	
	


	
	$('.profileItem2').each(function(i){
		this.id = 'profileItem2'+i;
	});
	
	$('#productProfiles2 .profileItem2').click(function(){
		switchProfile2(this.id);
	});

	$('#productProfiles2 .profileItem2 img').each(function(){
		var src = $(this).attr('src');
		var fpath = src.substring(0,src.lastIndexOf('/')+1);
		var file = src.substring(src.lastIndexOf('/')+1);
	});	
	
	
}

function switchProfile(newID)
{
	var newItem = $('#'+newID);
	var w = $(newItem).width();
	var firstSpot = $('#productProfiles .profileItem:first');
	
	$('#profileInfoDisplay').empty().hide();
	
	if($('#productProfiles .profileItem.current').length > 0)
	{
		//alert('one is expanded');
		if(w == 267)
		{
			if($(firstSpot).attr('id') != newID)
			{
				//alert('need to swap and expand');				
				setTimeout(function(){swapPositions(firstSpot,newItem,$(newItem).attr('id'),w,'profile',false);}, 450);
			}
			else
			{
				//alert('do not need to swap, expanding');
				finishProfileExpand(newItem, false);
			}
			
			if($('#productProfiles .profileItem').length == 1)
				$('#productProfiles').animate({paddingBottom:0},200);
		}
		else
		{
			//alert('closing open item');
			$(newItem).siblings('.profileItem').hide();
			
			$('#productProfiles .profileItem').each(function(){
				$(this).removeClass('current');
				$(this).find('.btn').children('span').text('details');
				$(this).css('left','auto');
				
				if($(this).width() > 267){
					$(this).animate({width:267, height:143},350);
					$(this).find('.profileTitle').animate({width:257},350);
					$(this).find('.profileTitle .color').animate({width:267},350);
				}
				
				var src = $(this).children('img').attr('src');
				var fpath = src.substring(0,src.lastIndexOf('/')+1);
				var file = src.substring(src.lastIndexOf('/')+1);
				if(file.substring(0,3) == 'lrg'){
					$(this).children('img').attr('src',fpath+file.substring(4));
				}
			});
			
			if($('#productProfiles .profileItem').length == 1)
				$('#productProfiles').animate({paddingBottom:0},200);
			
			setTimeout(function(){$(newItem).siblings('.profileItem').fadeIn();}, 450);
		}
	}
	else
	{
		//alert('no items are expanded');
		if($(firstSpot).attr('id') != newID)
		{
			$(newItem).siblings('.profileItem').hide();
			setTimeout(function(){swapPositions(firstSpot,newItem,$(newItem).attr('id'),w,'profile',true);}, 450);
		}
		else
		{
			$(newItem).siblings('.profileItem').fadeOut(200);
			finishProfileExpand(newItem, true);
		}
	}
}







function switchProfile2(newID)
{
	var newItem = $('#'+newID);
	var w = $(newItem).width();
	var firstSpot = $('#productProfiles2 .profileItem2:first');
	
	$('#profileInfoDisplay2').empty().hide();
	
	if($('#productProfiles2 .profileItem2.current').length > 0)
	{
		//alert('one is expanded');
		if(w == 267)
		{
			if($(firstSpot).attr('id') != newID)
			{
				//alert('need to swap and expand');				
				setTimeout(function(){swapPositions2(firstSpot,newItem,$(newItem).attr('id'),w,'profile',false);}, 450);
			}
			else
			{
				//alert('do not need to swap, expanding');
				finishProfileExpand2(newItem, false);
			}
			
			if($('#productProfiles2 .profileItem2').length == 1)
				$('#productProfiles2').animate({paddingBottom:0},200);
		}
		else
		{
			//alert('closing open item');
			$(newItem).siblings('.profileItem2').hide();
			
			$('#productProfiles2 .profileItem2').each(function(){
				$(this).removeClass('current');
				$(this).find('.btn').children('span').text('details');
				$(this).css('left','auto');
				
				if($(this).width() > 267){
					$(this).animate({width:267, height:143},350);
					$(this).find('.profileTitle2').animate({width:257},350);
					$(this).find('.profileTitle2 .color').animate({width:267},350);
				}
				
				var src = $(this).children('img').attr('src');
				var fpath = src.substring(0,src.lastIndexOf('/')+1);
				var file = src.substring(src.lastIndexOf('/')+1);
				if(file.substring(0,3) == 'lrg'){
					$(this).children('img').attr('src',fpath+file.substring(4));
				}
			});
			
			if($('#productProfiles2 .profileItem2').length == 1)
				$('#productProfiles2').animate({paddingBottom:0},200);
			
			setTimeout(function(){$(newItem).siblings('.profileItem2').fadeIn();}, 450);
		}
	}
	else
	{
		//alert('no items are expanded');
		if($(firstSpot).attr('id') != newID)
		{
			$(newItem).siblings('.profileItem2').hide();
			setTimeout(function(){swapPositions2(firstSpot,newItem,$(newItem).attr('id'),w,'profile',true);}, 450);
		}
		else
		{
			$(newItem).siblings('.profileItem2').fadeOut(200);
			finishProfileExpand2(newItem, true);
		}
	}
}









function switchStyle(newID)
{
	//alert(newID);
	var newItem = '#'+newID;
	
	var w = $(newItem).children('.imageExpander').width();
	var firstSpot = $('#productStyles .expanderWrapper:first');
	
	$('#styleInfoDisplay').empty().hide();
	
	if($('#productStyles .expanderWrapper.current').length > 0)
	{
		//one is expanded, check if opening or closing next one
		//alert('one is expanded');
		if(w == 125)
		{
			//if opening, switch
			//alert('expanding, should switch');
			if($(firstSpot).attr('id') != newID)
				setTimeout(function(){swapPositions(firstSpot,newItem,$(newItem).attr('id'),w,'style',false)}, 350);
			else
				finishExpand(newItem, true);
		}
		else
		{
			//if closing, close
			//alert('closing open item');
			$('#productStyles .expanderWrapper').each(function(){
				$(this).removeClass('current');
				
				if($(this).css('paddingTop') != 0)
					$(this).animate({paddingTop: 0},350);
			
				if($(this).children('.imageExpander').width() > 125){
					$(this).children('.imageExpander').animate({width:125, height:125},350);
					$(this).find('.imageTitle').removeClass('top');
					$(this).find('.imageTitle').animate({width:115},350);
					$(this).find('.imageTitle .color').animate({width:125},350);
				}
			});
		}
	}
	else
	{
		//open first one
		//alert('expanding first item');
		if($(firstSpot).attr('id') != newID && w == 125)
			setTimeout(function(){swapPositions(firstSpot,newItem,$(newItem).attr('id'),w,'style',true)}, 350);
		else if(w == 125)
			finishExpand(newItem, true);
	}
}

function swapPositions(firstSpot, newItem, id, w, type, animate)
{
	var tItem = $(newItem);
	var copy_to = $(firstSpot).clone(true);
	var copy_from = $(tItem).clone(true);
	
	//alert(firstSpot);
	//alert(tItem);
	
	//prevent unecessary resizing
	if(type == 'style' && !animate)
	{
		$(copy_from).addClass('current').css('paddingTop',0);
		$(copy_from).find('.imageExpander').css({width:267, height:267});
		$(copy_from).find('.imageTitle').addClass('top').width(257);
		$(copy_from).find('.imageTitle .color').width(267);
		
		$(copy_to).removeClass('current');
		$(copy_to).css('paddingTop',142);
		$(copy_to).find('.imageExpander').css({width:125, height:125});
		$(copy_to).find('.imageTitle').removeClass('top').width(115);
		$(copy_to).find('.imageTitle .color').width(125);
	}
	else if(type == 'profile' && !animate)
	{
		var src = $(newItem).children('img').attr('src');
		var fpath = src.substring(0,src.lastIndexOf('/')+1);
		var file = src.substring(src.lastIndexOf('/')+1);
	
		$(copy_from).addClass('current').css('left','auto').css({width:686, height:197});
		$(copy_from).find('.btn').children('span').text('close');
		$(copy_from).find('.profileTitle').width(676);
		$(copy_from).find('.profileTitle .color').width(686);
		$(copy_from).children('img').attr('src',fpath+'lrg-'+file)
		
		var ssrc = $(copy_to).children('img').attr('src');
		var sfpath = ssrc.substring(0,ssrc.lastIndexOf('/')+1);
		var sfile = ssrc.substring(ssrc.lastIndexOf('/')+1);
		
		$(copy_to).removeClass('current').css('left',420).css({width:267, height:143});
		$(copy_to).find('.btn').children('span').text('details');
		$(copy_to).find('.profileTitle').width(257);
		$(copy_to).find('.profileTitle .color').width(267);
		
		if(sfile.substring(0,3) == 'lrg')
			$(copy_to).children('img').attr('src',sfpath+sfile.substring(4));
	}
	
	$(firstSpot).replaceWith(copy_from);
	$(tItem).replaceWith(copy_to);
	
	newItem = $('#'+id);
	//$(newItem).siblings('.profileItem').hide();
	
	if(type == 'style')
		finishExpand(newItem, animate);
	else if(type == 'profile')
		finishProfileExpand(newItem, animate);
}



function swapPositions2(firstSpot, newItem, id, w, type, animate)
{
	var tItem = $(newItem);
	var copy_to = $(firstSpot).clone(true);
	var copy_from = $(tItem).clone(true);
	
	//alert(firstSpot);
	//alert(tItem);
	
	//prevent unecessary resizing
	if(type == 'style' && !animate)
	{
		$(copy_from).addClass('current').css('paddingTop',0);
		$(copy_from).find('.imageExpander').css({width:267, height:267});
		$(copy_from).find('.imageTitle').addClass('top').width(257);
		$(copy_from).find('.imageTitle .color').width(267);
		
		$(copy_to).removeClass('current');
		$(copy_to).css('paddingTop',142);
		$(copy_to).find('.imageExpander').css({width:125, height:125});
		$(copy_to).find('.imageTitle').removeClass('top').width(115);
		$(copy_to).find('.imageTitle .color').width(125);
	}
	else if(type == 'profile' && !animate)
	{
		var src = $(newItem).children('img').attr('src');
		var fpath = src.substring(0,src.lastIndexOf('/')+1);
		var file = src.substring(src.lastIndexOf('/')+1);
	
		$(copy_from).addClass('current').css('left','auto').css({width:686, height:197});
		$(copy_from).find('.btn').children('span').text('close');
		$(copy_from).find('.profileTitle2').width(676);
		$(copy_from).find('.profileTitle2 .color').width(686);
		$(copy_from).children('img').attr('src',fpath+'lrg-'+file)
		
		var ssrc = $(copy_to).children('img').attr('src');
		var sfpath = ssrc.substring(0,ssrc.lastIndexOf('/')+1);
		var sfile = ssrc.substring(ssrc.lastIndexOf('/')+1);
		
		$(copy_to).removeClass('current').css('left',420).css({width:267, height:143});
		$(copy_to).find('.btn').children('span').text('details');
		$(copy_to).find('.profileTitle2').width(257);
		$(copy_to).find('.profileTitle2 .color').width(267);
		
		if(sfile.substring(0,3) == 'lrg')
			$(copy_to).children('img').attr('src',sfpath+sfile.substring(4));
	}
	
	$(firstSpot).replaceWith(copy_from);
	$(tItem).replaceWith(copy_to);
	
	newItem = $('#'+id);
	//$(newItem).siblings('.profileItem').hide();
	
	if(type == 'style')
		finishExpand2(newItem, animate);
	else if(type == 'profile')
		finishProfileExpand2(newItem, animate);
}


function finishExpand(newItem, animate)
{
	var w = 267;
	$('#styleInfoDisplay').html($(newItem).find('.info').html());	
	
	if(animate)
	{
		$(newItem).find('.imageTitle').addClass('top');
		$(newItem).addClass('current');
		
		$(newItem).find('.imageExpander').animate({width:w, height:w},350);
		$(newItem).find('.imageTitle').animate({width:w-10},350);
		$(newItem).find('.imageTitle .color').animate({width:w},350,function(){$('#styleInfoDisplay').fadeIn();});
	}
	else
	{
		$('#styleInfoDisplay').fadeIn();
	}
	
	$(newItem).siblings('.expanderWrapper').animate({paddingTop:142},350);
}

function finishExpand2(newItem, animate)
{
	var w = 267;
	$('#styleInfoDisplay2').html($(newItem).find('.info2').html());	
	
	if(animate)
	{
		$(newItem).find('.imageTitle').addClass('top');
		$(newItem).addClass('current');
		
		$(newItem).find('.imageExpander').animate({width:w, height:w},350);
		$(newItem).find('.imageTitle').animate({width:w-10},350);
		$(newItem).find('.imageTitle .color').animate({width:w},350,function(){$('#styleInfoDisplay').fadeIn();});
	}
	else
	{
		$('#styleInfoDisplay2').fadeIn();
	}
	
	$(newItem).siblings('.expanderWrapper').animate({paddingTop:142},350);
}

function finishProfileExpand(newItem, animate)
{
	var src = $(newItem).children('img').attr('src');
	var fpath = src.substring(0,src.lastIndexOf('/')+1);
	var file = src.substring(src.lastIndexOf('/')+1);

	$(newItem).show();

	w = 686;
	$('#profileInfoDisplay').html($(newItem).find('.info').html());
	
	if(animate)
	{
		$(newItem).addClass('current').find('.btn').children('span').text('close');
		$(newItem).children('img').attr('src',fpath+'lrg-'+file)
	
		$(newItem).animate({width:w, height:197},350,function(){$('#profileInfoDisplay').fadeIn();$(newItem).siblings('.profileItem').css('left',420).fadeIn();});		
		$(newItem).find('.profileTitle').animate({width:w-10},350);
		$(newItem).find('.profileTitle .color').animate({width:w},350);
	}
	else
	{
		$('#profileInfoDisplay').fadeIn();
		$(newItem).siblings('.profileItem').css('left',420).fadeIn();
	}
	
	if($('#productProfiles .profileItem').length == 1)
		$('#productProfiles').animate({paddingBottom: 163},350);
}


function finishProfileExpand2(newItem, animate)
{
	var src = $(newItem).children('img').attr('src');
	var fpath = src.substring(0,src.lastIndexOf('/')+1);
	var file = src.substring(src.lastIndexOf('/')+1);

	$(newItem).show();

	w = 686;
	$('#profileInfoDisplay2').html($(newItem).find('.info2').html());
	
	if(animate)
	{
		$(newItem).addClass('current').find('.btn').children('span').text('close');
		$(newItem).children('img').attr('src',fpath+'lrg-'+file)
	
		$(newItem).animate({width:w, height:197},350,function(){$('#profileInfoDisplay2').fadeIn();$(newItem).siblings('.profileItem2').css('left',420).fadeIn();});		
		$(newItem).find('.profileTitle2').animate({width:w-10},350);
		$(newItem).find('.profileTitle2 .color').animate({width:w},350);
	}
	else
	{
		$('#profileInfoDisplay2').fadeIn();
		$(newItem).siblings('.profileItem2').css('left',420).fadeIn();
	}
	
	if($('#productProfiles2 .profileItem2').length == 1)
		$('#productProfiles2').animate({paddingBottom: 163},350);
}


function submitZip(o, e)
{
	var charCode;
	if(e && e.which)
		charCode = e.which;
	else if(window.event){
		e = window.event;
		charCode = e.keyCode;
	}
	
	if(charCode == 13){
		alert('Zipcode submit placeholder function');
	}
}

//eliminate text orphans
function eliminateOrphans()
{
	var tags = 'p, h2, h3, h4, h5, h6, #contSlideshow .sliderContent .info .text';
	$(tags).not(':empty').each(function() {
		var text = $(this).html(),
			arrWords = text.split(' '),
			widow = arrWords.pop();
		
		if(arrWords.length > 2)
			removeOrphan(this, text, arrWords, widow);
	});
}

function removeOrphan(tag, text, arrWords, widow)
{
	var ind = text.lastIndexOf(widow);
	var before = text.substring(0, ind-1);
	
	//if this widow isn't inside an html tag
	if(widow.indexOf(">") == -1 || widow.indexOf("=") == -1)
	{
		$(tag).html(before+'&nbsp;'+widow);
	}
	//if this widow is inside an html tag
	else
	{
		for(var j=text.length-widow.length; j>0; j--)
		{
			if(text[j] == "<")
			{
				//alert(j + " is the beginning of the tag");
				for(var k=j; k>0; k--)
				{
					if(text[k] == " ")
					{
						//alert("next space is at " + k);
						var newbefore = text.substring(0, k);
						var endingtag = text.substring(k+1, text.length);
						$(tag).html(newbefore+'&nbsp;'+endingtag);
						break;
					}
				}
			}	
		}
	}
}
