var flashvars	= {};
var params		= {};
var attributes	= {};
swfobject.embedSWF("swf/smack_top.swf", "main-visual", "961", "258", "9.0.0", "", flashvars, params, attributes);

var result = 0;
var food = new Array();

jQuery(function($){
	//tab
	var opentab = 'tab_cat';
	if (location.search.indexOf('dog') > -1){
		opentab = 'tab_dog';
	}
	$('div#tab_calculator').imageTabs({
		animate: 'fadeIn',
		duration: 'slow',
		opentab: opentab
	});
	
	//計算ステータス
	$.ajax({
		url: 'xml/calculator_status.xml',
		dataType: 'xml',
		success: function(data){
			$(data).find('item').each(function() {
				var category = $(this).attr('category');
				food[category] = new Array();
				
				$(this).find('status').each(function () {
					var status = '';
					var name = $(this).attr('name');
					$(this).find('column').each(function () {
						if (name == '04') {
							//
							status += '\
<li class="' + $(this).attr('class') + '"><a onclick="calculate(\'' + category + '\', \'' + $(this).attr('id') + '\');return false;" href="#calculator"><span>' + $(this).find('label').text() + '</span></a></li>\
';
							//
							food[category][$(this).attr('id')] = '\
<p class="name"><a href="' + $(this).find('link').text() + '">' + $(this).find('label').text() + '</a></p>\
<p class="gram">0</p>\
<p class="cup">0</p>\
<p class="pic"><a href="' + $(this).find('link').text() + '"><img src="' + $(this).find('pic').text() + '" alt="" /></a></p>\
<p class="button"><a href="' + $(this).find('link').text() + '"><img src="img/button_calculator_food.gif" width="108" height="22" alt="詳しくはこちら" /></a></p>\
<span class="value" style="display:none;">' + $(this).attr('value') + '</span>\
<span class="specific_gravity" style="display:none;">' + $(this).find('specific_gravity').text() + '</span>\
';

						} else {
							//
							selected = '';
							if ($(this).attr('class').indexOf('default') > -1) {
								selected = 'selected="selected"';
							}
							status += '\
<option class="' + $(this).attr('class') +'" value="' + $(this).attr('value') +'" ' + selected + '>' + $(this).find('label').text() + '</option>';
						}
					});
					
					$('#' + category + '_status_' + name).html(status);
				});

			});
			changeStatus_01('cat', $('#cat_status_01 option:selected').attr('class'));
			changeStatus_01('dog', $('#cat_status_01 option:selected').attr('class'));
		}
	});
	
	$('#cat_status_01').change(function(){
		var isGrowing = $('option:selected', this).attr("class");
		changeStatus_01('cat', isGrowing);
	});
	$('#dog_status_01').change(function(){
		var isGrowing = $('option:selected', this).attr("class");
		changeStatus_01('dog', isGrowing);
	});
});

function changeStatus_01(category, isGrowing) {
	jQuery('#' + category + '_status_02 option').each(function(){
		if (isGrowing == 'isGrowing') {
			if (jQuery(this).attr('class') != 'isGrowing') {
				jQuery(this).attr('disabled', 'disabled');
				jQuery(this).next().attr('selected', 'selected');
			} else {
				jQuery(this).attr('disabled', '');
			}
		} else {
			jQuery(this).attr('disabled', '');
		}
	});
}

//フード種類
function selectFood(type) {
	//年齢
	if (typeof jQuery('#' + type + '_status_01').val() == 'undefined' 
			|| jQuery('#' + type + '_status_01').val() == '' 
			|| isNaN(jQuery('#' + type + '_status_01').val()) ) {
		alert('年齢を選択してください。');
		jQuery('#' + type + '_status_01').css({'backgroundColor': "#FFD0D0"})
		return;
	}
	//年齢
	if (typeof jQuery('#' + type + '_status_02').val() == 'undefined' 
			|| jQuery('#' + type + '_status_02').val() == '' 
			|| isNaN(jQuery('#' + type + '_status_02').val()) ) {
		alert('避妊をしてますか？');
		jQuery('#' + type + '_status_02').css({'backgroundColor': "#FFD0D0"})
		return;
	}

	//体重
	if (typeof jQuery('#' + type + '_status_03').val() == 'undefined' 
			|| jQuery('#' + type + '_status_03').val() == '' 
			|| isNaN(jQuery('#' + type + '_status_03').val()) ) {
		alert('体重を数値で入力してください。');
		jQuery('#' + type + '_status_03').css({'backgroundColor': "#FFD0D0"})
		return;
	}
	jQuery('#' + type + '_status_01').css({'backgroundColor': "#fff"})
	jQuery('#' + type + '_status_02').css({'backgroundColor': "#fff"})
	jQuery('#' + type + '_status_03').css({'backgroundColor': "#fff"})
	
	//最適フード
	if (jQuery('#' + type + '_status_01 option:selected').attr("class") == 'isGrowing') {
		jQuery('#' + type + '_status_04 li').each(function(){
			if (jQuery(this).attr('class') != 'isGrowing') {
				jQuery(this).hide();
			}
		});
	} else {
		jQuery('#' + type + '_status_04 li').show();
	}

	//表示
	var boxHeight = jQuery('div.tabPanelGroup').height();
	jQuery('div.tabPanelGroup').css('height', boxHeight);
	jQuery('#tab_' + type + ' .phase_01').hide();
	jQuery('#tab_' + type + ' .phase_02').hide();
	jQuery('#tab_' + type + ' .phase_03').hide();
	jQuery('#tab_' + type + ' .phase_02').fadeIn(500);
	jQuery('div.tabPanelGroup').css('height', 'auto');
	
	//計算
	type_value = 1.2;
	if (type == 'dog') {
		type_value = 1.6;
	}
	result = Math.pow(jQuery('#' + type + '_status_03').val(), 0.75) * 70 * type_value * 100 * jQuery('#' + type + '_status_01').val() * jQuery('#' + type + '_status_02').val();
}

//最終計算
function calculate(type, id) {
	jQuery('#tab_' + type + ' .phase_03 .result').html(food[type][id]);

	var gram = Math.round(result * 10 / jQuery('#tab_' + type + ' .phase_03 .result .value').html()) / 10;
	jQuery('#tab_' + type + ' .phase_03 .result .gram').html(gram);
	jQuery('#tab_' + type + ' .phase_03 .result .cup').html(Math.round(gram * 10 / jQuery('#tab_' + type + ' .phase_03 .result .specific_gravity').html()) / 10);

	var boxHeight = jQuery('div.tabPanelGroup').height();
	jQuery('div.tabPanelGroup').css('height', boxHeight);
	jQuery('#tab_' + type + ' .phase_01').hide();
	jQuery('#tab_' + type + ' .phase_02').hide();
	jQuery('#tab_' + type + ' .phase_03').hide();
	jQuery('#tab_' + type + ' .phase_03').fadeIn(500);
	jQuery('div.tabPanelGroup').css('height', 'auto');
}

//フード一覧へ戻る
function backFood(type) {
	var boxHeight = jQuery('div.tabPanelGroup').height();
	jQuery('div.tabPanelGroup').css('height', boxHeight);
	jQuery('#tab_' + type + ' .phase_01').hide();
	jQuery('#tab_' + type + ' .phase_02').hide();
	jQuery('#tab_' + type + ' .phase_03').hide();
	jQuery('#tab_' + type + ' .phase_02').fadeIn(500);
	jQuery('div.tabPanelGroup').css('height', 'auto');
}

