//-------------------------------------------------------------
// Page Load Handler
//-------------------------------------------------------------
// Neatly handle multiple page load events
// by Simon Wilson
// - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
//-------------------------------------------------------------

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function hasClass( obj, cName ) { 
	return new RegExp('\\b'+cName+'\\b').test(obj.className);
}

addLoadEvent( doMeasurementFlashListeners );

function doMeasurementFlashListeners( ) {
	
	if (!( document.getElementById && document.getElementsByTagName ) ) return false;
	
	form = document.getElementById( 'measurements_simple' );
	if (!form) form = document.getElementById( 'measurements_advanced' );
	if (!form) return false;
	

	inputs = form.getElementsByTagName( 'input' );
	selects = form.getElementsByTagName( 'select' );

	for( var i = 0; i < inputs.length; i++ ) {
		if ( hasClass( inputs[i], 'measurement' ) ) {
			inputs[i].onfocus = function() {
				replace_measurements_flash( this.id );	
			}
		}
	}
	
	for( var i = 0; i < selects.length; i++ ) {
		if ( hasClass( selects[i], 'measurement' ) ) {
			selects[i].onfocus = function() {
				replace_measurements_flash( this.id );	
			}
		}
	}
}

function measurement_details( measurement ) {
	
	var file, name, desc;
	var details = new Array()
	
	switch( measurement ) {
			
		case 'chest':
			file = 'chest.swf';
			name = 'Chest';
			desc = 'Measure around the fullest part of your chest, making sure that you are fully relaxed.';
		break;
		
		case 'sleeve':
			file = 'sleeve.swf';
			name = 'Sleeve';
			desc = 'Measure from the centre point at the back of your neck to just above the first knuckle of your thumb, making sure that your arms are relaxed by your sides.';
		break;
		
		case 'hip':
			file = 'hips.swf';
			name = 'Hip';
			desc = 'Measure around the fullest part of your hips, making sure that the tape measure is over your buttocks and not under it.';
		break;
		
		case 'waist':
			file = 'waist.swf';
			name = 'Waist';
			desc = 'Measure around the widest part of your waist/stomach.  The tape measure should be above your trousers waistband.';
		break;
		
		case 'back_length':
			file = 'back.swf';
			name = 'Back Length';
			desc = 'Measure from the centre point at the back of your neck to just below your buttocks.';
		break;
		
		case 'left_yoke':
			file = 'lyolk.swf';
			name = 'Left Yoke';
			desc = 'Measure from the centre point at the back of your neck to the tip of your shoulder. This measurement determines where the shoulder seam lies.';
		break;
		
		case 'left_sleeve':
			file = 'lsleeve_simple.swf';
			name = 'Left Sleeve';
			desc = 'Measure from the tip of your shoulder (where the Yoke measurement ended) to just above the first knuckle of your thumb, making sure that your arms are relaxed by your sides.';
		break;
		
		case 'right_yoke':
			file = 'ryolk.swf';
			name = 'Right Yoke';
			desc = 'Measure from the centre point at the back of your neck to the tip of your shoulder. This measurement determines where the shoulder seam lies.';
		break;
		
		case 'right_sleeve':
			file = 'rsleeve_simple.swf';
			name = 'Right Sleeve';
			desc = 'Measure from the tip of your shoulder (where the Yoke measurement ended) to just above the first knuckle of your thumb, making sure that your arms are relaxed by your sides.';
		break;
		
		case 'neck':
		default:
			file = 'neck.swf';
			name = 'Collar';
			desc = 'Measure around the lower part of your neck, placing one finger between the tape measure and your neck.';
		break;		
	}
	
	details['file'] = file;
	details['name'] = name;
	details['desc'] = desc;
	
	return details;
}

function replace_measurements_flash( measurement ) {

	// Constants / Settings
	var FOLDER_PATH = "/assets/flash/measurements/";
		
	// Get Mesurement Details
	details = measurement_details( measurement )
	
	// Assign and Display
	var name = details['name']
	var desc = details['desc'];
	var file = details['file'];
	
	// define a new flash object
	var f1 = new FlashObject("/assets/flash/40s_measurement.swf", "sr_measurement_flash", "390", "380", "7.0.0", "#131313", false );
	
	// send parameters to flash 
	f1.addParam("quality", "high");

	// send vaiables to flash like this
	f1.addVariable("var_folder", FOLDER_PATH );
	f1.addVariable("var_flash2load", file );
	f1.addVariable("var_measurement_name", name );
	f1.addVariable("var_measurement_desc", desc );
	
	// draw the flash element to the screen.
	f1.write("sr_measurement");

}


function launchViewShirtPopup(url) {
	newwindow = window.open(url, 'viewShirt', 'height=550,width=730,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}