// ---------------------------------------------------
// Helper Functions
// ---------------------------------------------------

// mainfunc dynamically calls the method passed as a string (with args) 
function mainfunc (func){
    this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}

title_changes = {
	"1":"the head",
	"2":"the neck",
	"3":"the eye",
	"4":"the ear",
	"5":"the face",
	"6":"the throat",
	"8":"the back",
	"9":"the lower back",
	"10":"the shoulder",
	"11":"the arm",
	"12":"the hand",
	"15":"the hips",
	"16":"the buttock",
	"17":"the leg",
	"18":"the feet",
	"19":"the skin",
	"20":"something else"
};

methods_on_avatar = {
	"1":"frontHeadOn",
	"2":"frontNeckOn",
	"3":"frontHeadOn",
	"4":"frontHeadOn",
	"5":"frontHeadOn",
	"6":"frontNeckOn",
	"7":"chestOn",
	"8":"backOn",
	"9":"lowerBackOn",
	"10":"frontShouldersOn",
	"11":"frontArmsOn",
	"12":"frontHandsOn",
	"13":"abdomenOn",
	"14":"frontPelvisOn",
	"15":"frontHipsOn",
	"16":"buttocksOn",
	"17":"frontLegsOn",
	"18":"frontFeetOn",
	"19":"frontSkinOn",
	"20":"frontGeneralOn",
	"22":"frontHeadOn"
};

function ie6or7() {
	return (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5), 10) == 6 || 		
					parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5), 10) == 7) ? true : false;
};
	
	
// Hides each item in an array of css id's
function hideItems(items) {
	for (var i = items.length - 1; i >= 0; i--) {
		$(items[i]).hide();
	};
}

// Hides all front items on the avatar
function hideFront() {
	var items = ["headFront", "neckFront", "shoulderLeftFront", "shoulderRightFront", "chest", "abdomen", "leftArmFront", "rightArmFront", "leftHandFront", "rightHandFront", "leftHipFront", "rightHipFront", "pelvisFront", "leftLegFront", "rightLegFront", "leftFootFront", "rightFootFront"];
	hideItems(items);
}

// Hides all back items on the avatar
function hideBack() {
	var items = ["headBack", "neckBack", "shoulderLeftBack", "shoulderRightBack", "back", "lowerBack", "leftArmBack", "rightArmBack", "leftHandBack", "rightHandBack", "leftHipBack", "rightHipBack", "buttocks", "leftLegBack", "rightLegBack", "leftFootBack", "rightFootBack"];
	hideItems(items);
}

// Updates the 'list' with found items as well as displays the return to all symptoms link div
function ajaxRequest(id) {
	jQuery(".symptom_hidden").hide("slow");
	jQuery('#list').html('<div id="spinner-container" class="spinner-container"><img alt="spinner" id="spinner" src="/images/spinner.gif" style="display:none;" /></div>');
	jQuery('#body_area_body_area_id').val(id);
	
	new Ajax.Request('/chief_complaints/update_symptoms/' + id, {
		method: 'get',
		onComplete: function(transport) {
			jQuery('#symptom-title-return-link').show('slow')
		}
	});
}

// Handles all necessary steps for displaying an avatar item
function showItemsController(args) {
	if (args['title']) {
		$('symptom_title').update(args['title']);
	};
	
	hideFront();
	hideBack();

	// Shows all provided items
	for (var i = args['showItems'].length - 1; i >= 0; i--) {
		$(args['showItems'][i]).show();
	};
	ajaxRequest(args['id']);
}

function getQuerystring(key, default_) {
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}


function initializeAvatar() {
	$('avatarContainter').show();
	$('avatarBack').hide();
	hideFront();
	hideBack();
}


function frontHeadOn() {
	showFront();
	if (ie6or7()) {
		$('headFront').setStyle({
			marginLeft: '0px',
			marginTop: '2px'
		});
	};

	showItemsController({
		"title": 'Viewing Head Symptoms',
		"showItems": ["headFront"],
		"id": '1'
	});
}


function frontHeadOff() {
	$('headFront').hide();
}

function frontSkinOn(){
	showFront();
	if (ie6or7()) {
		$('headFront').setStyle({
			marginLeft: '0px',
			marginTop: '2px'
		});
	};

	showItemsController({
		"title": 'Viewing Skin Symptoms',
		"showItems": [],
		"id": '19'
	});	
}

function frontGeneralOn(){
	showFront();
	if (ie6or7()) {
		$('headFront').setStyle({
			marginLeft: '0px',
			marginTop: '2px'
		});
	};

	showItemsController({
		"title": 'Viewing Other Symptoms',
		"showItems": [],
		"id": '20'
	});	
}

function frontNeckOn() {
	showFront();
	if (ie6or7()) {
		$('neckFront').setStyle({
			marginLeft: '-143px',
			marginTop: '2px'
		});
	};

	showItemsController({
		"title": 'Viewing Neck Symptoms',
		"showItems": ["neckFront"],
		"id": '2'
	});
}


function frontNeckOff() {
	$('neckFront').hide();
}


function frontShouldersOn() {
	showFront();
	if (ie6or7()) {
		$('shoulderLeftFront').setStyle({
			marginLeft: '-189px',
			marginTop: '1px'
		});
		$('shoulderRightFront').setStyle({
			marginLeft: '-188px',
			marginTop: '1px'
		});
	}
	showItemsController({
		"title": 'Viewing Shoulder Symptoms',
		"showItems": ["shoulderLeftFront", "shoulderRightFront"],
		"id": '10'
	});
}


function frontShouldersOff() {
	$('shoulderLeftFront').hide();
	$('shoulderRightFront').hide();
}



function chestOn() {
	showFront();
	if (ie6or7()) {
		$('chest').setStyle({
			marginLeft: '-129px',
			marginTop: '2px'
		});
	};

	showItemsController({
		"id": '7',
		"title": 'Viewing Chest Symptoms',
		"showItems": ["chest"]
	});
}


function chestOff() {
	$('chest').hide();
}


function abdomenOn() {
	showFront();
	if (ie6or7()) {
		$('abdomen').setStyle({
			marginLeft: '-146px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '13',
		"title": 'Viewing Abdomen & Stomach Symptoms',
		"showItems": ["abdomen"]
	});
}


function abdomenOff() {
	$('abdomen').hide();
}


function frontArmsOn() {
	showFront();
	if (ie6or7()) {
		$('leftArmFront').setStyle({
			marginLeft: '-180px',
			marginTop: '2px'
		});
		$('rightArmFront').setStyle({
			marginLeft: '-180px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '11',
		"title": 'Viewing Arm Symptoms',
		"showItems": ["leftArmFront", "rightArmFront"]
	});
}


function frontArmsOff() {
	$('leftArmFront').hide();
	$('rightArmFront').hide();
}



function frontHandsOn() {
	showFront();
	if (ie6or7()) {
		$('leftHandFront').setStyle({
			marginLeft: '-196px',
			marginTop: '0px'
		});
		$('rightHandFront').setStyle({
			marginLeft: '-198px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '12',
		"title": 'Viewing Hand Symptoms',
		"showItems": ["leftHandFront", "rightHandFront"]
	});
}


function frontHandsOff() {
	$('leftHandFront').hide();
	$('rightHandFront').hide();
}



function frontHipsOn() {
	showFront();
	if (ie6or7()) {
		$('leftHipFront').setStyle({
			marginLeft: '-210px',
			marginTop: '-182px'
		});
		$('rightHipFront').setStyle({
			marginLeft: '-210px',
			marginTop: '185px'
		});
	}

	showItemsController({
		"id": '15',
		"title": 'Viewing Hip Symptoms',
		"showItems": ["leftHipFront", "rightHipFront"]
	});
}


function frontHipsOff() {
	$('leftHipFront').hide();
	$('rightHipFront').hide();
}



function frontPelvisOn() {
	showFront();
	if (ie6or7()) {
		$('pelvisFront').setStyle({
			marginLeft: '-182px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '14',
		"title": 'Viewing Pelvis Symptoms',
		"showItems": ["pelvisFront"]
	});
}


function frontPelvisOff() {
	$('pelvisFront').hide();
}



function frontLegsOn() {
	showFront();
	if (ie6or7()) {
		$('leftLegFront').setStyle({
			marginLeft: '-185px',
			marginTop: '2px'
		});
		$('rightLegFront').setStyle({
			marginLeft: '-185px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '17',
		"title": 'Viewing Leg Symptoms',
		"showItems": ["leftLegFront", "rightLegFront"]
	});
}


function frontLegsOff() {
	$('leftLegFront').hide();
	$('rightLegFront').hide();
}



function frontFeetOn() {
	showFront();
	if (ie6or7()) {
		$('leftFootFront').setStyle({
			marginLeft: '-202px',
			marginTop: '2px'
		});
		$('rightFootFront').setStyle({
			marginLeft: '-202px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '18',
		"title": 'Viewing Feet Symptoms',
		"showItems": ["leftFootFront", "rightFootFront"]
	});
}


function frontFeetOff() {
	$('leftFootFront').hide();
	$('rightFootFront').hide();
}



function backHeadOn() {
	showBack();
	if (ie6or7()) {
		$('headBack').setStyle({
			marginLeft: '-185px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '1',
		"title": 'Viewing Head Symptoms',
		"showItems": ["headBack"]
	});
}


function backHeadOff() {
	$('headBack').hide();
}



function backNeckOn() {
	showBack();
	if (ie6or7()) {
		$('neckBack').setStyle({
			marginLeft: '-162px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '2',
		"title": 'Viewing Neck Symptoms',
		"showItems": ["neckBack"]
	});
}


function backNeckOff() {
	$('neckBack').hide();
}



function backShouldersOn() {
	showBack();
	if (ie6or7()) {
		$('shoulderLeftBack').setStyle({
			marginLeft: '-190px',
			marginTop: '2px'
		});
		$('shoulderRightBack').setStyle({
			marginLeft: '-190px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '10',
		"title": 'Viewing Shoulder Symptoms',
		"showItems": ["shoulderLeftBack", "shoulderRightBack"]
	});
}


function backShouldersOff() {
	$('shoulderLeftBack').hide();
	$('shoulderRightBack').hide();
}



function backOn() {
	showBack();
	if (ie6or7()) {
		$('back').setStyle({
			marginLeft: '-142px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '8',
		"title": 'Viewing Back Symptoms',
		"showItems": ["back"]
	});
}


function backOff() {
	$('back').hide();
}



function lowerBackOn() {
	showBack();
	if (ie6or7()) {
		$('lowerBack').setStyle({
			marginLeft: '-158px',
			marginTop: '4px'
		});
	}

	showItemsController({
		"id": '9',
		"title": 'Viewing Lower Back Symptoms',
		"showItems": ["lowerBack"]
	});
}


function lowerBackOff() {
	$('lowerBack').hide();
}



function backArmsOn() {
	showBack();
	if (ie6or7()) {
		$('leftArmBack').setStyle({
			marginLeft: '-176px',
			marginTop: '2px'
		});
		$('rightArmBack').setStyle({
			marginLeft: '-176px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '11',
		"title": 'Viewing Arm Symptoms',
		"showItems": ["leftArmBack", "rightArmBack"]
	});
}


function backArmsOff() {
	$('leftArmBack').hide();
	$('rightArmBack').hide();
}



function backHandsOn() {
	showBack();
	if (ie6or7()) {
		$('leftHandBack').setStyle({
			marginLeft: '-198px',
			marginTop: '2px'
		});
		$('rightHandBack').setStyle({
			marginLeft: '-197px',
			marginTop: '-40px'
		});
	}

	showItemsController({
		"id": '12',
		"title": 'Viewing Hand Symptoms',
		"showItems": ["leftHandBack", "rightHandBack"]
	});
}


function backHandsOff() {
	$('leftHandBack').hide();
	$('rightHandBack').hide();
}



function backHipsOn() {
	showBack();
	if (ie6or7()) {
		$('leftHipBack').setStyle({
			marginLeft: '-216px',
			marginTop: '2px'
		});
		$('rightHipBack').setStyle({
			marginLeft: '-216px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '15',
		"title": 'Viewing Hip Symptoms',
		"showItems": ["leftHipBack", "rightHipBack"]
	});
}


function backHipsOff() {
	$('leftHipBack').hide();
	$('rightHipBack').hide();
}



function buttocksOn() {
	showBack();
	if (ie6or7()) {
		$('buttocks').setStyle({
			marginLeft: '-146px',
			marginTop: '2px'
		});
	}

	showItemsController({
		"id": '16',
		"title": 'Viewing Buttock Symptoms',
		"showItems": ["buttocks"]
	});
}


function buttocksOff() {
	$('buttocks').hide();
}



function backLegsOn() {
	showBack();
	if (ie6or7()) {
		$('leftLegBack').setStyle({
			marginLeft: '-186px',
			marginTop: '2px'
		});
		$('rightLegBack').setStyle({
			marginLeft: '-186px',
			marginTop: '0px'
		});
	}

	showItemsController({
		"id": '17',
		"title": 'Viewing Leg Symptoms',
		"showItems": ["leftLegBack", "rightLegBack"]
	});
}


function backLegsOff() {
	$('leftLegBack').hide();
	$('rightLegBack').hide();
}



function backFeetOn() {
	showBack();
	if (ie6or7()) {
		$('leftFootBack').setStyle({
			marginLeft: '-200px',
			marginTop: '45px'
		});
		$('rightFootBack').setStyle({
			marginLeft: '-200px',
			marginTop: '1px'
		});
	}
	showItemsController({
		"id": '18',
		"title": 'Viewing Feet Symptoms',
		"showItems": ["leftFootBack", "rightFootBack"]
	});
}


function backFeetOff() {
	$('leftFootBack').hide();
	$('rightFootBack').hide();
}


function showFront() {
	hideBack();
	$('avatarBack').hide();
	$('avatarFront').show();
}


function showBack() {
	hideFront();
	$('avatarBack').hide();
	$('avatarFront').hide();
	$('avatarBack').show();
}


function showBackHomepage() {
	$('avatarFront').hide();
	$('avatarBack').show();
}


function showFrontHomepage() {
	$('avatarBack').hide();
	$('avatarFront').show();
}

function showOther() {
	request_url = "/chief_complaints/update_symptoms/20";
	new Ajax.Request(request_url, {
		method: 'get'
	});
}


function getId(id) {
	return (id == 3 || id == 4) ? "1" : id;
}

function getViewType(id){
	if (id == 9 || id == 8 || id == 16){
		return "back";
	}	else	{
		return "front";
	}
}

// Called from the popup menu above avatar on the home page
function selectBodyArea() {
	id = $('body_area_body_area_id').value;
	view_type = getViewType(id);
	href = "/symptoms?ba=" + id + "&view=" + view_type;		
	method_id = getId(id);
	window.location = href;
	mainfunc(methods_on_avatar[method_id]);
}

// Called from the popup menu above avatar on the Symptoms page
function selectBodyAreaSymptomsPage() {
	id = $('body_area_body_area_id').value;
	method_id = getId(id);
	mainfunc(methods_on_avatar[method_id]);
};

function showSkinTrauma() {
		new Ajax.Request('/chief_complaints/update_symptoms/19', { method:'get'});
}

