// expand quick links in footer

Form = {

	numberOfWindows: 20,
	defaultNumberOfWindows: 4,
	selectBoxId: '#cf2_field_17',
	classBaseNumber: 18,
	selectBox: null,
    internalFormIndex: '2',

	init: function ( rootId ) {
		Form.injectSelectBox( rootId );
		Form.addWidthHeightClasses();
		Form.moveRequiredIndicators();
	},

	injectSelectBox: function( rootId ) {
        // Make sure we have a valid rootId before replacing
        if ( rootId.length > 0 ) {
            Form.selectBoxId = '#' + rootId;

            // We have to modify the class Base number as well
            var _idSplit = rootId.split( '_' );

            // Get the index of the id split
            var _baseIndex = _idSplit[_idSplit.length-1];

            // Get the internal form index to be used elsewhere in other ids and classes
            var _formSplit = _idSplit[0].split( 'f' );

            // Set the internal form index
            var _formIndex = _formSplit[_formSplit.length-1];

            // Make sure we got a valid form internal index
            if ( parseInt( _formIndex ) > 0 ) {
                // Set the internal form index now
                Form.internalFormIndex = _formIndex;
            }

            // Make sure we got a valid base index
            if ( parseInt( _baseIndex ) > 0 ) {
                // Set the class base number now
                Form.classBaseNumber = parseInt( _baseIndex ) + 1;
            }
        }

        // Proceed as normal from here on
		firstWindow = jQuery(Form.selectBoxId);
		if (!firstWindow.length) return;

		Form.selectBox = jQuery(Form.selectBoxId);

		Form.selectBox.change(Form.updateWindowsVisibility);
		Form.changeVisibility(Form.defaultNumberOfWindows);
	},

	updateWindowsVisibility: function() {
		Form.changeVisibility(Form.selectBox.val());
	},

	changeVisibility: function(numberVisible) {
		// I have to do some horrid element retrieval because
		// IE doesn't support "windows[i].removeClass('hidden')"
		// - boo!

		windows = jQuery('.window');

		for (i = 0; i < numberVisible; i++ ) {
			titleIndex = Form.classBaseNumber + (i * 4) + 1;
			widthBoxIndex = Form.classBaseNumber + (i * 4) + 2;
			heightBoxIndex = Form.classBaseNumber + (i * 4) + 3;

			jQuery('#li-' + Form.internalFormIndex + '-' + titleIndex).removeClass('hidden');
			jQuery('#li-' + Form.internalFormIndex + '-' + widthBoxIndex).removeClass('hidden');
			jQuery('#li-' + Form.internalFormIndex + '-' + heightBoxIndex).removeClass('hidden');
		}

		for (i = numberVisible; i < windows.length; i++) {
			titleIndex = Form.classBaseNumber + (i * 4) + 1;
			widthBoxIndex = Form.classBaseNumber + (i * 4) + 2;
			heightBoxIndex = Form.classBaseNumber + (i * 4) + 3;

			jQuery('#li-' + Form.internalFormIndex + '-' + titleIndex).addClass('hidden');
			jQuery('#li-' + Form.internalFormIndex + '-' + widthBoxIndex).addClass('hidden');
			jQuery('#li-' + Form.internalFormIndex + '-' + heightBoxIndex).addClass('hidden');
		}

	},

	addWidthHeightClasses: function()
	{
		for (i = 0; i < Form.numberOfWindows; i++) {
			widthBoxIndex = Form.classBaseNumber + (i * 4) + 2;
			heightBoxIndex = Form.classBaseNumber + (i * 4) + 3;
			jQuery('#li-' + Form.internalFormIndex + '-' + widthBoxIndex).addClass('width');
			jQuery('#li-' + Form.internalFormIndex + '-' + heightBoxIndex).addClass('height');
		}
	},

	moveRequiredIndicators: function()
	{
		els = jQuery('.fldrequired').each(function() {
			el = jQuery(this).prev().find('span').addClass('required');
			el.text(el.html() + '*');
		});
	}

}

jQuery( document ).ready( function ( $ ) {
    // Find all the elements using our special title now
    $( "select[title='Select Number of Windows']" ).each( function () {
        // Get the element and its appropriate id
        // This is required since we need to pass the id for the initialization
        Form.init( $( this ).attr( 'id' ) );
    } );

    // Do the radio field hack here instead of inside
    $( "input[title='Select Purchase Type']" ).each( function () {
		$( "label[for='" + $( this ).attr( 'id' ) + "']").each( function () {
            // Add the class to the label now
            $( this ).addClass( 'narrowRadioLabel1' );
        } );
    } );
} );
