var calculatorCurrentWindow = 0;

var calculatorCurrentLeaf1 = 0;
var calculatorCurrentLeaf2 = 0;
var calculatorCurrentLeaf3 = 0;

var calculatorRequest = 0;

var calculatorWindowsLoaded = new Array();

$(function()
{
	calculatorInit();
});

function calculatorInit()
{
	$('#profile').change(function(){
		calculatorProfileTypeUpdate();
		calculatorResetCost();
	});
	$('#profile_type').change(function(){calculatorResetCost();});
	$('#podokonnik_width').change(function(){calculatorResetCost();});
	$('#otlivi_width').change(function(){calculatorResetCost();});
	$('#okosi_width').change(function(){calculatorResetCost();});
	$('#montag').change(function(){calculatorResetCost();});
	$('#demontag').change(function(){calculatorResetCost();});
	calculatorLoadWindow(2);
}
function calculatorProfileTypeUpdate()
{
	if($('#profile').val() != 3)
	{
		$('#profile_type_select').html('Тип стеклопакета <select id="profile_type" name="profile_type"><option value="1">Однокамерный</option><option value="2">Энергосберегающий</option><option value="3">Двухкамерный</option></select>');
	}
	else
	{
		$('#profile_type_select').html('Тип стеклопакета <select id="profile_type" name="profile_type"><option value="3">Двухкамерный</option></select>');
	}
	$('#profile_type').change(function(){calculatorResetCost();});
}
function calculatorSetLoading()
{
	calculatorResetLeafs();
	$('#top-params').hide();
	$('#left-params').hide();
	$('#params-view').removeClass();
	$('#params-view').addClass('params-view params-3');
	$('#view').html('&nbsp;');
}

function calculatorResetLeafs()
{
	calculatorCurrentLeaf1 = 0;
	calculatorCurrentLeaf2 = 0;
	calculatorCurrentLeaf3 = 0;
}

function updateLeafs()
{
	if(calculatorCurrentWindow == 1)
	{
		$('#stype-1').show();
		$('#stype-2').hide();
		$('#stype-3').hide();
	}
	else if(calculatorCurrentWindow == 2)
	{
		$('#stype-1').show();
		$('#stype-2').show();
		$('#stype-3').hide();
	}
	else if(calculatorCurrentWindow == 3)
	{
		$('#stype-1').show();
		$('#stype-2').show();
		$('#stype-3').show();
	}
	else if(calculatorCurrentWindow == 4)
	{
		$('#stype-1').hide();
		$('#stype-2').hide();
		$('#stype-3').hide();
	}
	
	for(i=1;i<=3;i++)
	{
		for(j=1;j<=4;j++)
		{
			$('#leaf'+i+'-'+j).html('<img src="/images/leaf-'+j+'.png" width="23" height="37" alt="" />');
			$('#leaf'+i+'-'+j).attr('href', 'javascript:changeLeaf('+i+','+j+','+j+');void(0);');
		}
	}
}

function changeLeaf(section, type, setType)
{
	if(section == 1)
	{
		old = calculatorCurrentLeaf1;
		calculatorCurrentLeaf1 = setType;
	}
	else if(section == 2)
	{
		old = calculatorCurrentLeaf2;
		calculatorCurrentLeaf2 = setType;
	}
	else if(section == 3)
	{
		old = calculatorCurrentLeaf3;
		calculatorCurrentLeaf3 = setType;
	}
	
	$('#leaf'+section+'-'+type).attr('href', 'javascript:changeLeaf('+section+','+type+','+old+');void(0);');
	$('#leaf'+section+'-'+type).html('<img src="/images/leaf-'+old+'.png" width="23" height="37" alt="" />');
		
	updateWindowImage();
}

function updateWindowImage()
{
	if(calculatorCurrentWindow == 1)
	{
		$('#view').html('<img src="/images/windows/'+calculatorCurrentLeaf1+'.png" alt="" />');
	}
	else if(calculatorCurrentWindow == 2)
	{
		$('#view').html('<img src="/images/windows/'+calculatorCurrentLeaf1+'.png" alt="" style="float: left;" /><img src="/images/windows/'+calculatorCurrentLeaf2+'.png" alt="" style="float: left;" />');
	}
	else if(calculatorCurrentWindow == 3)
	{
		$('#view').html('<img src="/images/windows/'+calculatorCurrentLeaf1+'.png" alt="" style="float: left;" /><img src="/images/windows/'+calculatorCurrentLeaf2+'.png" alt="" style="float: left;" /><img src="/images/windows/'+calculatorCurrentLeaf3+'.png" alt="" style="float: left;" />');
	}
	else if(calculatorCurrentWindow == 4)
	{		
		$('#view').html('<img src="/images/windows/5.png" alt="" />');
	}
}

function calculatorLoadWindow(id)
{
	if(calculatorCurrentWindow != id)
	{
		if(calculatorRequest != 1)
		{
			calculatorCurrentWindow = id;
			calculatorRequest =1;
			
			calculatorSetLoading();
				
			try{
				if(typeof calculatorWindowsLoaded[id] == 'object'){
					calculatorLoadWindowCallback(calculatorWindowsLoaded[id]);
					return;
				}
			}catch(e){
			}
	
			$.getJSON(
			  '/ajax/Base/calculatorLoadWindow/',
			  {
			  	window_id: id
			  },
			  calculatorLoadWindowCallback
			);
		}
		else
		{
			$('#construct-'+calculatorCurrentWindow).attr('checked', 1);
		}
	}
}

function calculatorLoadWindowCallback(data)
{
	calculatorWindowsLoaded[data.id] = data;
		
	$('#params-view').removeClass();
	$('#params-view').addClass('params-view params-' + calculatorCurrentWindow);
	$('#top-params-value').html(data.width);
	$('#left-params').html(data.heigth);
	
	$('#top-params').show();
	$('#left-params').show();
	
	$('#width').change(function(){calculatorResetCost();});
	$('#heigth').change(function(){calculatorResetCost();});
	
	updateWindowImage();
	updateLeafs();
	calculatorResetCost();
	
	calculatorRequest =0;
}

function calculatorResetCost()
{
	$('#zakaz').hide();
	$('#total').hide();
	$('#raschet').show();
}

function calculatorSetLoadingCost()
{
	$('#raschet').hide();
	$('#loading-cost').show();
}

function calculatorLoadCost()
{
	if(calculatorRequest != 1)
	{
		calculatorRequest =1;
		
		calculatorSetLoadingCost();
		
		if($('#montag').attr('checked'))
		{
			montag_value = 1;
		}
		else
		{
			montag_value = 0;
		}
		if($('#demontag').attr('checked'))
		{
			demontag_value = 1;
		}
		else
		{
			demontag_value = 0;
		}
		
		$.getJSON(
		  '/ajax/Base/calculatorLoadCost/',
		  {
		  	window_id: calculatorCurrentWindow,
		  	leaf_1: calculatorCurrentLeaf1,
		  	leaf_2: calculatorCurrentLeaf2,
		  	leaf_3: calculatorCurrentLeaf3,
		  	width: $('#width').val(),
		  	heigth: $('#heigth').val(),
		  	profile: $('#profile').val(),
		  	profile_type: $('#profile_type').val(),
		  	podokonnik_width: $('#podokonnik_width').val(),
		  	otlivi_width: $('#otlivi_width').val(),
		  	okosi_width: $('#okosi_width').val(),
		  	montag: montag_value,
		  	demontag: demontag_value
		  },
		  calculatorLoadCostCallback
		);
	}
}

function calculatorLoadCostCallback(data)
{
	$('#cost').html(data.cost + '&euro;');
	
	$('#loading-cost').hide();
	$('#zakaz').show();
	$('#total').show();
	
	calculatorRequest =0;
}
