﻿
var acs_fleetid = -1;
var SimulatorHTML = new Array();
function SimulatorFormCreater(mode){
	var tr = 0;
	var page = '';
	if(mode == 'attacker'){
		$.each(basic_info,function(i,array){
			if(i != 'officier' && i != 'defense'){
				$.each(array,function(row,value){
					tr++;
					if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
					if(row == 212){
						SimulatorHTML[tr] += "<th colspan=2></th>";
					} else if(row%100 != 0) {
						if(i == 'fleet' || i == 'defense') {
							SimulatorHTML[tr] += "<th><input type='text' id='attacker-"+acs_fleetid+"-"+i+"-"+row+"' name=\"attacker["+acs_fleetid+"]["+i+"]["+row+"]\" value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=9></th>";
							SimulatorHTML[tr] += "<th><input type='text' id='attacker-"+acs_fleetid+"-"+i+"-"+row+"-upgrade' style='width:20px;' name=\"attacker["+acs_fleetid+"]["+i+"]["+row+"-upgrade]\" value='0' maxlength=2 onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11></th>";
						} else if(i == 'officier'){
							if(row == mode){
								SimulatorHTML[tr] += "<th colspan=2><input type='text' id='attacker-"+acs_fleetid+"-"+i+"-"+row+"' name=\"attacker["+acs_fleetid+"]["+i+"]["+row+"]\" value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=2></th>";
							}else{
								SimulatorHTML[tr] += "<th colspan=2>&nbsp;</th>";
							}
						}else{
							SimulatorHTML[tr] += "<th colspan=2><input type='text' id='attacker-"+acs_fleetid+"-"+i+"-"+row+"' name=\"attacker["+acs_fleetid+"]["+i+"]["+row+"]\" value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=9></th>";
						}
					}
				});
			} else if (  i == 'defense' ){
				$.each(array,function(row,value){
					tr++;
					if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
					if(row%100 != 0) {
						SimulatorHTML[tr] += "<th colspan=2></th>";
					}
				});
			}else{
				$.each(array,function(index,submode){
					$.each(array[index],function(row,value){
						tr++;
						if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
						if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
						if(index == mode){
							SimulatorHTML[tr] += "<th colspan=2><input type='text' id='attacker-"+acs_fleetid+"-"+i+"-"+row+"' name='attacker["+acs_fleetid+"]["+i+"]["+row+"]' value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=2></th>";
						}else{
							SimulatorHTML[tr] += "<th colspan=2>&nbsp;</th>";
						}
					});
				});
			}
		});
	}else if(mode == 'defender'){
		$.each(basic_info,function(i,array){
			if(i != 'officier'){
				$.each(array,function(row,value){
					tr++;
					if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
					if(row%100 != 0) {
						if(i == 'fleet' || (i == 'defense' && row < 407)) {
							SimulatorHTML[tr] += "<th><input type='text' id='defender-"+acs_fleetid+"-"+i+"-"+row+"' name='defender["+acs_fleetid+"]["+i+"]["+row+"]' value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=9></th>";
							SimulatorHTML[tr] += "<th><input type='text' style='width:20px;' id='defender-"+acs_fleetid+"-"+i+"-"+row+"-upgrade' name=\"defender["+acs_fleetid+"]["+i+"]["+row+"-upgrade]\" value='0' maxlength=2 onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11></th>";

						}else{
							SimulatorHTML[tr] += "<th colspan=2><input type='text' id='defender-"+acs_fleetid+"-"+i+"-"+row+"' name='defender["+acs_fleetid+"]["+i+"]["+row+"]' value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=2></th>";
						}
					}
				});
			}else{
				$.each(array,function(index,submode){
					$.each(array[index],function(row,value){
						tr++;
						if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
						if(index == mode){
							SimulatorHTML[tr] += "<th colspan=2><input type='text' id='defender-"+acs_fleetid+"-"+i+"-"+row+"' name='defender["+acs_fleetid+"]["+i+"]["+row+"]' value='0' onfocus=\"if(this.value == '0') this.value='';\" onblur=\"if(this.value == '') this.value='0';\" size=11 maxlength=2></th>";
						}else{
							SimulatorHTML[tr] += "<th colspan=2>&nbsp;</th>";
						}
					});
				});
			}
		});
		SimulatorHTML[0] = "<td class=c colspan=2 width=35%>공격자</td><td class=c width=30%>연구</td><td class=c colspan=2 width=35%>방어자</td>";
		for(i=0;i<=tr;i++){
			if(i == 29) page += "<td class=c colspan=2></td><td class=c>인력</td><td class=c colspan=2></td>";
			if(i == 32) page += "<td class=c colspan=2></td><td class=c>좌표</td><td class=c colspan=2></td>";
			page += "<tr>"+SimulatorHTML[i]+"</tr>";
		}
		page = "<table cellpadding=3 width=100%>"+page+"</table>";
		$("#simulator-form-"+acs_fleetid).html(page);
	}else{
		$.each(basic_info,function(i,array){
			if(i != 'officier'){
				$.each(array,function(row,value){
					tr++;
					if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';

					if(row%100 == 0){
						SimulatorHTML[tr] += "<td class=c width=25%></td><td class=c width=10%>등급</td><td class=c>";
					}else{
						SimulatorHTML[tr] += "<th>";
					}
					SimulatorHTML[tr] += value;
					if(row%100 == 0){
						SimulatorHTML[tr] += "</td><td class=c width=25%></td><td class=c width=10%>등급</td>";
					}else{
						SimulatorHTML[tr] += "</th>";
					}
				});
			}else{
				$.each(array,function(index,submode){
					$.each(array[index],function(row,value){
						tr++;
						if(typeof(SimulatorHTML[tr]) == 'undefined') SimulatorHTML[tr] = '';
						SimulatorHTML[tr] += "<th>";
						SimulatorHTML[tr] += value;
						SimulatorHTML[tr] += "</th>";
					});
				});
			}
		});
	}
}

function ChangeACSSimulatorSlot ( acs_id ){
	if(acs_id != acs_fleetid){
		if(acs_fleetid != -1) $("#simulator-form-"+acs_fleetid).hide();
		acs_fleetid = acs_id;
		if($("#simulator-form-"+acs_fleetid).html() == ''){
			SimulatorHTML = new Array();
			SimulatorFormCreater('attacker');
			SimulatorFormCreater('name'); 
			SimulatorFormCreater('defender');
			getElementByCache("jquery","#simulator-form").hide();
		}else{
			$("#simulator-form-"+acs_fleetid).show();
		}
	}
}


function ChangeSimulatorMode ( mode ) {

		$.each(basic_info,function(i,array){
			if(i != 'officier'){
				$.each(array,function(row,value){
					if(row%100 == 0){
					}else{
						if(mode == 'legorian'){
							getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").attr({"disabled":"true","value":0});
							getElementByCache("jquery","#defender-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").attr({"disabled":"true","value":0});
						} else if ( mode == 'acs_target'){
						//	getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row).hide();
						//	getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").hide();
						} else {
							getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").removeAttr("disabled");
							getElementByCache("jquery","#defender-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").removeAttr("disabled");
						}
					}
				});
			}else{
				$.each(array,function(index,submode){
					$.each(array[index],function(row,value){
						if(mode == 'legorian'){
							getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row).attr({"disabled":"true","value":0});
							getElementByCache("jquery","#defender-"+acs_fleetid+"-"+i+"-"+row).attr({"disabled":"true","value":0});
						} else if ( mode == 'acs_target'){
						//	getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row).hide();
						//	getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").hide();
						} else {
							getElementByCache("jquery","#attacker-"+acs_fleetid+"-"+i+"-"+row).removeAttr("disabled");
							getElementByCache("jquery","#defender-"+acs_fleetid+"-"+i+"-"+row).removeAttr("disabled");
						}
					});
				});
			}
		});
}


function SimulatorDataInsert ( mode , data ){

		$.each(basic_info,function(i,array){
			if(i != 'officier'){
				$.each(array,function(row,value){
					if(row%100 == 0){
					}else{
						$("#"+mode+"-"+acs_fleetid+"-"+i+"-"+row).attr("value",data[i][row]);
						if((i == 'fleet' || i == 'defense') && row != ''){
							getElementByCache("jquery","#"+mode+"-"+acs_fleetid+"-"+i+"-"+row+"-upgrade").attr("value",data[i][row+"-upgrade"]);
						}
					}
				});
			}else{
				$.each(array,function(index,submode){
					$.each(array[index],function(row,value){
						if(typeof(data[i][row]) != 'undefined'){
							getElementByCache("jquery","#"+mode+"-"+acs_fleetid+"-"+i+"-"+row).attr("value",data[i][row]);
						}else{
							getElementByCache("jquery","#"+mode+"-"+acs_fleetid+"-"+i+"-"+row).attr("value",0);
						}
					});
				});
			}
		});
		$("#"+mode+"-position-galaxy").attr("value",data['position']['galaxy']);
		$("#"+mode+"-position-system").attr("value",data['position']['system']);
		$("#"+mode+"-position-planet").attr("value",data['position']['planet']);
		if(typeof(data['position']['planettype']) == 'string') {
			$("#"+mode+"-position-planettype option:selected").removeAttr("selected");
			$("#"+mode+"-position-planettype option[value="+data['position']['planettype']+"]").attr("selected",true);
		} else {
			$("#"+mode+"-position-planettype option[value=1]").attr("selected",true);
		}
		$("#"+mode+"-resource-metal").attr("value",data['resource']['metal']);
		$("#"+mode+"-resource-crystal").attr("value",data['resource']['crystal']);
		$("#"+mode+"-resource-deuterium").attr("value",data['resource']['deuterium']);
}

function ACSSelectedPlanetSimulate() {
	var fleetid = -1;
	$("#acs-simulator-selecting input[type=checkbox]:checked").each(function(){
		fleetid++;
		if(fleetid < 9){
			ChangeACSSimulatorSlot ( fleetid );
			SimulatorDataInsert ( 'attacker' , basic_simulator['default'] );
			SimulatorDataInsert ( 'attacker' , basic_simulator[$(this).val()] );
		}
	});
	ChangeACSSimulatorSlot ( 0 );
}

var TabbedContent = {
	init: function() {	
		$('.tab_item').mouseover(function() {

			var background = $(this).parent().find('.moving_bg');
			
			$(background).stop().animate({
				left: $(this).position()['left']
			}, {
				duration: 300
			});
			
			TabbedContent.slideContent($(this));
			
		});
	},
	
	slideContent: function(obj) {
		
		var margin = $(obj).parent().parent().find('.slide_content').width();
		margin = margin * ($(obj).prevAll().size() - 1);
		margin = margin * -1;
		
		$(obj).parent().parent().find('.tabslider').stop().animate({
			marginLeft: margin + 'px'
		}, {
			duration: 300
		});
	}
}
