function trim(str) 
{
	str = str.replace(/^\s+/, '');
	
	for (var i = str.length - 1; i > 0; i--) 
	{
		if (/\S/.test(str.charAt(i))) 
		{
			str = str.substring(0, i + 1);
			break;
		}
	}
	
	return str;
};

function default_value(content)
{
	if (trim(content.value) == content.defaultValue)
	{
		content.value = '';
	}
};

function new_value(content)
{
	if (trim(content.value) == '')
	{
		content.value = content.defaultValue;
	}
	else
	{
	   content.text = trim(content.value);
	}
};

jQuery(document).ready(function () {
    var startContent = $('#Content h1');
    if (startContent != null) {
        $('#Content h1').attr("id", "startcontent");
    }
    else {
        $('.article_content').attr("id", "startcontent");
    }

    // Takes care of phantom Page Elements container when they have no content.
    var PE = $('.PageElements');
    if (PE.length > 0) {
        if (trim(PE[0].innerHTML) == '') {
            PE.hide();
        }
    }

    var ic = $('#image_carousel'),
	 items = null,
	 insertBuffer = [],
	 item = null;

    function emptyCarouselBuffer() {
        insertBuffer.sort(function () { return 0.5 - Math.random(); });
        while (insertBuffer.length > 0) {
            $('#image_carousel').append(insertBuffer.pop());
        }
    }

    if (ic) {
        items = $('.gallery_item', ic).detach().toArray();

        while (items.length > 0) {
            item = items.shift(); //Take first item off array

            //Does item match insert buffer?
            if (insertBuffer && insertBuffer[0]) {
                if ($(item).attr('title') === $(insertBuffer[0]).attr('title')) {
                    //Match - add it to the buffer
                    insertBuffer[insertBuffer.length] = item;
                }
                else {
                    //No match. Dump the buffer
                    emptyCarouselBuffer();

                    //Add the item as the first entry in the new buffer.
                    insertBuffer[insertBuffer.length] = item;
                }
            }
            else {
                insertBuffer[insertBuffer.length] = item;
            }
        }
        emptyCarouselBuffer();
        var imageCarousel = document.getElementById("image_carousel");
        if (imageCarousel != null) {  
            if (navigator.userAgent != "SITEARCHIVER") {
                $('#image_carousel').scroller({ transition: 'fade', perPage: 1, itemClass: 'gallery_item', leftButton: '<div id="left-button"><span>&laquo;</span></div>', rightButton: '<div id="right-button"><span>&raquo;</span></div>', dotCounter: true, auto: true, animateSpeed: SLIDE_SPEED, autoSpeed: SLIDE_DELAY });
            }
        }
    }

    // Legend tag fix.
    $('legend').wrapInner('<span></span>');
});

function CheckXForm() {
    if ($('.x-form') != null) {
        $('.x-form td').each(function () {
            if ($('input', this).hasClass('req')) {
                $('label', this).addClass('star');
            }
            if ($('select', this).hasClass('req')) {
                $('label', this).addClass('star');
            }

            if ($('textarea', this).hasClass('req')) {
                $('label', this).addClass('star');
            }

            if ($(this).children('span.xformvalidator').length > 0) {
                var valCount = $(this).children('span.xformvalidator').length;
                var valItem = $(this).children('span.xformvalidator');

                if (valCount == 1) {
                    if (valItem.css('display') != 'none') {
                        $(this).wrapInner('<div class="form_element error"></div>');
                    }
                }
                else if (valCount > 1) {
                    var totalCount = 0;
                    $(this).children('span.xformvalidator').each(function () {
                        if ($(this).css('display') != 'none') {
                            totalCount++;
                        }
                    });
                    if (totalCount > 0) {
                        $(this).wrapInner('<div class="form_element error"></div>');
                    }
                }
            }
        });
    }
}
